public async void TestCommandCannotComplete()
     {
         var history = new CommandHistory("CommandShouldNotComplete");
         var command = new DeserializableCommand(history, "LockToken");
 
         var r = await _pingDeviceProcessor.HandleCommandAsync(command);
         Assert.Equal(r, CommandProcessingResult.CannotComplete);
     }
        public async void TestCommandSuccess()
        {
            var history = new CommandHistory("PingDevice");
            var command = new DeserializableCommand(history, "LockToken");

            var r = await _pingDeviceProcessor.HandleCommandAsync(command);
            Assert.Equal(r, CommandProcessingResult.Success);
        }
        public async void TestCommandRetryLater()
        {
            var history = new CommandHistory("StartTelemetry");
            var command = new DeserializableCommand(history, "LockToken");

            var r = await _startCommandProcessor.HandleCommandAsync(command);
            Assert.Equal(r, CommandProcessingResult.RetryLater);
        }
 public async void CannotCompleteExceptionCommandTests()
 {
     var history = new CommandHistory("ChangeSetPointTemp");
     var command = new DeserializableCommand(history, "LockToken");
 
     var r = await _changeSetPointTempCommandProcessor.HandleCommandAsync(command);
     Assert.Equal(r, CommandProcessingResult.CannotComplete);
 }
 public async void CannotCompleteCommandTests()
 {
     var history = new CommandHistory("CommandShouldNotComplete");
     var command = new DeserializableCommand(history, "LockToken");
     //null pararameters
     var r = await _changeSetPointTempCommandProcessor.HandleCommandAsync(command);
     Assert.Equal(r, CommandProcessingResult.CannotComplete);
 }
        public async void CannotParseAsDoubleCommandTests()
        {
            var history = new CommandHistory("ChangeSetPointTemp");
            var command = new DeserializableCommand(history, "LockToken");
            history.Parameters = new ExpandoObject();
            history.Parameters.SetPointTemp = "ThisIsNotADouble";

            var r = await _changeSetPointTempCommandProcessor.HandleCommandAsync(command);
            Assert.Equal(r, CommandProcessingResult.CannotComplete);
        }
        public async void NoSetPointParameterCommandTests()
        {
            var history = new CommandHistory("ChangeSetPointTemp");
            var command = new DeserializableCommand(history, "LockToken");
            history.Parameters = new ExpandoObject();
            history.Parameters.setpointtemp = "1.0";

            var r = await _changeSetPointTempCommandProcessor.HandleCommandAsync(command);
            Assert.Equal(r, CommandProcessingResult.RetryLater);
        }
        public DeserializableCommand(Client.Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            Debug.Assert(
                !string.IsNullOrEmpty(message.LockToken),
                "message.LockToken is a null reference or empty string.");
            _lockToken = message.LockToken;

            byte[] messageBytes = message.GetBytes(); // this needs to be saved if needed later, because it can only be read once from the original Message

            string jsonData = Encoding.UTF8.GetString(messageBytes);
            _commandHistory = JsonConvert.DeserializeObject<CommandHistory>(jsonData);
        }
        /// <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;
        }
        public async Task SendCommand(string deviceId, CommandHistory command)
        {
            var commandAsBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command));
            var notificationMessage = new Message(commandAsBytes);

            notificationMessage.Ack = DeliveryAcknowledgement.Full;
            notificationMessage.MessageId = command.MessageId;

            await AzureRetryHelper.OperationWithBasicRetryAsync(async () =>
                                                                await this._deviceManager.SendAsync(deviceId, notificationMessage));

            await this._deviceManager.CloseAsyncDevice();
        }
 public DeserializableCommand(CommandHistory history, string lockToken)
 {
    this._commandHistory = history;
    this._lockToken = lockToken;
 }