//Rule signing code, don't modify:213FF4A152684885 using System; using log4net; using Perytons.Analyzer.Core; using Perytons.Analyzer.Core.Graphs; using System.Collections.Generic; using System.Drawing; using System.Net.Sockets; using System.Net; using System.Runtime.InteropServices; using Perytons.Analyzer.Core.Preferences; // This rule show how to send some of the received messages to UPD namespace Perytons.Analyzer.Core.Monitoring { public class SendToUDP : RuleBase // don't remove this line { // Declare your global rule variables here private UdpClient client; private IPEndPoint endpoint; // This method is called once, before message processing begins public SendToUDP(GraphBuilder builder) : base(builder) // don't remove this line { // open udp port string connectionString = PreferencesHandler.Instance.ConnectionString; // get connection string from preferences if (string.IsNullOrEmpty(connectionString)) { Loggers.EventsLogger.Error("Connection string is empty"); return; } string[] parts = connectionString.Split(':'); int port; if (!int.TryParse(parts[1], out port)) { Loggers.EventsLogger.Info("Failed to open UDP connection, bad IP/port address: " + connectionString + " (use 'IP':port convention, for example 192.168.1.1:100 or www.perytons.com:100)"); return; } client = new UdpClient(); try { client.Connect(parts[0], port); } catch (Exception e) { Loggers.EventsLogger.Info("Couldn't open the destination port: " + connectionString, e); return; } } // define udp message structure [StructLayout(LayoutKind.Sequential, Pack = 1)] struct UDPMessage { public byte UW; // (0x055 indicates message start). public uint MessageBumber; public byte Rssi; // signal strength public long Time; // start time in usec public byte Len; // payload length in bytes [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] public byte[] MacPayload; } // This method will be called on each message received, if // this is a relevant message send it over the UDP public override void DoRule(Packet packet) { packet.AddToTimeView();// add all messages to time view if ((client != null) && (packet.Field("DeviceIndex").Exist)) { if (packet.Field("DeviceIndex").Value == 16) // device index = 1 { // build UDP message UDPMessage message = new UDPMessage(); message.UW = 0x055; // (0x055 indicates message start). message.MessageBumber = (uint)packet.Field("InfoMessageNum").Value; message.Rssi = (byte)packet.Field("InfoSignalStrength").Value; message.Time = packet.Field("InfoStartTime").Value; // read payload string, convert to array of bytes string payload = packet.Field("MacDataList").ValueText.Replace(" ", "");// get the payload data without spaces message.Len = (byte)(payload.Length / 2); message.MacPayload = new byte[128]; int discarded; byte[] b = HexEncoding.GetBytesReverse(payload, out discarded); b.CopyTo(message.MacPayload,0); // Turn into bytes byte[] data = Utils.StructToBytes(message); // send message over UDP client.Send(data, data.Length); } } base.DoRule(packet); // don't remove this line } // close UDP port public override void Cleanup() { if (client != null) { client.Close(); // close UDP port } base.Cleanup(); // don't remove this line } } }