Ejemplo n.º 1
0
        BuildInfoObj(
            Int32 asdu,
            Int32 addr,
            Double value,
            Boolean sbo               = false,
            Int32 cmdqualif           = 0,
            QualityDescriptor quality = null,
            CP56Time2a time_tag       = null,
            Double kconv1             = 1,
            Double kconv2             = 0,
            Boolean transient         = false
            )
        {
            InformationObject sc = null;

            if (time_tag == null)
            {
                time_tag = new CP56Time2a(DateTime.Now);
            }
            else
            { // has time tag, so change ASDU if necessary to embed a timetag
                var maptowithcp56time = new Dictionary <TypeID, TypeID>();
                maptowithcp56time.Add(TypeID.M_SP_NA_1, TypeID.M_SP_TB_1);
                maptowithcp56time.Add(TypeID.M_DP_NA_1, TypeID.M_DP_TB_1);
                maptowithcp56time.Add(TypeID.M_ST_NA_1, TypeID.M_ST_TB_1);
                maptowithcp56time.Add(TypeID.M_BO_NA_1, TypeID.M_BO_TB_1);
                maptowithcp56time.Add(TypeID.M_ME_NA_1, TypeID.M_ME_TD_1);
                maptowithcp56time.Add(TypeID.M_ME_NB_1, TypeID.M_ME_TE_1);
                maptowithcp56time.Add(TypeID.M_ME_NC_1, TypeID.M_ME_TF_1);
                maptowithcp56time.Add(TypeID.M_IT_NA_1, TypeID.M_IT_TB_1);
                if (maptowithcp56time.TryGetValue((TypeID)asdu, out var newasdu))
                {
                    asdu = (Int32)newasdu;
                }
            }

            Boolean bval;
            Int32   ival;
            UInt32  uival;

            switch ((TypeID)asdu)
            {
            case TypeID.M_SP_NA_1:     // 1
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SinglePointInformation(addr,
                                               bval,
                                               quality);
                break;

            case TypeID.M_SP_TB_1:     // 30
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SinglePointWithCP56Time2a(addr,
                                                  bval,
                                                  quality,
                                                  time_tag
                                                  );
                break;

            case TypeID.M_DP_NA_1:     // 3
                if (transient)
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   DoublePointValue.INTERMEDIATE,
                                                   quality);
                }
                else
                if (kconv1 == -1)
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   value != 0 ? DoublePointValue.OFF : DoublePointValue.ON,
                                                   quality);
                }
                else
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   value != 0 ? DoublePointValue.ON : DoublePointValue.OFF,
                                                   quality);
                }
                break;

            case TypeID.M_DP_TB_1:     // 31
                if (transient)
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      DoublePointValue.INTERMEDIATE,
                                                      quality,
                                                      time_tag);
                }
                else
                if (kconv1 == -1)
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      value != 0 ? DoublePointValue.OFF : DoublePointValue.ON,
                                                      quality,
                                                      time_tag);
                }
                else
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      value != 0 ? DoublePointValue.ON : DoublePointValue.OFF,
                                                      quality,
                                                      time_tag);
                }
                break;

            case TypeID.M_ST_NA_1:     // 5
                value = value * kconv1 + kconv2;
                if (value > 63)
                {
                    value            = 63;
                    quality.Overflow = true;
                }
                else
                if (value < -64)
                {
                    value            = -64;
                    quality.Overflow = true;
                }
                sc = new StepPositionInformation(addr,
                                                 System.Convert.ToInt16(value),
                                                 transient,
                                                 quality);
                break;

            case TypeID.M_ST_TB_1:     // 32
                value = value * kconv1 + kconv2;
                if (value > 63)
                {
                    value            = 63;
                    quality.Overflow = true;
                }
                else
                if (value < -64)
                {
                    value            = -64;
                    quality.Overflow = true;
                }
                sc = new StepPositionWithCP56Time2a(addr,
                                                    System.Convert.ToInt16(value),
                                                    transient,
                                                    quality,
                                                    time_tag);
                break;

            case TypeID.M_ME_NA_1:     // 9
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalized(addr,
                                                 System.Convert.ToInt16(value),
                                                 quality);
                break;

            case TypeID.M_ME_ND_1:     // 21
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalizedWithoutQuality(addr,
                                                               System.Convert.ToInt16(value));
                break;

            case TypeID.M_ME_TD_1:     // 34
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalizedWithCP56Time2a(addr,
                                                               System.Convert.ToInt16(value),
                                                               quality,
                                                               time_tag);
                break;

            case TypeID.M_ME_NB_1:     // 11
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueScaled(addr,
                                             System.Convert.ToInt16(value),
                                             quality);
                break;

            case TypeID.M_ME_TE_1:     // 35
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueScaledWithCP56Time2a(addr,
                                                           System.Convert.ToInt16(value),
                                                           quality,
                                                           time_tag);
                break;

            case TypeID.M_ME_NC_1:     // 13
                value = value * kconv1 + kconv2;
                sc    = new MeasuredValueShort(addr,
                                               System.Convert.ToSingle(value),
                                               quality);
                break;

            case TypeID.M_ME_TF_1:     // 36
                value = value * kconv1 + kconv2;
                sc    = new MeasuredValueShortWithCP56Time2a(addr,
                                                             System.Convert.ToSingle(value),
                                                             quality,
                                                             time_tag);
                break;

            case TypeID.M_IT_NA_1:     //15
                break;

            case TypeID.M_IT_TB_1:     // 37
                break;

            case TypeID.M_PS_NA_1:     // 20
                Log(" TODO Packed single point information with status change detection! ");
                break;

            case TypeID.M_EP_TD_1:     // 38
                break;

            case TypeID.M_EP_TE_1:     // 39
                break;

            case TypeID.M_EP_TF_1:     // 40
                break;

            case TypeID.M_BO_NA_1:     // 7
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc = new Bitstring32(addr, uival, quality);
                break;

            case TypeID.M_BO_TB_1:     // 33
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc = new Bitstring32WithCP56Time2a(addr, uival, quality, time_tag);
                break;

            case TypeID.C_SC_NA_1:     // 45
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SingleCommand(addr,
                                      bval,
                                      sbo,
                                      cmdqualif);
                break;

            case TypeID.C_DC_NA_1:     // 46
                if (kconv1 == -1)
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.OFF) : System.Convert.ToInt32(DoublePointValue.ON);
                }
                else
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.ON) : System.Convert.ToInt32(DoublePointValue.OFF);
                }
                sc =
                    new DoubleCommand(addr,
                                      ival,
                                      sbo,
                                      cmdqualif);
                break;

            case TypeID.C_RC_NA_1:     // 47
                if (kconv1 == -1)
                {
                    sc =
                        new StepCommand(addr,
                                        value >= 1
                                    ? StepCommandValue.LOWER
                                    : StepCommandValue.HIGHER,
                                        sbo,
                                        cmdqualif);
                }
                else
                {
                    sc =
                        new StepCommand(addr,
                                        value >= 1
                                    ? StepCommandValue.HIGHER
                                    : StepCommandValue.LOWER,
                                        sbo,
                                        cmdqualif);
                }
                break;

            case TypeID.C_SE_NA_1:     // 48
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandNormalized(addr,
                                                  System.Convert.ToInt16(value),
                                                  new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_SE_NB_1:     // 49
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandScaled(addr,
                                              new ScaledValue(System.Convert.ToInt16(value)),
                                              new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_SE_NC_1:     // 50
                value = value * kconv1 + kconv2;
                sc    =
                    new SetpointCommandShort(addr,
                                             System.Convert.ToSingle(value),
                                             new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_BO_NA_1:     // 51
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc =
                    new Bitstring32Command(addr,
                                           uival);
                break;

            case TypeID.C_SC_TA_1:     //  58
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SingleCommandWithCP56Time2a(addr,
                                                    bval,
                                                    sbo,
                                                    cmdqualif,
                                                    time_tag);
                break;

            case TypeID.C_DC_TA_1:     // 59
                if (kconv1 == -1)
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.OFF) : System.Convert.ToInt32(DoublePointValue.ON);
                }
                else
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.ON) : System.Convert.ToInt32(DoublePointValue.OFF);
                }
                sc =
                    new DoubleCommandWithCP56Time2a(addr,
                                                    ival,
                                                    sbo,
                                                    cmdqualif,
                                                    time_tag);
                break;

            case TypeID.C_RC_TA_1:     // 60
                if (kconv1 == -1)
                {
                    sc =
                        new StepCommandWithCP56Time2a(addr,
                                                      value >= 1
                                    ? StepCommandValue.LOWER
                                    : StepCommandValue.HIGHER,
                                                      sbo,
                                                      cmdqualif,
                                                      time_tag);
                }
                else
                {
                    sc =
                        new StepCommandWithCP56Time2a(addr,
                                                      value >= 1
                                    ? StepCommandValue.HIGHER
                                    : StepCommandValue.LOWER,
                                                      sbo,
                                                      cmdqualif,
                                                      time_tag);
                }
                break;

            case TypeID.C_SE_TA_1:     // 61
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandNormalizedWithCP56Time2a(addr,
                                                                System.Convert.ToInt16(value),
                                                                new SetpointCommandQualifier(sbo, 0),
                                                                time_tag);
                break;

            case TypeID.C_SE_TB_1:     // 62
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandScaledWithCP56Time2a(addr,
                                                            new ScaledValue(System.Convert.ToInt16(value)),
                                                            new SetpointCommandQualifier(sbo, 0),
                                                            time_tag);
                break;

            case TypeID.C_SE_TC_1:     // 63
                value = value * kconv1 + kconv2;
                sc    =
                    new SetpointCommandShortWithCP56Time2a(addr,
                                                           System.Convert.ToSingle(value),
                                                           new SetpointCommandQualifier(sbo, 0),
                                                           time_tag);
                break;

            case TypeID.C_BO_TA_1:     // 64
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc =
                    new Bitstring32CommandWithCP56Time2a(addr,
                                                         uival,
                                                         time_tag);
                break;

            case TypeID.C_IC_NA_1:     // 100
                sc =
                    new InterrogationCommand(0,
                                             System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_CI_NA_1:     // 101
                sc =
                    new CounterInterrogationCommand(0,
                                                    System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_RD_NA_1:     // 102
                sc = new ReadCommand(addr);
                break;

            case TypeID.C_CS_NA_1:     // 103
                sc = new ClockSynchronizationCommand(0, time_tag);
                break;

            case TypeID.C_RP_NA_1:     // 105
                sc =
                    new ResetProcessCommand(addr,
                                            System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_TS_TA_1:     // 107
                sc =
                    new TestCommandWithCP56Time2a(System
                                                  .Convert
                                                  .ToUInt16(value),
                                                  time_tag);
                break;

            case TypeID.P_ME_NA_1:     // 110
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new ParameterNormalizedValue(System
                                                 .Convert
                                                 .ToInt32(addr),
                                                 System.Convert.ToInt32(value),
                                                 System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_ME_NB_1:     // 111
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new ParameterScaledValue(addr,
                                             new ScaledValue(System.Convert.ToInt16(value)),
                                             System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_ME_NC_1:     // 112
                value = value * kconv1 + kconv2;
                sc    =
                    new ParameterFloatValue(addr,
                                            System.Convert.ToSingle(value),
                                            System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_AC_NA_1:     // 113
                sc =
                    new ParameterActivation(System.Convert.ToInt32(addr),
                                            System.Convert.ToByte(cmdqualif));
                break;

            default:
                break;
            }
            return(sc);
        }
Ejemplo n.º 2
0
        private void HandleASDU(ASDU asdu)
        {
            DebugLog("Handle received ASDU");

            bool messageHandled = false;

            switch (asdu.TypeId)
            {
            case TypeID.C_IC_NA_1:             /* 100 - interrogation command */

                DebugLog("Rcvd interrogation command C_IC_NA_1");

                if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION))
                {
                    if (server.interrogationHandler != null)
                    {
                        InterrogationCommand irc = (InterrogationCommand)asdu.GetElement(0);

                        if (server.interrogationHandler(server.InterrogationHandlerParameter, this, asdu, irc.QOI))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }

                break;

            case TypeID.C_CI_NA_1:             /* 101 - counter interrogation command */

                DebugLog("Rcvd counter interrogation command C_CI_NA_1");

                if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.DEACTIVATION))
                {
                    if (server.counterInterrogationHandler != null)
                    {
                        CounterInterrogationCommand cic = (CounterInterrogationCommand)asdu.GetElement(0);

                        if (server.counterInterrogationHandler(server.counterInterrogationHandlerParameter, this, asdu, cic.QCC))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }

                break;

            case TypeID.C_RD_NA_1:             /* 102 - read command */

                DebugLog("Rcvd read command C_RD_NA_1");

                if (asdu.Cot == CauseOfTransmission.REQUEST)
                {
                    DebugLog("Read request for object: " + asdu.Ca);

                    if (server.readHandler != null)
                    {
                        ReadCommand rc = (ReadCommand)asdu.GetElement(0);

                        if (server.readHandler(server.readHandlerParameter, this, asdu, rc.ObjectAddress))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }

                break;

            case TypeID.C_CS_NA_1:             /* 103 - Clock synchronization command */

                DebugLog("Rcvd clock sync command C_CS_NA_1");

                if (asdu.Cot == CauseOfTransmission.ACTIVATION)
                {
                    if (server.clockSynchronizationHandler != null)
                    {
                        ClockSynchronizationCommand csc = (ClockSynchronizationCommand)asdu.GetElement(0);

                        if (server.clockSynchronizationHandler(server.clockSynchronizationHandlerParameter,
                                                               this, asdu, csc.NewTime))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }

                break;

            case TypeID.C_TS_NA_1:             /* 104 - test command */

                DebugLog("Rcvd test command C_TS_NA_1");

                if (asdu.Cot != CauseOfTransmission.ACTIVATION)
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.ACTIVATION_CON;
                }

                this.SendASDUInternal(asdu);

                messageHandled = true;

                break;

            case TypeID.C_RP_NA_1:             /* 105 - Reset process command */

                DebugLog("Rcvd reset process command C_RP_NA_1");

                if (asdu.Cot == CauseOfTransmission.ACTIVATION)
                {
                    if (server.resetProcessHandler != null)
                    {
                        ResetProcessCommand rpc = (ResetProcessCommand)asdu.GetElement(0);

                        if (server.resetProcessHandler(server.resetProcessHandlerParameter,
                                                       this, asdu, rpc.QRP))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }


                break;

            case TypeID.C_CD_NA_1:             /* 106 - Delay acquisition command */

                DebugLog("Rcvd delay acquisition command C_CD_NA_1");

                if ((asdu.Cot == CauseOfTransmission.ACTIVATION) || (asdu.Cot == CauseOfTransmission.SPONTANEOUS))
                {
                    if (server.delayAcquisitionHandler != null)
                    {
                        DelayAcquisitionCommand dac = (DelayAcquisitionCommand)asdu.GetElement(0);

                        if (server.delayAcquisitionHandler(server.delayAcquisitionHandlerParameter,
                                                           this, asdu, dac.Delay))
                        {
                            messageHandled = true;
                        }
                    }
                }
                else
                {
                    asdu.Cot = CauseOfTransmission.UNKNOWN_CAUSE_OF_TRANSMISSION;
                    this.SendASDUInternal(asdu);
                }

                break;
            }

            if (messageHandled == false)
            {
                messageHandled = fileServer.HandleFileAsdu(asdu);
            }

            if ((messageHandled == false) && (server.asduHandler != null))
            {
                if (server.asduHandler(server.asduHandlerParameter, this, asdu))
                {
                    messageHandled = true;
                }
            }

            if (messageHandled == false)
            {
                asdu.Cot = CauseOfTransmission.UNKNOWN_TYPE_ID;
                this.SendASDUInternal(asdu);
            }
        }