Esempio n. 1
0
        public void TransformTestWithoutUnit()
        {
            string jsonContent        = @"{
                  'gwy': 'b19IoTWorx',
                  'name': 'Device_190130_AV_67',
                  'value': '400.000000',
                  'timestamp': '2019-06-10T23:48:43.667Z',
                  'status': true
                }";
            var    telemetryDataPoint = JsonConvert.DeserializeObject <BACNetTelemetryMsg>(jsonContent, _jsonSerializerSettings);
            var    eventData          = new EventData(new byte[0]);
            BACNetIoTHubMessage bacNetIoTHubMessage = new BACNetIoTHubMessage(telemetryDataPoint, eventData.SystemProperties, eventData.Properties);

            DeviceDocument inputDeviceDocument = new DeviceDocument()
            {
                id = telemetryDataPoint.name
            };
            DeviceDocument output = IoTWorxBuildingDataProcessingFunction.ApplyTelemetryToDeviceDoc(bacNetIoTHubMessage, inputDeviceDocument);

            output.EventEnqueuedUtcTime.ShouldBe(DateTime.UtcNow, TimeSpan.FromMilliseconds(1000));
            output.PresentValue.ShouldBe("400.000000");
            output.ValueUnits.ShouldBeEmpty();
            output.DeviceTimestamp.ShouldBe(DateTime.Parse("2019-06-10T23:48:43.667Z"));
            output.DeviceStatus.ToLower().ShouldBe(bool.TrueString.ToLower());
        }
Esempio n. 2
0
        public void TransformTestWithUnit()
        {
            string jsonContent =
                @"{
                    'gwy': 'b19IoTWorx',
                    'name': 'Device_190131_AV_90',
                    'value': '73.000000 DEGREES-FAHRENHEIT',
                    'timestamp': '2019-06-10T23:48:43.667Z',
                    'status': true
                }";
            var telemetryDataPoint = JsonConvert.DeserializeObject <BACNetTelemetryMsg>(jsonContent, _jsonSerializerSettings);
            var eventData          = new EventData(new byte[0]);
            BACNetIoTHubMessage bacNetIoTHubMessage = new BACNetIoTHubMessage(telemetryDataPoint, eventData.SystemProperties, eventData.Properties);

            DeviceDocument inputDeviceDocument = new DeviceDocument()
            {
                id         = telemetryDataPoint.name,
                DeviceName = "190131",
                ObjectType = "AnalogValue",
                Instance   = 90
            };
            DeviceDocument output = IoTWorxBuildingDataProcessingFunction.ApplyTelemetryToDeviceDoc(bacNetIoTHubMessage, inputDeviceDocument);

            //output.Gateway.ShouldBe("b19IoTWorx");
            output.id.ShouldBe((string)telemetryDataPoint.name);
            output.EventEnqueuedUtcTime.ShouldBe(DateTime.UtcNow, TimeSpan.FromMilliseconds(1000));
            output.DeviceName.ShouldBe("190131");
            output.ObjectType.ShouldBe("AnalogValue");
            output.Instance.ShouldBe(90);
            output.PresentValue.ShouldBe("73.000000");
            output.ValueUnits.ShouldBe("DEGREES-FAHRENHEIT");
            output.DeviceTimestamp.ShouldBe(DateTime.Parse("2019-06-10T23:48:43.667Z"));
            output.DeviceStatus.ToLower().ShouldBe(bool.TrueString.ToLower());
            Console.Write(JsonConvert.SerializeObject(output));
        }
        public async Task FlagsUnprovisionedDevicesTest()
        {
            BACNetTelemetryMsg ioTWorxBacNetMsg = new BACNetTelemetryMsg()
            {
                name      = "Device_190130_AV_67",
                value     = "180",
                status    = "",
                timestamp = DateTime.UtcNow.ToString("o")
            };
            BACNetIoTHubMessage iotBacNetEventHubMessage = new BACNetIoTHubMessage(
                ioTWorxBacNetMsg,
                ed.SystemProperties, new Dictionary <string, object>());

            var messages = new List <BACNetIoTHubMessage>()
            {
                { iotBacNetEventHubMessage }
            };

            await IoTWorxBuildingDataProcessingFunction.HandleMessageBatch(
                messages, provisionedDeviceDocuments, outputDeviceDocs, outputEvents, unprovisionedDeviceOutput,
                LoggerUtils.Logger <object>(), new CancellationToken());

            //Unprovisioned devices should be written to the DocDb and flagged accordingly
            outputDeviceDocs.Items.Count.ShouldBe(1);
            DeviceDocument deviceDocument = outputDeviceDocs.Items[0];

            deviceDocument.id.ShouldBe(ioTWorxBacNetMsg.name);
            deviceDocument.PresentValue.ShouldBe(ioTWorxBacNetMsg.value);
            deviceDocument.DeviceStatus.ShouldBe("Unprovisioned");

            unprovisionedDeviceOutput.Items.Count.ShouldBe(1);

            //Don't send Unprovisioned device events downstream
            outputEvents.Items.ShouldBeEmpty();
        }
        public async Task OnlyProvisionedDeviceDataSentToEventHub()
        {
            //Create 2 messages, one for a provisioned and the other an unprovisioned device
            BACNetTelemetryMsg ioTWorxBacNetMsg = new BACNetTelemetryMsg()
            {
                name      = "Device_190130_AV_67",
                value     = "180",
                status    = "true",
                timestamp = DateTime.UtcNow.ToString("o")
            };
            BACNetIoTHubMessage provisionedIoTBacNetEventHubMessage = new BACNetIoTHubMessage(
                ioTWorxBacNetMsg,
                ed.SystemProperties, new Dictionary <string, object>());

            //Clone Provisioned into Unprovisioned and modify the name
            var unprovisionedIoTBacNetEventHubMessage =
                JsonConvert.DeserializeObject <BACNetIoTHubMessage>(JsonConvert.SerializeObject(provisionedIoTBacNetEventHubMessage));

            unprovisionedIoTBacNetEventHubMessage.BACNetMsg.name = "unprovisioned_device_name";

            var messages = new List <BACNetIoTHubMessage>()
            {
                { provisionedIoTBacNetEventHubMessage },
                { unprovisionedIoTBacNetEventHubMessage }
            };

            //Ensure device 1 is "Provisioned" (and 2 isn't)
            var provisionedDeviceDocuments = new List <DeviceDocument>()
            {
                { new DeviceDocument()
                  {
                      id = provisionedIoTBacNetEventHubMessage.BACNetMsg.name, DeviceStatus = "Provisioned"
                  } }
            };

            await IoTWorxBuildingDataProcessingFunction.HandleMessageBatch(
                messages, provisionedDeviceDocuments, outputDeviceDocs, outputEvents, unprovisionedDeviceOutput,
                LoggerUtils.Logger <object>(), new CancellationToken());

            //Unprovisioned devices should be written to the DocDb and flagged accordingly
            outputDeviceDocs.Items.Count.ShouldBe(2);
            unprovisionedDeviceOutput.Items.Count.ShouldBe(1);

            DeviceDocument deviceDocument = outputDeviceDocs.Items[0];

            deviceDocument.id.ShouldBe(provisionedIoTBacNetEventHubMessage.BACNetMsg.name);
            deviceDocument.PresentValue.ShouldBe(provisionedIoTBacNetEventHubMessage.BACNetMsg.value);
            deviceDocument.DeviceStatus.ShouldBe("true");

            DeviceDocument unprovisionedDeviceDocument = outputDeviceDocs.Items[1];

            unprovisionedDeviceDocument.id.ShouldBe(unprovisionedIoTBacNetEventHubMessage.BACNetMsg.name);
            unprovisionedDeviceDocument.PresentValue.ShouldBe(unprovisionedIoTBacNetEventHubMessage.BACNetMsg.value);
            unprovisionedDeviceDocument.DeviceStatus.ShouldBe("Unprovisioned");
        }
        public async Task UnprovisionedRemainsTest()
        {
            BACNetTelemetryMsg ioTWorxBacNetMsg = new BACNetTelemetryMsg()
            {
                name      = "Device_190130_AV_67",
                value     = "180",
                status    = "true",
                timestamp = DateTime.UtcNow.ToString("o")
            };
            BACNetIoTHubMessage iotBacNetEventHubMessage = new BACNetIoTHubMessage(
                ioTWorxBacNetMsg,
                ed.SystemProperties, new Dictionary <string, object>());

            var messages = new List <BACNetIoTHubMessage>()
            {
                { iotBacNetEventHubMessage }
            };

            //Ensure this device is Provisioned as "Unprovisioned" and stays that way
            var provisionedDeviceDocuments = new List <DeviceDocument>()
            {
                { new DeviceDocument()
                  {
                      id = ioTWorxBacNetMsg.name, DeviceStatus = "Unprovisioned"
                  } }
            };

            await IoTWorxBuildingDataProcessingFunction.HandleMessageBatch(
                messages, provisionedDeviceDocuments, outputDeviceDocs, outputEvents, unprovisionedDeviceOutput,
                LoggerUtils.Logger <object>(), new CancellationToken());

            //Unprovisioned devices should be written to the DocDb and remain in Unprovisioned state
            outputDeviceDocs.Items.Count.ShouldBe(1);
            DeviceDocument outDeviceDocument = outputDeviceDocs.Items[0];

            outDeviceDocument.id.ShouldBe(ioTWorxBacNetMsg.name);
            outDeviceDocument.PresentValue.ShouldBe(ioTWorxBacNetMsg.value);
            outDeviceDocument.DeviceStatus.ShouldBe("Unprovisioned");

            unprovisionedDeviceOutput.Items.Count.ShouldBe(1);

            //Don't send Unprovisioned device events downstream
            outputEvents.Items.Count.ShouldBe(0);
        }