Esempio n. 1
0
        public OMSSCADACommon.Responses.Response ReadSingleDigital(string id)
        {
            Digital digital = null;

            OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response();

            // getting PV from db
            ProcessVariable pv;

            if (dbContext.GetProcessVariableByName(id, out pv))
            {
                digital = (Digital)pv;
            }

            // does this ID exist in the database
            if (digital == null)
            {
                response.ResultMessage = ResultMessage.INVALID_ID;
                return(response);
            }

            DigitalVariable digitalVariable = new DigitalVariable()
            {
                Id    = id,
                State = digital.State
            };

            response.Variables.Add(digitalVariable);

            return(response);
        }
Esempio n. 2
0
        public OMSSCADACommon.Responses.Response ReadSingleAnalog(string id)
        {
            Analog analog = null;

            OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response();

            ProcessVariable pv;

            if (dbContext.GetProcessVariableByName(id, out pv))
            {
                analog = (Analog)pv;
            }
            // does this ID exist in the database
            if (analog == null)
            {
                response.ResultMessage = ResultMessage.INVALID_ID;
                return(response);
            }

            AnalogVariable analogVariable = new AnalogVariable()
            {
                Id    = id,
                Value = analog.AcqValue
            };

            response.Variables.Add(analogVariable);

            return(response);
        }
Esempio n. 3
0
        public OMSSCADACommon.Responses.Response ReadAll()
        {
            Console.WriteLine("Response ReadAll");

            // to do:
            // while (!Database.IsConfigurationRunning)

            List <ProcessVariable> pvs = dbContext.GetProcessVariable().ToList();

            OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response();

            foreach (ProcessVariable pv in pvs)
            {
                switch (pv.Type)
                {
                case VariableTypes.DIGITAL:
                    Digital digital = (Digital)pv;
                    response.Variables.Add(new DigitalVariable()
                    {
                        VariableType = ResponseVarType.Digital, Id = digital.Name, State = (OMSSCADACommon.States)digital.State
                    });
                    break;

                case VariableTypes.ANALOG:
                    Analog analog = (Analog)pv;
                    // to do: fix this
                    response.Variables.Add(new AnalogVariable()
                    {
                        VariableType = ResponseVarType.Analog, Id = analog.Name, Value = analog.AcqValue, UnitSymbol = "w"
                    });
                    break;


                case VariableTypes.COUNTER:
                    Counter counter = (Counter)pv;
                    response.Variables.Add(new CounterVariable()
                    {
                        VariableType = ResponseVarType.Counter, Id = counter.Name, Value = counter.Value
                    });
                    break;
                }
            }

            response.ResultMessage = ResultMessage.OK;

            return(response);
        }
Esempio n. 4
0
        public OMSSCADACommon.Responses.Response WriteSingleDigital(string id, CommandTypes command)
        {
            Console.WriteLine("WriteSingleDigital!");

            Digital digital = null;

            OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response();

            // sporno
            //while (!Database.IsConfigurationRunning)

            // getting PV from db
            ProcessVariable pv;

            if (dbContext.GetProcessVariableByName(id, out pv))
            {
                digital = (Digital)pv;
            }

            // does this ID exist in the database
            if (digital == null)
            {
                response.ResultMessage = ResultMessage.INVALID_ID;
                return(response);
            }

            // is this a valid command for this digital device
            if (!CommandValidator.ValidateDigitalCommand(digital, command))
            {
                response.ResultMessage = ResultMessage.INVALID_DIG_COMM;
                return(response);
            }


            RTU rtu;

            if ((rtu = dbContext.GetRTUByName(digital.ProcContrName)) != null)
            {
                IORequestBlock iorb = new IORequestBlock()
                {
                    RequestType           = RequestType.SEND,
                    ProcessControllerName = digital.ProcContrName
                };

                iorb.ReqAddress = (ushort)rtu.GetCommandAddress(digital);

                switch (rtu.Protocol)
                {
                case IndustryProtocols.ModbusTCP:

                    ModbusHandler mdbHandler = new ModbusHandler
                    {
                        Header = new ModbusApplicationHeader()
                        {
                            TransactionId = 0,
                            Length        = 5,
                            ProtocolId    = (ushort)IndustryProtocols.ModbusTCP,
                            DeviceAddress = rtu.Address
                        },

                        Request = new WriteRequest()
                        {
                            FunCode   = FunctionCodes.WriteSingleCoil,
                            StartAddr = (ushort)rtu.GetCommandAddress(digital),
                            Value     = (ushort)command
                        }
                    };

                    iorb.SendBuff      = mdbHandler.PackData();
                    iorb.SendMsgLength = iorb.SendBuff.Length;
                    break;
                }

                IORequests.EnqueueRequest(iorb);
                Console.WriteLine("enqued {0}", BitConverter.ToString(iorb.SendBuff, 0, 12));

                digital.Command = command;

                response.ResultMessage = ResultMessage.OK;
            }
            else
            {
                // rtu does not exist
            }

            return(response);
        }
Esempio n. 5
0
        // napravila, ali nisam testirala do kraja, to je nekad za buducnost, svakako ne treba sad :)
        public OMSSCADACommon.Responses.Response WriteSingleAnalog(string id, float value)
        {
            Console.WriteLine("WriteSingleAnalog!");

            Analog analog = null;

            OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response();

            // to do:
            //while (!Database.IsConfigurationRunning)


            // getting PV from db
            ProcessVariable pv;

            if (dbContext.GetProcessVariableByName(id, out pv))
            {
                analog = (Analog)pv;
            }
            // does this ID exist in the database
            if (analog == null)
            {
                response.ResultMessage = ResultMessage.INVALID_ID;
                return(response);
            }

            // to do:
            // ovde analogProcessor provera opsega, alarma...bla, bla



            RTU rtu;

            if ((rtu = dbContext.GetRTUByName(analog.ProcContrName)) != null)
            {
                IORequestBlock iorb = new IORequestBlock()
                {
                    RequestType           = RequestType.SEND,
                    ProcessControllerName = analog.ProcContrName
                };

                iorb.ReqAddress = (ushort)rtu.GetCommandAddress(analog);

                bool shouldCommand = false;
                switch (rtu.Protocol)
                {
                case IndustryProtocols.ModbusTCP:

                    ModbusHandler mdbHandler = new ModbusHandler
                    {
                        Header = new ModbusApplicationHeader()
                        {
                            TransactionId = 0,
                            Length        = 5,
                            ProtocolId    = (ushort)IndustryProtocols.ModbusTCP,
                            DeviceAddress = rtu.Address
                        },

                        Request = new WriteRequest()
                        {
                            FunCode   = FunctionCodes.WriteSingleCoil,
                            StartAddr = (ushort)rtu.GetCommandAddress(analog)
                        }
                    };

                    if (shouldCommand = AnalogProcessor.SetNewWorkPoint(analog, value))
                    {
                        mdbHandler.Request.FunCode = FunctionCodes.WriteSingleRegister;
                        ((WriteRequest)mdbHandler.Request).Value = (ushort)analog.RawCommValue;
                    }

                    iorb.SendBuff      = mdbHandler.PackData();
                    iorb.SendMsgLength = iorb.SendBuff.Length;
                    break;
                }

                IORequests.EnqueueRequest(iorb);
                Console.WriteLine("enqued {0}", BitConverter.ToString(iorb.SendBuff, 0, 12));
                response.ResultMessage = ResultMessage.OK;
            }
            else
            {
                // rtu does not exist
            }

            return(response);
        }