Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to clear ASDUReceivedHandler ?? #20

Open
Saleem344 opened this issue Jan 25, 2021 · 1 comment
Open

how to clear ASDUReceivedHandler ?? #20

Saleem344 opened this issue Jan 25, 2021 · 1 comment

Comments

@Saleem344
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants