Beispiel #1
0
        private void Input_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            byte[] message = e.Message;
            byte[] msg     = message;

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                if (header == null)
                {
                    return;
                }

                Slave slave = config.Slaves.Where(s => s.UnitId == header.UnitId).FirstOrDefault();
                byte? alias = slave?.Alias.Value;

                foreach (var filter in InputFilters)
                {
                    msg = filter.Execute(message, alias);
                    msg ??= message;
                    logger?.LogDebug("Filter executed.");
                }

                OutputChannel.SendAsync(msg).GetAwaiter();
                logger?.LogDebug("Message sent to output channel.");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault input channel receive.");
            }
        }
        public async Task PublishInput(MbapHeader header, ushort transactionId)
        {
            if (!NativeEnabled && !AppInsightsEnabled)
            {
                return;
            }

            DiagnosticsEvent telem = new DiagnosticsEvent(name, header.UnitId, transactionId, header.TransactionId,
                                                          DateTime.UtcNow.ToString("dd-MM-yyyyThh:mm:ss.ffff"));

            cache.Add(transactionId.ToString(), new Tuple <byte, long>(header.UnitId, DateTime.Now.Ticks), 20);

            if (NativeEnabled && mqttClient != null && mqttClient.IsConnected)
            {
                string jsonString = JsonConvert.SerializeObject(telem);
                byte[] msg        = Encoding.UTF8.GetBytes(jsonString);
                await mqttClient.PublishAsync(QualityOfServiceLevelType.AtMostOnce, outputPiSystem, "application/json",
                                              msg);
            }

            if (AppInsightsEnabled)
            {
                tclient?.TrackEvent(name, telem.GetEventProperties(), telem.GetEventMetrics());
            }
        }
Beispiel #3
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));
            }
        }
        private void Input_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            MbapHeader header = MbapHeader.Decode(e.Message);

            if (header == null)
            {
                logger?.LogWarning("MBAP Header returned null");
                return;  //assume keep alive
            }
            if (!map.HasItem(header.UnitId))
            {
                byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.GatewayPathsNotAvailable);
                this.InputChannel.SendAsync(errorMsg).GetAwaiter();
                return;
            }

            if (!map.GetItem(header.UnitId).Authorize(e.Message))
            {
                byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.IllegalAddress);
                this.InputChannel.SendAsync(errorMsg).GetAwaiter();
                return;
            }

            byte[] message = e.Message;
            byte[] msg     = null;

            foreach (var filter in InputFilters)
            {
                msg = filter.Execute(message);
                msg ??= message;
            }

            OutputChannel.SendAsync(msg).GetAwaiter();
        }
Beispiel #5
0
        public async Task PublishOutput(MbapHeader header, ushort transactionId)
        {
            if (!NativeEnabled && !AppInsightsEnabled)
            {
                return;
            }

            if (cache.Contains(transactionId.ToString()))
            {
                Tuple <byte, long> tuple = cache.Get <Tuple <byte, long> >(transactionId.ToString());
                cache.Remove(transactionId.ToString());
                TimeSpan         ts    = TimeSpan.FromTicks(DateTime.Now.Ticks - tuple.Item2);
                DiagnosticsEvent telem = new DiagnosticsEvent(name, header.UnitId, transactionId, header.TransactionId, Math.Round(ts.TotalMilliseconds), DateTime.UtcNow.ToString("dd-MM-yyyyThh:mm:ss.ffff"));


                if (NativeEnabled && mqttClient != null && mqttClient.IsConnected)
                {
                    string jsonString = JsonConvert.SerializeObject(telem);
                    byte[] data       = Encoding.UTF8.GetBytes(jsonString);
                    await mqttClient.PublishAsync(QualityOfServiceLevelType.AtMostOnce, outputPiSystem, "application/json", data);
                }

                if (AppInsightsEnabled)
                {
                    tclient?.TrackEvent(name, telem.GetEventProperties(), telem.GetEventMetrics());
                }
            }
        }
 private void _read()
 {
     _header   = new MbapHeader(m_io, this, m_root);
     _status   = ((ModbusStatusCode)m_io.ReadBitsInt(1));
     _function = ((ModbusFunctionCode)m_io.ReadBitsInt(7));
     m_io.AlignToByte();
     _data = m_io.ReadBytes((Header.Length - 2));
 }
        public async Task SendAsync(byte[] message)
        {
            MbapHeader header = MbapHeader.Decode(message);

            if (slaveChannels.ContainsKey(header.UnitId))
            {
                SlaveChannel slaveChannel = slaveChannels[header.UnitId];
                await slaveChannel.SendAsync(message);
            }
        }
Beispiel #8
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.");
     }
 }
Beispiel #9
0
        public async Task SendAsync(byte[] message)
        {
            MbapHeader header = MbapHeader.Decode(message);

            if (connections.ContainsKey(header.UnitId))
            {
                TcpConnection connection = connections[header.UnitId].Item1;
                byte[]        msg        = mapper.MapIn(message, connections[header.UnitId].Item2);
                await connection.SendAsync(msg);

                logger?.LogDebug($"Modbus message sent to UnitId {header.UnitId} TCP channel.");
            }
            else
            {
                logger?.LogWarning($"No tcp connection found with Unit ID = {header.UnitId}");
            }
        }
        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.");
            }
        }
        private async void ReceiveOutput(string topic, string contentType, byte[] message)
        {
            logger?.LogDebug("SCADA client channel receiving output.");

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                byte[]     msg    = mapper.MapOut(message);
                await channel.SendAsync(msg);

                MbapHeader actual = MbapHeader.Decode(msg);
                await diagnostics.PublishOutput(actual, header.TransactionId);

                logger?.LogDebug("SCADA client channel was sent output message.");
            }
            catch (Exception ex)
            {
                logger?.LogError($"SCADA client receive output error - {ex.Message}");
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("----Test SCADA Echo Client-----");
            Console.WriteLine("press any key to continue");
            Console.ReadKey();

            //Console.WriteLine("Enter VRTU IP address or hostname ? ");

            //string publicIP = "40.121.83.251";
            //string publicIP = "172.18.144.1";
            string publicIP = GetIPAddressString(System.Net.Dns.GetHostName());
            Random ran      = new Random();

            byte[] buffer = new byte[100];
            ran.NextBytes(buffer);
            MbapHeader header = new MbapHeader()
            {
                UnitId        = 1,
                ProtocolId    = 1,
                TransactionId = 1,
                Length        = 100
            };

            byte[] array  = header.Encode();
            byte[] output = new byte[buffer.Length + array.Length];
            Buffer.BlockCopy(array, 0, output, 0, array.Length);
            Buffer.BlockCopy(buffer, 0, output, array.Length, buffer.Length);
            CancellationTokenSource cts = new CancellationTokenSource();
            IPEndPoint endpoint         = new IPEndPoint(IPAddress.Parse(publicIP), 502);
            IChannel   channel          = ChannelFactory.Create(false, endpoint, 1024, 4048, cts.Token);

            channel.OnError += Channel_OnError;
            channel.OnClose += Channel_OnClose;
            channel.OnOpen  += Channel_OnOpen;
            channel.OpenAsync().Wait();
            channel.SendAsync(output).GetAwaiter();

            Console.WriteLine("Message sent");
            Console.ReadKey();
        }
Beispiel #13
0
        public async Task SendAsync(byte[] message)
        {
            if (client == null || !client.IsConnected)
            {
                logger?.LogWarning("Module channel client is unavailable to send.");
                return;
            }

            try
            {
                MbapHeader header   = MbapHeader.Decode(message);
                string     pisystem = UriGenerator.GetRtuPiSystem(hostname, virtualRtuId, deviceId, header.UnitId, false);
                await client.PublishAsync(QualityOfServiceLevelType.AtMostOnce, pisystem, "application/json", message);

                await diag?.PublishOutput(header);

                logger?.LogDebug("Published message on module channel");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault sending on module channel.");
            }
        }
 private void ModbusMessageReceived(string resource, string contentType, byte[] message)
 {
     try
     {
         MbapHeader header = MbapHeader.Decode(message);
         string     key    = GetCacheKey(header);
         if (cache.Contains(key))
         {
             cache.Remove(key);
             OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, message));
             logger?.LogDebug("Output channel received message.");
             diag?.PublishOutput(header).GetAwaiter();
         }
         else
         {
             logger?.LogWarning("Vrtu channel cannot match received message.");
         }
     }
     catch (Exception ex)
     {
         logger?.LogError(ex, "Fault receiving message on vrtu channel.");
     }
 }
        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));
            }
        }
Beispiel #16
0
        private static void Main(string[] args)
        {
            Console.WriteLine("8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8");
            Console.WriteLine("8      8    \" 8    8 8    8 8    8");
            Console.WriteLine("8eeeee 8e     8eeee8 8e   8 8eeee8 ");
            Console.WriteLine("    88 88     88   8 88   8 88   8");
            Console.WriteLine("e   88 88   e 88   8 88   8 88   8 ");
            Console.WriteLine("8eee88 88eee8 88   8 88eee8 88   8");
            Console.WriteLine("");
            Console.WriteLine("8\"\"\"\"8");
            Console.WriteLine("8    \" e     e  eeee eeeee eeeee ");
            Console.WriteLine("8e     8     8  8    8   8   8");
            Console.WriteLine("88     8e    8e 8eee 8e  8   8e");
            Console.WriteLine("88   e 88    88 88   88  8   88");
            Console.WriteLine("88eee8 88eee 88 88ee 88  8   88");
            Console.WriteLine("");


            Console.WriteLine("press any key to continue");
            Console.ReadKey();

            //string publicIP = "168.62.59.20";
            string publicIP = "20.185.9.100";

            Console.Write("Enter for default IP (127.0.0.1)? ");
            string inputIpAddress = Console.ReadLine();

            if (!string.IsNullOrEmpty(inputIpAddress))
            {
                publicIP = inputIpAddress;
            }
            else
            {
                publicIP = "127.0.0.1";
            }


            Random ran = new Random();

            byte[] buffer = new byte[100];
            ran.NextBytes(buffer);
            MbapHeader header = new MbapHeader
            {
                UnitId        = 2,
                ProtocolId    = 1,
                TransactionId = 1,
                Length        = 6
            };

            byte[] body = { 3, 79, 27, 0, 10 };


            byte[] array  = header.Encode();
            byte[] output = new byte[buffer.Length + array.Length];
            Buffer.BlockCopy(array, 0, output, 0, array.Length);
            Buffer.BlockCopy(buffer, 0, output, array.Length, buffer.Length);

            byte[]     o2 = Convert.FromBase64String("AAEAAAAGAQNPGwAK");
            MbapHeader mh = MbapHeader.Decode(o2);

            output = o2;
            string x = BitConverter.ToString(o2);

            CancellationTokenSource cts = new CancellationTokenSource();
            IPEndPoint endpoint         = new IPEndPoint(IPAddress.Parse(publicIP), 502);

            channel            = ChannelFactory.Create(false, endpoint, 1024, 4048, cts.Token);
            channel.OnError   += Channel_OnError;
            channel.OnClose   += Channel_OnClose;
            channel.OnOpen    += Channel_OnOpen;
            channel.OnReceive += Channel_OnReceive;

            channel.OpenAsync().GetAwaiter();
            while (!ican)
            {
                Task t = Task.Delay(2000);
                Task.WaitAll(t);
            }

            //channel.SendAsync(output).GetAwaiter();

            bool test = true;

            while (test)
            {
                Console.Write("Send a message [y/n] ? ");

                string decision = Console.ReadLine();
                if (decision.ToLowerInvariant() == "y")
                {
                    byte[] payload = Convert.FromBase64String("AAEAAAAGAQNPGwAK");
                    //MbapHeader header2 = MbapHeader.Decode(payload);
                    //header2.UnitId = 2;
                    //byte[] headerBytes = header2.Encode();
                    //byte[] buffer2 = new byte[payload.Length];
                    //Buffer.BlockCopy(headerBytes, 0, buffer2, 0, headerBytes.Length);
                    //Buffer.BlockCopy(payload, headerBytes.Length, buffer2, headerBytes.Length, payload.Length - headerBytes.Length);

                    //dummy++;
                    //payload[1] = dummy;

                    //string bc = BitConverter.ToString(payload);
                    //channel.SendAsync(buffer2).GetAwaiter();
                    channel.SendAsync(payload).GetAwaiter();
                    //channel.SendAsync(output).GetAwaiter();
                    Console.WriteLine($"Sent message length {output.Length}");
                }
                else
                {
                    test = false;
                }
            }

            Console.WriteLine("terminating...");
            Console.ReadKey();
        }
        private void _read()
        {
            _header   = new MbapHeader(m_io, this, m_root);
            _function = ((ModbusFunctionCode)m_io.ReadU1());
            switch (Function)
            {
            case ModbusFunctionCode.ReadFileRecord: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadFileRecordFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadInputRegisters: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadInputRegistersFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.WriteFileRecord: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new WriteFileRecordFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadWriteMultiupleRegisters: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadWriteMultiupleRegistersFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadInputStatus: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadInputStatusFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.MaskWriteRegister: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new MaskWriteRegisterFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.WriteSingleRegister: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new WriteSingleRegisterFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadHoldingRegister: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadHoldingRegistersFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadFifoQueue: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadFifoQueueFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.WriteSingleCoil: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new WriteSingleCoilFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadDeviceIdentification: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadDeviceIdentificationFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.WriteMultipleCoils: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new WriteMultipleCoilsFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.WriteMultipleRegisters: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new WriteMultipleRegistersFunction(io___raw_data, this, m_root);
                break;
            }

            case ModbusFunctionCode.ReadCoilStatus: {
                __raw_data = m_io.ReadBytes((Header.Length - 2));
                var io___raw_data = new KaitaiStream(__raw_data);
                _data = new ReadCoilStatusFunction(io___raw_data, this, m_root);
                break;
            }

            default: {
                _data = m_io.ReadBytes((Header.Length - 2));
                break;
            }
            }
        }
 private string GetCacheKey(MbapHeader header)
 {
     return($"{header.UnitId}-{header.TransactionId}");
 }