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
        AsduReceivedHandler(object parameter, IMasterConnection connection, ASDU asdu)
        {
            var srv        = IEC10Xconns[(int)parameter];
            var conNameStr = srv.name + " - ";

            Log(conNameStr + asdu.ToString(), LogLevelDetailed);

            try
            {
                if (IsMongoLive)
                {
                    var DB             = Client.GetDatabase(JSConfig.mongoDatabaseName);
                    var collection_rtd =
                        DB
                        .GetCollection
                        <rtData>(RealtimeDataCollectionName);
                    Double   val        = 0;
                    Int32    objaddr    = 0;
                    Int32    dur        = 0;
                    Boolean  isselect   = false;
                    Boolean  cmdhastime = false;
                    DateTime cmdtime    = new DateTime();

                    Double  dstkconv1 = 1;
                    Double  dstkconv2 = 0;
                    Boolean dstsbo    = false;
                    Int32   dstdur    = 0;

                    Double  srcval      = 0;
                    Int32   srcobjaddr  = 0;
                    Int32   srcconn     = 0;
                    Boolean srcsbo      = false;
                    Int32   srcdur      = 0;
                    Int32   srcasdu     = 0;
                    Int32   srcca       = 0;
                    Int32   srcpointkey = 0;
                    String  srctag      = "";
                    Double  srckconv1   = 1;
                    Double  srckconv2   = 0;

                    switch (asdu.TypeId)
                    {
                    case TypeID.C_SC_NA_1:     // 45
                    {
                        var cmd = (SingleCommand)asdu.GetElement(0);
                        isselect = cmd.Select;
                        dur      = cmd.QU;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.State);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SC_TA_1:     // 58
                    {
                        var cmd = (SingleCommandWithCP56Time2a)asdu.GetElement(0);
                        isselect   = cmd.Select;
                        dur        = cmd.QU;
                        objaddr    = cmd.ObjectAddress;
                        val        = System.Convert.ToDouble(cmd.State);
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_DC_NA_1:     // 46
                    {
                        var cmd = (DoubleCommand)asdu.GetElement(0);
                        isselect = cmd.Select;
                        dur      = cmd.QU;
                        objaddr  = cmd.ObjectAddress;
                        if (cmd.State != DoubleCommand.ON && cmd.State != DoubleCommand.OFF)
                        {
                            connection.SendACT_CON(asdu, true);         // activation confirm negative
                            Log(conNameStr + "  Invalid double state command " + cmd.State, LogLevelBasic);
                            LastPointKeySelectedOk = 0;
                            return(true);
                        }
                        val = cmd.State == DoubleCommand.ON ? 1 : 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_DC_TA_1:     // 59
                    {
                        var cmd = (DoubleCommandWithCP56Time2a)asdu.GetElement(0);
                        isselect = cmd.Select;
                        dur      = cmd.QU;
                        objaddr  = cmd.ObjectAddress;
                        if (cmd.State != DoubleCommand.ON && cmd.State != DoubleCommand.OFF)
                        {
                            connection.SendACT_CON(asdu, true);         // activation confirm negative
                            Log(conNameStr + "  Invalid double state command " + cmd.State, LogLevelBasic);
                            LastPointKeySelectedOk = 0;
                            return(true);
                        }
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = cmd.State == DoubleCommand.ON ? 1 : 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_RC_NA_1:     // 47
                    {
                        var cmd = (StepCommand)asdu.GetElement(0);
                        isselect = cmd.Select;
                        dur      = cmd.QU;
                        objaddr  = cmd.ObjectAddress;
                        if (cmd.State != StepCommandValue.HIGHER && cmd.State != StepCommandValue.LOWER)
                        {
                            connection.SendACT_CON(asdu, true);         // activation confirm negative
                            Log(conNameStr + "  Invalid step state command " + cmd.State, LogLevelBasic);
                            LastPointKeySelectedOk = 0;
                            return(true);
                        }
                        val = cmd.State == StepCommandValue.HIGHER ? 1 : 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_RC_TA_1:     // 60
                    {
                        var cmd = (StepCommandWithCP56Time2a)asdu.GetElement(0);
                        isselect = cmd.Select;
                        dur      = cmd.QU;
                        objaddr  = cmd.ObjectAddress;
                        if (cmd.State != StepCommandValue.HIGHER && cmd.State != StepCommandValue.LOWER)
                        {
                            connection.SendACT_CON(asdu, true);         // activation confirm negative
                            Log(conNameStr + "  Invalid step state command " + cmd.State, LogLevelBasic);
                            LastPointKeySelectedOk = 0;
                            return(true);
                        }
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = cmd.State == StepCommandValue.HIGHER ? 1 : 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_NA_1:     // 48
                    {
                        var cmd = (SetpointCommandNormalized)asdu.GetElement(0);
                        isselect = cmd.QOS.Select;
                        dur      = cmd.QOS.QL;
                        objaddr  = cmd.ObjectAddress;
                        val      = cmd.NormalizedValue;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_TA_1:     // 61
                    {
                        var cmd = (SetpointCommandNormalizedWithCP56Time2a)asdu.GetElement(0);
                        isselect   = cmd.QOS.Select;
                        dur        = cmd.QOS.QL;
                        objaddr    = cmd.ObjectAddress;
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = cmd.NormalizedValue;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_NB_1:     // 49
                    {
                        var cmd = (SetpointCommandScaled)asdu.GetElement(0);
                        isselect = cmd.QOS.Select;
                        dur      = cmd.QOS.QL;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.ScaledValue);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_TB_1:     // 62
                    {
                        var cmd = (SetpointCommandScaledWithCP56Time2a)asdu.GetElement(0);
                        isselect   = cmd.QOS.Select;
                        dur        = cmd.QOS.QL;
                        objaddr    = cmd.ObjectAddress;
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = System.Convert.ToDouble(cmd.ScaledValue);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_NC_1:     // 50
                    {
                        var cmd = (SetpointCommandShort)asdu.GetElement(0);
                        isselect = cmd.QOS.Select;
                        dur      = cmd.QOS.QL;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.Value);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_SE_TC_1:     // 63
                    {
                        var cmd = (SetpointCommandShortWithCP56Time2a)asdu.GetElement(0);
                        isselect   = cmd.QOS.Select;
                        dur        = cmd.QOS.QL;
                        objaddr    = cmd.ObjectAddress;
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = System.Convert.ToDouble(cmd.Value);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_BO_NA_1:     // 51
                    {
                        var cmd = (Bitstring32Command)asdu.GetElement(0);
                        isselect = false;
                        dur      = 0;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.Value);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_BO_TA_1:     // 64
                    {
                        var cmd = (Bitstring32CommandWithCP56Time2a)asdu.GetElement(0);
                        isselect   = false;
                        dur        = 0;
                        objaddr    = cmd.ObjectAddress;
                        cmdtime    = cmd.Timestamp.GetDateTime();
                        cmdhastime = true;
                        val        = System.Convert.ToDouble(cmd.Value);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.P_ME_NA_1:     // 110
                    {
                        var cmd = (ParameterNormalizedValue)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QPM;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.NormalizedValue);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.P_ME_NB_1:     // 111
                    {
                        var cmd = (ParameterScaledValue)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QPM;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.ScaledValue);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.P_ME_NC_1:     // 112
                    {
                        var cmd = (ParameterFloatValue)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QPM;
                        objaddr  = cmd.ObjectAddress;
                        val      = System.Convert.ToDouble(cmd.Value);
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.P_AC_NA_1:     // 113
                    {
                        var cmd = (ParameterActivation)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QPA;
                        objaddr  = cmd.ObjectAddress;
                        val      = 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_RP_NA_1:     // 105 reset process
                    {
                        var cmd = (ResetProcessCommand)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QRP;
                        objaddr  = cmd.ObjectAddress;
                        val      = 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    // case TypeID.C_IC_NA_1: // 100 already managed by interrogation handler function
                    case TypeID.C_TS_NA_1:     // 104 test command
                    {
                        Log(conNameStr + "  Test command C_TS_NA_1", LogLevelDetailed);
                        connection.SendACT_CON(asdu, false);         // activation confirm positive
                    }
                        return(true);

                    case TypeID.C_TS_TA_1:     // 107 test command
                    {
                        Log(conNameStr + "  Test command C_TS_TA_1", LogLevelDetailed);
                        connection.SendACT_CON(asdu, false);         // activation confirm positive
                    }
                        return(true);

                    case TypeID.C_CI_NA_1:     // 101
                    {
                        var cmd = (CounterInterrogationCommand)asdu.GetElement(0);
                        isselect = false;
                        dur      = cmd.QCC;
                        objaddr  = cmd.ObjectAddress;        // should be zero
                        val      = 0;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_RD_NA_1:     // 102
                    {
                        var cmd = (ReadCommand)asdu.GetElement(0);
                        objaddr = cmd.ObjectAddress;
                        Log(conNameStr + "  " + cmd.ToString() + " Obj Address " + objaddr, LogLevelBasic);
                    }
                    break;

                    case TypeID.C_CS_NA_1:     // 103 clock sync
                    {
                        ClockSynchronizationCommand qsc =
                            (ClockSynchronizationCommand)asdu.GetElement(0);
                        connection.SendACT_CON(asdu, false);         // activation confirm positive
                        Log(conNameStr + "  Received clock sync command with time " +
                            qsc.NewTime.ToString(), LogLevelBasic);
                        LastPointKeySelectedOk = 0;
                        return(true);
                    }

                    default:
                        connection.SendACT_CON(asdu, true);
                        Log(conNameStr + "  Not implemented type of ASDU received: " + asdu.TypeId, LogLevelBasic);
                        LastPointKeySelectedOk = 0;
                        return(true);
                    }

                    var filter1 = Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationConnectionNumber", srv.protocolConnectionNumber);

                    var filter2 = Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationCommonAddress", asdu.Ca);

                    var filter3 = Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationObjectAddress", objaddr);

                    var filter4 = Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationASDU", asdu.TypeId);

                    var filter = Builders <rtData>
                                 .Filter
                                 .And(filter1, filter2, filter3, filter4);

                    if (asdu.TypeId == TypeID.C_RD_NA_1)
                    { // READ COMMAND, look for object by object address of any type to send it
                        filter = Builders <rtData>
                                 .Filter
                                 .And(filter1, filter2, filter3);
                    }

                    var list =
                        collection_rtd.Find(filter).ToList();
                    if (list.Count > 0)
                    {
                        Log(conNameStr + "  Command found.", LogLevelBasic);
                        foreach (var dst in list[0].protocolDestinations)
                        {
                            if (dst.protocolDestinationConnectionNumber == srv.protocolConnectionNumber)
                            {
                                dstkconv1 = dst.protocolDestinationKConv1.ToDouble();
                                dstkconv2 = dst.protocolDestinationKConv2.ToDouble();
                                dstsbo    = dst.protocolDestinationCommandUseSBO.ToBoolean();
                                dstdur    = dst.protocolDestinationCommandDuration.ToInt32();

                                if (asdu.TypeId == TypeID.C_RD_NA_1)
                                {                                        // READ REQUEST
                                    connection.SendACT_CON(asdu, false); // activation confirm positive
                                    ApplicationLayerParameters cp =
                                        srv.server.GetApplicationLayerParameters();
                                    var newAsdu = new ASDU(cp,
                                                           CauseOfTransmission.REQUEST,
                                                           false,
                                                           false,
                                                           System.Convert.ToByte(dst.protocolDestinationCommonAddress),
                                                           dst.protocolDestinationCommonAddress.ToInt32(),
                                                           true);
                                    var quality = new QualityDescriptor();
                                    quality.Invalid = list[0].invalid.ToBoolean() ||
                                                      list[0].overflow.ToBoolean() ||
                                                      list[0].transient.ToBoolean();
                                    quality.Substituted = list[0].substituted.ToBoolean();
                                    quality.Blocked     = false;
                                    quality.NonTopical  = false;
                                    InformationObject io = BuildInfoObj(
                                        System.Convert.ToInt32(dst.protocolDestinationASDU),
                                        System.Convert.ToInt32(dst.protocolDestinationObjectAddress),
                                        list[0].value.ToDouble(),
                                        false,
                                        0,
                                        quality
                                        );
                                    if (io != null)
                                    {
                                        newAsdu.AddInformationObject(io);
                                        srv.server.EnqueueASDU(newAsdu);
                                    }
                                    return(true);
                                }

                                if (isselect && !dstsbo)
                                {                                       // tried a select when there is no select
                                    connection.SendACT_CON(asdu, true); // activation confirm negative
                                    Log(conNameStr + "  Select tried but not expected!", LogLevelBasic);
                                    LastPointKeySelectedOk = 0;
                                    return(true);
                                }

                                if (dur != dstdur)
                                {                                       // duration spec different than expected, reject commmand
                                    connection.SendACT_CON(asdu, true); // activation confirm negative
                                    Log(conNameStr + "  QU/QL command qualifier not expected: " + dur + ", " + dstdur + " wanted ", LogLevelBasic);
                                    LastPointKeySelectedOk = 0;
                                    return(true);
                                }

                                srcconn     = list[0].protocolSourceConnectionNumber.ToInt32();
                                srcdur      = list[0].protocolSourceCommandDuration.ToInt32();
                                srcobjaddr  = list[0].protocolSourceObjectAddress.ToInt32();
                                srcasdu     = list[0].protocolSourceASDU.ToInt32();
                                srcca       = list[0].protocolSourceCommonAddress.ToInt32();
                                srckconv1   = list[0].kconv1.ToDouble();
                                srckconv2   = list[0].kconv2.ToDouble();
                                srcpointkey = list[0]._id.ToInt32();
                                srctag      = list[0].tag.ToString();
                                break;
                            }
                        }
                    }
                    else
                    {
                        connection.SendACT_CON(asdu, true); // activation confirm negative
                        if (asdu.TypeId == TypeID.C_RD_NA_1)
                        {
                            Log(conNameStr + "  Request to read object not found, address: " + objaddr, LogLevelBasic);
                        }
                        else
                        {
                            Log(conNameStr + "  Command not found!", LogLevelBasic);
                        }
                        LastPointKeySelectedOk = 0;
                        return(true);
                    }

                    if (srcasdu == 0)
                    {
                        Log(conNameStr + "  Command rejected!", LogLevelBasic);
                        connection.SendACT_CON(asdu, true); // activation confirm negative
                        LastPointKeySelectedOk = 0;
                        return(true);
                    }

                    if (cmdhastime) // check command time
                    {
                        if (DateTime.Now.Subtract(cmdtime).TotalSeconds > timeToExpireCommandsWithTime)
                        { // expired
                            Log(conNameStr + "  Command with time expired after " +
                                timeToExpireCommandsWithTime + "s, diff: " +
                                (DateTime.Now.Subtract(cmdtime).TotalSeconds - timeToExpireCommandsWithTime) + "s",
                                LogLevelBasic);
                            connection.SendACT_CON(asdu, true); // activation confirm negative
                            LastPointKeySelectedOk = 0;
                            return(true);
                        }
                    }

                    connection.SendACT_CON(asdu, false); // activation confirm positive

                    if (isselect)
                    {
                        LastPointKeySelectedOk = srcpointkey; // flag selected point
                        Log(conNameStr + "  Select!", LogLevelBasic);
                        return(true);                         // do not forward a select
                    }

                    if (!isselect && dstsbo && LastPointKeySelectedOk != srcpointkey)
                    {  // tried execute without select first when there is select expected
                        connection.SendACT_CON(asdu, true);
                        Log(conNameStr + "  Tried execute without select first!", LogLevelBasic);
                        LastPointKeySelectedOk = 0;
                        return(true);
                    }
                    LastPointKeySelectedOk = 0;
                    Log(conNameStr + "  Execute (forward to queue)!", LogLevelBasic);

                    switch (asdu.TypeId)
                    {
                    case TypeID.C_SC_NA_1:     // 45
                    case TypeID.C_SC_TA_1:     // 58
                    case TypeID.C_DC_NA_1:     // 46
                    case TypeID.C_DC_TA_1:     // 59
                    case TypeID.C_RC_NA_1:     // 47
                    case TypeID.C_RC_TA_1:     // 60
                        if (dstkconv1 == -1)   // invert digital for kconv1 -1
                        {
                            val = val == 0 ? 1 : 0;
                        }
                        break;

                    case TypeID.C_SE_NA_1:     // 48
                    case TypeID.C_SE_TA_1:     // 61
                    case TypeID.C_SE_NB_1:     // 49
                    case TypeID.C_SE_TB_1:     // 62
                    case TypeID.C_SE_NC_1:     // 50
                    case TypeID.C_SE_TC_1:     // 63
                    case TypeID.P_ME_NA_1:     // 110
                    case TypeID.P_ME_NB_1:     // 111
                    case TypeID.P_ME_NC_1:     // 112
                    case TypeID.P_AC_NA_1:     // 113
                    case TypeID.C_RP_NA_1:     // 105
                        val = val * dstkconv1 + dstkconv2;
                        break;

                    case TypeID.C_BO_NA_1:     // 51
                    case TypeID.C_BO_TA_1:     // 64
                        if (dstkconv1 == -1)   // invert digital bits for kconv1 -1
                        {
                            val = System.Convert.ToInt32(val);
                        }
                        break;

                    default:
                        break;
                    }

                    switch ((TypeID)srcasdu)
                    {
                    case TypeID.C_SC_NA_1:     // 45
                    case TypeID.C_SC_TA_1:     // 58
                    case TypeID.C_DC_NA_1:     // 46
                    case TypeID.C_DC_TA_1:     // 59
                    case TypeID.C_RC_NA_1:     // 47
                    case TypeID.C_RC_TA_1:     // 60
                        if (srckconv1 == -1)   // invert digital for kconv1 -1
                        {
                            srcval = val == 0 ? 1 : 0;
                        }
                        else
                        {
                            srcval = val;
                        }
                        break;

                    case TypeID.C_SE_NA_1:     // 48
                    case TypeID.C_SE_TA_1:     // 61
                    case TypeID.C_SE_NB_1:     // 49
                    case TypeID.C_SE_TB_1:     // 62
                    case TypeID.C_SE_NC_1:     // 50
                    case TypeID.C_SE_TC_1:     // 63
                    case TypeID.P_ME_NA_1:     // 110
                    case TypeID.P_ME_NB_1:     // 111
                    case TypeID.P_ME_NC_1:     // 112
                    case TypeID.P_AC_NA_1:     // 113
                    case TypeID.C_RP_NA_1:     // 105
                        srcval = val * srckconv1 + srckconv2;
                        break;

                    case TypeID.C_BO_NA_1:     // 51
                    case TypeID.C_BO_TA_1:     // 64
                        if (srckconv1 == -1)   // invert digital bits for kconv1 -1
                        {
                            srcval = ~System.Convert.ToInt32(val);
                        }
                        else
                        {
                            srcval = System.Convert.ToInt32(val);
                        }
                        break;

                    default:
                        break;
                    }

                    // not sure how to detect the client connection as can be more than one
                    String orgip = "";
                    foreach (var conn in srv.clientConnections)
                    {
                        orgip = orgip + srv.clientConnections[0].RemoteEndpoint.ToString() + " ";
                    }

                    var collection_cmd =
                        DB
                        .GetCollection
                        <rtCommandNoAck>(CommandsQueueCollectionName);
                    var rtcmd = new rtCommandNoAck
                    {
                        protocolSourceConnectionNumber = srcconn,
                        protocolSourceCommonAddress    = srcca,
                        protocolSourceObjectAddress    = srcobjaddr,
                        protocolSourceASDU             = srcasdu,
                        protocolSourceCommandDuration  = srcdur,
                        protocolSourceCommandUseSBO    = srcsbo,
                        pointKey            = srcpointkey,
                        tag                 = srctag,
                        value               = srcval,
                        valueString         = srcval.ToString(),
                        originatorUserName  = "******" + srv.name,
                        originatorIpAddress = orgip,
                        timeTag             = DateTime.Now
                    };
                    collection_cmd.InsertOne(rtcmd);
                }
            }
            catch (Exception e)
            {
                Log("  Exception Mongo");
                Log(e);
                Log(e
                    .ToString()
                    .Substring(0,
                               e.ToString().IndexOf(Environment.NewLine)));
            }

            return(true);
        }
Ejemplo n.º 3
0
        InterrogationHandler(
            object parameter,
            IMasterConnection connection,
            ASDU asdu,
            byte qoi
            )
        {
            var srv        = IEC10Xconns[(int)parameter];
            var conNameStr = srv.name + " - ";

            Log(conNameStr + "[" + qoi + "] Group interrogation BEGIN", LogLevelBasic);

            try
            {
                var Client     = new MongoClient(JSConfig.mongoConnectionString);
                var DB         = Client.GetDatabase(JSConfig.mongoDatabaseName);
                var collection =
                    DB.GetCollection <rtData>(RealtimeDataCollectionName);

                ApplicationLayerParameters cp =
                    connection.GetApplicationLayerParameters();

                // query mongodb for all data to distribute in this connection

                // for group 20 (general interogation) request filter by all in destination connection but those marked with group -1
                // for other groups requests filter by by those marked with group this same group
                var filter_conn = Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationConnectionNumber", srv.protocolConnectionNumber);

                var filter_cmd = Builders <rtData> .Filter.Ne("origin", "command");

                var filter_gen_int = Builders <rtData> .Filter.And(filter_conn, filter_cmd, Builders <rtData> .Filter.Ne("protocolDestinations.protocolDestinationGroup", -1));

                var filter_oth_grp = Builders <rtData> .Filter.And(filter_conn, filter_cmd,
                                                                   Builders <rtData> .Filter.Or(
                                                                       Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationGroup", qoi), // accept 20-36 or 0-16
                                                                       Builders <rtData> .Filter.Eq("protocolDestinations.protocolDestinationGroup", qoi - 20)
                                                                       )
                                                                   );

                var sort = Builders <rtData> .Sort.Descending("$natural");

                var options = new FindOptions <rtData, rtData>();
                options.Sort = sort;
                var filter = filter_gen_int;
                if (qoi != 20)
                {
                    filter = filter_oth_grp;
                }
                var list =
                    collection.Find(filter).ToList();
                int CompareASDUInDest(rtData x, rtData y)
                {
                    var asdu1 = x.protocolDestinations[0].protocolDestinationASDU;

                    foreach (var dst in x.protocolDestinations)
                    {
                        if (dst.protocolDestinationConnectionNumber == srv.protocolConnectionNumber)
                        {
                            asdu1 = dst.protocolDestinationASDU;
                        }
                    }
                    var asdu2 = y.protocolDestinations[0].protocolDestinationASDU;

                    foreach (var dst in y.protocolDestinations)
                    {
                        if (dst.protocolDestinationConnectionNumber == srv.protocolConnectionNumber)
                        {
                            asdu2 = dst.protocolDestinationASDU;
                        }
                    }
                    if (asdu1 == asdu2)
                    {
                        return(0);
                    }
                    if (asdu1 > asdu2)
                    {
                        return(1);
                    }
                    return(-1);
                }

                list.Sort(CompareASDUInDest); // order by ASDU

                Log(conNameStr + "[" + qoi + "] Group request, " + list.Count() + " objects to send.", LogLevelBasic);

                connection.SendACT_CON(asdu, false); // confirm positive
                var  lastasdu   = -1;
                var  cntasduobj = 0;
                ASDU nwasdu     = null;
                foreach (rtData entry in list)
                {
                    Log(conNameStr + "[" + qoi + "] " + entry.tag.ToString() + " " + entry.value + " Key " + entry._id, LogLevelDetailed);
                    foreach (var dst in entry.protocolDestinations)
                    {
                        var q = new QualityDescriptor();
                        q.Invalid     = entry.invalid.ToBoolean();
                        q.Substituted = entry.substituted.ToBoolean();
                        q.NonTopical  = false;
                        q.Blocked     = false;
                        q.Overflow    = entry.overflow.ToBoolean();
                        if (dst.protocolDestinationConnectionNumber == srv.protocolConnectionNumber)
                        {
                            if ((lastasdu != dst.protocolDestinationASDU && cntasduobj > 0) || cntasduobj >= 30)
                            {
                                if (nwasdu != null)
                                {
                                    Log(conNameStr + "[" + qoi + "] Send ASDU TI:" + System.Convert.ToByte(nwasdu.TypeId) + "." + nwasdu.TypeId + " CA:" + nwasdu.Ca +
                                        " with " + nwasdu.NumberOfElements + " objects.");
                                    connection.SendASDU(nwasdu);
                                    nwasdu     = null;
                                    cntasduobj = 0;
                                    lastasdu   = -1;
                                }
                            }
                            switch (dst.protocolDestinationASDU.ToInt32())
                            {
                            case 1:
                            case 30:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var bval = entry.value != 0 ? true : false;
                                    if (dst.protocolDestinationKConv1 == -1)
                                    {
                                        bval = !bval;
                                    }
                                    nwasdu
                                    .AddInformationObject(new SinglePointInformation(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                     bval,
                                                                                     q));
                                    cntasduobj++;
                                }
                                break;

                            case 3:
                            case 31:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var dpval = entry.value != 0 ? DoublePointValue.ON : DoublePointValue.OFF;
                                    if (dst.protocolDestinationKConv1 == -1)
                                    {
                                        dpval = entry.value != 0 ? DoublePointValue.OFF : DoublePointValue.ON;
                                    }
                                    if (entry.transient.ToBoolean())
                                    {
                                        dpval = DoublePointValue.INTERMEDIATE;
                                    }
                                    nwasdu
                                    .AddInformationObject(new DoublePointInformation(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                     dpval,
                                                                                     q));
                                    cntasduobj++;
                                }
                                break;

                            case 5:
                            case 32:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var val = dst.protocolDestinationKConv1.ToDouble() * System.Convert.ToDouble(entry.value) + dst.protocolDestinationKConv2.ToDouble();
                                    if (val > 63)
                                    {
                                        val        = 63;
                                        q.Overflow = true;
                                    }
                                    else
                                    if (val < -64)
                                    {
                                        val        = -64;
                                        q.Overflow = true;
                                    }
                                    nwasdu
                                    .AddInformationObject(new StepPositionInformation(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                      System.Convert.ToInt16(val),
                                                                                      entry.transient.ToBoolean(),
                                                                                      q));
                                    cntasduobj++;
                                }
                                break;

                            case 9:
                            case 34:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var val = dst.protocolDestinationKConv1.ToDouble() * System.Convert.ToDouble(entry.value) + dst.protocolDestinationKConv2.ToDouble();
                                    if (val > 32767)
                                    {
                                        val        = 32767;
                                        q.Overflow = true;
                                    }
                                    else
                                    if (val < -32768)
                                    {
                                        val        = -32768;
                                        q.Overflow = true;
                                    }
                                    nwasdu
                                    .AddInformationObject(new MeasuredValueNormalized(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                      System.Convert.ToInt16(val),
                                                                                      new QualityDescriptor()));
                                    cntasduobj++;
                                }
                                break;

                            case 11:
                            case 35:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var val = dst.protocolDestinationKConv1.ToDouble() * System.Convert.ToDouble(entry.value) + dst.protocolDestinationKConv2.ToDouble();
                                    if (val > 32767)
                                    {
                                        val        = 32767;
                                        q.Overflow = true;
                                    }
                                    else
                                    if (val < -32768)
                                    {
                                        val        = -32768;
                                        q.Overflow = true;
                                    }
                                    nwasdu
                                    .AddInformationObject(new MeasuredValueScaled(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                  System.Convert.ToInt16(val),
                                                                                  new QualityDescriptor()));
                                    cntasduobj++;
                                }
                                break;

                            case 13:
                            case 36:
                                if (cntasduobj == 0)
                                {
                                    nwasdu =
                                        new ASDU(cp,
                                                 (CauseOfTransmission)qoi,
                                                 false,
                                                 false,
                                                 System.Convert.ToByte(cp.OA),
                                                 System.Convert.ToInt32(dst.protocolDestinationCommonAddress.ToDouble()),
                                                 false);
                                }
                                if (nwasdu != null)
                                {
                                    var val = dst.protocolDestinationKConv1.ToDouble() * System.Convert.ToDouble(entry.value) + dst.protocolDestinationKConv2.ToDouble();
                                    nwasdu
                                    .AddInformationObject(new MeasuredValueShort(System.Convert.ToInt32(dst.protocolDestinationObjectAddress.ToDouble()),
                                                                                 System.Convert.ToSingle(val),
                                                                                 new QualityDescriptor()));
                                    cntasduobj++;
                                }
                                break;

                            default:
                                break;
                            }
                            lastasdu = dst.protocolDestinationASDU.ToInt32();
                            break;
                        }
                    }
                }
                if (nwasdu != null)
                {
                    Log(conNameStr + "[" + qoi + "] Send ASDU TI:" + System.Convert.ToByte(nwasdu.TypeId) + "." + nwasdu.TypeId + " CA:" + nwasdu.Ca +
                        " with " + nwasdu.NumberOfElements + " objects.", LogLevelBasic);
                    connection.SendASDU(nwasdu);
                    nwasdu = null;
                }
                connection.SendACT_TERM(asdu);
                Log(conNameStr + "[" + qoi + "] Group interrogation END", LogLevelBasic);
            }
            catch (Exception e)
            {
                if (e.Message == "Connection not active")
                {
                    return(true);
                }

                Log("Exception on Interrogation");
                Log(e);
                Log(e
                    .ToString()
                    .Substring(0, e.ToString().IndexOf(Environment.NewLine)));
                System.Threading.Thread.Sleep(3000);
                connection.SendACT_CON(asdu, true); // negative confirm
                return(true);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public void TestASDUAddTooMuchInformationObjects()
        {
            ConnectionParameters cp = new ConnectionParameters();

            ASDU asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, false);

            int addedCounter = 0;
            int ioa          = 100;

            while (asdu.AddInformationObject(new SinglePointInformation(ioa, false, new QualityDescriptor())))
            {
                ioa++;
                addedCounter++;
            }

            Assert.AreEqual(60, addedCounter);

            asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, true);

            addedCounter = 0;
            ioa          = 100;

            while (asdu.AddInformationObject(new SinglePointInformation(ioa, false, new QualityDescriptor())))
            {
                ioa++;
                addedCounter++;
            }

            Assert.AreEqual(240, addedCounter);

            asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, false);

            addedCounter = 0;
            ioa          = 100;

            while (asdu.AddInformationObject(new SinglePointWithCP24Time2a(ioa, false, new QualityDescriptor(), new CP24Time2a())))
            {
                ioa++;
                addedCounter++;
            }

            Assert.AreEqual(34, addedCounter);

            asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, true);

            addedCounter = 0;
            ioa          = 100;

            while (asdu.AddInformationObject(new SinglePointWithCP56Time2a(ioa, false, new QualityDescriptor(), new CP56Time2a())))
            {
                ioa++;
                addedCounter++;
            }

            Assert.AreEqual(30, addedCounter);

            asdu = new ASDU(cp, CauseOfTransmission.PERIODIC, false, false, 0, 1, false);

            addedCounter = 0;
            ioa          = 100;

            while (asdu.AddInformationObject(new MeasuredValueShortWithCP56Time2a(ioa, 0.0f, QualityDescriptor.VALID(), new CP56Time2a())))
            {
                ioa++;
                addedCounter++;
            }

            Assert.AreEqual(16, addedCounter);
        }
Ejemplo n.º 5
0
        // This process watches (via change stream) for point updates
        // Forward data to its connections
        static async void ProcessMongoCS(JSONSCADAConfig jsConfig)
        {
            do
            {
                try
                {
                    var Client     = ConnectMongoClient(jsConfig);
                    var DB         = Client.GetDatabase(jsConfig.mongoDatabaseName);
                    var collection =
                        DB
                        .GetCollection
                        <rtData>(RealtimeDataCollectionName);

                    bool isMongoLive =
                        DB
                        .RunCommandAsync((Command <BsonDocument>) "{ping:1}")
                        .Wait(1000);
                    if (!isMongoLive)
                    {
                        throw new Exception("Error on connection " + jsConfig.mongoConnectionString);
                    }

                    Log("MongoDB CMD CS - Start listening for realtime data updates via changestream...");
                    // observe updates and replaces, avoid updates with sourceDataUpdateField (those are handled by cs_data_processor.js)
                    var filter = "{ $or: [{ $and:[{ 'updateDescription.updatedFields.sourceDataUpdate': { $exists: false } },{ operationType: 'update' }] }, { operationType: 'replace'}] }";

                    var pipeline =
                        new EmptyPipelineDefinition <ChangeStreamDocument <rtData
                                                                           >
                                                     >().Match(filter);
                    var changeStreamOptions = new ChangeStreamOptions
                    {
                        FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
                    };
                    using (var cursor = await collection.WatchAsync(pipeline, changeStreamOptions))
                    {
                        await cursor
                        .ForEachAsync(change =>
                        {
                            // process change event, only process updates and replaces
                            if (
                                change.OperationType == ChangeStreamOperationType.Update ||
                                change.OperationType == ChangeStreamOperationType.Replace
                                )
                            {
                                if (change.FullDocument != null)
                                {
                                    if (change.FullDocument.protocolDestinations != null)
                                    {
                                        foreach (var dst in change.FullDocument.protocolDestinations)
                                        {
                                            foreach (IEC10X_connection
                                                     srv
                                                     in
                                                     IEC10Xconns
                                                     )
                                            {
                                                if (dst.protocolDestinationConnectionNumber == srv.protocolConnectionNumber)
                                                {
                                                    var conNameStr  = srv.name + " - ";
                                                    var quality     = new QualityDescriptor();
                                                    quality.Invalid = false;
                                                    if (change.FullDocument.invalid != null)
                                                    {
                                                        quality.Invalid |= change.FullDocument.invalid.ToBoolean();
                                                    }
                                                    if (change.FullDocument.overflow != null)
                                                    {
                                                        quality.Invalid |= change.FullDocument.overflow.ToBoolean();
                                                    }
                                                    if (change.FullDocument.transient != null)
                                                    {
                                                        quality.Invalid |= change.FullDocument.transient.ToBoolean();
                                                    }
                                                    if (change.FullDocument.substituted != null)
                                                    {
                                                        quality.Substituted = change.FullDocument.substituted.ToBoolean();
                                                    }
                                                    quality.Blocked        = false;
                                                    quality.NonTopical     = false;
                                                    CP56Time2a cp56timesrc = null;
                                                    if (change.FullDocument.timeTagAtSource != null)
                                                    {
                                                        cp56timesrc         = new CP56Time2a(System.Convert.ToDateTime(change.FullDocument.timeTagAtSource).AddHours(dst.protocolDestinationHoursShift.ToDouble()));
                                                        cp56timesrc.Invalid = false;
                                                        if (change.FullDocument.timeTagAtSourceOk != null)
                                                        {
                                                            cp56timesrc.Invalid = !change.FullDocument.timeTagAtSourceOk.ToBoolean();
                                                        }
                                                        else
                                                        {
                                                            cp56timesrc.Invalid = true;
                                                        }
                                                    }
                                                    var io = BuildInfoObj(
                                                        dst.protocolDestinationASDU.ToInt32(),
                                                        dst.protocolDestinationObjectAddress.ToInt32(),
                                                        change.FullDocument.value.ToDouble(),
                                                        false,
                                                        0,
                                                        quality,
                                                        cp56timesrc
                                                        );
                                                    if (io != null)
                                                    {
                                                        // queue data to make possible to assemble an ASDU with many elements, will send on DequeueIecInfo
                                                        InfoCA ica = new InfoCA()
                                                        {
                                                            io = io,
                                                            ca = dst.protocolDestinationCommonAddress.ToInt32()
                                                        };
                                                        srv.infoCAQueue.Enqueue(ica);

                                                        if (LogLevel >= LogLevelDetailed)
                                                        {
                                                            Log(conNameStr + "Spont Tag:" +
                                                                change.FullDocument.tag + " Value:" + change.FullDocument.value +
                                                                " Key:" + change.FullDocument._id + " TI:" + dst.protocolDestinationASDU.ToInt32() + " CA:" + dst.protocolDestinationCommonAddress + (cp56timesrc == null ? "" : " " + cp56timesrc.ToString()),
                                                                LogLevelDetailed);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        });
                    }
                }
                catch (Exception e)
                {
                    Log("Exception MongoCS");
                    Log(e);
                    Log(e
                        .ToString()
                        .Substring(0,
                                   e.ToString().IndexOf(Environment.NewLine)));
                    Thread.Sleep(3000);
                }
            }while (true);
        }