Skip to content

Commit

Permalink
Changes to suppoort optional Sender in PublishEvent from Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottEllisNovatex authored and neilstephens committed Nov 23, 2024
1 parent 21e106a commit dd45e81
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
32 changes: 18 additions & 14 deletions Code/Ports/PyPort/PyPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void PyPort::Build()
// Pass in a pointer to our SetTimer method, so it can be called from Python code - bit circular - I know!
// Also pass in a PublishEventCall method, so Python can send us Events to Publish.
pWrapper = std::make_unique<PythonWrapper>(this->Name, pIOS, std::bind(&PyPort::SetTimer, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&PyPort::PublishEventCall, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
std::bind(&PyPort::PublishEventCall, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
LOGDEBUG("pWrapper Created #####");
try
{
Expand Down Expand Up @@ -333,7 +333,7 @@ void PyPort::RemoveHTTPHandlers()
}
}

std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::string& EventTypeStr, size_t& ODCIndex, const std::string& QualityStr, const std::string& PayloadStr, const std::string& Name)
std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::string& EventTypeStr, size_t& ODCIndex, const std::string& QualityStr, const std::string& PayloadStr, const std::string& SourcePort)
{
EventType EventTypeResult = EventTypeFromString(EventTypeStr);
if (EventTypeResult >= EventType::AfterRange)
Expand All @@ -360,14 +360,14 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
LOGERROR("Invalid Connection State passed from Python Code to ODC - {}", PayloadStr);
return nullptr;
}
pubevent = std::make_shared<EventInfo>(EventType::ConnectState, 0, Name);
pubevent = std::make_shared<EventInfo>(EventType::ConnectState, 0, SourcePort);
pubevent->SetPayload<EventType::ConnectState>(std::move(state));
}
break;

case EventType::Binary:
{
pubevent = std::make_shared<EventInfo>(EventType::Binary, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::Binary, ODCIndex, SourcePort, QualityResult);
bool val = (PayloadStr.find('1') != std::string::npos);
pubevent->SetPayload<EventType::Binary>(std::move(val));
}
Expand All @@ -376,7 +376,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::Analog:
try
{
pubevent = std::make_shared<EventInfo>(EventType::Analog, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::Analog, ODCIndex, SourcePort, QualityResult);
double dval = std::stod(PayloadStr);
pubevent->SetPayload<EventType::Analog>(std::move(dval));
}
Expand All @@ -400,7 +400,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::ControlRelayOutputBlock:
try
{
pubevent = std::make_shared<EventInfo>(EventType::ControlRelayOutputBlock, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::ControlRelayOutputBlock, ODCIndex, SourcePort, QualityResult);
// Payload String looks like: "|LATCH_ON|Count 1|ON 100ms|OFF 100ms|"
EventTypePayload<EventType::ControlRelayOutputBlock>::type val;
auto Parts = split(PayloadStr, '|');
Expand Down Expand Up @@ -429,7 +429,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::AnalogOutputInt16:
try
{
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputInt16, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputInt16, ODCIndex, SourcePort, QualityResult);
EventTypePayload<EventType::AnalogOutputInt16>::type val;
int16_t ival = std::stoi(PayloadStr);
val.first = ival;
Expand All @@ -444,7 +444,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::AnalogOutputInt32:
try
{
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputInt32, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputInt32, ODCIndex, SourcePort, QualityResult);
EventTypePayload<EventType::AnalogOutputInt32>::type val;
int32_t ival = std::stoi(PayloadStr);
val.first = ival;
Expand All @@ -458,7 +458,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::AnalogOutputFloat32:
try
{
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputFloat32, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputFloat32, ODCIndex, SourcePort, QualityResult);
EventTypePayload<EventType::AnalogOutputFloat32>::type val;
float fval = std::stof(PayloadStr);
val.first = fval;
Expand All @@ -472,7 +472,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
case EventType::AnalogOutputDouble64:
try
{
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputDouble64, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::AnalogOutputDouble64, ODCIndex, SourcePort, QualityResult);
EventTypePayload<EventType::AnalogOutputDouble64>::type val;
double dval = std::stod(PayloadStr);
val.first = dval;
Expand All @@ -485,7 +485,7 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
break;
case EventType::OctetString:
{
pubevent = std::make_shared<EventInfo>(EventType::OctetString, ODCIndex, Name, QualityResult);
pubevent = std::make_shared<EventInfo>(EventType::OctetString, ODCIndex, SourcePort, QualityResult);
pubevent->SetPayload<EventType::OctetString>(std::string(PayloadStr));
}
break;
Expand Down Expand Up @@ -520,12 +520,16 @@ std::shared_ptr<odc::EventInfo> PyPort::CreateEventFromStrParams(const std::stri
// We pass this method string values for fields (from Python) parse them and create the ODC event then send it.
// A pointer to this method is passed to the Wrapper class so it can be called when we get information from the Python code.
// We are not passing a callback - just nullstr. So dont expect feedback.
void PyPort::PublishEventCall(const std::string &EventTypeStr, size_t ODCIndex, const std::string &QualityStr, const std::string &PayloadStr )
void PyPort::PublishEventCall(const std::string &EventTypeStr, size_t ODCIndex, const std::string &QualityStr, const std::string &PayloadStr, const std::string &SourcePort )
{
//LOGDEBUG("PyPort Publish Event {}, {}, {}, {}", EventTypeStr, ODCIndex, QualityStr, PayloadStr);

std::string SrcPort = SourcePort;
if (SourcePort.length() == 0)
{
SrcPort = Name; // If not provided, use default
}
// Separate call to allow testing
std::shared_ptr<EventInfo> pubevent = CreateEventFromStrParams(EventTypeStr, ODCIndex, QualityStr, PayloadStr, Name);
std::shared_ptr<EventInfo> pubevent = CreateEventFromStrParams(EventTypeStr, ODCIndex, QualityStr, PayloadStr, SrcPort);
if (pubevent)
{
if(MyConf->pyEnablePublishCallbackHandler)
Expand Down
2 changes: 1 addition & 1 deletion Code/Ports/PyPort/PyPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PyPort: public DataPort
void Event(std::shared_ptr<const EventInfo> event, const std::string& SenderName, SharedStatusCallback_t pStatusCallback) override;

void RestHandler(const std::string& url, const std::string& content, const ResponseCallback_t& pResponseCallback);
void PublishEventCall(const std::string &EventTypeStr, size_t ODCIndex, const std::string &QualityStr, const std::string &PayloadStr);
void PublishEventCall(const std::string &EventTypeStr, size_t ODCIndex, const std::string &QualityStr, const std::string &PayloadStr, const std::string &SourcePort);

static std::shared_ptr<odc::EventInfo> CreateEventFromStrParams(const std::string& EventTypeStr, size_t& ODCIndex, const std::string& QualityStr, const std::string& PayloadStr, const std::string &Name);

Expand Down
10 changes: 6 additions & 4 deletions Code/Ports/PyPort/PythonWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ static PyObject* odc_PublishEvent(PyObject* self, PyObject* args)
const char* EventType;
const char* Payload;
const char* Quality;
const char* SourcePort = "";
uint64_t guid;

// Now parse the arguments provided, three Unsigned ints (I) and a pyObject (O) and the function name.
if (!PyArg_ParseTuple(args, "LsIss:PublishEvent", &guid, &EventType, &ODCIndex, &Quality, &Payload))
// The parameters after | are optional - in this case the SourcePort
if (!PyArg_ParseTuple(args, "LsIss|s:PublishEvent", &guid, &EventType, &ODCIndex, &Quality, &Payload, &SourcePort))
{
PythonWrapper::PyErrOutput();
Py_RETURN_NONE; // This will throw an execption in the python code.
Expand All @@ -184,9 +186,9 @@ static PyObject* odc_PublishEvent(PyObject* self, PyObject* args)
// Will create an async wait and call the Python code at the correct time.
// At constrution, we have passed in a pointer to the PyPort SetTimer method, so we can call it
// The PyPort ensures that pyWrapper is managed within a strand
LOGDEBUG("Python Publish Event {}, {}, {}, {}", EventType, ODCIndex, Quality, Payload);
LOGDEBUG("Python Publish Event {}, {}, {}, {}, {}", EventType, ODCIndex, Quality, Payload, SourcePort);
auto fn = thisPyWrapper->GetPythonPortPublishEventCallFn();
fn(EventType, ODCIndex, Quality, Payload);
fn(EventType, ODCIndex, Quality, Payload, SourcePort);
}
else
{
Expand All @@ -210,7 +212,7 @@ static PyObject* odc_GetEventQueueSize(PyObject* self, PyObject* args)
uint64_t guid;

// Now parse the arguments provided, three Unsigned ints (I) and a pyObject (O) and the function name.
if (!PyArg_ParseTuple(args, "L:PublishEvent", &guid))
if (!PyArg_ParseTuple(args, "L:GetEventQueueSize", &guid))
{
PythonWrapper::PyErrOutput();
Py_RETURN_NONE; // This will throw an execption in the python code.
Expand Down
2 changes: 1 addition & 1 deletion Code/Ports/PyPort/PythonWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
using namespace odc;

typedef std::function<void (uint32_t, uint32_t)> SetTimerFnType;
typedef std::function<void ( const char*, uint32_t, const char*, const char*)> PublishEventCallFnType;
typedef std::function<void ( const char*, uint32_t, const char*, const char*, const char*)> PublishEventCallFnType;

// Class to store the evnt as a stringified version, mainly so that when Python is retreving these records, it does minimal processing.
/*class EventQueueType
Expand Down

0 comments on commit dd45e81

Please sign in to comment.