Esempio n. 1
0
        /// <summary>
        ///     Send message to the AR using the given message parameters.
        /// </summary>
        /// <param name="messagingParameters">Messaging parameters.</param>
        /// <returns>-</returns>
        /// <exception cref="CouldNotSendHttpMessageException">Will be thrown if the message could not be send.</exception>
        public MessagingResult Send(MessagingParameters messagingParameters)
        {
            var messageRequest = new MessageRequest
            {
                SensorAlternateId     = messagingParameters.OnboardResponse.SensorAlternateId,
                CapabilityAlternateId = messagingParameters.OnboardResponse.CapabilityAlternateId,
                Messages = new List <Api.Dto.Messaging.Inner.Message>()
            };

            foreach (var message in messagingParameters.EncodedMessages.Select(encodedMessage =>
                                                                               new Api.Dto.Messaging.Inner.Message
            {
                Content = encodedMessage, Timestamp = UtcDataService.NowAsUnixTimestamp()
            }))
            {
                messageRequest.Messages.Add(message);
            }

            HttpContent requestBody = new StringContent(JsonConvert.SerializeObject(messageRequest), Encoding.UTF8,
                                                        "application/json");
            var httpResponseMessage = _httpClient
                                      .PostAsync(messagingParameters.OnboardResponse.ConnectionCriteria.Measures, requestBody).Result;

            if (httpResponseMessage.IsSuccessStatusCode)
            {
                return(new MessagingResultBuilder().WithApplicationMessageId(messagingParameters.ApplicationMessageId)
                       .Build());
            }

            Log.Error("Sending the message was not successful. HTTP response was " +
                      httpResponseMessage.StatusCode + ". Please check exception for more details.");
            throw new CouldNotSendHttpMessageException(httpResponseMessage.StatusCode,
                                                       httpResponseMessage.Content.ReadAsStringAsync().Result);
        }
Esempio n. 2
0
        /// <summary>
        ///     Send message to the AR using the given message parameters.
        /// </summary>
        /// <param name="messagingParameters">Messaging parameters.</param>
        /// <returns>-</returns>
        /// <exception cref="CouldNotSendMqttMessageException">Will be thrown if the message could not be send.</exception>
        public MessagingResult Send(MessagingParameters messagingParameters)
        {
            var messageRequest = new MessageRequest
            {
                SensorAlternateId     = messagingParameters.OnboardResponse.SensorAlternateId,
                CapabilityAlternateId = messagingParameters.OnboardResponse.CapabilityAlternateId,
                Messages = new List <Api.Dto.Messaging.Inner.Message>()
            };

            foreach (var message in messagingParameters.EncodedMessages.Select(encodedMessage =>
                                                                               new Api.Dto.Messaging.Inner.Message
            {
                Content = encodedMessage, Timestamp = UtcDataService.NowAsUnixTimestamp()
            }))
            {
                messageRequest.Messages.Add(message);
            }

            var messagePayload = JsonConvert.SerializeObject(messageRequest);

            var mqttMessage = new MqttApplicationMessageBuilder()
                              .WithTopic(messagingParameters.OnboardResponse.ConnectionCriteria.Measures)
                              .WithPayload(messagePayload)
                              .WithExactlyOnceQoS()
                              .WithRetainFlag()
                              .Build();

            _mqttClient.PublishAsync(mqttMessage, CancellationToken.None);

            return(new MessagingResultBuilder().WithApplicationMessageId(messagingParameters.ApplicationMessageId)
                   .Build());
        }
Esempio n. 3
0
        private static RequestEnvelope Header(MessageHeaderParameters messageHeaderParameters)
        {
            Log.Debug("Begin creating the header of the message.");

            var requestEnvelope = new RequestEnvelope
            {
                ApplicationMessageId = messageHeaderParameters.ApplicationMessageId ??
                                       MessageIdService.ApplicationMessageId(),
                ApplicationMessageSeqNo = Parameters.ApplicationMessageSeqNo,
                TechnicalMessageType    = messageHeaderParameters.TechnicalMessageType,
                Mode      = messageHeaderParameters.Mode,
                Timestamp = UtcDataService.NowAsTimestamp()
            };

            if (!string.IsNullOrEmpty(messageHeaderParameters.TeamSetContextId))
            {
                requestEnvelope.TeamSetContextId = messageHeaderParameters.TeamSetContextId;
            }

            if (messageHeaderParameters.Recipients != null)
            {
                foreach (var recipient in messageHeaderParameters.Recipients)
                {
                    requestEnvelope.Recipients.Add(recipient);
                }
            }

            if (messageHeaderParameters.ChunkInfo != null)
            {
                requestEnvelope.ChunkInfo = messageHeaderParameters.ChunkInfo;
            }

            Log.Debug("Finished creating the header of the message.");

            return(requestEnvelope);
        }