Beispiel #1
0
        // MOCK CLASS FOR TESTING...

        public override MReading GetReading()
        {
            MReading output = new MReading();

            output.Id     = "DEV-" + output.Id;
            output.TypeId = 1;

            var rand = new Random();

            output.Payload.Add(new KeyValuePair <String, String>("p1", rand.Next(200, 500).ToString()));
            output.Payload.Add(new KeyValuePair <String, String>("p2", "250"));
            output.Payload.Add(new KeyValuePair <String, String>("p3", "400"));
            output.Payload.Add(new KeyValuePair <String, String>("p4", "400"));

            output.Time = DateTime.UtcNow;

            RecentReadings.Add(output);

            if (RecentReadings.Count > 10)
            {
                RecentReadings.RemoveAt(0);
            }

            output.CreateHash();

            return(output);
        }
Beispiel #2
0
        public int CheckReading(MReading reading)
        {
            // GET [PLACEHOLDERS] FROM RULE...

            Regex rx = new Regex(@"\[.+?\]");

            // LOOP THROUGH DEVICE RULES, GET VALUES FROM READINGS AND EVALUATE THE RULE...
            // NO EVAL FUNCTION IN C# SO WE USE A DATATABLE AND PARSE THE SQL STRING AS A BOOL...

            foreach (MAlert alert in Alerts)
            {
                string toEval = alert.Rule;

                List <Match> ruleParams = rx.Matches(toEval).ToList();

                bool paramsApplied = true;

                foreach (Match m in ruleParams)
                {
                    string v = reading.Payload.Where(x => ("[" + x.Key + "]").ToUpper() == m.ToString().ToUpper()).Select(x => x.Value).FirstOrDefault();

                    if (v != null)
                    {
                        toEval = toEval.Replace(m.ToString(), v);
                    }
                    else
                    {
                        paramsApplied = false;
                    }
                }

                if (paramsApplied)
                {
                    System.Data.DataTable table = new System.Data.DataTable();
                    table.Columns.Add("expression", string.Empty.GetType(), toEval);
                    System.Data.DataRow row = table.NewRow();
                    table.Rows.Add(row);

                    alert.Eval = bool.Parse((string)row["expression"]);
                }
                else
                {
                    alert.Eval = false;
                }
            }

            int alertCount = Alerts.Where(x => x.Eval).Count();

            if (alertCount > 0)
            {
                Alerted = true;
            }

            return(alertCount);
        }
Beispiel #3
0
        public virtual MReading GetReading()
        {
            // Work to do here, search i2c bus for device, go through manual for sensor and find out the command to send, and how many bytes to expect back...

            //var myDevice = Pi.I2C.AddDevice(0x20);
            //myDevice.Write(0x44);
            //var response = myDevice.Read();

            MReading output = new MReading();

            return(output);
        }
Beispiel #4
0
        private async void ReadLoop()
        {
            // READ SENSOR AT DEFINED INTERVAL, AND QUEUE THE READING FOR SENDING TO THE HUB...

            while (true)
            {
                await Task.Delay(_device.Interval * 1000);

                MReading reading = _device.Sensor.GetReading();
                Console.WriteLine("Queued:" + JsonConvert.SerializeObject(reading));
                Console.WriteLine();
                _queue.Queue(reading);
            }
        }
Beispiel #5
0
        private async void SendLoop()
        {
            // PULL READINGS OFF THE QUEUE AND SEND THEM TO THE HUB...

            while (true)
            {
                MReading toSend = _queue.GetQueued();

                if (toSend != null)
                {
                    int alertCount = _device.CheckReading(toSend);

                    if (!_env.IsDevelopment())
                    {
                        _device.UpdatePins();
                    }

                    toSend.Alerts = alertCount;

                    string toSendString = JsonConvert.SerializeObject(toSend);

                    using var message = new Message(Encoding.ASCII.GetBytes(toSendString))
                          {
                              ContentType     = "application/json",
                              ContentEncoding = "utf-8",
                          };

                    message.Properties.Add("alerts", alertCount.ToString());

                    await _client.SendEventAsync(message);

                    if (alertCount > 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }

                    Console.WriteLine("Sent: " + JsonConvert.SerializeObject(toSend));

                    Console.ResetColor();

                    Console.WriteLine();
                }
            }
        }
Beispiel #6
0
 public void Queue(MReading reading)
 {
     _readings.Enqueue(reading);
 }