YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
MonitorPython.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef MONITORPYTHON_H
7#define MONITORPYTHON_H
8
9#include <string>
10#include "MonitorBinding.h"
11
12#include "Python.h"
13
14#include <mutex>
15
17{
18
19public:
21 ~MonitorPython() override;
22
23 bool load(const yarp::os::Property& options) override;
24 bool setParams(const yarp::os::Property& params) override;
25 bool getParams(yarp::os::Property& params) override;
26
27 bool acceptData(yarp::os::Things& thing) override;
30
31 bool peerTrigged() override;
32 bool canAccept() override;
33
34 bool setAcceptConstraint(const char* constraint) override
35 {
36 if(!constraint) {
37 return false;
38 }
39 MonitorPython::constraint = constraint;
40 trimString(MonitorPython::constraint);
41 return true;
42 }
43
44 const char* getAcceptConstraint() override {
45 return constraint.c_str();
46 }
47
48 bool hasAccept() override {
49 return bHasAcceptCallback;
50 }
51
52 bool hasUpdate() override {
53 return bHasUpdateCallback;
54 }
55
56 bool hasUpdateReply() override {
57 return bHasUpdateReplyCallback;
58 }
59
60private:
61 std::string constraint;
62 bool bHasAcceptCallback;
63 bool bHasUpdateCallback;
64 bool bHasUpdateReplyCallback;
65
66 std::string m_path;
67 std::string m_pythonScriptName = "monitor"; // Python module containing the functions/classes
68 std::recursive_mutex m_monitor_mutex;
69
70 bool classWrapper(PyObject* &pClassInstance, std::string methodName, PyObject* &pClassMethodArgs, PyObject* &pValue);
71 bool functionWrapper(std::string functionName, PyObject* &pArgs, PyObject* &pValue);
72 bool hasPythonFunction(const std::string& functionName);
73 bool ensureYarpModuleLoaded();
74
75 PyObject* m_classInstance{nullptr}; // Python object of the created class
76 PyObject* m_lastUpdateResult{nullptr};
77 PyObject* m_pModule{nullptr};
78
79public:
81
82private:
83 void trimString(std::string& str);
84 void searchReplace(std::string& str,
85 const std::string& oldStr, const std::string& newStr);
86 bool isKeyword(const char* str);
87 bool loadPythonModule(const std::string& moduleName, PyObject* &pModule);
88};
89
90
91#endif //MONITORPYTHON_H
bool peerTrigged() override
bool hasAccept() override
bool setParams(const yarp::os::Property &params) override
bool load(const yarp::os::Property &options) override
yarp::os::Things & updateData(yarp::os::Things &thing) override
bool acceptData(yarp::os::Things &thing) override
MonitorTrigger< MonitorPython > * trigger
const char * getAcceptConstraint() override
bool hasUpdate() override
bool canAccept() override
bool hasUpdateReply() override
bool getParams(yarp::os::Property &params) override
MonitorPython()
Class MonitorPython.
yarp::os::Things & updateReply(yarp::os::Things &thing) override
~MonitorPython() override
bool setAcceptConstraint(const char *constraint) override
A class for storing options and configuration information.
Definition Property.h:33
Base class for generic things.
Definition Things.h:18