示例#1
0
    def test_events(self):
        msclt = MySubHandler()
        cond = msclt.setup()
        sub = self.opc.create_subscription(100, msclt)
        handle = sub.subscribe_events()

        ev = opcua.Event()
        msg = "this is my msg "
        ev.message = msg
        tid = datetime.datetime.now()
        ev.time = tid
        ev.source_node = self.opc.get_server_node().get_id()
        ev.source_name = "our server node"
        ev.severity = 500
        self.srv.trigger_event(ev)

        with cond:
            ret = cond.wait(50000)
        if sys.version_info.major > 2:
            self.assertEqual(
                ret,
                True)  # we went into timeout waiting for subcsription callback
        else:
            pass  # XXX
        self.assertIsNot(msclt.ev, None)  # we did not receive event
        self.assertEqual(msclt.ev.message, msg)
        self.assertEqual(msclt.ev.time.to_datetime(), tid)
        self.assertEqual(msclt.ev.severity, 500)
        self.assertEqual(msclt.ev.source_node,
                         self.opc.get_server_node().get_id())

        #time.sleep(0.1)
        sub.unsubscribe(handle)
        sub.delete()
示例#2
0
 def test_localized_text(self):
     event = opcua.Event()
     txt = "This is string"
     event.message = txt  #message is of type LocalizedText
     self.assertEqual(txt, event.message)
示例#3
0
        # get Objects node, this is where we should put our custom stuff
        objects = server.get_objects_node()
        print("I got objects folder: ", objects)

        # now adding some object to our addresse space
        myobject = objects.add_object(idx, "NewObject")
        myvar = myobject.add_variable(idx, "MyVariable", [16, 56])
        myprop = myobject.add_property(idx, "myprop", 9.9)
        myfolder = myobject.add_folder(idx, "myfolder")

        # uncomment next lines to subscribe to changes on server side
        #sclt = SubHandler()
        #sub = server.create_subscription(100, sclt)
        #handle = sub.subscribe_data_change(myvar) #keep handle if you want to delete the particular subscription later

        ev = opcua.Event()
        counter = 0
        while True:
            counter += 1
            ev.message = "This is event nr: " + str(counter)
            ev.source_node = objects.get_id()
            ev.time = datetime.datetime.now()
            print("Sending event: ", ev)
            server.trigger_event(ev)
            myvar.set_value([counter, counter + 10])
            time.sleep(1)
        # start ipython shell so users can test things
        #embed()
    finally:
        server.stop()