示例#1
0
        public void Check_MotionSensor_Reports_NoMotion_Data()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("report", "motion", "158d00011c0", 52754,
                                     new Dictionary <string, object>
            {
                { "no_motion", "120" }
            });

            // Act
            MotionSensor device = _fixture.GetDeviceByCommand <MotionSensor>(cmd);

            var noMotionRaised  = false;
            var moMotionSeconds = 0;

            device.OnNoMotion += (_, args) =>
            {
                noMotionRaised  = true;
                moMotionSeconds = args.Seconds;
            };

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.Equal("motion", device.Type);
            Assert.Equal("158d00011c0", device.Sid);
            Assert.Equal("no motion", device.Status);
            Assert.True(noMotionRaised);
            Assert.Equal(120, device.NoMotion);
        }
示例#2
0
        private async Task StartReceivingMessagesAsync(CancellationToken ct)
        {
            // Receive messages
            while (!ct.IsCancellationRequested)
            {
                try
                {
                    var str = await _transport.ReceiveAsync().ConfigureAwait(false);

                    var respCmd = ResponseCommand.FromString(str);

                    if (LogRawCommands)
                    {
                        _logger?.LogInformation(str);
                    }

                    if (!_commandsToActions.TryGetValue(respCmd.Command, out var actionCommand))
                    {
                        _logger?.LogInformation($"Command '{respCmd.RawCommand}' is not a response command, skipping it");
                        continue;
                    }

                    actionCommand(respCmd);
                }
                catch (Exception e)
                {
                    _logger?.LogError(e, "Unexpected error");
                }
            }
        }
        public void Check_SmokeSensor_Alarm_Event_Raised()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("report", "smoke", "158d0001d8f8f7", 25885,
                                     new Dictionary <string, object>
            {
                { "alarm", "0" },
            });

            // Act
            SmokeSensor device = _fixture.GetDeviceByCommand <SmokeSensor>(cmd);

            var alarmEventRaised = false;

            device.OnAlarm += (_, args) =>
            {
                alarmEventRaised = true;
            };

            var cmd1 = Helpers
                       .CreateCommand("report", "smoke", "158d0001d8f8f7", 25885,
                                      new Dictionary <string, object>
            {
                { "alarm", "1" },
            });

            device.ParseData(ResponseCommand.FromString(cmd1).Data);

            // Assert
            Assert.True(device.Alarm);
            Assert.True(alarmEventRaised);
        }
示例#4
0
        public void Check_MotionSensor_Reports_Motion_Data()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("report", "motion", "158d00011c0", 52754,
                                     new Dictionary <string, object>
            {
                { "status", "motion" }
            });

            // Act
            MotionSensor device = _fixture.GetDeviceByCommand <MotionSensor>(cmd);

            var motionRaised = false;

            device.OnMotion += (_, args) =>
            {
                motionRaised = true;
            };

            var timeDiff = DateTime.Now - device.MotionDate.Value;

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.Equal("motion", device.Type);
            Assert.Equal("158d00011c0", device.Sid);
            Assert.Equal("motion", device.Status);
            Assert.True(timeDiff <= TimeSpan.FromSeconds(1));
            Assert.True(motionRaised);
        }
示例#5
0
        public void Check_WaterLeakSensor_Raised_NoLeak()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("heartbeat", "sensor_wleak.aq1", "158d0001d561e2", 18101,
                                     new Dictionary <string, object>
            {
                { "voltage", "3005" }
            });

            // Act
            WaterLeakSensor device = _deviceFactory.GetDeviceByCommand <WaterLeakSensor>(cmd);

            bool noleakRaised = false;

            device.OnNoLeak += (_, __) => noleakRaised = true;;

            cmd = Helpers
                  .CreateCommand("report", "sensor_wleak.aq1", "158d0001d561e2", 18101,
                                 new Dictionary <string, object>
            {
                { "status", "no_leak" }
            });

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.True(noleakRaised);
        }
示例#6
0
        public void Check_DoorWindowSensor_Raised_Closed_Event()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("heartbeat", "magnet", "158d0001233529", 64996,
                                     new Dictionary <string, object>
            {
                { "voltage", 2985 }
            });

            // Act
            DoorWindowSensor device = _deviceFactory.GetDeviceByCommand <DoorWindowSensor>(cmd);

            bool closedRaised = false;

            device.OnClose += (_, __) => closedRaised = true;

            cmd = Helpers
                  .CreateCommand("report", "magnet", "158d0001233529", 64996,
                                 new Dictionary <string, object>
            {
                { "status", "close" }
            });

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.True(closedRaised);
        }
        public T GetDeviceByCommand <T>(string cmd) where T : MiHomeDevice
        {
            var respCmd = ResponseCommand.FromString(cmd);
            T   device  = MiHomeDeviceFactory.CreateByModel(respCmd.Model, respCmd.Sid) as T;

            device.ParseData(respCmd.Data);
            return(device);
        }
        public void CheckToken(string str, string token)
        {
            // Act
            var rcmd = ResponseCommand.FromString(str);

            // Assert
            Assert.Equal(token, rcmd.Token);
        }
        public void CheckData()
        {
            // Arrange
            var str = "{\"cmd\":\"report\",\"model\":\"plug\",\"sid\":\"158d0001826509\",\"short_id\":11109,\"data\":\"{\\\"temperature\\\":\\\"2246\\\"}\"}";
            // Act
            var rcmd = ResponseCommand.FromString(str);

            // Assert
            Assert.Equal("{\"temperature\":\"2246\"}", rcmd.Data);
        }
        public void CheckCommandType(string cmd, ResponseCommandType expected)
        {
            // Arrange
            var str = "{\"cmd\":\"" + cmd + "\",\"model\":\"sensor_ht\",\"sid\":\"158d0001826509\",\"short_id\":11109,\"data\":\"{\\\"temperature\\\":\\\"2246\\\"}\"}";
            // Act
            var rcmd = ResponseCommand.FromString(str);

            // Assert
            Assert.Equal(expected, rcmd.Command);
        }
示例#11
0
        public void Check_ThSensor_Raised_Temperature_And_Humdity_Change()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("heartbeat", "sensor_ht", "158d000156a94b", 20911,
                                     new Dictionary <string, object>
            {
                { "voltage", 2995 },
                { "temperature", "1601" },
                { "humidity", "7214" },
            });

            ThSensor device = _fixture.GetDeviceByCommand <ThSensor>(cmd);

            bool  temperatureEventRaised = false;
            bool  humidityEventRaised    = false;
            float newTemperature         = 0;
            float newHumidity            = 0;

            device.OnTemperatureChange += (_, args) =>
            {
                temperatureEventRaised = true;
                newTemperature         = args.Temperature;
            };

            device.OnHumidityChange += (_, args) =>
            {
                humidityEventRaised = true;
                newHumidity         = args.Humidity;
            };

            // Act
            cmd = Helpers
                  .CreateCommand("heartbeat", "sensor_ht", "158d000156a94b", 20911,
                                 new Dictionary <string, object>
            {
                { "voltage", 2995 },
                { "temperature", "1901" },
                { "humidity", "7502" },
            });

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.True(temperatureEventRaised);
            Assert.True(humidityEventRaised);
            Assert.Equal(19.01f, newTemperature);
            Assert.Equal(75.02f, newHumidity);
        }
示例#12
0
        public void Check_Gateway_Hearbeat_Data()
        {
            // Arrange
            Gateway device = new Gateway("34ce1188db36", new Mock <IMessageTransport>().Object);

            var cmd = Helpers
                      .CreateCommand("heartbeat", "gateway", "34ce1188db36", 0,
                                     new Dictionary <string, object>
            {
                { "ip", "192.168.1.1" }
            });

            // Act
            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.Equal("gateway", device.Type);
            Assert.Equal("34ce1188db36", device.Sid);
            Assert.Equal("192.168.1.1", device.Ip);
        }
示例#13
0
        public void Check_Gateway_Report_Data()
        {
            // Arrange
            Gateway device = new Gateway("34ce1188db36", new Mock <IMessageTransport>().Object);

            var cmd = Helpers
                      .CreateCommand("report", "gateway", "34ce1188db36", 0,
                                     new Dictionary <string, object>
            {
                { "rgb", 0 },
                { "illumination", 495 }
            });

            // Act
            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.Equal("gateway", device.Type);
            Assert.Equal("34ce1188db36", device.Sid);
            Assert.Equal(0, device.Rgb);
            Assert.Equal(495, device.Illumination);
        }
示例#14
0
        public void Check_Switch_LongPressClick_Raised()
        {
            // Arrange
            var cmd = Helpers
                      .CreateCommand("report", "switch", "34ce0067db36", 34308, new Dictionary <string, object>());

            // Act
            Switch device = _fixture.GetDeviceByCommand <Switch>(cmd);

            bool longClickRaised = false;

            device.OnLongPress += (_, args) => longClickRaised = true;

            cmd = Helpers
                  .CreateCommand("report", "switch", "34ce0067db36", 34308, new Dictionary <string, object>
            {
                { "status", "long_click_press" },
            });

            device.ParseData(ResponseCommand.FromString(cmd).Data);

            // Assert
            Assert.True(longClickRaised);
        }