Creating an Event Source

At times you want to send events to an Ocularis server, and use the servers event distribution system to send notifications to operators and clients. This is – fortunately – a pretty simple operation, so let’s look at a sample.

We are going to create a small WinForm app that simulates two sensors with two rules each, without further ado, lets start by creating a simple WinForm app.

Then, add references to System.Messaging and EventTypes.dll (part of the Ocularis SDK). System.Messaging enables .NET to use Microsoft Message Queuing, while EventTypes provides the classes of object we will be sending to the Ocularis Server. Ocularis is using Microsoft Message Queue which provides a safe and reliable store- and forward mechanism. That means that even if the server is down, or unreachable, events will be sent when it comes back up. No additional code is necessary, but naturally the events will arrive with a delay if the server is unreachable for some period of time – but at least you will eventually be notified.

In your implementation file, you can add these two lines

using System.Messaging;
using EventTypes;

First things first; we need to define some GUIDs. We need one for the system itself, one for each sensor, and one for each rule – so all in all we need 7 GUIDs. I made mine like so:

private static string SystemGuid = "F23E4B1B-DB17-4aac-B26B-8447998347B1";

private static string Sensor1Guid = "4897F900-A1F5-4b6c-8683-6200FED219D9";
private static string Sensor1Rule1Guid = "B2D5D3C4-5724-46fa-A999-D77A221AAF76";
private static string Sensor1Rule2Guid = "4F6CF214-82B6-49fb-A2D9-1592B98803A1";

private static string Sensor2Guid = "9F6759F1-A6FB-4f9d-85C9-2D06D21B645A";
private static string Sensor2Rule1Guid = "01417A33-90DC-4498-8D84-3B0A25E02DD5";
private static string Sensor2Rule2Guid = "2948263F-F684-4afb-9EC6-A50079946299";

The GUIDs are created using Visual Studios GUIDGEN tool (Tools/Create GUID).

In the handler for [Register With Server] we need to create the code to register at the Ocularis Server. To do this we need to create an XML fragment that describes the system (this is just a string with some XML in it), wrap the fragment in an object and send it across using the Message Queue.

In my example, constructing the XML fragment is done like this:

string strConfig = "";
strConfig += "<source>";

// add sensor 1
strConfig += "<sensor name=\"sensor 1\">";
strConfig += "<guid>" + Sensor1Guid + "</guid>";

// add rules to sensor 1
strConfig += "<event><name>Rule 1</name><category>VMD</category>";
strConfig += "<guid>" + Sensor1Rule1Guid + "</guid></event>";
strConfig += "<event><name>Rule 2</name><category>VMD</category>";
strConfig += "<guid>" + Sensor1Rule2Guid + "</guid></event>";

strConfig += "</sensor>";

// add sensor 2
strConfig += "<sensor name=\"sensor 2\">";
strConfig += "<guid>" + Sensor2Guid + "</guid>";

// add rules to sensor 2
strConfig += "<event><name>Rule 1</name><category>VMD</category>";
strConfig += "<guid>" + Sensor2Rule1Guid + "</guid></event>";
strConfig += "<event><name>Rule 2</name><category>VMD</category>";
strConfig += "<guid>" + Sensor2Rule2Guid + "</guid></event>";

strConfig += "</sensor>";

strConfig += "</source>";

Now let us register at the server. In the application, the server input field is mapped to _ctrlServerIP (a textbox), so we proceed with this..

try
{
  // prepare event object
  EventTypes.SourceSetup src = new SourceSetup();
  src.m_strName = "Prescienta Event Demo";
  src.m_strGuid = SystemGuid;
  src.m_iSourceType = 0;
  src.m_strConfig = strConfig;

  // prepare event queue
  _serverQueue = new MessageQueue();
  _serverQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
  _serverQueue = new System.Messaging.MessageQueue("FormatName:DIRECT=TCP:" + _ctrlServerIP.Text + "\\PRIVATE$\\onssieventqueue");

  _serverQueue.Send(src);
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}

That’s it. If it all went well, we should see the source in the admin, with the two sensors and the rules attached.

To trigger an event, we simply create another object and send it across the server queue.

private void SendEvent(string strEventGuid)
{
  if (_serverQueue == null)
  {
    MessageBox.Show("No queue created");
    return;
  }

  try
  {
     // create an event object
     EventTypes.GenericEvent genEvent = new GenericEvent();
     genEvent.m_strDesc = "User Created Event";
     genEvent.m_strServerGuid = SystemGuid;
     genEvent.m_strEventGuid = strEventGuid;
     genEvent.m_strTimestamp = DateTime.Now.ToUniversalTime().ToString();

     // send event
     _serverQueue.Send(genEvent);
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

The solution files can be found at OnSSI’s SDK Page

Advertisements

Author: prescienta

Prescientas ruler

1 thought on “Creating an Event Source”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s