// Simulate service starting up and receiving messages public void Test_NBIRTH_DBIRTH_Energy() { var spg = new SparkPlugGenerator(new MockLogger(), new MockDataService(), publishTopic); var inputMqtt = new MockMQTTWrapper(); var outputMqtt = new MockMQTTWrapper(); var deviceEUI = "123"; inputMqtt.MQTTConnected += spg.OnMQTTClientConnected; inputMqtt.MessageReceived += spg.ProcessMessage; spg.MessageProcessed += outputMqtt.Publish; outputMqtt.MessagePublished += OnMessagePublished; // trigger NBIRTH inputMqtt.Connect(); var energyMsg = new Ppuplink.EnergyMessage() { Deviceeui = deviceEUI, Phaseid = 1, Timesent = 1612234855, Energyexportreactive = 1, Energyexportreal = 1, Energyimportreactive = 1, Energyimportreal = 1 }; // trigger DBIRTH and energy var topic = $"application/powerpilot/uplink/energy/{deviceEUI}"; inputMqtt.InjectMessage(new MQTTMessageArgs(topic, energyMsg.ToByteArray())); Thread.Sleep(10); var messages = outputMqtt.GetMessagesReceived(); // expect NBIRTH var nbirth = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.NBIRTH}/PowerPilotCS"); Assert.NotNull(nbirth); //expect DBIRTH var dbirth = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DBIRTH}/PowerPilotCS/{deviceEUI}"); Assert.NotNull(dbirth); var payload = Payload.Parser.ParseFrom(dbirth.Payload); Assert.Equal(53, payload.Metrics.Count); //expect energy var energy = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DDATA}/PowerPilotCS/{deviceEUI}"); Assert.NotNull(energy); payload = Payload.Parser.ParseFrom(energy.Payload); Assert.Equal(4, payload.Metrics.Count); Assert.Equal(3, outputMqtt.GetNumPublishedMessages()); }
private async Task <Payload> BuildDBIRTH(PowerPilotCommon.Models.PostgreSQL.Connection connection) { Payload payload = new Payload() { Seq = GetSeq(), Timestamp = (ulong)DataServiceClient.ConvertToTimestamp(DateTime.Now) }; if (connection.Line1 > 0) { payload = await GetPhaseMessages(connection, payload, 1); } if (connection.Line2.HasValue && connection.Line2 > 0) { payload = await GetPhaseMessages(connection, payload, 2); } if (connection.Line3.HasValue && connection.Line3 > 0) { payload = await GetPhaseMessages(connection, payload, 3); } var energy = await _dataService.GetEnergy(connection.DeviceEUI); if (energy != null) { energy = new Ppuplink.EnergyMessage() { Timesent = (ulong)DataServiceClient.ConvertToTimestamp(DateTime.Now), }; } payload = AddEnergyMetrics(energy, payload); var uplink = await _dataService.GetUplink(connection.DeviceEUI); if (uplink != null) { uplink = new Ppuplink.UplinkMessage() { Timesent = (ulong)DataServiceClient.ConvertToTimestamp(DateTime.Now), }; } payload = AddUplinkMetrics(uplink, payload); return(payload); }
private Payload AddEnergyMetrics(Ppuplink.EnergyMessage energyMessage, Payload payload) { //no phase for energy message var phaseID = ""; ulong timestamp; if (energyMessage == null) { timestamp = (ulong)DataServiceClient.ConvertToTimestamp(DateTime.Now); } else { timestamp = energyMessage.Timesent; } payload.Metrics.Add(GetMetricDouble(Energy.TYPE, phaseID, Energy.IMPORT_ACTIVE, energyMessage?.Energyimportreal, timestamp)); payload.Metrics.Add(GetMetricDouble(Energy.TYPE, phaseID, Energy.IMPORT_REACTIVE, energyMessage?.Energyimportreactive, timestamp)); payload.Metrics.Add(GetMetricDouble(Energy.TYPE, phaseID, Energy.EXPORT_ACTIVE, energyMessage?.Energyexportreal, timestamp)); payload.Metrics.Add(GetMetricDouble(Energy.TYPE, phaseID, Energy.EXPORT_REACTIVE, energyMessage?.Energyexportreactive, timestamp)); return(payload); }
// Test that a DBIRTH can be sent if there ARE existing messages in the db public void Test_DBIRTH_ExistingValues() { var dataService = new MockDataService(); var spg = new SparkPlugGenerator(new MockLogger(), dataService, publishTopic); var inputMqtt = new MockMQTTWrapper(); var outputMqtt = new MockMQTTWrapper(); var deviceEUI = "123"; inputMqtt.MessageReceived += spg.ProcessMessage; spg.MessageProcessed += outputMqtt.Publish; outputMqtt.MessagePublished += OnMessagePublished; var energyMsg = new Ppuplink.EnergyMessage() { Deviceeui = deviceEUI, Phaseid = 1, Timesent = 1612234855, Energyexportreactive = 1, Energyexportreal = 1, Energyimportreactive = 1, Energyimportreal = 1 }; dataService.Energy = energyMsg; var pqMsg = new Ppuplink.PQMessage() { Deviceeui = deviceEUI, Timesent = 1612234855, Currentmax = 10, Voltagemax = 250.0, Phaseid = 1 }; dataService.PQ = pqMsg; var uplinkMsg = new Ppuplink.UplinkMessage() { Deviceeui = deviceEUI, Timesent = 1612234855, Fctn = 1, Messageid = 1, Rssi = 10, Snr = -10, Frequency = 8000, Messagetype = 32 }; dataService.Uplink = uplinkMsg; var voltageStats = new Ppuplink.VoltageStatsMessage() { Deviceeui = deviceEUI, Timesent = 1612234855, Starttime = "2021/01/01", Stoptime = "2021/01/02", H0213 = 1, H213215 = 1 }; dataService.Uplink = uplinkMsg; // trigger DBIRTH and energy var topic = $"application/powerpilot/uplink/energy/{deviceEUI}"; inputMqtt.InjectMessage(new MQTTMessageArgs(topic, energyMsg.ToByteArray())); Thread.Sleep(10); var messages = outputMqtt.GetMessagesReceived(); // expect DBIRTH var dbirth = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DBIRTH}/PowerPilotCS/{deviceEUI}"); Assert.NotNull(dbirth); var payload = Payload.Parser.ParseFrom(dbirth.Payload); Assert.Equal(53, payload.Metrics.Count); // expect energy var energy = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DDATA}/PowerPilotCS/{deviceEUI}"); Assert.NotNull(energy); payload = Payload.Parser.ParseFrom(energy.Payload); Assert.Equal(4, payload.Metrics.Count); Assert.Equal(2, outputMqtt.GetNumPublishedMessages()); }
public void Test_REBIRTH() { var dataService = new MockDataService(); var spg = new SparkPlugGenerator(new MockLogger(), dataService, publishTopic); var inputMqtt = new MockMQTTWrapper(); var outputMqtt = new MockMQTTWrapper(); var deviceEUI = "123"; inputMqtt.MessageReceived += spg.ProcessMessage; spg.MessageProcessed += outputMqtt.Publish; outputMqtt.MessagePublished += OnMessagePublished; // trigger initial DBIRTH var energyMsg = new Ppuplink.EnergyMessage() { Deviceeui = deviceEUI, Phaseid = 1, Timesent = 1612234855, Energyexportreactive = 1, Energyexportreal = 1, Energyimportreactive = 1, Energyimportreal = 1 }; // trigger DBIRTH and energy var topic = $"application/powerpilot/uplink/energy/{deviceEUI}"; inputMqtt.InjectMessage(new MQTTMessageArgs(topic, energyMsg.ToByteArray())); Thread.Sleep(10); var messages = outputMqtt.GetMessagesReceived(); // expect energy var energy = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DDATA}/PowerPilotCS/{deviceEUI}"); Assert.NotNull(energy); var payload = Payload.Parser.ParseFrom(energy.Payload); Assert.Equal(4, payload.Metrics.Count); // send REBIRTH cmd var metric = new Payload.Types.Metric() { Name = $"{DeviceCommands.TYPE}/{DeviceCommands.REBIRTH}", Timestamp = 1612234855 }; Payload cmdPayload = new Payload() { Seq = 1, Timestamp = (ulong)DateTimeOffset.Now.ToUnixTimeSeconds(), }; cmdPayload.Metrics.Add(metric); topic = $"spBv1.0/DTX/{SparkPlugMessageTypes.DCMD}/PowerPilotCS/{deviceEUI}"; inputMqtt.InjectMessage(new MQTTMessageArgs(topic, cmdPayload.ToByteArray())); Thread.Sleep(10); messages = outputMqtt.GetMessagesReceived(); // expect second DBIRTH var dbirths = messages.Where(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DBIRTH}/PowerPilotCS/{deviceEUI}"); Assert.Equal(2, dbirths.Count()); }