Beispiel #1
0
 public static object Decode(Data data)
 {
     switch (data.Type)
     {
         case DataType.Bool:
             return BitConverter.ToBoolean(data.Value, 0);
         case DataType.Byte:
             return data.Value[0];
         case DataType.Bytes:
             return data.Value;
         case DataType.Double:
             return BitConverter.ToDouble(data.Value, 0);
         case DataType.Float:
             return BitConverter.ToSingle(data.Value, 0);
         case DataType.Int:
             return BitConverter.ToInt32(data.Value, 0);
         case DataType.Short:
             return BitConverter.ToInt16(data.Value, 0);
         case DataType.UInt:
             return BitConverter.ToUInt32(data.Value, 0);
         case DataType.UShort:
             return BitConverter.ToUInt16(data.Value, 0);
         case DataType.Long:
             return BitConverter.ToInt64(data.Value, 0);
         case DataType.ULong:
             return BitConverter.ToUInt64(data.Value, 0);
         case DataType.String:
             return System.Text.Encoding.UTF8.GetString(data.Value);
     }
     return null;
 }
 public bool Process(Data data, IAdapter adapter)
 {
     if (data.Device == device && (data.Service & service) > 0)
     {
         double value = Util.DataAdapter.GetGraphableValue(data);
         if (comparison == "<")
         {
             if (value < threshold) Email(value, adapter);
         }
         else if (comparison == "<=")
         {
             if (value <= threshold) Email(value, adapter);
         }
         else if (comparison == "==")
         {
             if (value == threshold) Email(value, adapter);
         }
         else if (comparison == ">=")
         {
             if (value >= threshold) Email(value, adapter);
         }
         else if (comparison == ">")
         {
             if (value > threshold) Email(value, adapter);
         }
     }
     return true;
 }
Beispiel #3
0
 public bool Create(Device device, Data.Connection connection)
 {
     if (device == null || device.Guid == Guid.Empty) return false;
     devices_mutex.WaitOne();
     if (devices.ContainsKey(device.Guid))
     {
         devices_mutex.ReleaseMutex();
         return false;
     }
     if (Data.StoredProcedure.DeviceCreate(device.Guid, device.Name, device.Description, device.Type, device.Profile, device.Configuration, connection))
     {
         device.Status = DeviceStatus.Offline;
         devices[device.Guid] = device;
         devices_mutex.ReleaseMutex();
         Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.DeviceCreation, Device = device, Timestamp = DateTime.Now });
         return true;
     }
     devices_mutex.ReleaseMutex();
     return false;
 }
Beispiel #4
0
 static void Read()
 {
     try
     {
         while (running)
         {
             string line = port.ReadLine();
             string[] parts = line.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
             if (parts.Length != 3) continue;
             Data temp1d = new Data { Device = temp1, Service = Util.ServiceType.TemperatureSensor_Temperature, Timestamp = DateTime.Now, Type = DataType.Short };
             Data light1d = new Data { Device = light1, Service = Util.ServiceType.LuminositySensor_Luminosity, Timestamp = DateTime.Now, Type = DataType.Short };
             Data light2d = new Data { Device = light2, Service = Util.ServiceType.LuminositySensor_Luminosity, Timestamp = DateTime.Now, Type = DataType.Short };
             Util.DataAdapter.Encode(Convert.ToInt16(parts[0]), temp1d);
             Util.DataAdapter.Encode(Convert.ToInt16(parts[1]), light1d);
             Util.DataAdapter.Encode(Convert.ToInt16(parts[2]), light2d);
             client.DataUpload(new Data[] { temp1d, light1d, light2d });
             Console.WriteLine("[Data] " + line);
         }
     }
     catch { }
 }
Beispiel #5
0
 public void DataSend(Data data)
 {
     DataSend(new Data[] { data });
 }
Beispiel #6
0
 public static void Encode(byte[] value, Data data)
 {
     data.Type = DataType.Bytes;
     data.Value = value;
 }
Beispiel #7
0
 public static void Encode(bool value, Data data)
 {
     data.Type = DataType.Bool;
     data.Value = BitConverter.GetBytes(value);
 }
Beispiel #8
0
 public static void Encode(float value, Data data)
 {
     data.Type = DataType.Float;
     data.Value = BitConverter.GetBytes(value);
 }
Beispiel #9
0
 /// <summary>
 /// Determine whether data is graphable
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public static bool IsGraphable(Data data)
 {
     switch (data.Type)
     {
         case DataType.Bool:
         case DataType.Byte:
         case DataType.Double:
         case DataType.Float:
         case DataType.Int:
         case DataType.Short:
         case DataType.UInt:
         case DataType.UShort:
         case DataType.Long:
         case DataType.ULong:
             return true;
     }
     return false;
 }
Beispiel #10
0
 /// <summary>
 /// Use this method to get graphable data value
 /// DataAdapter.GetGraphableValue(Data);
 /// </summary>
 /// <param name="data">data to be decoded</param>
 /// <returns>An always graphable double</returns>
 public static double GetGraphableValue(Data data)
 {
     object value = Decode(data);
     if(value.GetType() == typeof(int))
             return Convert.ToDouble(value);
     if (value.GetType() == typeof(uint))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(short))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(ushort))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(byte))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(bool))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(double))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(float))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(long))
         return Convert.ToDouble(value);
     if (value.GetType() == typeof(ulong))
         return Convert.ToDouble(value);
     return 0;
 }
Beispiel #11
0
 private void DataReceivedHandler(Data data)
 {
     devices_mutex.WaitOne();
     if (!devices.ContainsKey(data.Device))
     {
         devices_mutex.ReleaseMutex();
         return;
     }
     devices_mutex.ReleaseMutex();
     if (DataReceived != null)
         DataReceived(this, data);
 }
 public bool Process(Data data, IAdapter adapter)
 {
     adapter.AddToStatistics(data);
     return true;
 }
Beispiel #13
0
 void device_timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     if (device == null || client == null) return;
     if (System.Configuration.ConfigurationManager.AppSettings["Reactivity.Nodes.Computer.Feedback"] != "true") return;
     //send data back
     try
     {
         Data cpu_data = new Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ComputerNode_CPU };
         Util.DataAdapter.Encode(cpu.NextValue(), cpu_data);
         Data memory_data = new Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ComputerNode_Memory };
         Util.DataAdapter.Encode(memory.NextValue(), memory_data);
         if (client != null && client.IsOpen)
             client.DataUpload(new Data[] { cpu_data, memory_data });
     }
     catch { TryReset(); }
 }
Beispiel #14
0
 void client_DataReceived(object source, Data data)
 {
     if (device == null) return;
     if (data.Device != device.Guid) return;
     Console.WriteLine("Received Command: " + Convert.ToInt32(Util.DataAdapter.Decode(data)).ToString());
     if (System.Configuration.ConfigurationManager.AppSettings["Reactivity.Nodes.Computer.DenyControl"] == "true") return;
     try
     {
         switch (Convert.ToInt32(Util.DataAdapter.Decode(data)))
         {
             case 1:
                 WindowsController.ExitWindows(RestartOptions.LogOff, false);
                 break;
             case -1:
                 WindowsController.ExitWindows(RestartOptions.LogOff, true);
                 break;
             case 2:
                 WindowsController.ExitWindows(RestartOptions.Reboot, false);
                 break;
             case -2:
                 WindowsController.ExitWindows(RestartOptions.Reboot, true);
                 break;
             case 3:
                 WindowsController.ExitWindows(RestartOptions.Suspend, false);
                 break;
             case -3:
                 WindowsController.ExitWindows(RestartOptions.Suspend, true);
                 break;
             case 4:
                 WindowsController.ExitWindows(RestartOptions.Hibernate, false);
                 break;
             case -4:
                 WindowsController.ExitWindows(RestartOptions.Hibernate, true);
                 break;
             case 5:
                 WindowsController.ExitWindows(RestartOptions.ShutDown, false);
                 break;
             case -5:
                 WindowsController.ExitWindows(RestartOptions.ShutDown, true);
                 break;
             case 6:
                 WindowsController.ExitWindows(RestartOptions.PowerOff, false);
                 break;
             case -6:
                 WindowsController.ExitWindows(RestartOptions.PowerOff, true);
                 break;
         }
         this.Stop();
     }
     catch { }
 }
 public bool Process(Data data, IAdapter adapter)
 {
     adapter.FeedSubscription(data);
     return true;
 }
Beispiel #16
0
 public bool Remove(Guid device, Data.Connection connection)
 {
     if (device == Guid.Empty) return false;
     devices_mutex.WaitOne();
     if (!devices.ContainsKey(device))
     {
         devices_mutex.ReleaseMutex();
         return false;
     }
     if (Data.StoredProcedure.DeviceRemoveByGuid(device, connection))
     {
         Guid node = Guid.Empty;
         Device oldDevice = devices[device];
         devices.Remove(device);
         devices_mutex.ReleaseMutex();
         Nodes.NodeSession.Notify(new NodeEvent { Type = NodeEventType.DeviceDeregister, Guid = device, Timestamp = DateTime.Now });
         SubscriptionManager.Instance.UpdateDevice(device);
         Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.DeviceRemoval, Device = oldDevice, Timestamp = DateTime.Now });
         return true;
     }
     devices_mutex.ReleaseMutex();
     return false;
 }
Beispiel #17
0
 public bool Update(Device device, Data.Connection connection)
 {
     if (device == null || device.Guid == Guid.Empty) return false;
     devices_mutex.WaitOne();
     if (!devices.ContainsKey(device.Guid))
     {
         devices_mutex.ReleaseMutex();
         return false;
     }
     if (Data.StoredProcedure.DeviceUpdateByGuid(device.Guid, device.Name, device.Description, device.Type, device.Profile, device.Configuration, connection))
     {
         devices[device.Guid].Name = device.Name;
         devices[device.Guid].Description = device.Description;
         devices[device.Guid].Type = device.Type;
         devices[device.Guid].Profile = device.Profile;
         devices[device.Guid].Configuration = device.Configuration;
         device = devices[device.Guid];
         devices_mutex.ReleaseMutex();
         Nodes.NodeSession.Notify(new NodeEvent { Type = NodeEventType.DeviceUpdate, Device = device, Timestamp = DateTime.Now });
         Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.DeviceUpdate, Device = device, Timestamp = DateTime.Now });
         return true;
     }
     devices_mutex.ReleaseMutex();
     return false;
 }
Beispiel #18
0
 public void DataUpload(Data[] data)
 {
     SessionCheck();
     service.DataUpload(data, session);
 }
Beispiel #19
0
 public static void Encode(ulong value, Data data)
 {
     data.Type = DataType.String;
     data.Value = BitConverter.GetBytes(value);
 }
Beispiel #20
0
 public void DataUpload(Data data)
 {
     DataUpload(new Data[] { data });
 }
Beispiel #21
0
 /// <summary>
 /// Add a new rule to the chain
 /// </summary>
 /// <param name="rule"></param>
 /// <param name="connection"></param>
 /// <returns></returns>
 public int RuleCreate(Rule rule, Data.Connection connection)
 {
     if (rule == null) return 0;
     rules_mutex.WaitOne();
     rule.ID = Data.StoredProcedure.RuleCreate(rule.Name, rule.Description, rule.Configuration, rule.Precedence, rule.IsEnabled, connection);
     if (rule.ID > 0)
     {
         rule.Status = RuleStatus.Unknown;
         rules.Add(rule);
         rules.Sort();
     }
     rules_mutex.ReleaseMutex();
     if (rule.ID > 0)
     {
         Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.RuleCreation, Rule = rule, Timestamp = DateTime.Now });
         Reload();
     }
     return rule.ID;
 }
Beispiel #22
0
 /// <summary>
 /// Remove a rule
 /// </summary>
 /// <param name="id"></param>
 /// <param name="connection"></param>
 public bool RuleRemove(int id, Data.Connection connection)
 {
     if (id <= 0) return false;
     rules_mutex.WaitOne();
     Rule rule = null;
     //find it first
     for (int i = 0; i < rules.Count; i++)
         if (rules[i].ID == id)
         {
             rule = rules[i];
             break;
         }
     if (rule != null)
     {
         //remove from buffer and database
         if (Data.StoredProcedure.RuleRemoveById(id, connection))
         {
             rules.Remove(rule);
             rules.Sort();
             rules_mutex.ReleaseMutex();
             Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.RuleRemoval, Rule = rule, Timestamp = DateTime.Now });
             Reload();
             // clear out cache
             modules_mutex.WaitOne();
             hashes.Remove(rule.ID);
             assemblies.Remove(rule.ID);
             modules_mutex.ReleaseMutex();
             return true;
         }
     }
     rules_mutex.ReleaseMutex();
     return false;
 }
Beispiel #23
0
 public static void Encode(double value, Data data)
 {
     data.Type = DataType.Double;
     data.Value = BitConverter.GetBytes(value);
 }
Beispiel #24
0
 /// <summary>
 /// Update the rule
 /// </summary>
 /// <param name="rule"></param>
 /// <param name="connection"></param>
 /// <returns></returns>
 public bool RuleUpdate(Rule rule, Data.Connection connection)
 {
     if (rule == null || rule.ID <= 0) return false;
     rules_mutex.WaitOne();
     Rule oldRule = null;
     //find it first
     for (int i = 0; i < rules.Count; i++)
         if (rules[i].ID == rule.ID)
         {
             oldRule = rules[i];
             break;
         }
     if (oldRule != null)
     {
         // update rule in database
         if (Data.StoredProcedure.RuleUpdateById(rule.ID, rule.Name, rule.Description, rule.Configuration, rule.Precedence, rule.IsEnabled, connection))
         {
             rules.Remove(oldRule);
             rules.Add(rule);
             rules.Sort();
             rules_mutex.ReleaseMutex();
             Clients.ClientSession.Notify(new ClientEvent { Type = ClientEventType.RuleUpdate, Rule = rule, Timestamp = DateTime.Now });
             Reload();
             return true;
         }
     }
     rules_mutex.ReleaseMutex();
     return false;
 }
Beispiel #25
0
 public static void Encode(ushort value, Data data)
 {
     data.Type = DataType.Short;
     data.Value = BitConverter.GetBytes(value);
 }
Beispiel #26
0
 private void DataReceived(Guid device, short service, DataType type, byte[] args)
 {
     Console.WriteLine("[Receive] Data type = " + type.ToString() + ", service = " + service.ToString());
     if (!client.DeviceRegister(device)) return;
     Console.WriteLine("[Receive] Device has been registered, go on.");
     Data data = new Data { Device = device, Service = service, Type = type, Timestamp = DateTime.Now};
     switch(type)
     {
         case DataType.Short:
             Util.DataAdapter.Encode(BitConverter.ToInt16(args, 0), data);
             Console.WriteLine("[Receive] Short data: " + BitConverter.ToInt16(args, 0).ToString());
             break;
         case DataType.Bool:
             Util.DataAdapter.Encode(args[0] > 0, data);
             Console.WriteLine("[Receive] Boolean data: " + (args[0] > 0).ToString());
             break;
         case DataType.Bytes:
             Util.DataAdapter.Encode(args, data);
             break;
         default:
             data.Value = args;
             break;
     }
     client.DataUpload(data);
 }
Beispiel #27
0
 public static void Encode(byte value, Data data)
 {
     data.Type = DataType.Byte;
     data.Value = new byte[] { value };
 }
Beispiel #28
0
 private void SubscriptionNotifiedHandler(Guid subscription, Data data)
 {
     if (!UserIsLoggedIn) return;
     if (subscriptions.ContainsKey(subscription) && subscriptions_callbacks[subscription] != null)
         subscriptions_callbacks[subscription](subscriptions[subscription], data);
 }
Beispiel #29
0
 public static void Encode(string value, Data data)
 {
     data.Type = DataType.String;
     data.Value = System.Text.Encoding.UTF8.GetBytes(value);
 }
Beispiel #30
0
 public void DataSend(Data[] data)
 {
     SessionCheck();
     if (!UserIsLoggedIn || !UserHasPermission(User.PERMISSION_CONTROL)) return;
     service.DataSend(data, session);
 }