public async Task<CommandResult> SendCmd(string command, object arg)
        {
            int cid = Interlocked.Increment(ref _cmdId);

            Command cmd = new Command()
            {
                Id = cid,
                Name = command
            };
            cmd.Parameters = Newtonsoft.Json.Linq.JToken.FromObject(arg);
            Stopwatch sw = new Stopwatch();

            TaskCompletionSource<CommandResult> tks = new TaskCompletionSource<CommandResult>();
            _cmdData.Add(cid, tks);

            sw.Start();

            base.SendCommand(cmd);

            if (await Task.WhenAny(tks.Task, Task.Delay(_timeout)) != tks.Task)
            {
                throw new Exception("Timeout");
            }

            CommandResult rv = await tks.Task;
            sw.Stop();        
            
            _cmdData.Remove(cid);
            rv.ExecutionTime = sw.ElapsedMilliseconds;
            return rv;
        }
        /// <summary>
        /// Send custom DeviceHive command to the device
        /// </summary>
        protected void SendCommand(Command command)
        {
            if (_commandMapping == null) // device isn't registered yet
                throw new InvalidOperationException("Device received command but devices isn't registered yet");

            CommandMetadata commandMetadata;
            if (!_commandMapping.TryGetValue(command.Name, out commandMetadata))
                throw new InvalidOperationException(string.Format("Command {0} is not registered", command.Name));

            byte[] data;

            using (var stream = new MemoryStream())
            using (var writer = new BinaryWriter(stream))
            {
                writer.Write(command.Id.Value);
                WriteParameterValue(writer, commandMetadata.Parameters, command.Parameters);
                data = stream.ToArray();
            }

            SendMessage(commandMetadata.Intent, data);
        }
        private void SendCommandResult(DeviceBase device, Command cCommand)
        {
            Logger.InfoFormat("Sending command '{0}' status '{1}' from device {2} ({3})",
                cCommand.Name, cCommand.Status, device.ID, device.Name);

            try
            {
                DeviceClient.UpdateCommand(device.ID, device.Key, cCommand);
            }
            catch (Exception ex)
            {
                // not critical - do nothing
                Logger.Error(string.Format("Exception while sending command '{0}' status '{1}' from device {2} ({3})",
                    cCommand.Name, cCommand.Status, device.ID, device.Name), ex);
            }
        }
 private void DispatchCommandTask(DeviceBase device, Command cCommand)
 {
     // invoke device
     DeviceCommandResult result;
     try
     {
         var command = new DeviceCommand(cCommand.Name.Trim(),
             cCommand.Parameters == null ? null : cCommand.Parameters.DeepClone(),
             cCommand.UserId);
         result = device.HandleCommand(command, _cancellationSource.Token);
     }
     catch (OperationCanceledException)
     {
         return;
     }
     catch (Exception ex)
     {
         // operation faulted - log the error and send failed result
         Logger.Error(string.Format("Exception while handling a command '{0}' by device {1} ({2})", cCommand.Name, device.ID, device.Name), ex);
         result = new DeviceCommandResult("Failed", "An error occurred while handling the command");
     }
             
     // send command result
     cCommand.Status = result.Status;
     cCommand.Result = result.Result == null ? null : JToken.FromObject(result.Result, device.JsonSerializer);
     SendCommandResult(device, cCommand);
 }
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="deviceGuid">Device unique identifier.</param>
 /// <param name="command">Command object.</param>
 public CommandEventArgs(string deviceGuid, Command command)
 {
     DeviceGuid = deviceGuid;
     Command = command;
 }
示例#6
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="command">Command object</param>
 public CommandEventArgs(Guid deviceGuid, Command command)
 {
     _command = command;
     _deviceGuid = deviceGuid;
 }