private async Task<bool> SendMessageAndWaitForResponse(ServiceClient serviceClient, string deviceId, UpdateAccessKeyRequest updateAccessKey)
        {
            var serviceMessage = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(updateAccessKey)));
            serviceMessage.Ack = DeliveryAcknowledgement.Full;
            serviceMessage.MessageId = Guid.NewGuid().ToString();

            var feedbackReceiver = serviceClient.GetFeedbackReceiver();
            var feedbackReceived = false;
            var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token;
            var feedbackTask = Task.Run(async () =>
            {
                while (!feedbackReceived && !cancellationToken.IsCancellationRequested)
                {
                    var feedbackBatch = await feedbackReceiver.ReceiveAsync(TimeSpan.FromSeconds(0.5));
                    if (feedbackBatch != null)
                    {
                        feedbackReceived = feedbackBatch.Records.Any(fm =>
                            fm.DeviceId == deviceId
                            && fm.OriginalMessageId == serviceMessage.MessageId);
                        if (feedbackReceived)
                        {
                            await feedbackReceiver.CompleteAsync(feedbackBatch);
                        }
                    }
                }
            }, cancellationToken);

            await Task.WhenAll(
                feedbackTask,
                serviceClient.SendAsync(deviceId, serviceMessage));

            return feedbackReceived;
        }
Exemple #2
0
        //------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructor
        public AzureIOTApplication(string connectionString)
        {
            //create an eventhubclient for receiving messages from the Azure IOT Hub
            eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, "messages/events");
            //create a sericeclient, able to send messages to the Azure IOT Hub Devices
            serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
        }
Exemple #3
0
        public async Task OpenAsync()
        {
            serviceClient = Microsoft.Azure.Devices.ServiceClient.CreateFromConnectionString(serviceConnectionString);

            await serviceClient.OpenAsync();

            registryManager = Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(ownerConnectionString);
            await registryManager.OpenAsync();
        }
Exemple #4
0
 // Please set the following connection strings in app.config for this WebJob to run:
 // AzureWebJobsDashboard and AzureWebJobsStorage
 static void Main()
 {
     _host = new JobHost();
     var connectionString = ConfigurationManager.ConnectionStrings["IoTHub"].ConnectionString;
     _eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, "messages/events");
     _serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
     Task.WaitAll(_eventHubClient.GetRuntimeInformation().PartitionIds.Select(Listen).ToArray());
     _host.RunAndBlock();
 }
 public IOTHubDeviceExplorer(string iotHubConnenctionString, int devicesCount, string protocolGatewayName)
 {
    
     this.iotHubConnectionString = iotHubConnenctionString;
     this.maxCountOfDevices = devicesCount;
     this.protocolGatewayHostName = protocolGatewayName;
     this.registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);
     this.serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
 }
Exemple #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Send Cloud-to-Device message\n");
            serviceClient = ServiceClient.CreateFromConnectionString(connectionString);

            Console.WriteLine("Press any key to send a C2D message.");
            Console.ReadLine();
            SendCloudToDeviceMessageAsync().Wait();
            Console.ReadLine();
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Sending Cloud-to-Device messages.");

            _serviceClient = ServiceClient.CreateFromConnectionString(_connectionString);

            Console.WriteLine("Press ENTER to send a cloud to device message.");
            while (true)
            {
                Console.ReadLine();
                SendCloudToDeviceMessage(_deviceId);
            }
        }
Exemple #8
0
 static void Main(string[] args)
 {
     Console.WriteLine("Send Cloud-to-Device message\n");
     serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
     ReceiveFeedbackAsync();
     string text = "";
     do
     {
         Console.Write("Write a message to send: ");
         text = Console.ReadLine();
         SendCloudToDeviceMessageAsync(text).Wait();
         Console.ReadLine();
     } while (text != "1");
 }
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
            eventHubClient = EventHubClient.CreateFromConnectionString(iotHubConnectionString, iotHubD2cEndpoint);

            var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

            foreach (string partition in d2cPartitions)
            {
                ReceiveMessagesFromDeviceAsync(cancellationToken, partition);
            }
            while (!cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(1000);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Send Cloud-to-Device message\n");
            serviceClient = ServiceClient.CreateFromConnectionString(connectionString);

            Console.WriteLine("Press any key to send a C2D message.");
            while (true)
            {
                Console.ReadLine();
                string guid = Guid.NewGuid().ToString();
                for (int i = 0; i < 3; i++)     //Send 3 messages to the device and receive the feedback
                {
                    SendCloudToDeviceMessageAsync(guid).Wait();
                    ReceiveFeedbackAsync(guid).Wait();
                }
                Console.WriteLine("Finished sending all messages");
            }
        }
Exemple #11
0
 public static async Task SendMessageToDeviceAsync(MAD.ServiceClient serviceClient, string targetDeviceId, string message)
 {
     var payload = new MAD.Message(Encoding.UTF8.GetBytes(message));
     await serviceClient.SendAsync(targetDeviceId, payload);
 }
 public EventProcessor()
 {
     var iotHubConnectionString = CloudConfigurationManager.GetSetting("IoTHubConnectionString");
     client = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
 }
 public void Init(string devId, string cmd)
 {
     devMsg = new Message(Encoding.ASCII.GetBytes(cmd));
     serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
     deviceId = devId;
 }
Exemple #14
0
 public ServiceAppServices(string connectionString)
 {
     serviceClient = MAD.ServiceClient.CreateFromConnectionString(connectionString);
 }
Exemple #15
0
 // Azure Function -> Console App    [Sender]
 public static async Task SendMessageToDeviceAsync(MAD.ServiceClient serviceClient, string targetDeviceId, string message)
 {
     // Skicka meddelande "message" till Device "targetDeviceId" mha SendAsync
     var payload = new MAD.Message(Encoding.UTF8.GetBytes(message));
     await serviceClient.SendAsync(targetDeviceId, payload);
 }