Esempio n. 1
0
 private void ModuleReceived(string resource, string contentType, byte[] message)
 {
     //received a message from subscription
     try
     {
         MbapHeader header = MbapHeader.Decode(message);
         diag?.PublishInput(header).GetAwaiter();
         logger?.LogDebug("Diagnostics sent input.");
         OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, message));
     }
     catch (Exception ex)
     {
         logger?.LogError(ex, "Unable to decode MBAP header module channel input.");
     }
 }
Esempio n. 2
0
        public async Task SendAsync(byte[] message)
        {
            if (client == null || !client.IsConnected)
            {
                logger?.LogWarning("MQTT client is not available to forward message.");
                return;
            }

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                if (map.HasItem(header.UnitId))
                {
                    if (!subscriptions.Contains(header.UnitId))
                    {
                        string resource = map.GetItem(header.UnitId).RtuOutputEvent;
                        await client.SubscribeAsync(resource, QualityOfServiceLevelType.AtMostOnce,
                                                    ModbusMessageReceived);

                        logger?.LogInformation(
                            $"MQTT client channel subscribed {resource} with Unit ID = {header.UnitId}");
                        subscriptions.Add(header.UnitId);
                    }

                    cache.Add(GetCacheKey(header), new Tuple <ushort, byte[]>(header.TransactionId, message), 20.0);
                    string pisystem = map.GetItem(header.UnitId).RtuInputEvent;
                    await client.PublishAsync(QualityOfServiceLevelType.AtMostOnce, pisystem,
                                              "application/octet-stream", message);

                    logger?.LogDebug($"VRTU published to {pisystem}");
                    await diag?.PublishInput(header);
                }
                else
                {
                    logger?.LogWarning($"Unit Id = {header.UnitId} in Modbus message not found in RTU map.");
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault sending MQTT client channel.");
            }
        }