Example #1
0
        /// <summary>
        /// Sends the Observations to the MQTT broker
        /// </summary>
        /// <param name="sendAdditionalProperties">If true, the 'Writeable' and 'Forceable' properties will be sent in the Observation</param>
        /// <returns></returns>
        private async Task <bool> UpdateValues(SubscriptionReader si, ReadResult <SubscriptionResultItem> results, bool sendAdditionalProperties = false)
        {
            if (!results.Success)
            {
                Prompts.AddRange(results.Prompts);
                return(false);
            }

            var devices = results.DataRead.GroupBy(a => a.ValueItemChangeEvent.Id.Remove(a.ValueItemChangeEvent.Id.LastIndexOf('/')).Remove(0, 2));

            foreach (var device in devices)
            {
                var observations  = new List <Observation>();
                var deviceMessage = new IotEdgeMessage
                {
                    Format       = "rec2.3",
                    Observations = observations,
                    DeviceId     = device.Key
                };

                AddUpdatedValuesToMessage(observations, device.Key, device.ToList(), si.CachedSubscribedItems, sendAdditionalProperties);

                var messageBuilder = new MqttApplicationMessageBuilder();
                var message        = messageBuilder.WithRetainFlag().WithAtLeastOnceQoS().WithTopic(ValuePushTopic).WithPayload(deviceMessage.ToJson()).Build();
                Logger.LogTrace(LogCategory.Processor, this.Name, $"Sending Message to MQTT Broker: {deviceMessage.ToJson()}");
                await ManagedMqttClient.PublishAsync(message);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Sends the Observations to the MQTT broker
        /// </summary>
        /// <param name="sendAdditionalProperties">If true, the 'Writeable' and 'Forceable' properties will be sent in the Observation</param>
        /// <returns></returns>
        private async Task <bool> UpdateValues(SubscriptionReader si, ReadResult <SubscriptionResultItem> results, bool sendAdditionalProperties = false)
        {
            if (!results.Success)
            {
                Prompts.AddRange(results.Prompts);
                return(false);
            }

            if (results.DataRead.Any(a => a.ValueItemChangeEvent.State == EwsValueStateEnum.Error))
            {
                CheckAndRetryValuesWithError(si, results);
            }

            var signalChanges = results.DataRead.GroupBy(a => a.ValueItemChangeEvent.Id.Remove(a.ValueItemChangeEvent.Id.LastIndexOf('/')).Remove(0, 2)).ToList();
            var devices       = _tempSignals.GroupBy(a => a.DatabasePath.Remove(a.DatabasePath.LastIndexOf('/')));

            foreach (var device in devices)
            {
                var observations  = new List <Observation>();
                var deviceMessage = new IotEdgeMessage
                {
                    Format       = "rec2.3",
                    Observations = observations,
                    DeviceId     = device.Key
                };
                var signalChangesForDevice = signalChanges.FirstOrDefault(a => a.Key == device.Key);
                AddUpdatedValuesToMessage(observations, device.Key, signalChangesForDevice == null || !signalChangesForDevice.ToList().Any() ? new List <SubscriptionResultItem>() : signalChangesForDevice.ToList(), si.CachedSubscribedItems, sendAdditionalProperties);

                if (deviceMessage.Observations != null && deviceMessage.Observations.Count > 0)
                {
                    var messageBuilder        = new MqttApplicationMessageBuilder();
                    var managedMessageBuilder = new ManagedMqttApplicationMessageBuilder();
                    var message = messageBuilder.WithRetainFlag().WithAtLeastOnceQoS().WithTopic(ValuePushTopic).WithPayload(deviceMessage.ToJson()).Build();
                    Logger.LogTrace(LogCategory.Processor, this.Name, $"Sending Message to MQTT Broker: {deviceMessage.ToJson()}");
                    await ManagedMqttClient.PublishAsync(managedMessageBuilder.WithApplicationMessage(message).Build());
                }
            }

            return(true);
        }
        private List <(ValueTypeStateless valueTypeStateless, Signal signal)> GetListOfValuesToSetInEbo(IotEdgeMessage iotEdgeMessage)
        {
            var toSet = new List <(ValueTypeStateless valueTypeStateless, Signal signal)>();

            foreach (var option in iotEdgeMessage.Actuations)
            {
                var theSignalToSet = Signals.FirstOrDefault(a => a.DatabasePath == $"{iotEdgeMessage.DeviceId}/{option.ActuatorId}");

                if (theSignalToSet == null)
                {
                    Logger.LogInfo(LogCategory.Processor, this.Name, $"{iotEdgeMessage.DeviceId}/{option.ActuatorId} does not exist in the list of monitored values. Skipping.");
                    if (iotEdgeMessage.Exceptions == null)
                    {
                        iotEdgeMessage.Exceptions = new List <ExceptionElement>();
                    }
                    iotEdgeMessage.Exceptions.Add(new ExceptionElement
                    {
                        ExceptionTime = DateTimeOffset.UtcNow,
                        Exception     = $"{iotEdgeMessage.DeviceId}/{option.ActuatorId} does not exist in the list of monitored values. Please make sure that the configuration CSV file contains this point",
                        Retry         = 1,
                        SensorId      = option.ActuatorId
                    });
                }
                else
                {
                    toSet.Add(new ValueTuple <ValueTypeStateless, Signal>(
                                  new ValueTypeStateless {
                        Id = theSignalToSet.EwsIdForWrite, Value = option.Value
                    },
                                  theSignalToSet));
                }
            }
            return(toSet);
        }
Example #4
0
 public static string ToJson(this IotEdgeMessage self) => JsonConvert.SerializeObject(self, JsonConverter.Settings);