public async Task PublishSensorDataAsync(SensorMessage sensor, CancellationToken stoppingToken) { if (!_mqttClient.IsConnected) { await ConnectAsync(stoppingToken); } string fullTopic = $"{_settings.ApplicationRootNode}/sensors/{sensor.TagName}"; string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(sensor); await _mqttClient.PublishAsync(fullTopic, serialized); }
private static void Publish(object state) { bool active; Guid deviceGuid; lock (_Lock) { active = _Active; deviceGuid = _DeviceID; } if (active == false) { return; } int NumOfSensors = 8; if (_PrevDatapoints == null) { _PrevDatapoints = new SensorMessage[NumOfSensors]; } SensorMessage[] datapoints = new SensorMessage[NumOfSensors]; datapoints[0].SensorTypeID = 5; //Air Humidity datapoints[1].SensorTypeID = 6; //Air Temperature datapoints[2].SensorTypeID = 16; //WaterFlow datapoints[3].SensorTypeID = 19; //WaterLevel datapoints[4].SensorTypeID = 8; // WaterTemperature datapoints[5].SensorTypeID = 4; // Water PH datapoints[6].SensorTypeID = 13; // Water Conductivity datapoints[7].SensorTypeID = 11; // Ambient Light datapoints[0].value = RandomFluctuation(_PrevDatapoints[0].value, 3, 100, 0); datapoints[1].value = RandomFluctuation(_PrevDatapoints[1].value, 3, 50, -30); datapoints[2].value = RandomFluctuation(_PrevDatapoints[2].value, 1, 10, 0); datapoints[3].value = RandomFluctuation(_PrevDatapoints[3].value, 0.5f, 100, 0); datapoints[4].value = RandomFluctuation(_PrevDatapoints[4].value, 0.5f, 100, 0); datapoints[5].value = RandomFluctuation(_PrevDatapoints[5].value, 0.1f, 12, 2); datapoints[6].value = RandomFluctuation(_PrevDatapoints[6].value, 0.1f, 6, 0); datapoints[7].value = RandomFluctuation(_PrevDatapoints[7].value, 10, 100, 0); for (int i = 0; i < datapoints.Length; i++) { datapoints[i].duration = publishInterval; _PrevDatapoints[i].value = datapoints[i].value; } var KeyValue = new KeyValuePair <Guid, SensorMessage[]>(deviceGuid, datapoints); DatapointService.Instance.BufferAndSendReadings(KeyValue, $"Virtual device"); }
private void DoSomething() { double readFromSensor = 100; // Create a sensor message SensorMessage message = new SensorMessage { MessageType = typeof(double), TagName = this.GetTagName(), Timestamp = DateTime.UtcNow, Value = readFromSensor }; // send the message _onUpdate(message); }
private void OnTimerElepsed(object sender, ElapsedEventArgs e) { if (!_stoppingToken.IsCancellationRequested) { decimal tem = _random.Next(141421, 314160) / 10000M; SensorMessage message = new SensorMessage { TagName = TagName, Timestamp = DateTime.UtcNow, MessageType = typeof(decimal), Value = tem }; if (_onUpdate != null) { _onUpdate.Invoke(message); } } }
private async Task RunSimulator() { Random generator = new Random(); while (!cancellationToken.IsCancellationRequested) { double value = generator.NextDouble(); var message = new SensorMessage(currentConfig.DeviceSensorId, value); var props = model.CreateBasicProperties(); model.BasicPublish(streamConfig.Exchange, streamConfig.SensorQueue, props, message.ToRabbitMessage()); Console.WriteLine(message.ToString()); await Task.Delay(5000); } }
private void ToggleSensors(SensorMessage sensorsMessage) { this.Sensors.SensorSwitches.Id = sensorsMessage.Id; foreach (var sensorItem in sensorsMessage.Sensors) { if (sensorItem.A != null) { sensorsMessage.Type = 'A'; if (Accelerometer.GetDefault() == null) { throw new UnsupportedSensorException("Accelerometer does not exist"); } App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.A.Value.ToString() } }); this.Sensors.SensorSwitches.A = sensorItem.A.Value; } else if (sensorItem.G != null) { sensorsMessage.Type = 'G'; if (Gyrometer.GetDefault() == null) { throw new UnsupportedSensorException("Gyrometer does not exist"); } App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.G.Value.ToString() } }); this.Sensors.SensorSwitches.G = sensorItem.G.Value; } else if (sensorItem.M != null) { sensorsMessage.Type = 'M'; if (Compass.GetDefault() == null) { throw new UnsupportedSensorException("Compass does not exist"); } this.Sensors.SensorSwitches.M = sensorItem.M.Value; App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.M.Value.ToString() } }); this.Sensors.SensorSwitches.M = sensorItem.M.Value; } else if (sensorItem.L != null) { sensorsMessage.Type = 'L'; this.Sensors.SensorSwitches.L = sensorItem.L.Value; App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.L.Value.ToString() } }); } else if (sensorItem.Q != null) { sensorsMessage.Type = 'Q'; if (OrientationSensor.GetDefault() == null) { throw new UnsupportedSensorException("OrientationSensor does not exist"); } App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.Q.Value.ToString() } }); this.Sensors.SensorSwitches.Q = sensorItem.Q.Value; } else if (sensorItem.P != null) { sensorsMessage.Type = 'P'; if (LightSensor.GetDefault() == null) { throw new UnsupportedSensorException("LightSensor does not exist"); } App.Telemetry.TrackEvent( "Sensor", new Dictionary <string, string> { { "sensor.type", sensorsMessage.Type.ToString() }, { "sensor.value", sensorItem.P.Value.ToString() } }); this.Sensors.SensorSwitches.P = sensorItem.P.Value; } // outside of scope - applies to last only this.Sensors.SensorSwitches.Delta = sensorItem.Delta; this.Sensors.SensorSwitches.Interval = sensorItem.Interval; } this.Sensors.Start(); }
public void OnSensorMessage(SensorMessage sensorMessage) { pilotApi.SetPower(SafePower); }
private void OnSensorChanged(SensorMessage sensor) { _logger.LogInformation("Sensor changed::: {@sensor}", sensor); // Publish data _busClient.PublishSensorDataAsync(sensor, _cancelationToken).Wait(); }
private void ToggleSensors(SensorMessage sensorsMessage) { Sensors.SensorSwitches.Id = sensorsMessage.Id; foreach (var sensorItem in sensorsMessage.Sensors) { if (sensorItem.A != null) { sensorsMessage.Type = 'A'; if (Accelerometer.GetDefault() == null) { throw new UnsupportedSensorException(); } Sensors.SensorSwitches.A = sensorItem.A.Value; } else if (sensorItem.G != null) { sensorsMessage.Type = 'G'; if (Gyrometer.GetDefault() == null) { throw new UnsupportedSensorException(); } Sensors.SensorSwitches.G = sensorItem.G.Value; } else if (sensorItem.M != null) { sensorsMessage.Type = 'M'; if (Compass.GetDefault() == null) { throw new UnsupportedSensorException(); } Sensors.SensorSwitches.M = sensorItem.M.Value; } else if (sensorItem.L != null) { sensorsMessage.Type = 'L'; Sensors.SensorSwitches.L = sensorItem.L.Value; } else if (sensorItem.Q != null) { sensorsMessage.Type = 'Q'; if (OrientationSensor.GetDefault() == null) { throw new UnsupportedSensorException(); } Sensors.SensorSwitches.Q = sensorItem.Q.Value; } else if (sensorItem.P != null) { sensorsMessage.Type = 'P'; if (LightSensor.GetDefault() == null) { throw new UnsupportedSensorException(); } Sensors.SensorSwitches.P = sensorItem.P.Value; } //outside of scope - applies to last only Sensors.SensorSwitches.Delta = sensorItem.Delta; Sensors.SensorSwitches.Interval = sensorItem.Interval; } Sensors.Start(); }