コード例 #1
0
        // 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());
        }
コード例 #2
0
        public void Test_Alarm_NormalVoltage()
        {
            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 alarmMessage = new Ppuplink.AlarmMessage()
            {
                Deviceeui = deviceEUI,
                Timesent  = 1612234855,
                Alarmtype = "normalvoltage",
                Phaseid   = 1,
                Value     = 237.3
            };

            // trigger DBIRTH and alarm
            var topic = $"application/powerpilot/uplink/alarm/{deviceEUI}";

            inputMqtt.InjectMessage(new MQTTMessageArgs(topic, alarmMessage.ToByteArray()));

            Thread.Sleep(10);
            var messages = outputMqtt.GetMessagesReceived();

            // expect alarm
            var alarm = messages.Find(m => m.Topic == $"spBv1.0/DTX/{SparkPlugMessageTypes.DDATA}/PowerPilotCS/{deviceEUI}");

            Assert.NotNull(alarm);
            var payload = Payload.Parser.ParseFrom(alarm.Payload);

            Assert.Single(payload.Metrics);
            Assert.Equal($"{Alarm.TYPE}/1/{Alarm.VOLTAGE_NORMAL}", payload.Metrics[0].Name);

            // should have extra DBIRTH for coming back to life
            Assert.Equal(3, outputMqtt.GetNumPublishedMessages());
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        // 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());
        }