本文整理汇总了Python中opcua.Server.get_node方法的典型用法代码示例。如果您正苦于以下问题:Python Server.get_node方法的具体用法?Python Server.get_node怎么用?Python Server.get_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opcua.Server
的用法示例。
在下文中一共展示了Server.get_node方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runTest
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
def runTest(self):
tmpfile = NamedTemporaryFile()
path = tmpfile.name
tmpfile.close()
#create cache file
server = Server(cacheFile = path)
#modify cache content
id = ua.NodeId(ua.ObjectIds.Server_ServerStatus_SecondsTillShutdown)
s = shelve.open(path, "w", writeback = True)
s[id.to_string()].attributes[ua.AttributeIds.Value].value = ua.DataValue(123)
s.close()
#ensure that we are actually loading from the cache
server = Server(cacheFile = path)
self.assertEqual(server.get_node(id).get_value(), 123)
os.remove(path)
示例2: ServerPython
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
class ServerPython(object):
def __init__(self):
self._server = None
self.nodes = None
self.get_node = None
self.get_namespace_array = None
def start_server(self, endpoint):
logger.info("Starting python-opcua server")
self._server = Server()
self._server.set_endpoint(endpoint)
self._server.set_server_name("OpcUa Modeler Server")
self.nodes = self._server.nodes
self.get_node = self._server.get_node
self.get_namespace_array = self._server.get_namespace_array
self.load_type_definitions = self._server.load_type_definitions
self.load_enums = self._server.load_enums
# now remove freeopcua namespace, not necessary when modeling and
# ensures correct idx for exported nodesets
ns_node = self._server.get_node(ua.NodeId(ua.ObjectIds.Server_NamespaceArray))
nss = ns_node.get_value()
ns_node.set_value(nss[:1])
self._server.start()
def stop_server(self):
if self._server is not None:
self._server.stop()
self._server = None
self.get_node = None
self.get_namespace_array = None
def import_xml(self, path):
return self._server.import_xml(path)
def export_xml(self, nodes, uris, path):
exp = XmlExporter(self._server)
exp.build_etree(nodes, uris=uris)
exp.write_xml(path)
示例3: database
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
serverevgen.event.Severity = 111
# Configure server to use sqlite as history database (default is a simple in memory dict)
server.iserver.history_manager.set_storage(HistorySQLite(":memory:"))
# starting!
server.start()
# enable history for this particular node, must be called after start since it uses subscription
server.iserver.enable_history_data_change(myvar, period=None, count=100)
# enable history for myobj events
server.iserver.enable_history_event(myobj, period=None)
# enable history for server events
server_node = server.get_node(ua.ObjectIds.Server)
server.iserver.enable_history_event(server_node, period=None)
try:
count = 0
while True:
time.sleep(1)
count += 0.1
myvar.set_value(math.sin(count))
myevgen.trigger(message="This is MyFirstEvent with MyNumericProperty and MyStringProperty.")
myevgen2.trigger(message="This is MySecondEvent with MyOtherProperty.")
serverevgen.trigger(message="Server Event Message")
finally:
# close connection, remove subscriptions, etc
server.stop()
示例4: TestServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
#.........这里部分代码省略.........
uri = 'http://my_very_custom.Namespace.com'
idx = self.opc.register_namespace(uri)
root = self.opc.get_root_node()
myvar = root.add_variable(idx, 'var_in_custom_namespace', [5])
myid = myvar.nodeid
self.assertEqual(idx, myid.NamespaceIndex)
def test_server_method(self):
def func(parent, variant):
variant.Value *= 2
return [variant]
o = self.opc.get_objects_node()
v = o.add_method(3, 'Method1', func, [ua.VariantType.Int64], [ua.VariantType.Int64])
result = o.call_method(v, ua.Variant(2.1))
self.assertEqual(result, 4.2)
def test_xml_import(self):
self.srv.import_xml("tests/custom_nodes.xml")
o = self.opc.get_objects_node()
v = o.get_child(["MyXMLFolder", "MyXMLObject", "MyXMLVariable"])
val = v.get_value()
self.assertEqual(val, "StringValue")
def test_historize_variable(self):
o = self.opc.get_objects_node()
var = o.add_variable(3, "test_hist", 1.0)
self.srv.iserver.enable_history_data_change(var, timedelta(days=1))
time.sleep(1)
var.set_value(2.0)
var.set_value(3.0)
self.srv.iserver.disable_history_data_change(var)
def test_historize_events(self):
srv_node = self.srv.get_node(ua.ObjectIds.Server)
srvevgen = self.srv.get_event_generator()
self.srv.iserver.enable_history_event(srv_node, period=None)
srvevgen.trigger(message="Message")
self.srv.iserver.disable_history_event(srv_node)
def test_references_for_added_nodes_method(self):
objects = self.opc.get_objects_node()
o = objects.add_object(3, 'MyObject')
nodes = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Forward, includesubtypes=False)
self.assertTrue(o in nodes)
nodes = o.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
self.assertTrue(objects in nodes)
self.assertEqual(o.get_parent(), objects)
self.assertEqual(o.get_type_definition(), ua.ObjectIds.BaseObjectType)
@uamethod
def callback(parent):
return
m = o.add_method(3, 'MyMethod', callback)
nodes = o.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Forward, includesubtypes=False)
self.assertTrue(m in nodes)
nodes = m.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
self.assertTrue(o in nodes)
self.assertEqual(m.get_parent(), o)
# This should work for following BaseEvent tests to work (maybe to write it a bit differentlly since they are not independent)
def test_get_event_from_type_node_BaseEvent(self):
ev = opcua.common.events.get_event_obj_from_type_node(opcua.Node(self.opc.iserver.isession, ua.NodeId(ua.ObjectIds.BaseEventType)))
check_base_event(self, ev)
def test_get_event_from_type_node_Inhereted_AuditEvent(self):
示例5: parent
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
myobj = objects.add_object(idx, "MyObject")
# Creating a custom event: Approach 1
# The custom event object automatically will have members from its parent (BaseEventType)
etype = server.create_custom_event_type(
2,
"MyFirstEvent",
ua.ObjectIds.BaseEventType,
[("MyNumericProperty", ua.VariantType.Float), ("MyStringProperty", ua.VariantType.String)],
)
myevgen = server.get_event_generator(etype, myobj)
myevgen.event.Severity = 500
# Creating a custom event: Approach 2
base_etype = server.get_node(ua.ObjectIds.BaseEventType)
custom_etype = base_etype.add_subtype(2, "MySecondEvent")
custom_etype.add_property(2, "MyIntProperty", ua.Variant(None, ua.VariantType.Int32))
custom_etype.add_property(2, "MyBoolProperty", ua.Variant(None, ua.VariantType.Boolean))
mysecondevgen = server.get_event_generator(custom_etype, myobj)
# starting!
server.start()
try:
# time.sleep is here just because we want to see events in UaExpert
import time
for i in range(1, 10):
time.sleep(10)
示例6: TestAttrsWidget
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
class TestAttrsWidget(unittest.TestCase):
def setUp(self):
self.server = Server()
self.server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
self.server.start()
self.widget = AttrsWidget(QTreeView())
def tearDown(self):
self.server.stop()
def find_item(self, text):
idxlist = self.widget.model.match(self.widget.model.index(0, 0), Qt.DisplayRole, text, 1, Qt.MatchExactly | Qt.MatchRecursive)
idx = idxlist[0]
return idx
def modify_item(self, text, val, match_to_use=0):
"""
modify the current item and set its displayed value to 'val'
"""
idxlist = self.widget.model.match(self.widget.model.index(0, 0), Qt.DisplayRole, text, match_to_use + 1, Qt.MatchExactly | Qt.MatchRecursive)
if not idxlist:
raise RuntimeError("Item with text '{}' not found".format(text))
idx = idxlist[match_to_use]
self.widget.view.setCurrentIndex(idx)
idx = idx.sibling(idx.row(), 1)
self.widget.view.edit(idx)
editor = self.widget.view.focusWidget()
if not editor:
print(app.focusWidget())
print("NO EDITOR WIDGET")
#QTest.keyClick(self.widget.view, Qt.Key_Return)
from IPython import embed
embed()
raise RuntimeError("Could not get editor widget!, it does not have the focus")
if hasattr(editor, "_current_node"):
editor._current_node = val
elif hasattr(editor, "setCurrentText"):
editor.setCurrentText(val)
else:
editor.setText(val)
self.widget.view.commitData(editor)
self.widget.view.closeEditor(editor, QAbstractItemDelegate.NoHint)
self.widget.view.reset()
def modify_value(self, val):
self.modify_item("Value", val, 1)
def test_display_objects_node(self):
objects = self.server.nodes.objects
self.widget.show_attrs(objects)
self.modify_item("BrowseName", "5:titi")
self.assertEqual(objects.get_browse_name().to_string(), "5:titi")
self.modify_item("BrowseName", "0:Objects") # restore states for other tests
def test_display_var_double(self):
objects = self.server.nodes.objects
myvar = objects.add_variable(1, "myvar1", 9.99, ua.VariantType.Double)
self.widget.show_attrs(myvar)
self.modify_value("8.45")
self.assertEqual(myvar.get_value(), 8.45)
def test_display_var_bytes(self):
objects = self.server.nodes.objects
myvar = objects.add_variable(1, "myvar_bytes", b"jkl", ua.VariantType.ByteString)
self.widget.show_attrs(myvar)
self.modify_value("titi")
self.assertEqual(myvar.get_value(), b"titi")
def test_change_data_type(self):
objects = self.server.nodes.objects
myvar = objects.add_variable(1, "myvar1", 9.99, ua.VariantType.Double)
self.widget.show_attrs(myvar)
dtype = myvar.get_data_type()
self.assertEqual(dtype, ua.NodeId(ua.ObjectIds.Double))
new_dtype = ua.NodeId(ua.ObjectIds.String)
self.modify_item("DataType", self.server.get_node(new_dtype))
self.assertEqual(myvar.get_data_type(), new_dtype)
# now try to write a value which is a string
self.modify_value("mystring")
self.assertEqual(myvar.get_value(), "mystring")
def test_change_value_rank(self): # need to find a way to modify combo box with QTest
objects = self.server.nodes.objects
myvar = objects.add_variable(1, "myvar1", 9.99, ua.VariantType.Double)
self.widget.show_attrs(myvar)
rank = myvar.get_value_rank()
self.modify_item("ValueRank", "ThreeDimensions")
self.assertEqual(myvar.get_value_rank(), ua.ValueRank.ThreeDimensions)
示例7:
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
idx = server.register_namespace(uri)
# Import customobject type
server.import_xml('customobject.xml')
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
# get nodeid of custom object type by :
# 1) Use node ID
# 2) Or Full path
# 3) Or As child from parent
nodeid_a = ua.NodeId.from_string('ns=1;i=1002')
nodeid_b = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "1:MyObjectType"]).nodeid
nodeid_c = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["1:MyObjectType"]).nodeid
myobject_type_nodeid = nodeid_a
# populating our address space
myobj = objects.add_object(idx, "MyObject",)
myobj = objects.add_object(idx, "MyCustomObject", myobject_type_nodeid)
# starting!
server.start()
try:
while True:
time.sleep(1)
finally:
示例8:
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_node [as 别名]
# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
# Example 1 - create a basic object
#-------------------------------------------------------------------------------
myobj = objects.add_object(idx, "MyObject")
#-------------------------------------------------------------------------------
# Example 2 - create a new object type and a instance of the new object type
#-------------------------------------------------------------------------------
types = server.get_node(ua.ObjectIds.BaseObjectType)
object_type_to_derive_from = server.get_root_node().get_child(["0:Types",
"0:ObjectTypes",
"0:BaseObjectType"])
mycustomobj_type = types.add_object_type(idx, "MyCustomObjectType")
mycustomobj_type.add_variable(0, "var_should_be_there_after_instantiate", 1.0) # demonstrates instantiate
myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid)
#-------------------------------------------------------------------------------
# Example 3 - import a new object from xml address space and create a instance of the new object type
#-------------------------------------------------------------------------------
# Import customobject type
server.import_xml('customobject.xml')