public object Post(SensorReading request) { // Make sure we have a sensor reading queue for the sensor address if (!readings.ContainsKey(request.SensorAddress)) readings.Add(request.SensorAddress, new Queue<SensorReading>()); // Add the sensor reading readings[request.SensorAddress].Enqueue(request); // If our queue is full of readings, pop the last one off the queue if (readings[request.SensorAddress].Count > READINGS_TO_KEEP) readings[request.SensorAddress].Dequeue(); if (!zoneRelays.ContainsKey(request.SensorAddress)) zoneRelays.Add(request.SensorAddress, false); zoneRelays[request.SensorAddress] = request.RelayOn; // Notify any listeners of our sensor reading ServerEvents.NotifyChannel("sensor", request); // Now, based on the sensor reading, turn the sprinklers on or off // This is very basic logic, to demonstrate the capabilities if (zoneRelays.ContainsKey(request.SensorAddress)) { if (request.Reading > TURN_OFF_LEVEL && zoneRelays[request.SensorAddress] == true) ServerEvents.NotifyChannel("relay", new ZoneControl() { RelayOn = true }); else if (request.Reading < TURN_ON_LEVEL && zoneRelays[request.SensorAddress] == false) ServerEvents.NotifyChannel("relay", new ZoneControl() { RelayOn = false }); } return null; }
private async void ListenForData() { await device.ReadAPIPacket() .ContinueWith(frames => { foreach (var frame in frames.Result) { Debug.WriteLine("{2}: {0:X}\t{1,4}", frame.Address, frame.A1.ToString("X4"), DateTime.Now.ToString("mm:ss.fff")); SensorReading reading = new SensorReading() { SensorAddress = frame.Address.ToString("X"), RawReading = frame.A1, Timestamp = DateTime.Now, Reading = frame.A1 / 1024m, RelayOn = false }; if (relayPin != null) { GpioPinValue value = relayPin.Read(); reading.RelayOn = (value == GpioPinValue.High); } try { client.Post(reading); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } } }); ListenForData(); }