Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1.  public interface IAlarm
  2.     {
  3.         string MilestoneCameraId { get; set; }
  4.         string AnalyticEventName { get; set; }
  5.         string Description { get; set; }
  6.     }
  7.  
  8.     public class BaseAlarm : IAlarm
  9.     {
  10.         public string MilestoneCameraId { get; set; }
  11.         public string AnalyticEventName { get; set; }
  12.         public string Description { get; set; }
  13.  
  14.         public BaseAlarm(string milestoneCameraId, string analyticEventName, string description)
  15.         {
  16.             MilestoneCameraId = milestoneCameraId;
  17.             AnalyticEventName = analyticEventName;
  18.             Description = description;
  19.         }
  20.     }
  21.  
  22.  
  23.     public class SendAlarm
  24.     {
  25.         public List<Item> LstItems;
  26.         Item _selectedItem;
  27.  
  28.         public SendAlarm()
  29.         {
  30.             LoadCams();
  31.         }
  32.  
  33.         public void LoadCams()
  34.         {
  35.             List<Item> lstItemsAll = Configuration.Instance.GetItemsBySearch("", 50, 5, out _);
  36.             LstItems = new List<Item>();
  37.             foreach (Item itm in lstItemsAll)
  38.             {
  39.                 //pouze vstupy
  40.                 if ((itm.FQID.Kind == Kind.InputEvent || itm.FQID.Kind == Kind.Output) && itm.HasChildren == HasChildren.No)
  41.                     LstItems.Add(itm);
  42.             }
  43.         }
  44.  
  45.         public List<Item> GetCams()
  46.         {
  47.             return LstItems;
  48.         }
  49.  
  50.  
  51.         private Item SearchItem(string milestoneId)
  52.         {
  53.             foreach (Item itm in LstItems)
  54.             {
  55.                 if (itm.Name.Contains(milestoneId))
  56.                 {
  57.                     return itm;
  58.                 }
  59.             }
  60.             return null;
  61.         }
  62.  
  63.         private bool IsItemSelected(IAlarm alarm)
  64.         {
  65.             _selectedItem = SearchItem(alarm.MilestoneCameraId);
  66.             if (_selectedItem == null)
  67.             {
  68.                 //kamera v Milestone dle IP nenalezena, zkusim obnovit seznam kamer
  69.                 LoadCams();
  70.                 //znovu prohledam dle IP
  71.                 _selectedItem = SearchItem(alarm.MilestoneCameraId);
  72.                 if (_selectedItem == null)
  73.                 {
  74.                     LogError("Nenalezena kamera pro udalost " + alarm.AnalyticEventName + " dle IP " + alarm.MilestoneCameraId + " v seznamu kamer v Milestone!");
  75.                     return false;
  76.                 }
  77.             }
  78.             return true;
  79.         }
  80.  
  81.         private EventSource CreateEventSource()
  82.         {
  83.             return new EventSource()
  84.             {
  85.                 // Send empty - it is possible to send without an eventsource, but the intended design is that there should be a source
  86.                 // the FQID is primamry used to match up the ObjectId with the a camera.
  87.                 FQID = _selectedItem.FQID,
  88.                 // If FQID is null, then the Name can be an IP address, and the event server will make a lookup to find the camera
  89.                 Name = _selectedItem.Name
  90.             };
  91.         }
  92.  
  93.         private EventHeader CreateEventHeader(IAlarm alarm)
  94.         {
  95.             return new EventHeader
  96.             {
  97.                 ID = Guid.NewGuid(),
  98.                 Type = "LoRaEvent",
  99.                 Timestamp = DateTime.Now,
  100.                 Message = alarm.AnalyticEventName,
  101.                 Source = CreateEventSource(),
  102.                 CustomTag = "TagFromLoraServer"
  103.             };
  104.         }
  105.  
  106.         private AnalyticsEvent CreateAnalyticEvent(IAlarm alarm)
  107.         {
  108.             AnalyticsEvent tmp = new AnalyticsEvent
  109.             {
  110.                 EventHeader = CreateEventHeader(alarm),
  111.                 Location = "Event on cam 1",
  112.                 Description = alarm.Type == EAlarmType.Battery ? ((IBatteryAlarm)alarm).Value : alarm.Description,
  113.                 Vendor = new Vendor
  114.                 {
  115.                     Name = alarm.Type.ToString()
  116.                 }
  117.             };
  118.             return tmp;
  119.         }
  120.  
  121.         public bool SendAlarm(IAlarm alarm)
  122.         {
  123.             try
  124.             {
  125.                 if (!IsItemSelected(alarm))
  126.                     return false;
  127.                 //if (chkIncludeOverlay.Checked)
  128.                 //{
  129.                 //    analyticEvent.ObjectList = new AnalyticsObjectList
  130.                 //    {
  131.                 //        GetRectangle()
  132.                 //    };
  133.                 //}
  134.                 EnvironmentManager.Instance.SendMessage(
  135.                     new VideoOS.Platform.Messaging.Message(MessageId.Server.NewEventCommand)
  136.                     { Data = CreateAnalyticEvent(alarm) });
  137.                 return true;
  138.             }
  139.             catch (Exception e)
  140.             {
  141.                 LogError("Milestone.sendAlarm()" + e.Message);
  142.                 return false;
  143.             }
  144.  
  145.         }
  146.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement