Example #1
0
 public RpcActuator(ActuatorDeviceInfo actuatorDeviceInfo, RpcMqttClient rpcMqttClient)
 {
     _rpcMqttClient     = rpcMqttClient;
     ActuatorDeviceInfo = actuatorDeviceInfo;
 }
Example #2
0
        public async Task TestDecisionCommandProcessing()
        {
            var rpcRequestTopic      = "command/request";
            var rpcResponseTopic     = "command/response";
            var apiClient            = _factory.CreateClient();
            var mqttClientRepository = _factory.Services.GetService <MqttClientRepository>();
            var decisionCommand      = new DecisionCommand()
            {
                ParameterCommands = new []
                {
                    new ParameterCommand()
                    {
                        CommandImpact = CommandImpact.Decrease,
                        Parameter     = ParameterType.TemperatureInside
                    }
                }
            };
            var clientReceivedCommands = new List <RpcCommandRequest>();
            var devicePushClient       = await mqttClientRepository.Subscribe("_", ((s, bytes) => Task.CompletedTask));

            var deviceClient = await mqttClientRepository.Subscribe(rpcRequestTopic, (s, bytes) =>
            {
                var receivedCommand = bytes.DeserializeJsonBytes <RpcCommandRequest>();
                clientReceivedCommands.Add(receivedCommand);
                return(devicePushClient.PublishAsync(rpcResponseTopic, new RpcCommandResponse()
                {
                    CommandId = receivedCommand.CommandId,
                    FailedMessage = null,
                    IsFailed = false
                }));
            });

            var tempDecreaseDeviceInfo = new ActuatorDeviceInfo()
            {
                DeviceCode       = "Custom",
                Id               = Guid.NewGuid(),
                IsConstantImpact = false,
                Impacts          = new[] { CommandImpact.Decrease, CommandImpact.StrongDecrease },
                Parameter        = ParameterType.TemperatureInside,
            };

            await deviceClient.PublishAsync("data/device", tempDecreaseDeviceInfo);

            await Task.Delay(1000);

            var request = new StringContent(decisionCommand.ToJson());

            request.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var decisionCommandHttpResult = await apiClient.PostAsync("commands/decision", request);

            await Task.Delay(1000);

            decisionCommandHttpResult.EnsureSuccessStatusCode();
            var decisionCommandResult = (await decisionCommandHttpResult.Content.ReadAsStringAsync())
                                        .DeserializeJson <DecisionCommandProcessResult>();
            var executedParameterCommand = decisionCommandResult.ParameterCommandProcessResults.Single();
            var executedImpact           = executedParameterCommand.Impacts.Single();
            var receivedDeviceCommand    = clientReceivedCommands.Single();

            Assert.Null(executedParameterCommand.Error);
            Assert.False(executedParameterCommand.Failed);
            Assert.Equal(CommandImpact.Decrease, executedImpact);
            Assert.Equal(CommandImpact.Decrease, receivedDeviceCommand.Impact);
            Assert.Equal(ParameterType.TemperatureInside, receivedDeviceCommand.Parameter);
            Assert.Equal(tempDecreaseDeviceInfo.Id, receivedDeviceCommand.DeviceId);
        }