// 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()); }
public void Test_NBIRTH() { var spg = new SparkPlugGenerator(new MockLogger(), new MockDataService(), publishTopic); var inputMqtt = new MockMQTTWrapper(); var outputMqtt = new MockMQTTWrapper(); inputMqtt.MQTTConnected += spg.OnMQTTClientConnected; spg.MessageProcessed += outputMqtt.Publish; // mock connection event inputMqtt.Connect(); // expect NBIRTH message to be published Assert.Equal(1, outputMqtt.GetNumPublishedMessages()); var receivedMessages = outputMqtt.GetMessagesReceived(); Assert.Equal("spBv1.0/DTX/NBIRTH/PowerPilotCS", receivedMessages[0].Topic); //var payload = Payload.Parser.ParseFrom(lastMessage.Payload); //Assert.Equal((ulong)1, payload.Seq); }