示例#1
0
        private void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            try
            {
                MbapHeader header = MbapHeader.Decode(e.Message);

                if (!unitId.HasValue)
                {
                    unitId = header.UnitId;
                }

                if (unitId.HasValue && header.UnitId == unitId.Value)
                {
                    RtuPiSystem piSystem = map.GetItem((ushort)unitId.Value);

                    if (piSystem == null)
                    {
                        throw new Exception($"PI-System not found for unit id - {unitId.Value}");
                    }
                    else
                    {
                        client.SubscribeAsync(piSystem.RtuOutputEvent, QualityOfServiceLevelType.AtLeastOnce, ReceiveOutput).GetAwaiter();
                        client.PublishAsync(QualityOfServiceLevelType.AtLeastOnce, piSystem.RtuInputEvent, contentType, e.Message).GetAwaiter();
                    }
                }
                else
                {
                    throw new Exception("Unit Id missing from SCADA client message.");
                }
            }
            catch (Exception ex)
            {
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }
示例#2
0
        private async void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            logger?.LogDebug("SCADA client channel starting receive.");

            try
            {
                MbapHeader  header   = MbapHeader.Decode(e.Message);
                RtuPiSystem piSystem = map.GetItem(header.UnitId);

                if (piSystem == null)
                {
                    logger?.LogWarning("SCADA client receive cannot find RTU pi-system.");
                    throw new InvalidOperationException("RTU pi-system was not found.");
                }

                if (!subscribed.Contains(header.UnitId))
                {
                    //subscribe to pi-system for unit id
                    await connection.AddSubscriptionAsync(piSystem.RtuOutputEvent.ToLowerInvariant(), ReceiveOutput);

                    subscribed.Add(header.UnitId);
                }

                byte[] msg = mapper.MapIn(e.Message);
                await connection.SendAsync(piSystem.RtuInputEvent.ToLowerInvariant(), CONTENT_TYPE, msg);

                MbapHeader mheader = MbapHeader.Decode(msg);


                //await connection.Monitor.SendInAsync(ModuleType.VRTU.ToString(), e.Message, mheader.TransactionId);
            }
            catch (Exception ex)
            {
                logger?.LogError($"SCADA client receive error - {ex.Message}");
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }