commit
b04e651623
@ -0,0 +1,82 @@
|
||||
#ifndef PYIEC61850_EVENTHANDLER_HPP
|
||||
#define PYIEC61850_EVENTHANDLER_HPP
|
||||
|
||||
|
||||
#include "iec61850_client.h"
|
||||
#include <string>
|
||||
#include <Python.h>
|
||||
|
||||
|
||||
class PyThreadStateLock
|
||||
{
|
||||
public:
|
||||
PyThreadStateLock(void)
|
||||
{
|
||||
state = PyGILState_Ensure( );
|
||||
}
|
||||
|
||||
~PyThreadStateLock(void)
|
||||
{
|
||||
PyGILState_Release( state );
|
||||
}
|
||||
|
||||
private:
|
||||
PyGILState_STATE state;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class EventHandler {
|
||||
public:
|
||||
virtual ~EventHandler() {}
|
||||
virtual void setReceivedData(void *i_data_p) = 0;
|
||||
virtual void trigger() = 0;
|
||||
};
|
||||
|
||||
|
||||
class EventSubscriber {
|
||||
public:
|
||||
// TODO: use a map to store and find the instantiated EventSubscriber
|
||||
static EventSubscriber* m_last_created_event_subscriber;
|
||||
|
||||
EventSubscriber(): _event_handler_p(nullptr)
|
||||
{
|
||||
m_last_created_event_subscriber = this;
|
||||
|
||||
// add python thread support
|
||||
Py_Initialize();
|
||||
PyEval_InitThreads();
|
||||
}
|
||||
|
||||
virtual ~EventSubscriber()
|
||||
{
|
||||
deleteEventHandler();
|
||||
m_last_created_event_subscriber = nullptr;
|
||||
}
|
||||
|
||||
virtual void subscribe() = 0;
|
||||
|
||||
void deleteEventHandler()
|
||||
{
|
||||
if (_event_handler_p) {
|
||||
delete _event_handler_p;
|
||||
}
|
||||
_event_handler_p = nullptr;
|
||||
}
|
||||
|
||||
void setEventHandler(EventHandler *i_event_handler_p)
|
||||
{
|
||||
deleteEventHandler();
|
||||
_event_handler_p = i_event_handler_p;
|
||||
}
|
||||
|
||||
EventHandler *getEventHandler()
|
||||
{
|
||||
return _event_handler_p;
|
||||
}
|
||||
|
||||
private:
|
||||
EventHandler *_event_handler_p;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,63 @@
|
||||
#ifndef PYIEC61850_GOOSEHANDLER_HPP
|
||||
#define PYIEC61850_GOOSEHANDLER_HPP
|
||||
|
||||
#include "eventHandler.hpp"
|
||||
|
||||
|
||||
class GooseHandler: public EventHandler {
|
||||
public:
|
||||
virtual ~GooseHandler() {}
|
||||
|
||||
virtual void setReceivedData(void *i_data_p)
|
||||
{
|
||||
// copy the received data
|
||||
GooseSubscriber *l_my_data_p = static_cast<GooseSubscriber*>(i_data_p);
|
||||
_libiec61850_goose_subscriber = *l_my_data_p;
|
||||
}
|
||||
|
||||
GooseSubscriber _libiec61850_goose_subscriber;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class GooseSubscriberForPython: public EventSubscriber {
|
||||
public:
|
||||
|
||||
virtual void subscribe() {
|
||||
// install the libiec61850 callback:
|
||||
// the 'function pointer' is the 'static' method of this class
|
||||
GooseSubscriber_setListener(m_libiec61850_goose_subscriber,
|
||||
GooseSubscriberForPython::triggerGooseHandler,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Static method: it is the 'callback' for libiec61850 in C
|
||||
static void triggerGooseHandler(GooseSubscriber subscriber, void *parameter)
|
||||
{
|
||||
PyThreadStateLock PyThreadLock;
|
||||
|
||||
// TODO: search the appropriate 'EventSubscriber' object
|
||||
if (m_last_created_event_subscriber) {
|
||||
EventHandler *l_event_handler_p = m_last_created_event_subscriber->getEventHandler();
|
||||
if (l_event_handler_p) {
|
||||
l_event_handler_p->setReceivedData(&subscriber);
|
||||
l_event_handler_p->trigger();
|
||||
}
|
||||
else {
|
||||
printf("The EventHandler is undefined\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setters
|
||||
void setLibiec61850GooseSubscriber(const GooseSubscriber &i_libiec61850_goose_subscriber)
|
||||
{
|
||||
m_libiec61850_goose_subscriber = i_libiec61850_goose_subscriber;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Parameters
|
||||
GooseSubscriber m_libiec61850_goose_subscriber;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,66 @@
|
||||
#ifndef PYIEC61850_RCBHANDLER_HPP
|
||||
#define PYIEC61850_RCBHANDLER_HPP
|
||||
|
||||
#include "eventHandler.hpp"
|
||||
|
||||
|
||||
class RCBHandler: public EventHandler {
|
||||
public:
|
||||
virtual ~RCBHandler() {}
|
||||
|
||||
virtual void setReceivedData(void *i_data_p)
|
||||
{
|
||||
// copy the received data
|
||||
ClientReport *l_my_data_p = static_cast<ClientReport*>(i_data_p);
|
||||
_client_report = *l_my_data_p;
|
||||
}
|
||||
|
||||
ClientReport _client_report;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class RCBSubscriber: public EventSubscriber {
|
||||
public:
|
||||
|
||||
virtual void subscribe() {
|
||||
// install the libiec61850 callback:
|
||||
// the 'function pointer' is the 'static' method of this class
|
||||
IedConnection_installReportHandler(m_ied_connection,
|
||||
m_rcb_reference.c_str(),
|
||||
m_rcb_rpt_id.c_str(),
|
||||
RCBSubscriber::triggerRCBHandler,
|
||||
NULL);
|
||||
}
|
||||
|
||||
// Static method: it is the 'callback' for libiec61850 in C
|
||||
static void triggerRCBHandler(void *parameter, ClientReport report)
|
||||
{
|
||||
PyThreadStateLock PyThreadLock;
|
||||
|
||||
// TODO: search the appropriate 'EventSubscriber' object
|
||||
if (m_last_created_event_subscriber) {
|
||||
EventHandler *l_event_handler_p = m_last_created_event_subscriber->getEventHandler();
|
||||
if (l_event_handler_p) {
|
||||
l_event_handler_p->setReceivedData(&report);
|
||||
l_event_handler_p->trigger();
|
||||
}
|
||||
else {
|
||||
printf("The EventHandler is undefined\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setters
|
||||
void setIedConnection(const IedConnection &i_ied_connection) {m_ied_connection = i_ied_connection;}
|
||||
void setRcbReference(const char *i_rcb_reference) {m_rcb_reference = i_rcb_reference;}
|
||||
void setRcbRptId(const char *i_rcb_rpt_id) {m_rcb_rpt_id = i_rcb_rpt_id;}
|
||||
|
||||
protected:
|
||||
// Parameters
|
||||
IedConnection m_ied_connection;
|
||||
std::string m_rcb_reference;
|
||||
std::string m_rcb_rpt_id;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue