Ejemplo n.º 1
0
        public void ExecuteReadClass0Command()
        {
            DNP3ReadClass0CommandParameters dnp3CommandParam = new DNP3ReadClass0CommandParameters(GetApplicationSequence(), (byte)DNP3FunctionCode.READ, (ushort)TypeField.CLASS_0_DATA,
                                                                                                   (byte)Qualifier.PREFIX_NONE_NO_RANGE_FIELD, GetTransportSequence());
            IDNP3Function dnp3Fn = DNP3FunctionFactory.CreateReadClass0Function(dnp3CommandParam);

            this.functionExecutor.EnqueueCommand(dnp3Fn);
        }
Ejemplo n.º 2
0
 public void EnqueueCommand(IDNP3Function command)
 {
     if (this.connectionState == ConnectionState.CONNECTED)
     {
         commandQueue.Enqueue(command);
         processConnection.Set();
     }
 }
Ejemplo n.º 3
0
        public void ExecuteReadCommand(RegisterType type, uint index)
        {
            log.Log(new SCADA.Common.Logging.LogEventModel()
            {
                EventType = SCADA.Common.Logging.LogEventType.INFO, Message = $"ReadCommand ({type},{index})"
            });
            DNP3ReadCommandParameters dnp3CommandParam = new DNP3ReadCommandParameters(GetApplicationSequence(), (byte)DNP3FunctionCode.READ, GetTypeField(type),
                                                                                       (byte)Qualifier.PREFIX_2_OCTET_COUNT_OF_OBJECTS_2_OCTET, index, GetTransportSequence());
            IDNP3Function dnp3Fn = DNP3FunctionFactory.CreateReadFunction(dnp3CommandParam);

            this.functionExecutor.EnqueueCommand(dnp3Fn);
        }
Ejemplo n.º 4
0
        public void ExecuteWriteCommand(RegisterType type, uint index, uint value)
        {
            DNP3WriteCommandParameters dnp3CommandParam = new DNP3WriteCommandParameters(GetApplicationSequence(), (byte)DNP3FunctionCode.DIRECT_OPERATE, GetTypeField(type, true),
                                                                                         (byte)Qualifier.PREFIX_2_OCTET_COUNT_OF_OBJECTS_2_OCTET, 1, index, value, GetTransportSequence());
            IDNP3Function dnp3Fn = DNP3FunctionFactory.CreateWriteFunction(dnp3CommandParam);

            this.functionExecutor.EnqueueCommand(dnp3Fn);

            var point = storage.GetSingle(type, (int)index);

            if (point != null && point.RegisterType == RegisterType.BINARY_OUTPUT)
            {
                dom.AddOrUpdate(new SCADA.Common.Models.DomDbModel()
                {
                    Mrid = point.Mrid, TimeStamp = DateTime.Now.ToString()
                });
            }
        }
Ejemplo n.º 5
0
        private void ConnectionProcessorThread()
        {
            while (threadCancellationSignal)
            {
                try
                {
                    if (connectionState == ConnectionState.DISCONNECTED)
                    {
                        numberOfConnectionRetries = 0;
                        connection.Connect();
                        while (numberOfConnectionRetries < 10)
                        {
                            if (connection.CheckState())
                            {
                                this.connectionState      = ConnectionState.CONNECTED;
                                numberOfConnectionRetries = 0;
                                break;
                            }
                            else
                            {
                                numberOfConnectionRetries++;
                                if (numberOfConnectionRetries == 10)
                                {
                                    connection.Disconect();
                                    connectionState = ConnectionState.DISCONNECTED;
                                }
                            }
                        }
                    }
                    else
                    {
                        processConnection.WaitOne();
                        while (commandQueue.TryDequeue(out currentCommand))
                        {
                            lock (lockObj)
                            {
                                connection.Send(currentCommand.PackRequest());
                                byte[] message;
                                byte[] header     = connection.Recv(10);
                                int    recvLen    = CalculateRecvLength(header[2]);
                                byte[] dataChunks = connection.Recv(recvLen);
                                message = new byte[header.Length + recvLen];
                                Buffer.BlockCopy(header, 0, message, 0, 10);
                                Buffer.BlockCopy(dataChunks, 0, message, 10, recvLen);

                                bool unsolicited = CheckIfUnsolicited(message[11]);
                                if (unsolicited)
                                {
                                    HandleReceivedBytes(message, unsolicited);
                                    DNP3ConfirmCommandParamters dnp3Param = new DNP3ConfirmCommandParamters(0xc0, (byte)DNP3FunctionCode.CONFIRM, 0xc0); //podesiti parametre
                                    IDNP3Function function = DNP3FunctionFactory.CreateConfirmFunction(dnp3Param);
                                    connection.Send(function.PackRequest());
                                }
                                else
                                {
                                    HandleReceivedBytes(message, unsolicited);
                                }
                                currentCommand = null;
                            }
                        }
                    }
                }
                catch (SocketException se)
                {
                    currentCommand  = null;
                    connectionState = ConnectionState.DISCONNECTED;
                    Log().Log(new LogEventModel()
                    {
                        EventType = LogEventType.ERROR, Message = $"{se.Message}"
                    }).GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    currentCommand = null;
                    Log().Log(new LogEventModel()
                    {
                        EventType = LogEventType.WARN, Message = $"{ex.Message}"
                    }).GetAwaiter().GetResult();
                }
            }
        }