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); }
public async Task <DecisionCommandProcessResult> ProcessDecisionCommand(DecisionCommand decisionCommand) { if (decisionCommand.ParameterCommands.GroupBy(c => c.Parameter) .Any(x => x.Count() > 1)) { throw new InvalidOperationException($"Can't process command with duplicated parameter commands: {decisionCommand.ToJson()}"); } var commandTasks = decisionCommand.ParameterCommands.Select(ProcessParameterCommand).ToArray(); var parameterCommandResults = await Task.WhenAll(commandTasks).ConfigureAwait(false); return(new DecisionCommandProcessResult { ParameterCommandProcessResults = parameterCommandResults }); }