Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def runTest(self):
return # FIXME broken
tmpfile = NamedTemporaryFile()
path = tmpfile.name
tmpfile.close()
# create cache file
server = Server(shelffile=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(shelffile=path)
self.assertEqual(server.get_node(id).get_value(), 123)
os.remove(path)
self.assertEqual(v.get_value(), 6.78)
self.assertEqual(v.get_data_type(), ua.NodeId(ua.ObjectIds.Double))
self.assertEqual(a.get_data_type(), ua.NodeId(ua.ObjectIds.UInt16))
self.assertIn(a.get_value_rank(), (0, 1))
self.assertEqual(a.get_value(), [6, 1])
self.assertEqual(a2.get_value(), [[1, 2], [3, 4]])
self.assertEqual(a2.get_data_type(), ua.NodeId(ua.ObjectIds.UInt32))
self.assertIn(a2.get_value_rank(), (0, 2))
self.assertEqual(a2.get_attribute(ua.AttributeIds.ArrayDimensions).Value.Value, [2, 2])
# self.assertEqual(a3.get_value(), [[]]) # would require special code ...
self.assertEqual(a3.get_data_type(), ua.NodeId(ua.ObjectIds.ByteString))
self.assertIn(a3.get_value_rank(), (0, 2))
self.assertEqual(a3.get_attribute(ua.AttributeIds.ArrayDimensions).Value.Value, [1, 0])
nodes = [o, v, a, a2, a3]
self.opc.export_xml(nodes, "tmp_test_export-vars.xml")
self.opc.delete_nodes(nodes)
self.opc.import_xml("tmp_test_export-vars.xml")
self.assertEqual(v.get_value(), 6.78)
self.assertEqual(v.get_data_type(), ua.NodeId(ua.ObjectIds.Double))
self.assertEqual(a.get_data_type(), ua.NodeId(ua.ObjectIds.UInt16))
self.assertIn(a.get_value_rank(), (0, 1))
self.assertEqual(a.get_value(), [6, 1])
self.assertEqual(a2.get_value(), [[1, 2], [3, 4]])
self.assertEqual(a2.get_data_type(), ua.NodeId(ua.ObjectIds.UInt32))
self.assertIn(a2.get_value_rank(), (0, 2))
self.assertEqual(a2.get_attribute(ua.AttributeIds.ArrayDimensions).Value.Value, [2, 2])
# self.assertEqual(a3.get_value(), [[]]) # would require special code ...
self.assertEqual(a3.get_data_type(), ua.NodeId(ua.ObjectIds.ByteString))
self.assertIn(a3.get_value_rank(), (0, 2))
self.assertEqual(a3.get_attribute(ua.AttributeIds.ArrayDimensions).Value.Value, [1, 0])
self.add_variable_common(node, var_el)
accesslevel = node.get_attribute(ua.AttributeIds.AccessLevel).Value.Value
useraccesslevel = node.get_attribute(ua.AttributeIds.UserAccessLevel).Value.Value
# We only write these values if they are different from defaults
# Not sure where default is defined....
if accesslevel not in (0, ua.AccessLevel.CurrentRead.mask):
var_el.attrib["AccessLevel"] = str(accesslevel)
if useraccesslevel not in (0, ua.AccessLevel.CurrentRead.mask):
var_el.attrib["UserAccessLevel"] = str(useraccesslevel)
var = node.get_attribute(ua.AttributeIds.MinimumSamplingInterval)
if var.Value.Value:
var_el.attrib["MinimumSamplingInterval"] = str(var.Value.Value)
var = node.get_attribute(ua.AttributeIds.Historizing)
if var.Value.Value:
var_el.attrib["Historizing"] = 'true'
def _lsprint_long(pnode, depth, indent=""):
if not indent:
print("{0:30} {1:25} {2:25} {3:10} {4:30} {5:25}".format("DisplayName", "NodeId", "BrowseName", "DataType", "Timestamp", "Value"))
print("")
for node in pnode.get_children():
attrs = node.get_attributes([ua.AttributeIds.DisplayName,
ua.AttributeIds.BrowseName,
ua.AttributeIds.NodeClass,
ua.AttributeIds.WriteMask,
ua.AttributeIds.UserWriteMask,
ua.AttributeIds.DataType,
ua.AttributeIds.Value])
name, bname, nclass, mask, umask, dtype, val = [attr.Value.Value for attr in attrs]
update = attrs[-1].ServerTimestamp
if nclass == ua.NodeClass.Variable:
print("{0}{1:30} {2:25} {3:25} {4:10} {5!s:30} {6!s:25}".format(indent, name.to_string(), node.nodeid.to_string(), bname.to_string(), dtype.to_string(), update, val))
else:
print("{0}{1:30} {2:25} {3:25}".format(indent, name.to_string(), bname.to_string(), node.nodeid.to_string()))
if depth:
_lsprint_long(node, depth - 1, indent + " ")
def read(self, parameters):
self.logger.info("read")
request = ua.ReadRequest()
request.Parameters = parameters
data = self._uasocket.send_request(request)
response = ua.ReadResponse.from_binary(data)
self.logger.debug(response)
response.ResponseHeader.ServiceResult.check()
# cast to Enum attributes that need to
for idx, rv in enumerate(parameters.NodesToRead):
if rv.AttributeId == ua.AttributeIds.NodeClass:
dv = response.Results[idx]
if dv.StatusCode.is_good():
dv.Value.Value = ua.NodeClass(dv.Value.Value)
elif rv.AttributeId == ua.AttributeIds.ValueRank:
dv = response.Results[idx]
if dv.StatusCode.is_good() and dv.Value.Value in (-3, -2, -1, 0, 1, 2, 3, 4):
dv.Value.Value = ua.ValueRank(dv.Value.Value)
return response.Results
def _rdesc_from_node(parent, node):
results = node.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName])
nclass, qname, dname = [res.Value.Value for res in results]
rdesc = ua.ReferenceDescription()
rdesc.NodeId = node.nodeid
rdesc.BrowseName = qname
rdesc.DisplayName = dname
rdesc.NodeClass = nclass
if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType):
rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes)
else:
rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent)
typedef = node.get_type_definition()
if typedef:
rdesc.TypeDefinition = typedef
return rdesc
def _lsprint_long(pnode, depth, indent=""):
if not indent:
print("{0:30} {1:25} {2:25} {3:10} {4:30} {5:25}".format("DisplayName", "NodeId", "BrowseName", "DataType", "Timestamp", "Value"))
print("")
for node in pnode.get_children():
attrs = node.get_attributes([ua.AttributeIds.DisplayName,
ua.AttributeIds.BrowseName,
ua.AttributeIds.NodeClass,
ua.AttributeIds.WriteMask,
ua.AttributeIds.UserWriteMask,
ua.AttributeIds.DataType,
ua.AttributeIds.Value])
name, bname, nclass, mask, umask, dtype, val = [attr.Value.Value for attr in attrs]
update = attrs[-1].ServerTimestamp
if nclass == ua.NodeClass.Variable:
print("{0}{1:30} {2:25} {3:25} {4:10} {5!s:30} {6!s:25}".format(indent, name.to_string(), node.nodeid.to_string(), bname.to_string(), dtype.to_string(), update, val))
else:
print("{0}{1:30} {2:25} {3:25}".format(indent, name.to_string(), bname.to_string(), node.nodeid.to_string()))
if depth:
_lsprint_long(node, depth - 1, indent + " ")
def uaread():
parser = argparse.ArgumentParser(description="Read attribute of a node, per default reads value of a node")
add_common_args(parser)
parser.add_argument("-a",
"--attribute",
dest="attribute",
type=int,
default=ua.AttributeIds.Value,
help="Set attribute to read")
parser.add_argument("-t",
"--datatype",
dest="datatype",
default="python",
choices=['python', 'variant', 'datavalue'],
help="Data type to return")
args = parse_args(parser, requirenodeid=True)
client = Client(args.url, timeout=args.timeout)
client.set_security_string(args.security)
client.connect()
try:
node = get_node(client, args)
def add_etree_variable(self, node):
"""
Add a UA variable element to the XML etree
"""
var_el = self._add_node_common("UAVariable", node)
self._add_ref_els(var_el, node)
self.add_variable_common(node, var_el)
accesslevel = node.get_attribute(ua.AttributeIds.AccessLevel).Value.Value
useraccesslevel = node.get_attribute(ua.AttributeIds.UserAccessLevel).Value.Value
# We only write these values if they are different from defaults
# Not sure where default is defined....
if accesslevel not in (0, ua.AccessLevel.CurrentRead.mask):
var_el.attrib["AccessLevel"] = str(accesslevel)
if useraccesslevel not in (0, ua.AccessLevel.CurrentRead.mask):
var_el.attrib["UserAccessLevel"] = str(useraccesslevel)
var = node.get_attribute(ua.AttributeIds.MinimumSamplingInterval)
if var.Value.Value:
var_el.attrib["MinimumSamplingInterval"] = str(var.Value.Value)
var = node.get_attribute(ua.AttributeIds.Historizing)
if var.Value.Value:
var_el.attrib["Historizing"] = 'true'