public async void SendCommand() { var commandHistory = fixture.Create <CommandHistory>(); var deviceId = fixture.Create <string>(); commandHistory.DeliveryType = DeliveryType.Message; deviceManagerMock.Setup(dm => dm.SendAsync(deviceId, It.IsAny <Message>())).Returns(Task.FromResult(true)); deviceManagerMock.Setup(dm => dm.CloseAsyncDevice()).Returns(Task.FromResult(true)); await iotHubRepository.SendCommand(deviceId, commandHistory); deviceManagerMock.Verify(mock => mock.SendAsync(deviceId, It.IsAny <Message>()), Times.Once()); deviceManagerMock.Verify(mock => mock.CloseAsyncDevice(), Times.Once()); }
/// <summary> /// Sends a command to the provided device and updates the command history of the device /// </summary> /// <param name="device">Device to send the command to</param> /// <param name="commandName">Name of the command to send</param> /// <param name="parameters">Parameters to send with the command</param> /// <returns></returns> private async Task <dynamic> SendCommandAsyncWithDevice(dynamic device, string commandName, dynamic parameters) { string deviceId; if (device == null) { throw new ArgumentNullException("device"); } bool canDevicePerformCommand = CommandSchemaHelper.CanDevicePerformCommand(device, commandName); deviceId = DeviceSchemaHelper.GetDeviceID(device); if (!canDevicePerformCommand) { throw new UnsupportedCommandException(deviceId, commandName); } dynamic command = CommandHistorySchemaHelper.BuildNewCommandHistoryItem(commandName); CommandHistorySchemaHelper.AddParameterCollectionToCommandHistoryItem(command, parameters); CommandHistorySchemaHelper.AddCommandToHistory(device, command); await _iotHubRepository.SendCommand(deviceId, command); await _deviceRegistryCrudRepository.UpdateDeviceAsync(device); return(command); }
/// <summary> /// Sends a command to the provided device and updates the command history of the device /// </summary> /// <param name="device">Device to send the command to</param> /// <param name="commandName">Name of the command to send</param> /// <param name="parameters">Parameters to send with the command</param> /// <returns></returns> private async Task <CommandHistory> SendCommandAsyncWithDevice(DeviceModel device, string commandName, dynamic parameters) { if (device == null) { throw new ArgumentNullException("device"); } var deviceId = device.DeviceProperties.DeviceID; if (device.Commands.FirstOrDefault(x => x.Name == commandName) == null) { throw new UnsupportedCommandException(deviceId, commandName); } var commandHistory = new CommandHistory(commandName, parameters); if (device.CommandHistory == null) { device.CommandHistory = new List <CommandHistory>(); } device.CommandHistory.Add(commandHistory); await _iotHubRepository.SendCommand(deviceId, commandHistory); await _deviceRegistryCrudRepository.UpdateDeviceAsync(device); return(commandHistory); }
async Task SendCommand(string commandName) { var command = CommandSchemaHelper.CreateNewCommand(commandName); foreach (var partitionKey in _deviceService.GetDeviceIds()) { await _iotHubRepository.SendCommand(partitionKey, command); } }