コード例 #1
0
        public async Task ExecuteAsync_PublishSensorDeviceTypeTypeData_CorrectlyPublished()
        {
            var publishedMessages = new List <MqttApplicationMessage>();
            var sensorDeviceType  = new SensorDeviceType()
            {
                Id      = "DeviceType1",
                Sensors = new List <Sensor>()
                {
                    new Sensor()
                    {
                        Id = "Sensor1",
                    }
                }
            };

            //Arrange
            var config = new CollectorConfiguration
            {
                CollectorEnabled     = true,
                BluetoothAdapterName = "test",
                ScanIntervalSeconds  = 1,
                SensorDevices        = new List <SensorDevice>(),
                SensorDeviceTypes    = new List <SensorDeviceType>()
                {
                    sensorDeviceType
                }
            };

            _mqttClientMock.Setup(m => m.IsConnected).Returns(true);
            _mqttClientMock.Setup(m => m.PublishAsync(It.IsAny <MqttApplicationMessage>(), CancellationToken.None)).Callback((MqttApplicationMessage message, CancellationToken ct) => { publishedMessages.Add(message); });
            _configMock.Setup(c => c.Value).Returns(config);

            //Act
            var btGwService = _serviceProvider.GetService <CollectorService>();
            var cts         = new CancellationTokenSource();

#pragma warning disable CS4014
            // Run as fire & forget
            Task.Run(() => btGwService.StartAsync(cts.Token).ConfigureAwait(false));
#pragma warning restore CS4014

            await Task.Delay(100);

            await btGwService.HandleConnectedAsync(new MqttClientConnectedEventArgs(new MqttClientAuthenticateResult()));

            cts.Cancel();

            // Assert
            var topic   = MqttMessageHelper.GetSensorDeviceTypeTopic(sensorDeviceType.Id);
            var payload = MqttMessageHelper.SerializePayload(sensorDeviceType);
            var message = publishedMessages.FirstOrDefault(m => m.Topic.Equals(topic, StringComparison.OrdinalIgnoreCase));

            Assert.IsNotNull(message, "MQTT message for type data of sensor device type was not received");
            Assert.AreEqual(topic, message?.Topic, "Sensor device type message topic was not correct");
            Assert.AreEqual(payload, message?.ConvertPayloadToString(), "Sensor device type message payload was not correct");
        }
コード例 #2
0
        public async Task HandleConnectedAsync(MqttClientConnectedEventArgs e)
        {
            foreach (var sensorDeviceType in _sensorDeviceTypes)
            {
                await _mqttClient.PublishAsync(new MqttApplicationMessageBuilder()
                                               .WithPayload(MqttMessageHelper.SerializePayload(sensorDeviceType))
                                               .WithRetainFlag().WithTopic(MqttMessageHelper.GetSensorDeviceTypeTopic(sensorDeviceType.Id))
                                               .Build(), CancellationToken.None);
            }

            foreach (var sensorDevice in _sensorDevices)
            {
                await _mqttClient.PublishAsync(new MqttApplicationMessageBuilder()
                                               .WithPayload(MqttMessageHelper.SerializePayload(sensorDevice))
                                               .WithRetainFlag().WithTopic(MqttMessageHelper.GetSensorDeviceTopic(sensorDevice.Id))
                                               .Build(), CancellationToken.None);
            }
        }