def publish_callback(self, publishresult): self.logger.info("Publish callback called with result: %s", publishresult) while self.subscription_id is None: time.sleep(0.01) for notif in publishresult.NotificationMessage.NotificationData: if notif.TypeId == ua.FourByteNodeId( ua.ObjectIds.DataChangeNotification_Encoding_DefaultBinary ): datachange = ua.DataChangeNotification.from_binary( io.BytesIO(notif.to_binary())) self._call_datachange(datachange) elif notif.TypeId == ua.FourByteNodeId( ua.ObjectIds.EventNotificationList_Encoding_DefaultBinary): eventlist = ua.EventNotificationList.from_binary( io.BytesIO(notif.to_binary())) self._call_event(eventlist) elif notif.TypeId == ua.FourByteNodeId( ua.ObjectIds. StatusChangeNotification_Encoding_DefaultBinary): statuschange = ua.StatusChangeNotification.from_binary( io.BytesIO(notif.to_binary())) self._call_status(statuschange) else: self.logger.warning( "Notification type not supported yet for notification %s", notif) ack = ua.SubscriptionAcknowledgement() ack.SubscriptionId = self.subscription_id ack.SequenceNumber = publishresult.NotificationMessage.SequenceNumber self.server.publish([ack])
def instanciate_node(parent, node_type, idx): """ Instanciate a new node under 'parent' using a type """ results = node_type.get_attributes([ ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName ]) nclass, bname, dname = [res.Value.Value for res in results] #descs = node_type.get_children_descriptions(refs=ua.ObjectIds.HasTypeDefinition) typedef = ua.FourByteNodeId(ua.ObjectIds.BaseObjectType) #if len(descs) > 1: #print("DESCS", descs) #typedef = descs[0].TypeDefinition rdesc = ua.ReferenceDescription() rdesc.NodeId = node_type.nodeid rdesc.BrowseName = bname rdesc.DisplayName = dname rdesc.NodeClass = nclass rdesc.ReferenceTypeId = ua.TwoByteNodeId(ua.ObjectIds.HasComponent) rdesc.TypeDefinition = typedef print("MYRDESC", rdesc) return _instanciate_node(parent.server, parent.nodeid, rdesc, idx)
def test_nodeid(self): nid = ua.NodeId() self.assertEqual(nid.NodeIdType, ua.NodeIdType.TwoByte) nid = ua.NodeId(446, 3, ua.NodeIdType.FourByte) self.assertEqual(nid.NodeIdType, ua.NodeIdType.FourByte) d = nodeid_to_binary(nid) new_nid = nodeid_from_binary(io.BytesIO(d)) self.assertEqual(new_nid, nid) self.assertEqual(new_nid.NodeIdType, ua.NodeIdType.FourByte) self.assertEqual(new_nid.Identifier, 446) self.assertEqual(new_nid.NamespaceIndex, 3) tb = ua.TwoByteNodeId(53) fb = ua.FourByteNodeId(53) n = ua.NumericNodeId(53) n1 = ua.NumericNodeId(53, 0) s1 = ua.StringNodeId("53", 0) bs = ua.ByteStringNodeId(b"53", 0) gid = uuid.uuid4() g = ua.ByteStringNodeId(str(gid), 0) guid = ua.GuidNodeId(gid) self.assertEqual(tb, fb) self.assertEqual(tb, n) self.assertEqual(tb, n1) self.assertEqual(n1, fb) self.assertNotEqual(g, guid) self.assertEqual(tb, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(tb)))) self.assertEqual(fb, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(fb)))) self.assertEqual(n, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(n)))) self.assertEqual(s1, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(s1)))) self.assertEqual(bs, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(bs)))) self.assertEqual(guid, nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(guid))))
def test_nodeid(self): nid = ua.NodeId() self.assertEqual(nid.NodeIdType, ua.NodeIdType.TwoByte) nid = ua.NodeId(446, 3, ua.NodeIdType.FourByte) self.assertEqual(nid.NodeIdType, ua.NodeIdType.FourByte) d = nid.to_binary() new_nid = nid.from_binary(io.BytesIO(d)) self.assertEqual(new_nid, nid) self.assertEqual(new_nid.NodeIdType, ua.NodeIdType.FourByte) self.assertEqual(new_nid.Identifier, 446) self.assertEqual(new_nid.NamespaceIndex, 3) tb = ua.TwoByteNodeId(53) fb = ua.FourByteNodeId(53) n = ua.NumericNodeId(53) n1 = ua.NumericNodeId(53, 0) s = ua.StringNodeId(53, 0) # should we raise an exception??? s1 = ua.StringNodeId("53", 0) bs = ua.ByteStringNodeId(b"53", 0) gid = ua.Guid() g = ua.ByteStringNodeId(gid, 0) self.assertEqual(tb, fb) self.assertEqual(tb, n) self.assertEqual(tb, n1) self.assertEqual(n1, fb) self.assertNotEqual(n1, s) self.assertNotEqual(s, bs) self.assertNotEqual(s, g)
def check_answer(self, data, context): data = data.copy() typeid = ua.NodeId.from_binary(data) if typeid == ua.FourByteNodeId(ua.ObjectIds.ServiceFault_Encoding_DefaultBinary): self.logger.warning("ServiceFault from server received %s", context) hdr = ua.ResponseHeader.from_binary(data) hdr.ServiceResult.check() return False return True
def reconciliate_subscription(self, subscription): """ Reconciliate the server state with the client """ node = self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server)) # returns server and client handles monitored_items = node.call_method( ua.uatypes.QualifiedName("GetMonitoredItems"), ua.Variant(subscription.subscription_id, ua.VariantType.UInt32)) return subscription.reconciliate(monitored_items)
def run(self): self.logger.debug("starting keepalive thread with period of %s milliseconds", self.timeout) server_state = self.client.get_node(ua.FourByteNodeId(ua.ObjectIds.Server_ServerStatus_State)) while not self._dostop: with self._cond: self._cond.wait(self.timeout / 1000) if self._dostop: break self.logger.debug("renewing channel") self.client.open_secure_channel(renew=True) val = server_state.get_value() self.logger.debug("server state is: %s ", val) self.logger.debug("keepalive thread has stopped")
def activate_session(self, params): self.logger.info("activate session") result = ua.ActivateSessionResult() if not self.state == SessionState.Created: raise utils.ServiceError(ua.StatusCodes.BadSessionIdInvalid) result.ServerNonce = self.nonce for _ in params.ClientSoftwareCertificates: result.Results.append(ua.StatusCode()) self.state = SessionState.Activated id_token = ua.downcast_extobject(params.UserIdentityToken) if id_token.TypeId == ua.FourByteNodeId( ua.ObjectIds.UserNameIdentityToken_Encoding_DefaultBinary): if self.iserver.allow_remote_admin and id_token.UserName in ( "admin", "Admin"): self.user = User.Admin return result
def run(self): _logger.debug("starting keepalive thread with period of %s milliseconds", self.timeout) server_state = self.client.get_node(ua.FourByteNodeId(ua.ObjectIds.Server_ServerStatus_State)) while not self._dostop: with self._cond: self._cond.wait(self.timeout / 1000) if self._dostop: break _logger.debug("renewing channel") try: self.client.open_secure_channel(renew=True) except concurrent.futures.TimeoutError: _logger.debug("keepalive failed: timeout on open_secure_channel()") break val = server_state.get_value() _logger.debug("server state is: %s ", val) _logger.debug("keepalive thread has stopped")
def test_service_fault(self): request = ua.ReadRequest() request.TypeId = ua.FourByteNodeId(999) # bad type! with self.assertRaises(ua.UaStatusCodeError): self.clt.uaclient._uasocket.send_request(request)
def get_server_node(self): return self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server))
def get_server_node(self): """ Get Server node of server. Returns a Node object. """ return self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server))
def test_service_fault(self): request = ua.ReadRequest() request.TypeId = ua.FourByteNodeId(999) # bad type! with self.assertRaises(Exception): self.clt.bclient._send_request(request)