本文整理汇总了Python中opcua.Server.get_objects_node方法的典型用法代码示例。如果您正苦于以下问题:Python Server.get_objects_node方法的具体用法?Python Server.get_objects_node怎么用?Python Server.get_objects_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opcua.Server
的用法示例。
在下文中一共展示了Server.get_objects_node方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
class HelloServer:
def __init__(self, endpoint, name, model_filepath):
self.server = Server()
# This need to be imported at the start or else it will overwrite the data
self.server.import_xml(model_filepath)
self.server.set_endpoint(endpoint)
self.server.set_server_name(name)
objects = self.server.get_objects_node()
freeopcua_namespace = self.server.get_namespace_index("urn:freeopcua:python:server")
hellower = objects.get_child("0:Hellower")
hellower_say_hello = hellower.get_child("0:SayHello")
self.server.link_method(hellower_say_hello, say_hello_xml)
hellower.add_method(
freeopcua_namespace, "SayHello2", say_hello, [ua.VariantType.Boolean], [ua.VariantType.String])
hellower.add_method(
freeopcua_namespace, "SayHelloArray", say_hello_array, [ua.VariantType.Boolean], [ua.VariantType.String])
def __enter__(self):
self.server.start()
return self.server
def __exit__(self, exc_type, exc_val, exc_tb):
self.server.stop()
示例2: TestCmdLines
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
class TestCmdLines(unittest.TestCase):
'''
Test command lines
'''
@classmethod
def setUpClass(self):
self.srv = Server()
self.srv_url = 'opc.tcp://localhost:%d' % port_num
self.srv.set_endpoint(self.srv_url)
objects = self.srv.get_objects_node()
obj = objects.add_object(4, "directory")
var = obj.add_variable(4, "variable", 1.999)
var2 = obj.add_variable(4, "variable2", 1.777)
var2.set_writable()
self.srv.start()
def test_uals(self):
s = subprocess.check_output(["python", "tools/uals", "--url", self.srv_url])
self.assertIn(b"i=85", s)
self.assertNotIn(b"i=89", s)
self.assertNotIn(b"1.999", s)
s = subprocess.check_output(["python", "tools/uals", "--url", self.srv_url, "-d", "3"])
self.assertIn(b"1.999", s)
def test_uaread(self):
s = subprocess.check_output(["python", "tools/uaread", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable"])
self.assertIn(b"1.999", s)
def test_uawrite(self):
s = subprocess.check_output(["python", "tools/uawrite", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable2", "1.789"])
s = subprocess.check_output(["python", "tools/uaread", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable2"])
self.assertIn(b"1.789", s)
self.assertNotIn(b"1.999", s)
def test_uadiscover(self):
s = subprocess.check_output(["python", "tools/uadiscover", "--url", self.srv_url])
self.assertIn(b"opc.tcp://localhost", s)
self.assertIn(b"FreeOpcUa", s)
self.assertIn(b"urn:freeopcua:python:server", s)
@classmethod
def tearDownClass(self):
self.srv.stop()
示例3: mymain
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
def mymain():
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# 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 nodes
objects = server.get_objects_node()
# populating our address space
myobj = objects.add_object(idx, "MyObject")
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myvar.set_writable() # Set MyVariable to be writable by clients
# starting!
server.start()
server.stop()
示例4: Server
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
from opcua import ua, Server
from opcua.server.history_sql import HistorySQLite
if __name__ == "__main__":
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# 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()
# populating our address space
myobj = objects.add_object(idx, "MyObject")
myvar = myobj.add_variable(idx, "MyVariable", ua.Variant(0, ua.VariantType.Double))
myvar.set_writable() # Set MyVariable to be writable by clients
# 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)])
# create second event
etype2 = server.create_custom_event_type(2, 'MySecondEvent', ua.ObjectIds.BaseEventType,
[('MyOtherProperty', ua.VariantType.Float)])
示例5: uaserver
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
def uaserver():
parser = argparse.ArgumentParser(description="Run an example OPC-UA server. By importing xml definition and using uawrite command line, it is even possible to expose real data using this server")
# we setup a server, this is a bit different from other tool so we do not reuse common arguments
parser.add_argument("-u",
"--url",
help="URL of OPC UA server, default is opc.tcp://0.0.0.0:4840",
default='opc.tcp://0.0.0.0:4840',
metavar="URL")
parser.add_argument("-v",
"--verbose",
dest="loglevel",
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default='WARNING',
help="Set log level")
parser.add_argument("-x",
"--xml",
metavar="XML_FILE",
help="Populate address space with nodes defined in XML")
parser.add_argument("-p",
"--populate",
action="store_true",
help="Populate address space with some sample nodes")
parser.add_argument("-c",
"--disable-clock",
action="store_true",
help="Disable clock, to avoid seeing many write if debugging an application")
parser.add_argument("-s",
"--shell",
action="store_true",
help="Start python shell instead of randomly changing node values")
parser.add_argument("--certificate",
help="set server certificate")
parser.add_argument("--private_key",
help="set server private key")
args = parser.parse_args()
logging.basicConfig(format="%(levelname)s: %(message)s", level=getattr(logging, args.loglevel))
server = Server()
server.set_endpoint(args.url)
if args.certificate:
server.load_certificate(args.certificate)
if args.private_key:
server.load_private_key(args.private_key)
server.disable_clock(args.disable_clock)
server.set_server_name("FreeOpcUa Example Server")
if args.xml:
server.import_xml(args.xml)
if args.populate:
@uamethod
def multiply(parent, x, y):
print("multiply method call with parameters: ", x, y)
return x * y
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
myobj = objects.add_object(idx, "MyObject")
mywritablevar = myobj.add_variable(idx, "MyWritableVariable", 6.7)
mywritablevar.set_writable() # Set MyVariable to be writable by clients
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myarrayvar = myobj.add_variable(idx, "MyVarArray", [6.7, 7.9])
myprop = myobj.add_property(idx, "MyProperty", "I am a property")
mymethod = myobj.add_method(idx, "MyMethod", multiply, [ua.VariantType.Double, ua.VariantType.Int64], [ua.VariantType.Double])
server.start()
try:
if args.shell:
embed()
elif args.populate:
count = 0
while True:
time.sleep(1)
myvar.set_value(math.sin(count / 10))
myarrayvar.set_value([math.sin(count / 10), math.sin(count / 100)])
count += 1
else:
while True:
time.sleep(1)
finally:
server.stop()
sys.exit(0)
示例6: run
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
def run(nodejson, endpoint, namespace, description):
# now setup our server
server = Server()
#server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/")
server.set_endpoint(endpoint)
server.set_server_name(description)
# setup our own namespace
uri = namespace
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
variables = {}
nodeId = 5
def add_variable(f, vals, nodeId):
for x in vals:
k = x if isinstance(x, str) or isinstance(x, unicode) else x.keys()[0]
#v = f.add_variable("ns=6;i=600%d" % nodeId, k, ua.Variant([], ua.VariantType.Float))
v = f.add_variable("ns=6;i=600%d" % nodeId, x, ua.Variant([], ua.VariantType.Float))
# nodeid, browsename, value, [variant type]
#v = f.add_variable("ns=6;s=%s" % x, x, ua.Variant([], ua.VariantType.Float))
v.set_writable()
variables[k] = v
nodeId = nodeId+1
return nodeId
def add_folder(p, pid, val, nodeId):
for n, v in val.items():
mf = p.add_folder("ns=%d;i=%d00%d" % (pid, pid, nodeId), n)
#mf = p.add_folder("ns=%d;s=%s" % (pid, n), n)
if isinstance(v, list) or isinstance(v, tuple):
nodeId = add_variable(mf, v, nodeId+1)
else:
nodeId = add_folder(mf, pid+1, v, nodeId+1)
return nodeId
# populating our address space
add_folder(objects, 5, nodejson, nodeId)
'''
myobj = objects.add_object(idx, "MyObject")
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myvar.set_writable() # Set MyVariable to be writable by clients
myarrayvar = myobj.add_variable(idx, "myarrayvar", [6.7, 7.9])
myarrayvar = myobj.add_variable(idx, "myStronglytTypedVariable", ua.Variant([], ua.VariantType.UInt32))
myprop = myobj.add_property(idx, "myproperty", "I am a property")
mymethod = myobj.add_method(idx, "mymethod", func, [ua.VariantType.Int64], [ua.VariantType.Boolean])
inargx = ua.Argument()
inargx.Name = "x"
inargx.DataType = ua.NodeId(ua.ObjectIds.Int64)
inargx.ValueRank = -1
inargx.ArrayDimensions = []
inargx.Description = ua.LocalizedText("First number x")
inargy = ua.Argument()
inargy.Name = "y"
inargy.DataType = ua.NodeId(ua.ObjectIds.Int64)
inargy.ValueRank = -1
inargy.ArrayDimensions = []
inargy.Description = ua.LocalizedText("Second number y")
outarg = ua.Argument()
outarg.Name = "Result"
outarg.DataType = ua.NodeId(ua.ObjectIds.Int64)
outarg.ValueRank = -1
outarg.ArrayDimensions = []
outarg.Description = ua.LocalizedText("Multiplication result")
multiply_node = myobj.add_method(idx, "multiply", multiply, [inargx, inargy], [outarg])
'''
# starting!
server.start()
#print("Available loggers are: ", logging.Logger.manager.loggerDict.keys())
return server, variables
示例7: OPCServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import get_objects_node [as 别名]
class OPCServer(object):
def __init__(self, settings):
super(OPCServer, self).__init__()
server_settings = settings.get("server")
objects = settings.get("objects")
self._endpoint = server_settings["endpoint"]
self._name = server_settings["name"]
self._namespace = server_settings["namespace"]
self._init_server()
for o in objects:
self._add_object(o["name"])
for variable, value in o["ro_variables"].items():
self._add_variable(o["name"], variable, value)
for variable, value in o["rw_variables"].items():
self._add_variable(o["name"], variable, value, True)
def start(self):
if self._server:
self._server.start()
def _init_server(self):
self._server = Server()
self._server.set_endpoint(self._endpoint)
self._server.set_server_name(self._name)
self._index = self._server.register_namespace(self._namespace)
_log.info("Index: %r" % (self._index,))
self._objects_node = self._server.get_objects_node()
_log.info("Node: %r" % (self._objects_node))
self._objects = {}
_log.info("Endpoints: %r" % (self._server.get_endpoints()))
def stop(self):
self._server.stop()
def _add_object(self, object_name):
self._objects[object_name] = { 'object': self._objects_node.add_object(self._index, object_name), 'variables' : {}}
def _add_variable(self, object_name, variable_name, value, writable=False):
obj = self._objects.get(object_name, None)
if obj.get('object', None):
var = self._objects_node.add_variable(self._index, variable_name, str(value))
obj['variables'][variable_name] = var
if writable:
var.set_writable()
return True
return False
def _get_variable(self, object_name, variable_name):
obj = self._objects.get(object_name)
if obj.get('object', None):
var = obj['variables'].get(variable_name, None)
return var
return None
def set_value(self, object_name, variable_name, value):
var = self._get_variable(object_name, variable_name)
if var:
var.set_value(str(value))
return True
return False
def get_value(self, object_name, variable_name):
var = self._get_variable(object_name, variable_name)
if var:
return var.get_value()
return None