Example #1
0
 /// <summary>
 /// Delete a message from the message queue once we have processed it
 /// </summary>
 /// <param name="message"></param>
 private void DeleteMessage(SigfoxMessage message)
 {
     var deleteMessageRequest = new DeleteMessageRequest
     {
         QueueUrl      = _queueUrl,
         ReceiptHandle = message.RecieptHandle
     };
     var response = _sqsClient.DeleteMessage(deleteMessageRequest);
 }
Example #2
0
        /// <summary>
        /// Read all the messages from the queue and send them to the MQTT broker
        /// </summary>
        /// <param name="state"></param>
        private void DoData(object state)
        {
            try
            {
                var receiveMessageRequest = new ReceiveMessageRequest
                {
                    QueueUrl            = _queueUrl,
                    MaxNumberOfMessages = 10
                };

                List <SigfoxMessage> messages = new List <SigfoxMessage>();
                int finishedCounter           = 0;

                //Keep reading messages from the queue, until we get not results twice in a row.
                //https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html
                while (finishedCounter < 2)
                {
                    var receiveMessageResponse = _sqsClient.ReceiveMessage(receiveMessageRequest);
                    if (receiveMessageResponse.Messages.Count == 0)
                    {
                        finishedCounter++;
                    }

                    foreach (var message in receiveMessageResponse.Messages)
                    {
                        try
                        {
                            _log.Debug(message.Body);
                            JObject dmsg = JObject.Parse(message.Body);
                            var     smsg = new SigfoxMessage(dmsg)
                            {
                                RecieptHandle = message.ReceiptHandle
                            };
                            messages.Add(smsg);
                        }
                        catch (Exception ex)
                        {
                            _log.Warn(ex);
                        }
                    }
                }

                //order the messages by time so we send the data to the broker in the right order
                var orderedmessages = messages.OrderBy(x => x.TimeStamp).ToList();

                foreach (var message in orderedmessages)
                {
                    ProcessMessage(message);
                    DeleteMessage(message);
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }
        }
Example #3
0
 /// <summary>
 /// Publish the message variables to MQTT broker
 /// </summary>
 /// <param name="message"></param>
 private void ProcessMessage(SigfoxMessage message)
 {
     try
     {
         _log.Debug(message);
         foreach (var kvp in message.Variables)
         {
             object o = new { kvp.Value, Time = message.TimeStamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") };
             _publishData($"sigfox/{message.Site}/{kvp.Key}", JsonConvert.SerializeObject(o));
         }
     }
     catch (Exception ex)
     {
         _log.Warn(ex);
     }
 }