Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c# - how to clear ASDUReceivedHandler in IEC-60870-5-104?

I have developed Dotnet Core Web API using this library when I'm giving the request to the API I'm getting the IEC104 parameters data in API response now the issues is when I'm requesting the API again a second time or third time it is not giving the current updated records it is giving me the old records and new records both but I don't want the old records I want only new records.

The library I have considered is: https://github.com/mz-automation/lib60870.NET.git

My code:

using System;
using System.Collections.Generic;
using System.Threading;
using lib60870;
using lib60870.CS101;
using lib60870.CS104;
using ResultModels;

namespace IEC104Functions
{
    class IEC104FunctionsClass
    {
        public static List<dynamic> resultvqt = new List<dynamic>();

        private static void ConnectionHandler(object parameter, ConnectionEvent connectionEvent)
        {
            switch (connectionEvent)
            {
                case ConnectionEvent.OPENED:
                    Console.WriteLine("Connected");
                    break;
                case ConnectionEvent.CLOSED:
                    Console.WriteLine("Connection closed");
                    break;
                case ConnectionEvent.STARTDT_CON_RECEIVED:
                    Console.WriteLine("STARTDT CON received");
                    break;
                case ConnectionEvent.STOPDT_CON_RECEIVED:
                    Console.WriteLine("STOPDT CON received");
                    break;
            }
        }

        private static bool asduReceivedHandler(object parameter, ASDU asdu)
        {
            switch (asdu.TypeId)
            {
                case TypeID.M_SP_NA_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (SinglePointInformation)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_SP_TB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (SinglePointWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_DP_NA_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (DoublePointInformation)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_DP_TB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (DoublePointWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_ST_NA_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (StepPositionInformation)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_ST_TB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (StepPositionWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_BO_NA_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (Bitstring32)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_BO_TB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (Bitstring32WithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_ME_NA_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueNormalized)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.NormalizedValue,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_ME_TB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueNormalizedWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.NormalizedValue,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_ME_NB_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueScaled)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.ScaledValue,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_ME_TE_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueScaledWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.ScaledValue,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
                case TypeID.M_ME_NC_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueShort)asdu.GetElement(i);
                        ResultAsduVQT datavq = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = DateTime.Now
                        };
                        resultvqt.Add(datavq);
                    }
                    break;
                case TypeID.M_ME_TF_1:
                    for (int i = 0; i < asdu.NumberOfElements; i++)
                    {
                        var data = (MeasuredValueShortWithCP56Time2a)asdu.GetElement(i);
                        ResultAsduVQT datavqt = new ResultAsduVQT
                        {
                            IOA = data.ObjectAddress,
                            Value = data.Value,
                            Quality = data.Quality.EncodedValue,
                            Timestamp = data.Timestamp.GetDateTime()
                        };
                        resultvqt.Add(datavqt);
                    }
                    break;
            }
            return true;
        }

        public ResultAsduModel ReadAllParameters(string IpAddress)
        {
            Connection con = new Connection(IpAddress);
            con.DebugOutput = true;
            resultvqt = new List<dynamic>();
            
            con.SetASDUReceivedHandler(asduReceivedHandler, null);
            con.SetConnectionHandler(ConnectionHandler, null);
            bool running = true;

            con.Connect();
            while (running)
            {
                Thread.Sleep(100);
                running = false;
            }
            
            con.Close();

            ResultAsduModel result = new ResultAsduMo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...