mz-automation / lib60870.NET

Official repository for lib60870.NET an implementation of the IEC 60870-5-101/104 protocols in C#
https://www.mz-automation.de/communication-protocols/iec-60870-5-101-104-c-net-source-code-library/
GNU General Public License v3.0
123 stars 75 forks source link

how to clear ASDUReceivedHandler ?? #20

Open Saleem344 opened 3 years ago

Saleem344 commented 3 years ago

Hi, 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.

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 ResultAsduModel()
            {
                data = resultvqt,
                error = false,
                errormessage = null
            };

            return (result);
        }
    }
}

Can anyone help me with how to clear the ASDUReceivedhandler so that I can get only new records??

Thank you..

juwiefer-btc commented 1 year ago

You might not need this advice any more, but I guess the trouble is that a new connection sends an interrogation command to get the complete network image -- as it should. Simply do not open a new connection every time.