public Task SendCloudToDeviceMessageAsync(String data, ReceivedFeedBackData callback = null) { return(Task.Run(() => { SendCommand(data, callback); })); }
// this function is used to receive the feed back for the message sent private void ReceiveFeedback(ReceivedFeedBackData callback, string filter) { string audience = Fx.Format("{0}/messages/servicebound/feedback", HOST); string resourceUri = Fx.Format("{0}/messages/servicebound/feedback", HOST); string sasToken = GetSharedAccessSignature(SHARED_ACCESS_KEY_NAME, SHARED_ACCESS_KEY, resourceUri, new TimeSpan(1, 0, 0)); bool cbs = PutCbsToken(connection, HOST, sasToken, audience); if (cbs) { string entity = "/messages/servicebound/feedback"; // Form the filter for receiving message based on the time stamp Map filters = new Map(); if (filter != null) { filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"), new DescribedValue(new Amqp.Types.Symbol("apache.org:selector-filter:string"), filter)); } ReceiverLink receiveLink = new ReceiverLink(session, "receive-link", new Source() { Address = entity, FilterSet = filters }, null); Message received = receiveLink.Receive(); if (received != null) { receiveLink.Accept(received); Data data = (Data)received.BodySection; callback(Encoding.UTF8.GetString(data.Binary)); Debug.WriteLine(Encoding.UTF8.GetString(data.Binary)); } receiveLink.Close(); } }
//This Methods sends the given string to the device end point public void SendCloudToDeviceMessage(String data, ReceivedFeedBackData callback = null) { Task taskB = new Task(() => { SendCommand(data,callback); }); taskB.Start(); // taskB.Wait(); }
// this function is core function that sends the message to the device private void SendCommand(string Message, ReceivedFeedBackData callback = null) { try { Debug.WriteLine("Send " + Message + " " + DEVICE_ID); long time = DateTimeToTimestamp(DateTime.UtcNow); lock (SendSync) { address = new Address(HOST, PORT, null, null); connection = new Connection(address); session = new Session(connection); string audience = Fx.Format("{0}/messages/devicebound", HOST); string resourceUri = Fx.Format("{0}/messages/devicebound", HOST); //We send the SAS token to the CBS node before sending the actual data string sasToken = GetSharedAccessSignature(SHARED_ACCESS_KEY_NAME, SHARED_ACCESS_KEY, resourceUri, new TimeSpan(1, 0, 0)); bool cbs = PutCbsToken(connection, HOST, sasToken, audience); if (cbs) { // nodes to send the data string to = Fx.Format("/devices/{0}/messages/devicebound", DEVICE_ID); string entity = "/messages/devicebound"; SenderLink senderLink = new SenderLink(session, "sender-link", entity); var messageValue = Encoding.UTF8.GetBytes(Message); Message message = new Message() { BodySection = new Data() { Binary = messageValue } }; message.Properties = new Properties(); message.Properties.To = to; message.Properties.MessageId = Guid.NewGuid().ToString(); message.ApplicationProperties = new ApplicationProperties(); message.ApplicationProperties["iothub-ack"] = "full"; senderLink.Send(message); if (callback != null) { ReceiveFeedback(callback, "amqp.annotation.x-opt-enqueuedtimeutc > " + time); } senderLink.Close(); session.Close(); connection.Close(); } } } catch (Exception) { throw; } }
//This Methods sends the given string to the device end point public void SendCloudToDeviceMessage(String data, ReceivedFeedBackData callback = null) { Task taskB = new Task(() => { SendCommand(data, callback); }); taskB.Start(); // taskB.Wait(); }
// this function is used to receive the feed back for the message sent private void ReceiveFeedback(ReceivedFeedBackData callback,string filter) { string audience = Fx.Format("{0}/messages/servicebound/feedback", HOST); string resourceUri = Fx.Format("{0}/messages/servicebound/feedback", HOST); string sasToken = GetSharedAccessSignature(SHARED_ACCESS_KEY_NAME, SHARED_ACCESS_KEY, resourceUri, new TimeSpan(1, 0, 0)); bool cbs = PutCbsToken(connection, HOST, sasToken, audience); if (cbs) { string entity = "/messages/servicebound/feedback"; // Form the filter for receiving message based on the time stamp Map filters = new Map(); if (filter != null) { filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"), new DescribedValue(new Amqp.Types.Symbol("apache.org:selector-filter:string"), filter)); } ReceiverLink receiveLink = new ReceiverLink(session, "receive-link", new Source() { Address = entity, FilterSet = filters }, null); Message received = receiveLink.Receive(); if (received != null) { receiveLink.Accept(received); Data data = (Data)received.BodySection; callback(Encoding.UTF8.GetString(data.Binary)); Debug.WriteLine(Encoding.UTF8.GetString(data.Binary)); } receiveLink.Close(); } }