class QTZOCPNode(QtWidgets.QWidget): def __init__(self): super(QTZOCPNode, self).__init__() self.qle = QtWidgets.QTextEdit(self) self.qle.move(1, 1) self.qle.resize(640,480) self.init_zocp() self.show() def init_zocp(self): self.z = ZOCP() self.z.set_node_name("QT UI TEST") self.z.register_float("myFloat", 2.3, 'rw', 0, 5.0, 0.1) self.notifier = QtCore.QSocketNotifier( self.z.inbox.getsockopt(zmq.FD), QtCore.QSocketNotifier.Read ) self.notifier.setEnabled(True) self.notifier.activated.connect(self.zocp_event) self.z.on_modified = self.on_modified self.z.start() def zocp_event(self): print("ZOCP EVENT START") self.z.run_once(0) print("ZOCP EVENT END") def on_modified(self, peer, name, data, *args, **kwargs): t = self.qle.toPlainText() t = "{0}\n{1}".format(data, t) self.qle.setPlainText(t) def closeEvent(self, *args): print(args) self.z.stop()
class QTZOCPnumber(QtGui.QWidget): def __init__(self): super(QTZOCPnumber, self).__init__() # Set name of Node self.nodename = "QT LCDdisplay@%s" % socket.gethostname() self.init_zocp() self.initUI() def init_zocp(self): self.z = ZOCP() self.z.set_node_name(self.nodename) self.notifier = QtCore.QSocketNotifier( self.z.inbox.getsockopt(zmq.FD), QtCore.QSocketNotifier.Read ) self.notifier.setEnabled(True) self.notifier.activated.connect(self.zocp_event) self.z.on_peer_signaled = self.on_peer_signaled self.z.on_peer_enter = self.on_peer_enter self.z.start() def initUI(self): self.lcd1 = QtGui.QLCDNumber(self) self.lcd2 = QtGui.QLCDNumber(self) self.lcd3 = QtGui.QLCDNumber(self) # Register the ZOCP variables self.z.register_int("display1", 0, access='rs') self.z.register_int("display2", 0, access='rs') self.z.register_int("display3", 0, access='rs') grid = QtGui.QGridLayout() grid.setSpacing(10) grid.addWidget(self.lcd1, 1, 1) grid.addWidget(self.lcd2, 1, 2) grid.addWidget(self.lcd3, 1, 3) self.setLayout(grid) self.setGeometry(300, 300, 150, 70) self.setWindowTitle(self.nodename) self.show() def zocp_event(self): self.z.run_once(0) def on_peer_signaled(self, peer, name, data, *args, **kwargs): """ Called when a peer signals that some of its data is modified. peer: id of peer whose data has been changed name: name of peer whose data has been changed data: changed data, formatted as [emitter, value] emitter: name of the emitter on the subscribee value: value of the emitter """ zl.debug(" --> ZOCP PEER SIGNALED: %s modified %s" %(name, data)) # Check if there is data if(len(data) > 1): if(data[0] == "slider1"): self.lcd1.display(data[1]) elif(data[0] == "slider2"): self.lcd2.display(data[1]) elif(data[0] == "slider3"): self.lcd3.display(data[1]) def on_peer_enter(self, peer, name, *args, **kwargs): zl.debug(" -----> PEER ENTERED ",name," peer: ",peer) # This assumes that for testing the source to connect to is running on the same computer as the # the destination trying to connect to it is. sourceTargetName = "QT Sliders@%s" % socket.gethostname() if(name == sourceTargetName): self.z.signal_subscribe(self.z.get_uuid(), 'display1', peer, 'slider1') self.z.signal_subscribe(self.z.get_uuid(), 'display2', peer, 'slider2') self.z.signal_subscribe(self.z.get_uuid(), 'display3', peer, 'slider3') zl.debug("Nodes are subscribed") def closeEvent(self, *args): zl.debug(args) self.z.stop()
import gi from gi.repository import GObject from zocp import ZOCP import zmq GObject.threads_init() loop = GObject.MainLoop() z = ZOCP() z.set_node_name("GLibTest") z.register_percent('myPercent', 12, access='rw') def zocp_handle(*args, **kwargs): z.run_once() return True GObject.io_add_watch(z.inbox.getsockopt(zmq.FD), GObject.PRIORITY_DEFAULT, GObject.IO_IN, zocp_handle) z.start() try: loop.run() except Exception as e: print(e) finally: z.stop()