11 #include <boost/python.hpp> 14 #include <boost/date_time/posix_time/posix_time_types.hpp> 15 #include <boost/date_time/gregorian/gregorian.hpp> 16 #include <boost/date_time/c_local_time_adjustor.hpp> 33 #if PY_MAJOR_VERSION >= 3 34 #define PyString_Check PyUnicode_Check 35 #define PyString_AsString(S) PyBytes_AsString(PyUnicode_AsUTF8String(S)) 39 using namespace OpcUa;
56 boost::posix_time::ptime ref(boost::gregorian::date(1601, 1, 1));
57 boost::posix_time::ptime dt = ref + boost::posix_time::microseconds(
self.
Value / 10);
61 uint32_t precision = dt.time_of_day().num_fractional_digits();
62 PyObject * obj = PyDateTime_FromDateAndTime((
int)dt.date().year(),
63 (int)dt.date().month(),
65 dt.time_of_day().hours(),
66 dt.time_of_day().minutes(),
67 dt.time_of_day().seconds(),
68 dt.time_of_day().fractional_seconds() / pow(10, precision - 6));
69 return boost::python::object(boost::python::handle<>(obj));
74 boost::gregorian::date _date(PyDateTime_GET_YEAR(pydate), PyDateTime_GET_MONTH(pydate), PyDateTime_GET_DAY(pydate));
75 boost::posix_time::time_duration _duration(PyDateTime_DATE_GET_HOUR(pydate), PyDateTime_DATE_GET_MINUTE(pydate), PyDateTime_DATE_GET_SECOND(pydate), 0);
76 _duration += boost::posix_time::microseconds(PyDateTime_DATE_GET_MICROSECOND(pydate));
78 boost::posix_time::ptime myptime(_date, _duration);
79 boost::posix_time::ptime ref(boost::gregorian::date(1601, 1, 1));
80 return (myptime - ref).total_microseconds() * 10;
85 PyObject * pydate = bobj.ptr();
87 if (! PyDateTime_Check(pydate))
89 throw std::runtime_error(
"method take a python datetime as argument");
108 boost::python::converter::registry::push_back(&convertible, &construct, boost::python::type_id<DateTime>());
113 if (!PyDateTime_Check(obj_ptr)) {
return 0; }
118 static void construct(PyObject * obj_ptr, boost::python::converter::rvalue_from_python_stage1_data * data)
120 void * storage = ((boost::python::converter::rvalue_from_python_storage<DateTime> *)data)->storage.bytes;
124 data->convertible = storage;
136 return boost::python::incref(boost::python::object(text.
Text.c_str()).ptr());
145 boost::python::converter::registry::push_back(&convertible, &construct, boost::python::type_id<LocalizedText>());
150 if (!PyString_Check(obj_ptr)) {
return 0; }
155 static void construct(PyObject * obj_ptr, boost::python::converter::rvalue_from_python_stage1_data * data)
157 void * storage = ((boost::python::converter::rvalue_from_python_storage<LocalizedText> *)data)->storage.bytes;
158 const char * value = PyString_AsString(obj_ptr);
160 data->convertible = storage;
170 {
return std::shared_ptr<NodeId>(
new NodeId(
ToNodeId(encodedNodeId))); }
174 if (
self.IsInteger())
175 {
return object(
self.GetIntegerIdentifier()); }
177 else if (
self.IsString())
178 {
return object(
self.GetStringIdentifier()); }
180 else if (
self.IsGuid())
181 {
return object(
self.GetGuidIdentifier()); }
183 else if (
self.IsBinary())
184 {
return object(
self.GetBinaryIdentifier()); }
187 {
throw std::logic_error(
"Error unknown identifier."); }
204 {
return self.Status; }
210 {
return self.SourceTimestamp; }
216 {
return self.SourcePicoseconds; }
222 {
return self.ServerTimestamp; }
228 {
return self.ServerPicoseconds; }
247 return self.CreateSubscription(period, callback);
252 return self.GetNode(
NodeId(objectid));
261 return self.CreateSubscription(period, callback);
266 return self.GetNode(
NodeId(objectid));
280 PyEval_InitThreads();
286 to_python_converter<LocalizedText, LocalizedTextToPythonConverter>();
296 to_python_converter<Variant, variant_to_python_converter>();
298 class_<DateTime>(
"DateTime", init<>())
299 .def(init<int64_t>())
300 .def(
"now", &DateTime::DateTime::Current)
302 .def(
"from_time_t", &DateTime::FromTimeT, DateTimeFromTimeT_stub((
arg(
"sec"),
arg(
"usec") = 0)))
303 .staticmethod(
"from_time_t")
305 .def(
"to_time_t", &DateTime::ToTimeT)
316 class_<NodeId, std::shared_ptr<NodeId>>(
"NodeId")
317 .def(init<uint32_t, uint16_t>())
318 .def(init<std::string, uint16_t>())
320 .add_property(
"namespace_index", &NodeId::GetNamespaceIndex)
322 .add_property(
"encoding", &NodeId::GetEncodingValue)
323 .add_property(
"is_integer", &NodeId::IsInteger)
324 .add_property(
"is_binary", &NodeId::IsBinary)
325 .add_property(
"is_guid", &NodeId::IsGuid)
326 .add_property(
"is_string", &NodeId::IsString)
327 .def_readonly(
"namespace_uri", &NodeId::NamespaceURI)
335 class_<QualifiedName>(
"QualifiedName")
336 .def(init<uint16_t, std::string>())
337 .def(init<std::string, uint16_t>())
339 .def_readwrite(
"namespace_index", &QualifiedName::NamespaceIndex)
340 .def_readwrite(
"name", &QualifiedName::Name)
346 class_<DataValue, std::shared_ptr<DataValue>>(
"DataValue")
347 .def(init<const Variant &>())
349 #define
_property(X) add_property( #X, &DataValue_get_ ## X, &DataValue_set_ ## X)
352 ._property(source_timestamp)
353 ._property(source_picoseconds)
354 ._property(server_timestamp)
355 ._property(server_picoseconds)
361 class_<ApplicationDescription>(
"ApplicationDescription")
362 .def_readwrite(
"uri", &ApplicationDescription::ApplicationUri)
363 .def_readwrite(
"product_uri", &ApplicationDescription::ProductUri)
364 .def_readwrite(
"name", &ApplicationDescription::ApplicationName)
366 .def_readwrite(
"gateway_server_uri", &ApplicationDescription::GatewayServerUri)
367 .def_readwrite(
"discovery_profile_uri", &ApplicationDescription::DiscoveryProfileUri)
368 .def_readwrite_vector(
"discovery_urls", &ApplicationDescription::DiscoveryUrls)
373 class_<UserTokenPolicy>(
"UserTokenPolicy")
374 .def_readwrite(
"policy_id", &UserTokenPolicy::PolicyId)
375 .def_readwrite(
"token_type", &UserTokenPolicy::TokenType)
376 .def_readwrite(
"issued_token_type", &UserTokenPolicy::IssuedTokenType)
377 .def_readwrite(
"issuer_endpoint_url", &UserTokenPolicy::IssuerEndpointUrl)
378 .def_readwrite(
"security_policy_uri", &UserTokenPolicy::SecurityPolicyUri)
384 class_<EndpointDescription>(
"EndpointDescription")
385 .def_readwrite(
"url", &EndpointDescription::EndpointUrl)
387 .def_readwrite(
"security_mode", &EndpointDescription::SecurityMode)
388 .def_readwrite(
"security_policy_uri", &EndpointDescription::SecurityPolicyUri)
389 .def_readwrite_vector(
"user_identify_tokens", &EndpointDescription::UserIdentityTokens)
390 .def_readwrite(
"transport_profile_uri", &EndpointDescription::TransportProfileUri)
391 .def_readwrite(
"security_level", &EndpointDescription::SecurityLevel)
396 class_<ReferenceDescription>(
"ReferenceDescription")
397 .def_readwrite(
"reference_type_id", &ReferenceDescription::ReferenceTypeId)
398 .def_readwrite(
"is_forward", &ReferenceDescription::IsForward)
399 .def_readwrite(
"target_node_id", &ReferenceDescription::TargetNodeId)
400 .def_readwrite(
"browse_name", &ReferenceDescription::BrowseName)
402 .def_readwrite(
"target_node_class", &ReferenceDescription::TargetNodeClass)
403 .def_readwrite(
"target_node_type_definition", &ReferenceDescription::TargetNodeTypeDefinition)
408 class_<ReadValueId>(
"ReadValueId")
411 .def_readwrite(
"index_range", &ReadValueId::IndexRange)
412 .def_readwrite(
"data_encoding", &ReadValueId::DataEncoding)
417 class_<WriteValue>(
"WriteValue")
420 .def_readwrite(
"index_range", &WriteValue::IndexRange)
436 class_<Node>(
"Node", init<Services::SharedPtr, NodeId>())
438 .def(
"get_id", &Node::GetId)
439 .def(
"get_attribute", &Node::GetAttribute)
440 .def(
"set_attribute", &Node::SetAttribute)
441 .def(
"get_value", &Node::GetValue)
442 .def(
"set_value", (
void(
Node::*)(
const DataValue &)
const) &Node::SetValue)
443 .def(
"set_value", (
void(
Node::*)(
const Variant &)
const) &Node::SetValue)
445 .def(
"get_properties", &Node::GetProperties)
446 .def(
"get_variables", &Node::GetVariables)
447 .def(
"get_browse_name", &Node::GetBrowseName)
448 .def(
"get_children", (std::vector<Node> (
Node::*)()
const) &Node::GetChildren)
449 .def(
"get_child", (
Node(
Node::*)(
const std::vector<std::string> &)
const) &Node::GetChild)
471 class_<SubscriptionHandler, PySubscriptionHandler, boost::noncopyable>(
"SubscriptionHandler", init<>())
473 .staticmethod(
"data_change")
475 .staticmethod(
"event")
477 .staticmethod(
"status_change")
480 class_<Event>(
"Event", init<const NodeId &>())
484 .def(
"get_value_keys", &Event::GetValueKeys)
488 .add_property(
"message", make_getter(&
Event::Message, return_value_policy<return_by_value>()), make_setter(&
Event::Message, return_value_policy<return_by_value>()))
498 class_<Subscription, std::shared_ptr<Subscription>, boost::noncopyable>(
"Subscription", no_init)
501 .def(
"unsubscribe", (
void (
Subscription::*)(uint32_t)) &Subscription::UnSubscribe)
502 .def(
"subscribe_events", (uint32_t (
Subscription::*)()) &Subscription::SubscribeEvents)
503 .def(
"subscribe_events", (uint32_t (
Subscription::*)(
const Node &,
const Node &)) &Subscription::SubscribeEvents)
508 class_<UaClient, boost::noncopyable>(
"Client", init<>())
512 .def(
"disconnect", &UaClient::Disconnect)
513 .def(
"get_namespace_index", &UaClient::GetNamespaceIndex)
514 .def(
"get_root_node", &UaClient::GetRootNode)
515 .def(
"get_objects_node", &UaClient::GetObjectsNode)
516 .def(
"get_server_node", &UaClient::GetServerNode)
521 .def(
"get_server_endpoints", (std::vector<EndpointDescription> (
UaClient::*)(
const std::string &)) &UaClient::GetServerEndpoints)
522 .def(
"get_server_endpoints", (std::vector<EndpointDescription> (
UaClient::*)()) &UaClient::GetServerEndpoints)
523 .def(
"set_session_name", &UaClient::SetSessionName)
524 .def(
"get_session_name", &UaClient::GetSessionName)
525 .def(
"get_application_uri", &UaClient::GetApplicationURI)
526 .def(
"set_application_uri", &UaClient::SetApplicationURI)
527 .def(
"set_security_policy", &UaClient::SetSecurityPolicy)
528 .def(
"get_security_policy", &UaClient::GetSecurityPolicy)
534 class_<UaServer, boost::noncopyable >(
"Server", init<>())
536 .def(
"start", &UaServer::Start)
537 .def(
"stop", &UaServer::Stop)
538 .def(
"register_namespace", &UaServer::RegisterNamespace)
539 .def(
"get_namespace_index", &UaServer::GetNamespaceIndex)
540 .def(
"get_root_node", &UaServer::GetRootNode)
541 .def(
"get_objects_node", &UaServer::GetObjectsNode)
542 .def(
"get_server_node", &UaServer::GetServerNode)
546 .def(
"set_uri", &UaServer::SetServerURI)
547 .def(
"add_xml_address_space", &UaServer::AddAddressSpace)
548 .def(
"set_server_name", &UaServer::SetServerName)
549 .def(
"set_endpoint", &UaServer::SetEndpoint)
551 .def(
"trigger_event", &UaServer::TriggerEvent)
static std::shared_ptr< DateTime > makeOpcUaDateTime(const boost::python::object &bobj)
DateTimePythonToOpcUaConverter()
static uint16_t DataValue_get_server_picoseconds(const DataValue &self)
static void DefaultDataChange(const SubscriptionHandler &self_, uint32_t handle, const Node &node, const object &val, uint32_t attribute)
static std::shared_ptr< NodeId > NodeId_constructor(const std::string &encodedNodeId)
const uint8_t DATA_VALUE_STATUS_CODE
static void DataValue_set_server_picoseconds(DataValue &self, uint16_t ps)
static void * convertible(PyObject *obj_ptr)
Variant ToVariant2(const object &obj, VariantType vtype)
static void Node_SetValue(Node &self, const object &obj, VariantType vtype)
static void DataValue_set_source_picoseconds(DataValue &self, uint16_t ps)
static std::shared_ptr< DataValue > DataValue_constructor1(const object &obj, VariantType vtype)
object ToObject(const Variant &var)
static std::shared_ptr< Subscription > UaServer_CreateSubscription(UaServer &self, uint period, PySubscriptionHandler &callback)
static object NodeId_GetIdentifier(const NodeId &self)
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
const uint8_t DATA_VALUE_Server_TIMESTAMP
static uint64_t ToWinEpoch(PyObject *pydate)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(SubscriptionSubscribeDataChange_stubs, Subscription::SubscribeDataChange, 1, 2)
static object DataValue_get_value(const DataValue &self)
BOOST_PYTHON_MODULE(opcua)
static void DefaultEvent(const SubscriptionHandler &self_, uint32_t handle, const OpcUa::Event &event)
const uint8_t DATA_VALUE_SOURCE_PICOSECONDS
static PyObject * convert(const LocalizedText &text)
static void DefaultStatusChange(const SubscriptionHandler &self_, StatusCode status)
static StatusCode DataValue_get_status(const DataValue &self)
const uint8_t DATA_VALUE_SOURCE_TIMESTAMP
OPC UA Address space part. GNU LGPL.
const uint8_t DATA_VALUE_Server_PICOSECONDS
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
static DateTime DataValue_get_source_timestamp(const DataValue &self)
PythonStringToLocalizedTextConverter()
BOOST_PYTHON_FUNCTION_OVERLOADS(DateTimeFromTimeT_stub, DateTime::FromTimeT, 1, 2)
static void DataValue_set_server_timestamp(DataValue &self, const DateTime &dt)
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
internal::NamedArg< char > arg(StringRef name, const T &arg)
void py_opcua_enums()
Python bindings for freeopcua. GNU LGPL.
NodeId ToNodeId(const std::string &str, uint32_t defaultNamespace=0)
static DateTime DataValue_get_server_timestamp(const DataValue &self)
static std::shared_ptr< Subscription > UaClient_CreateSubscription(UaClient &self, uint period, PySubscriptionHandler &callback)
static boost::python::object ToPyDateTime(const DateTime &self)
static Node UaClient_GetNode(UaClient &self, ObjectId objectid)
static Node UaServer_GetNode(UaServer &self, ObjectId objectid)
static void DataValue_set_value(DataValue &self, const object &obj, VariantType vtype)
Python bindings for freeopcua. GNU LGPL.
static void DataValue_set_status(DataValue &self, const StatusCode &sc)
static void * convertible(PyObject *obj_ptr)
static uint16_t DataValue_get_source_picoseconds(const DataValue &self)
static PyObject * convert(const DateTime &dt)
EndpointDescription GetEndpoint(OpcUa::Binary::IOStream &stream)
std::unique_ptr< RemoteConnection > Connect(const std::string &host, unsigned port, const Common::Logger::SharedPtr &logger)
static void DataValue_set_source_timestamp(DataValue &self, const DateTime &dt)