Hi
When the values of the spinboxes is set a message should be published from the
GUI containing the value from the spinbox which then sets the qlabel with the
value. After this if I close and reload the gui the widgets should be set with
the values set from the previous time. When the embedded.py file closes/stops
running the gui is reset to its default state when no spinbox and qlabel are
set with values.
First execute the embedded.py file. Enter 'laser' when 'devices to be
activated' appears. Once this executed a publish and subscribe should happen
and a laser actor python file starts which launches the laser device on the
embedded.py file. Once this is complete launch GUI.py in a separate command
line and click add device for the device in the combo box.
Here are the files needed.
embedded.py
import paho.mqtt.client as mqtt
from mqtt import *
import os
import time
import json
def start():
try:
os.remove("list_of_devices_currently_active.txt")
print("Awaiting devices to be activated")
except:
print("Awaiting devices to be activated")
start()
devices = list(map(str,input("Devices to be activated: ").split(",")))
client = device()
client.run()
client.loop_start()
print("Connected to broker")
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI")
client.subscribe("microscope/light_sheet_microscope/UI")
print("Publishing message to topic", "microscope/light_sheet_microscope/UI")
client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type":
"system", "payload":{"name": devices, "cmd": "activating devices"}}, indent=2))
time.sleep(1)
def active_devices():
for item in devices:
device = (item + "Embedded")
deviceImport = __import__(device)
with open("list_of_devices_currently_active.txt", "a+") as myfile:
for item in devices:
myfile.write(item + "\n")
active_devices()
def readFile(fname):
print("List of devices currently active:")
try:
with open(fname, "r") as f:
for item in f:
print(item.rstrip("\n"))
except:
print("No devices added yet")
readFile("list_of_devices_currently_active.txt")
client = device()
client.run()
client.loop_start()
print("Connected to broker")
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI/states")
client.subscribe("microscope/light_sheet_microscope/UI/states")
client.loop_forever()
mqtt.py
import logging
import paho.mqtt.client as mqtt
import json
class device(mqtt.Client):
def on_connect(self, mqtt, obj, flags, rc):
pass
def on_message(self, mqtt, userdata, message):
m_decode = str(message.payload.decode("utf-8"))
print("message recieved= " + m_decode)
# print("File which you want to import(with .py extension)")
print("message topic=", message.topic)
print("message qos=", message.qos)
print("message retain flag=", message.retain)
m_in = json.loads(m_decode)
def run(self):
self.connect("localhost", 1883, 60)
GUI.py
import paho.mqtt.client as mqtt
import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
import json
import time
class MainWindow(QtWidgets.QMainWindow):
def __init__(self,parent = None):
QMainWindow.__init__(self)
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
self.setMinimumSize(QSize(800, 600))
self.setWindowTitle("PyQt button example -
pythonprogramminglanguage.com")
client = device()
client.run()
client.loop_start()
print("Connected to broker")
time.sleep(1)
print("Subscribing to topic",
"microscope/light_sheet_microscope/UI/devices")
client.subscribe("microscope/light_sheet_microscope/UI/devices")
print("Publishing message to topic",
"microscope/light_sheet_microscope/UI/devices")
client.publish("microscope/light_sheet_microscope/UI/devices",
json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2))
time.sleep(1)
pybutton = QPushButton('Add device', self)
pybutton.clicked.connect(self.importbutton)
pybutton.move(100, 400)
pybutton.resize(150, 32)
self.combo = QComboBox(self)
self.combo.move(100,350)
self.combo.resize(100, 32)
def readFile(fname):
try:
with open(fname, "r") as f:
for item in f:
self.combo.addItem(item)
except:
print("No devices active")
readFile("list_of_devices_currently_active.txt")
def importbutton(self):
client = device()
client.run()
client.loop_start()
print("Connected to broker")
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI/add
device")
client.subscribe("microscope/light_sheet_microscope/UI/add device")
print("Publishing message to topic",
"microscope/light_sheet_microscope/UI/add device")
client.publish("microscope/light_sheet_microscope/UI/add device",
json.dumps({"type": "system", "payload":{"cmd": "init device panel"}},
indent=2))
time.sleep(1)
client.loop_stop()
self.fileName_UI = self.combo.currentText()
self.loadGUI()
print("Device panel initialised" + "\n")
def loadGUI(self):
module = __import__(self.fileName_UI.rstrip("\n"))
my_class = getattr(module, "SubWindow")
sub = QMdiSubWindow()
sub.setWidget(my_class())
sub.setWindowTitle(self.fileName_UI)
self.mdi.addSubWindow(sub)
sub.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
# publishedMessage = mainWin.getGUIFilename()
sys.exit(app.exec_())
laser.py
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from laser2 import *
from mqtt import *
import json
import time
import sys
class SubWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(SubWindow, self).__init__(parent)
self.setMinimumSize(QSize(379, 268))
self.ui = Ui_Laser()
self.ui.setupUi(self)
def main():
app = QtWidgets.QApplication(sys.argv)
application = SubWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
laser2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Laser(object):
def setupUi(self, Laser):
Laser.setObjectName("Laser")
Laser.resize(379, 274)
Laser.setMinimumSize(QtCore.QSize(379, 268))
self.centralwidget = QtWidgets.QWidget(Laser)
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_2.setMaximum(100)
self.spinBox_2.setObjectName("spinBox_2")
self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1)
self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_7.setObjectName("spinBox_7")
self.gridLayout.addWidget(self.spinBox_7, 1, 3, 1, 1)
self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_13.setObjectName("label_13")
self.gridLayout.addWidget(self.label_13, 0, 5, 1, 1)
self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 0, 3, 1, 1)
self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_4.setMaximum(100)
self.spinBox_4.setObjectName("spinBox_4")
self.gridLayout.addWidget(self.spinBox_4, 1, 4, 1, 1)
self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_5.setMaximum(100)
self.spinBox_5.setObjectName("spinBox_5")
self.gridLayout.addWidget(self.spinBox_5, 1, 5, 1, 1)
self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox.setMaximum(100)
self.spinBox.setObjectName("spinBox")
self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1)
self.label_12 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_12.setObjectName("label_12")
self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1)
self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_14.setObjectName("label_14")
self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1)
self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_6.setMaximum(100)
self.spinBox_6.setObjectName("spinBox_6")
self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1)
self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1)
self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_3.setMaximum(100)
self.spinBox_3.setObjectName("spinBox_3")
self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1)
self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1)
self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_8.setObjectName("label_8")
self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1)
- show quoted text -
self.spinBox.valueChanged['int'].connect(self.hi)
self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum)
self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum)
self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum)
self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum)
self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum)
self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum)
QtCore.QMetaObject.connectSlotsByName(Laser)
def hi(self):
self.spinBox.valueChanged['int'].connect(self.hi)
def retranslateUi(self, Laser):
_translate = QtCore.QCoreApplication.translate
Laser.setWindowTitle(_translate("Laser", "MainWindow"))
self.label_13.setText(_translate("Laser", " %"))
self.label_11.setText(_translate("Laser", " %"))
self.label_12.setText(_translate("Laser", " %"))
self.label_14.setText(_translate("Laser", " %"))
self.label_10.setText(_translate("Laser", " %"))
self.label_9.setText(_translate("Laser", " %"))
self.label_8.setText(_translate("Laser", " %"))
self.pushButton.setText(_translate("Laser", "ON"))
self.pushButton_2.setText(_translate("Laser", "ON"))
self.pushButton_3.setText(_translate("Laser", "ON"))
self.pushButton_4.setText(_translate("Laser", "ON"))
self.pushButton_5.setText(_translate("Laser", "ON"))
self.pushButton_6.setText(_translate("Laser", "ON"))
self.pushButton_7.setText(_translate("Laser", "ON"))
self.label.setText(_translate("Laser", "445nm"))
self.label_2.setText(_translate("Laser", "488nm"))
self.label_3.setText(_translate("Laser", "515nm"))
self.label_4.setText(_translate("Laser", "561nm"))
self.label_5.setText(_translate("Laser", "594nm"))
self.label_6.setText(_translate("Laser", "638nm"))
self.label_7.setText(_translate("Laser", "LED"))
self.pushButton_8.setText(_translate("Laser", "ON"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Laser = QtWidgets.QMainWindow()
ui = Ui_Laser()
ui.setupUi(Laser)
Laser.show()
sys.exit(app.exec_())
laserEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Laser(Actor):
async def handle_message(self, message: Message):
print("Laser")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a laser!"
+"\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with Laser() as laser:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(laser, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())
Regards
Spencer
--
https://mail.python.org/mailman/listinfo/python-list