/// <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);
        }
Exemple #2
0
        public async Task <ActionResult> Index(string deviceId)
        {
            dynamic device = await _deviceLogic.GetDeviceAsync(deviceId);

            List <SelectListItem> commandListItems = CommandListItems(device);

            var deviceCommandsModel = new DeviceCommandModel
            {
                CommandHistory   = new List <dynamic>(CommandHistorySchemaHelper.GetCommandHistory(device)),
                CommandsJson     = JsonConvert.SerializeObject(device.Commands),
                SendCommandModel = new SendCommandModel
                {
                    DeviceId              = DeviceSchemaHelper.GetDeviceID(device),
                    CommandSelectList     = commandListItems,
                    CanSendDeviceCommands = DeviceSchemaHelper.GetHubEnabledState(device) == true &&
                                            PermsChecker.HasPermission(Permission.SendCommandToDevices)
                },
                DeviceId = DeviceSchemaHelper.GetDeviceID(device)
            };

            return(View(deviceCommandsModel));
        }
Exemple #3
0
        private async Task RunProcess(CancellationToken token)
        {
            FeedbackBatch batch;
            FeedbackReceiver <FeedbackBatch> batchReceiver;
            dynamic device;
            dynamic existingCommand;
            IEnumerable <FeedbackRecord> records;
            ServiceClient serviceClient;
            DateTime      updatedTime;

            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            serviceClient = null;
            try
            {
                serviceClient =
                    ServiceClient.CreateFromConnectionString(
                        _iotHubConnectionString);

                await serviceClient.OpenAsync();

                while (!token.IsCancellationRequested)
                {
                    batchReceiver = serviceClient.GetFeedbackReceiver();
                    batch         =
                        await batchReceiver.ReceiveAsync(
                            TimeSpan.FromSeconds(10.0));

                    if ((batch == null) ||
                        ((records = batch.Records) == null))
                    {
                        continue;
                    }

                    records =
                        records.Where(
                            t =>
                            (t != null) &&
                            !string.IsNullOrEmpty(t.DeviceId) &&
                            !string.IsNullOrEmpty(t.OriginalMessageId));
                    foreach (FeedbackRecord record in records)
                    {
                        device =
                            await _deviceLogic.GetDeviceAsync(record.DeviceId);

                        if (device == null)
                        {
                            continue;
                        }

                        Trace.TraceInformation(
                            "{0}{0}*** Processing Feedback Record ***{0}{0}DeviceId: {1}{0}OriginalMessageId: {2}{0}Result: {3}{0}{0}",
                            Console.Out.NewLine,
                            record.DeviceId,
                            record.OriginalMessageId,
                            record.StatusCode);

                        existingCommand =
                            CommandHistorySchemaHelper.GetCommandHistoryItemOrDefault(
                                device,
                                record.OriginalMessageId);

                        if (existingCommand == null)
                        {
                            continue;
                        }

                        updatedTime = record.EnqueuedTimeUtc;
                        if (updatedTime == default(DateTime))
                        {
                            updatedTime = batch.EnqueuedTime;
                        }

                        if (updatedTime == default(DateTime))
                        {
                            updatedTime = DateTime.UtcNow;
                        }

                        existingCommand.UpdatedTime = updatedTime;

                        existingCommand.Result = record.StatusCode.ToString();

                        if (record.StatusCode == FeedbackStatusCode.Success)
                        {
                            existingCommand.ErrorMessage = string.Empty;
                        }

                        CommandHistorySchemaHelper.UpdateCommandHistoryItem(
                            device,
                            existingCommand);

                        await _deviceLogic.UpdateDeviceAsync(device);
                    }

                    await batchReceiver.CompleteAsync(batch);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(
                    "Error in MessageFeedbackProcessor.RunProcess, Exception: {0}",
                    ex.Message);
            }

            if (serviceClient != null)
            {
                await serviceClient.CloseAsync();
            }

            _isRunning = false;
        }