本文整理汇总了Python中opcua.Server.register_namespace方法的典型用法代码示例。如果您正苦于以下问题:Python Server.register_namespace方法的具体用法?Python Server.register_namespace怎么用?Python Server.register_namespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opcua.Server
的用法示例。
在下文中一共展示了Server.register_namespace方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mymain
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import register_namespace [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()
示例2: Server
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import register_namespace [as 别名]
# logger.setLevel(logging.DEBUG)
#logger = logging.getLogger("opcua.binary_server_asyncio")
# logger.setLevel(logging.DEBUG)
#logger = logging.getLogger("opcua.uaprocessor")
# logger.setLevel(logging.DEBUG)
# now setup our server
server = Server()
#server.disable_clock()
#server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/")
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
server.set_server_name("FreeOpcUa Example Server")
# setup our own namespace
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
# create a new node type we can instantiate in our address space
dev = server.nodes.base_object_type.add_object_type(0, "MyDevice")
dev.add_variable(0, "sensor1", 1.0)
dev.add_property(0, "device_id", "0340")
ctrl = dev.add_object(0, "controller")
ctrl.add_property(0, "state", "Idle")
# populating our address space
# First a folder to organise our nodes
myfolder = server.nodes.objects.add_folder(idx, "myEmptyFolder")
# instanciate one instance of our device
mydevice = server.nodes.objects.add_object(idx, "Device0001", dev)
mydevice_var = mydevice.get_child(["0:controller", "0:state"]) # get proxy to our device state variable
示例3: uaserver
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import register_namespace [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)
示例4: run
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import register_namespace [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
示例5: OPCServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import register_namespace [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