|
| 1 | +namespace OSAE.EventGhost |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Text; |
| 5 | + using System.Net; |
| 6 | + using System.Net.Sockets; |
| 7 | + |
| 8 | + public class EventGhost : OSAEPluginBase |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Provides access to logging |
| 12 | + /// </summary> |
| 13 | + // Logging logging = Logging.GetLogger("EventGhost"); |
| 14 | + private static OSAE.General.OSAELog Log = new General.OSAELog(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Plugin name |
| 18 | + /// </summary> |
| 19 | + string pName; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// OSA Plugin Interface - Commands the be processed by the plugin |
| 25 | + /// </summary> |
| 26 | + /// <param name="method">Method containging the command to run</param> |
| 27 | + public override void ProcessCommand(OSAEMethod method) |
| 28 | + { |
| 29 | + |
| 30 | + |
| 31 | + String methodName = method.MethodName; |
| 32 | + String objectName = method.ObjectName; |
| 33 | + String combinedString = objectName + "_" + methodName; |
| 34 | + |
| 35 | + //get the called objects properties to see if there is a computer objet defined |
| 36 | + OSAEObject calledObject = OSAEObjectManager.GetObjectByName(method.ObjectName); |
| 37 | + OSAEObjectPropertyCollection ComputerProperty = calledObject.Properties; |
| 38 | + OSAEObjectCollection eventGhostObjects = OSAEObjectManager.GetObjectsByType("Computer"); |
| 39 | + OSAEObject computer = new OSAEObject(); |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + //if there are no properties defined then grab the first available Computer object |
| 44 | + //logging.AddToLog("The Count of the Computer Properties is : " + ComputerProperty.Count, true); |
| 45 | + if (ComputerProperty.Count == 0) |
| 46 | + { |
| 47 | + |
| 48 | + //get the last object in the list |
| 49 | + |
| 50 | + foreach (OSAEObject test in eventGhostObjects) |
| 51 | + { |
| 52 | + computer = test; |
| 53 | + } |
| 54 | + |
| 55 | + //add log entry if mulptiple computer objects were dectected and no property was used to select which object |
| 56 | + if (eventGhostObjects.Count > 1) { |
| 57 | + |
| 58 | + |
| 59 | + Log.Info("There are multiple eventghost computer objects detected. " + computer.Name + " with an address of: " + OSAEObjectPropertyManager.GetObjectPropertyValue(computer.Name, "IPAddress") + ":" + OSAEObjectPropertyManager.GetObjectPropertyValue(computer.Name, "EventGhost Port") + " is being used to transmit the package. Please add a property called 'Computer' with a value of the object name of the computer you wish to transmit the UDP packet to."); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + } |
| 64 | + // if the object type contains a property with ID "Computer" use that property to select the appropriate object |
| 65 | + else |
| 66 | + { |
| 67 | + |
| 68 | + OSAEObjectProperty whichComputer = OSAEObjectPropertyManager.GetObjectPropertyValue(calledObject.Name,"Computer"); |
| 69 | + computer = eventGhostObjects.Find(whichComputer.Value); |
| 70 | + |
| 71 | + //check to see if a computer object was found if none print the logged property value and select another object to use |
| 72 | + if (computer == null){ |
| 73 | + foreach (OSAEObject test in eventGhostObjects) |
| 74 | + { |
| 75 | + computer = test; |
| 76 | + } |
| 77 | + |
| 78 | + if (eventGhostObjects.Count > 1) |
| 79 | + { |
| 80 | + Log.Info("A computer object was unable to be matched to " + whichComputer.Value + " please ensure that " + calledObject.Name + " contains a property called Computer with a value mathing the name of the appropriate computer object."); |
| 81 | + Log.Info(computer.Name + " with an address of: " + computer.Address + ":" + OSAEObjectPropertyManager.GetObjectPropertyValue(computer.Name, "EventGhost Port") + " is being used to transmit the package."); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + // logging.AddToLog(address, true); |
| 90 | + // logging.AddToLog(objectName, true); |
| 91 | + // logging.AddToLog(combinedString, true); |
| 92 | + |
| 93 | + |
| 94 | + //get the server address indicated in the address for the object passed to the method |
| 95 | + String ipAddress = computer.Address; |
| 96 | + |
| 97 | + //check to make sure computer object has address, if not assume local host |
| 98 | + if (ipAddress.Equals("", StringComparison.Ordinal)) { |
| 99 | + ipAddress = "Localhost"; |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + OSAEObjectProperty portAddress = OSAEObjectPropertyManager.GetObjectPropertyValue(computer.Name, "Port"); |
| 104 | + |
| 105 | + //if not value for port use default value of 33333 |
| 106 | + int port = 0; |
| 107 | + |
| 108 | + if ((portAddress.Value).Equals("",StringComparison.Ordinal)){ |
| 109 | + port = 33333; |
| 110 | + } |
| 111 | + else{ |
| 112 | + port = Int32.Parse(portAddress.Value); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + // logging.AddToLog(port, true); |
| 118 | + // logging.AddToLog(ipAddress, true); |
| 119 | + IPAddress serverAddr = IPAddress.Parse(ipAddress); |
| 120 | + IPEndPoint endPoint = new IPEndPoint(serverAddr, port); |
| 121 | + |
| 122 | + //send the updpacket |
| 123 | + UdpClient udp = new UdpClient(); |
| 124 | + byte[] sendBytes = Encoding.ASCII.GetBytes(combinedString); |
| 125 | + udp.Send(sendBytes, sendBytes.Length, endPoint); |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// OSA Plugin Interface - called on start up to allow plugin to do any tasks it needs |
| 133 | + /// </summary> |
| 134 | + /// <param name="pluginName">The name of the plugin from the system</param> |
| 135 | + public override void RunInterface(string pluginName) |
| 136 | + { |
| 137 | + pName = pluginName; |
| 138 | + |
| 139 | + try |
| 140 | + { |
| 141 | + Log.Info("Starting EventGhost..."); |
| 142 | + |
| 143 | + // on startup ensure that there is at least one computer setup to receive eventghost commands |
| 144 | + OSAEObjectCollection computers = OSAEObjectManager.GetObjectsByType("COMPUTER"); |
| 145 | + |
| 146 | + // check to make sure that the Compuer Objecttype has the needed property |
| 147 | + //add eventghost property to the computer object type |
| 148 | + |
| 149 | + // need to implement check before i use the method below |
| 150 | + |
| 151 | + // OSAEObjectTypeManager.ObjectTypePropertyAdd("EventGhost Port", "String", "", "Computer", false); |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + foreach (OSAEObject acomputer in computers){ |
| 157 | + |
| 158 | + String computerName = acomputer.Name; |
| 159 | + String IPAddress = acomputer.Address; |
| 160 | + String eventGhostPort = (OSAEObjectPropertyManager.GetObjectPropertyValue(computerName, "EventGhost Port").Value); |
| 161 | + |
| 162 | + //if no port for property indicate that the default port of 333333 will be used |
| 163 | + if (eventGhostPort.Equals("", StringComparison.Ordinal)) { |
| 164 | + Log.Info("There is no port information for " + acomputer.Name + " the default port of 33333 will be used when this computer is called"); |
| 165 | + } |
| 166 | + //check to see if computer has address, if no log that "local host will be used" |
| 167 | + if ((acomputer.Address).Equals("", StringComparison.Ordinal)){ |
| 168 | + Log.Info("There is no address information for " + acomputer.Name + "localhost will be used for this location"); |
| 169 | + } |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + catch (Exception ex) |
| 175 | + { |
| 176 | + Log.Error("Error during RunInterface: " + ex.Message); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// OSA Plugin Interface - The plugin has been asked to shut down |
| 186 | + /// </summary> |
| 187 | + public override void Shutdown() |
| 188 | + { |
| 189 | + Log.Info("Stopping EventGhost..."); |
| 190 | + |
| 191 | + // Place any code required to shut down your plugin here leave empty if no action is required |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments