mz-automation / libiec61850

Official repository for libIEC61850, the open-source library for the IEC 61850 protocols
http://libiec61850.com/libiec61850
GNU General Public License v3.0
826 stars 443 forks source link

Howto use IedConnection_installReportHandler in python #380

Open Herve3 opened 2 years ago

Herve3 commented 2 years ago

Hi,

Just saw the new 1.5.1 release of libiec61850.

I am trying to understand how to use the new python handler for callback function (reportControlBlockHandler.hpp)

Is it possible to give an example please. Particularly for iec61850.IedConnection_installReportHandler(). I do not understand how to define reportCallbackFunction().

Thanks

Herve3 commented 2 years ago

Hi again,

I tried a few things to make this work. I understand I had to create a callback python function using setEventHandler.

So inside iec61850.i I added: %feature("director") EventHandler to be able to use it with swig.

Than in my program I add a PythonCallBack class overiding the c++ virtual eventHandler class:

class PythonCallback(iec61850.EventHandler):
    # Define Python class 'constructor'
    def __init__(self):
        # Call C++ base class constructor
        iec61850.EventHandler.__init__(self)
        print("INIT")

    def setReceivedData(self, report):
        print("setReceivedData")

    def trigger():
        print("TRIGGER")

And here is how I am uising it::

    pythonHandler = PythonCallback()

    rcbSubscriber = iec61850.RCBSubscriber()
    rcbSubscriber.setEventHandler(pythonHandler)
    rcbSubscriber.setIedConnection(con)
    rcbSubscriber.setRcbReference(rpt_ref)
    rcbSubscriber.setRcbRptId(iec61850.ClientReportControlBlock_getRptId(rcb))
    rcbSubscriber.subscribe()

I think I am very close of the solution.

When receiving a report, the librairie is calling the python setReceivedData method and then it is calling the python trigger method and making a core dump :(

darkjhesus commented 2 years ago

Hi Herve3, if you are using Python, I recommend you that compile libiec61850 in C# and use Pythonnet in python to access all function according to C# reference manual.

Herve3 commented 2 years ago

Hi Herve3, if you are using Python, I recommend you that compile libiec61850 in C# and use Pythonnet in python to access all function according to C# reference manual.

Thank you,

I am using the librairie in Linux. Is it possible to compile the lib C# API in linux?

In the libiec61850 documentation is is mentionned to use swig and there is a working example but with no callback functions. "The experimental Python binding can be created using SWIG with cmake."

Brandt1930 commented 1 year ago

Hi,

you dont need the EventHandler and no extra feature. Just use the iec61850.RCBHandler and the trigger() function with self._client_report

class PythonCallback(iec61850.RCBHandler):
    # Define Python class 'constructor'
    def __init__(self):
        # Call C++ base class constructor
        iec61850.RCBHandler.__init__(self)

    def trigger(self):
        report = self._client_report
        print("[Report] RCB Reference:", iec61850.ClientReport_getRcbReference(report))
        ...
mbourhis commented 1 year ago

Hi,

I recently wrote an example of an RCB subscriber in Python. You can find the code in the PullRequest https://github.com/mz-automation/libiec61850/pull/430 : https://github.com/mz-automation/libiec61850/blob/5f767ad03eded2e34d22a7a976e438e8d372de27/pyiec61850/examples/rcbSubscriptionExample.py

Here, an extract of the output:

New received RCB: simpleIOGenericIO/LLN0.RP.EventsRCB01
        DataSet name: simpleIOGenericIO/LLN0$Events
        Report id: Events1
        Sequence num: 2
        Timestamp in millsec: 1674488131896
        DataSet size: 4
        MMS value: False
        Reason for inclusion: integrity
        MMS value: False
        Reason for inclusion: integrity
        MMS value: False
        Reason for inclusion: integrity
        MMS value: False
        Reason for inclusion: integrity

New received RCB: simpleIOGenericIO/LLN0.BR.Measurements01
        DataSet name: simpleIOGenericIO/LLN0$Measurements
        Report id: Measurements
        Sequence num: 32
        Timestamp in millsec: 1674488131896
        DataSet size: 8
        MMS value: -0.971513032913208
        Reason for inclusion: data-change
        MMS value: other type
        Reason for inclusion: not-included
        MMS value: -0.3254937529563904
        Reason for inclusion: data-change
        MMS value: other type
        Reason for inclusion: not-included
        MMS value: 0.6197829842567444
        Reason for inclusion: data-change
        MMS value: other type
        Reason for inclusion: not-included
        MMS value: 0.9952340722084045
        Reason for inclusion: data-change
        MMS value: other type
        Reason for inclusion: not-included