コード例 #1
0
        public Task ExecuteAsync(OperationParameters parameters)
        {
            var deviceId = parameters.Arguments["deviceid"].ToString();
            if (string.IsNullOrWhiteSpace(deviceId))
            {
                throw new ArgumentNullException("deviceid");
            }

            object commandToExecuteObject = parameters.Arguments["commandtoexecute"];

            if (commandToExecuteObject == null)
            {
                throw new ArgumentNullException("Command To Execute");
            }
            var commandToExecute = commandToExecuteObject.ToString();

            // get serialized stream
            var bytes = Encoding.Default.GetBytes(commandToExecute);
            var messageId = Guid.NewGuid().ToString();
            var message = new Message(bytes)
            {
                MessageId = messageId // set the correlation id to get command response
            };

            // Set the Acknowledgement to Full to receive feedback
            message.Ack = DeliveryAcknowledgement.Full;

            return this.IoTHubContext.ServiceClient.SendAsync(deviceId, message);
        }
コード例 #2
0
        public async Task ExecuteAsync(OperationParameters parameters)
        {
            try
            {
                var deviceId = parameters.Arguments["deviceid"].ToString();
                if (string.IsNullOrWhiteSpace(deviceId))
                {
                    throw new ArgumentNullException("deviceid");
                }

                var deviceKey = parameters.Arguments["deviceKey"].ToString();

                if (string.IsNullOrWhiteSpace(deviceKey))
                {
                    throw new ArgumentNullException("devicekey");
                }

                object messageObject = parameters.Arguments["message"];

                var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageObject)));

                var deviceClient = this.IoTHubContext.CreateDeviceClient(deviceId, deviceKey);

                await deviceClient.SendEventAsync(message);
                Console.WriteLine("sent");
            }
            catch (Exception exception)
            {
                Console.WriteLine("sample");
                // ToDO: Log exception
                // return Task.FromResult(true);
            }
        }
コード例 #3
0
        public Task ExecuteAsync(OperationParameters parameters)
        {
            var deviceId = parameters.Arguments["deviceid"].ToString();

            if (string.IsNullOrWhiteSpace(deviceId))
            {
                throw new ArgumentNullException("deviceid");
            }

            object commandToExecuteObject = parameters.Arguments["commandtoexecute"];

            if (commandToExecuteObject == null)
            {
                throw new ArgumentNullException("Command To Execute");
            }
            var commandToExecute = commandToExecuteObject.ToString();

            // get serialized stream
            var bytes     = Encoding.Default.GetBytes(commandToExecute);
            var messageId = Guid.NewGuid().ToString();
            var message   = new Message(bytes)
            {
                MessageId = messageId // set the correlation id to get command response
            };

            // Set the Acknowledgement to Full to receive feedback
            message.Ack = DeliveryAcknowledgement.Full;

            return(this.IoTHubContext.ServiceClient.SendAsync(deviceId, message));
        }
コード例 #4
0
        public async Task ExecuteAsync(OperationParameters parameters)
        {
            try
            {
                var deviceId = parameters.Arguments["deviceid"].ToString();
                if (string.IsNullOrWhiteSpace(deviceId))
                {
                    throw new ArgumentNullException("deviceid");
                }

                var deviceKey = parameters.Arguments["deviceKey"].ToString();

                if (string.IsNullOrWhiteSpace(deviceKey))
                {
                    throw new ArgumentNullException("devicekey");
                }

                object messageObject = parameters.Arguments["message"];

                var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageObject)));

                var deviceClient = this.IoTHubContext.CreateDeviceClient(deviceId, deviceKey);

                await deviceClient.SendEventAsync(message);

                Console.WriteLine("sent");
            }
            catch (Exception exception)
            {
                Console.WriteLine("sample");
                // ToDO: Log exception
                // return Task.FromResult(true);
            }
        }
コード例 #5
0
 public static async Task SendTelemetry()
 {
     var sendTelemetry = new SendTelemetryOperation(context);
     var operationParameters = new OperationParameters();
     operationParameters.Arguments.Add("deviceid", deviceId);
     operationParameters.Arguments.Add("deviceKey", deviceKey);
     operationParameters.Arguments.Add("message", "sample");
     await sendTelemetry.ExecuteAsync(operationParameters);
     Console.WriteLine("Successfully sent");
 }
コード例 #6
0
        public static async Task ProcessOperation(string operationSelected)
        {
            var operationParameters = new OperationParameters();
            switch (operationSelected)
            {
                case ProvisionDevice:
                    Console.WriteLine("Plese enter device Id");
                    var input = Console.ReadLine();
                    var provisionDevice = new CreateDeviceOperation(context);
                    operationParameters.Arguments.Add("deviceid", input);
                    Console.WriteLine("Do you want to auto generate Key? (Y or N) ");
                    var selection = Console.Read();
                    switch (selection)
                    {
                        case 'Y':
                            operationParameters.Arguments.Add("auto", true);
                            await provisionDevice.ExecuteAsync(operationParameters);
                            break;
                        case 'N':
                            operationParameters.Arguments.Add("auto", false);
                            //Console.WriteLine("Please enter device Key");
                            var deviceKey = "PVjPkqj3loB1P0VJzMAkIg==";
                            operationParameters.Arguments.Add("deviceKey", deviceKey);
                            await provisionDevice.ExecuteAsync(operationParameters);
                            break;
                        default:
                            Console.WriteLine("Wrong input");
                            break;
                    }
                    break;

                case SendCommand:
                    var sendCommandOperation = new SendCommandOperation(context);
                    operationParameters.Arguments["deviceid"] = "T5720022";
                    operationParameters.Arguments["commandtoexecute"] = "Command";
                    Console.WriteLine("Sending Command");
                    await sendCommandOperation.ExecuteAsync(operationParameters);
                    Console.WriteLine("Successfully sent command");
                    break;
            }
        }