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); }