Commit 0dd0d36c authored by Spencer Du's avatar Spencer Du

class and functions added to embedded.py

parent ec22b789
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import paho.mqtt.client as mqtt\n",
"import os\n",
"import sys\n",
"import PyQt5\n",
"from PyQt5.QtWidgets import *\n",
"from PyQt5.QtCore import *\n",
"from PyQt5 import QtWidgets, uic, QtCore\n",
"from mqtt import *\n",
"import json\n",
"import time\n",
"\n",
"if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):\n",
"\tPyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)\n",
"if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):\n",
"\tPyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)\n",
"\n",
"class MainWindow(QtWidgets.QMainWindow):\n",
" def __init__(self,parent = None):\n",
" QMainWindow.__init__(self)\n",
" super(MainWindow, self).__init__(parent)\n",
" self.mdi = QMdiArea()\n",
" self.setCentralWidget(self.mdi)\n",
"\n",
" self.setMinimumSize(QSize(800, 600))\n",
" self.setWindowTitle(\"PyQt button example - pythonprogramminglanguage.com\")\n",
"\n",
" client = device()\n",
" client.run()\n",
"\n",
" client.loop_start()\n",
" print(\"Connected to broker\")\n",
" time.sleep(1)\n",
" print(\"Subscribing to topic\", \"microscope/light_sheet_microscope/UI/devices\")\n",
" client.subscribe(\"microscope/light_sheet_microscope/UI/devices\")\n",
" print(\"Publishing message to topic\", \"microscope/light_sheet_microscope/UI/devices\")\n",
" client.publish(\"microscope/light_sheet_microscope/UI/devices\", json.dumps({\"type\": \"system\", \"payload\":{\"cmd\": \"get all devices\"}}, indent=2))\n",
" time.sleep(1)\n",
" \n",
" pybutton = QPushButton('Add device', self)\n",
"\n",
" pybutton.clicked.connect(self.importbutton)\n",
"\n",
" pybutton.move(100, 400)\n",
" pybutton.resize(150, 32)\n",
"\n",
" self.combo = QComboBox(self) \n",
" self.combo.move(100,350)\n",
" self.combo.resize(100, 32)\n",
"\n",
" menubar = self.menuBar()\n",
" devicesMenu = menubar.addMenu(\"Devices\")\n",
"\n",
" def readFile(fname):\n",
" try:\n",
" with open(fname, \"r\") as f:\n",
" for item in f:\n",
" self.combo.addItem(item, self.importbutton)\n",
" except:\n",
" print(\"No devices active\")\n",
" readFile(\"list_of_device(s)_currently_active.txt\") \n",
" \n",
" def importbutton(self):\n",
" if not os.path.exists(\"laser.ini\"):\n",
" client = device()\n",
" client.run()\n",
"\n",
" client.loop_start()\n",
" print(\"\\n\" + \"Connected to broker\")\n",
" time.sleep(1)\n",
" print(\"Subscribing to topic\", \"microscope/light_sheet_microscope/UI/add device\")\n",
" client.subscribe(\"microscope/light_sheet_microscope/UI/add device\")\n",
" print(\"Publishing message to topic\", \"microscope/light_sheet_microscope/UI/add device\")\n",
" client.publish(\"microscope/light_sheet_microscope/UI/add device\", json.dumps({\"type\": \"system\", \"payload\":{\"cmd\": \"init device panel\"}}, indent=2))\n",
" time.sleep(1)\n",
" client.loop_stop() \n",
" self.fileName_UI = self.combo.currentText()\n",
" self.loadGUI()\n",
" print(\"Device panel initialised\" + \"\\n\")\n",
" else:\n",
" if os.path.exists(\"laser.ini\"):\n",
" client = device()\n",
" client.run()\n",
"\n",
" client.loop_start()\n",
" print(\"\\n\" + \"Connected to broker\")\n",
" time.sleep(1)\n",
" print(\"Subscribing to topic\", \"microscope/light_sheet_microscope/UI/laser\")\n",
" client.subscribe(\"microscope/light_sheet_microscope/UI/laser\")\n",
" print(\"Publishing message to topic\", \"microscope/light_sheet_microscope/UI/laser\")\n",
" client.publish(\"microscope/light_sheet_microscope/UI/laser\", json.dumps({\"type\": \"device\", \"payload\":{\"name\": \"laser\", \"cmd\": \"set config\"}}, indent=2))\n",
" time.sleep(1)\n",
" client.loop_stop()\n",
" self.fileName_UI = self.combo.currentText()\n",
" self.loadGUI()\n",
" print(\"Laser config set\" + \"\\n\")\n",
"\n",
" def loadGUI(self):\n",
" module = __import__(self.fileName_UI.rstrip(\"\\n\"))\n",
" my_class = getattr(module, \"SubWindow\")\n",
" \n",
" sub = QMdiSubWindow()\n",
" \n",
" sub.setWidget(my_class())\n",
" sub.setWindowTitle(self.fileName_UI)\n",
" self.mdi.addSubWindow(sub)\n",
" sub.show()\n",
"\n",
"if __name__ == \"__main__\":\n",
" app = QApplication(sys.argv)\n",
" mainWin = MainWindow()\n",
" mainWin.show()\n",
" # publishedMessage = mainWin.getGUIFilename()\n",
" sys.exit(app.exec_())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import paho.mqtt.client as mqtt
import os
import sys
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic, QtCore
from mqtt import *
import json
import time
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
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)
menubar = self.menuBar()
devicesMenu = menubar.addMenu("Devices")
def readFile(fname):
try:
with open(fname, "r") as f:
for item in f:
self.combo.addItem(item, self.importbutton)
except:
print("No devices active")
readFile("list_of_device(s)_currently_active.txt")
def importbutton(self):
if not os.path.exists("laser.ini"):
client = device()
client.run()
client.loop_start()
print("\n" + "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")
else:
if os.path.exists("laser.ini"):
client = device()
client.run()
client.loop_start()
print("\n" + "Connected to broker")
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI/laser")
client.subscribe("microscope/light_sheet_microscope/UI/laser")
print("Publishing message to topic", "microscope/light_sheet_microscope/UI/laser")
client.publish("microscope/light_sheet_microscope/UI/laser", json.dumps({"type": "device", "payload":{"name": "laser", "cmd": "set config"}}, indent=2))
time.sleep(1)
client.loop_stop()
self.fileName_UI = self.combo.currentText()
self.loadGUI()
print("Laser config set" + "\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_())
\ No newline at end of file
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
from cameras2 import *
import json
import time
class SubWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
......
......@@ -5,17 +5,17 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Cameras(Actor):
async def handle_message(self, message: Message):
print("Cameras")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
# await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a camera!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
......
[laser]
name=laser
[cameras]
name=cameras
\ No newline at end of file
......@@ -5,64 +5,52 @@ import time
import json
import configparser
def start():
try:
os.remove("list_of_device(s)_currently_active.txt")
os.remove("laser.ini")
print("Awaiting device(s) to be activated")
except:
print("Awaiting device(s) to be activated")
start()
devices = list(map(str,input("Device(s) to be activated: ").split(",")))
client = embedded()
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 device(s)"}}, indent=2))
time.sleep(1)
def active_devices():
for item in devices:
device = (item + "Embedded")
deviceImport = __import__(device)
with open("list_of_device(s)_currently_active.txt", "a+") as myfile:
class embedded:
def start():
try:
os.remove("list_of_device(s)_currently_active.txt")
print("Activating device(s)")
except:
print("Activating devices(s)")
config = configparser.RawConfigParser()
config.read("config.ini")
devices = config.sections()
global client
client = embedded()
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 device(s)"}}, indent=2))
time.sleep(1)
return devices
def import_devices(devices):
for item in devices:
myfile.write(item + "\n")
active_devices()
def readFile(fname):
print("List of device(s) currently active:")
try:
with open(fname, "r") as f:
for item in f:
print(item.rstrip("\n"))
except:
print("No device(s) added yet")
readFile("list_of_device(s)_currently_active.txt")
# print("Connected to broker")
# time.sleep(1)
# client.subscribe("microscope/light_sheet_microscope/UI/laser/#")
# if os.path.exists:
# parser = configparser.ConfigParser()
# parser.read("laser.ini")
# try:
# subscriptions = dict(parser.items("Subscriptions"))
# print("Subscribing to topics", subscriptions)
# client.subscribe(subscriptions)
# except:
# pass
# else:
# pass
device = (item + "Embedded")
deviceImport = __import__(device)
with open("list_of_device(s)_currently_active.txt", "a+") as myfile:
for item in devices:
myfile.write(item + "\n")
devices = start()
import_devices(devices)
def readFile(fname):
print("List of device(s) currently active:")
try:
with open(fname, "r") as f:
for item in f:
print(item.rstrip("\n"))
except:
print("No device(s) added yet")
readFile("list_of_device(s)_currently_active.txt")
client.loop_forever()
\ No newline at end of file
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
from mqtt import *
from filterwheel2 import *
import json
import time
class SubWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
......
......@@ -9,7 +9,6 @@
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Filterwheel(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
......
......@@ -5,17 +5,17 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class FW(Actor):
async def handle_message(self, message: Message):
print("Filter wheel")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
# await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a filter wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
......
......@@ -6,12 +6,12 @@
#
# WARNING! All changes made in this file will be lost!
import paho.mqtt.client as mqtt
from mqtt import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtWidgets
import os
import configparser
import time
import json
class Ui_Laser(object):
def setupUi(self, Laser):
......@@ -258,65 +258,77 @@ class Ui_Laser(object):
config.read("laser.ini")
try:
self.verticalSlider.setValue(config.getint("445nm", "intensity"))
except:
pass
print("455nm laser set")
except Exception as e:
print("455nm laser not set")
try:
self.lineEdit.setText(str(config.getint("445nm", "intensity")))
except:
pass
print("455nm laser textbox set")
except Exception as e:
print("455nm laser textbox not set")
try:
self.verticalSlider_2.setValue(config.getint("488nm", "intensity"))
except:
pass
print("488nm laser set")
except Exception as e:
print("488nm laser not set")
try:
self.lineEdit_2.setText(str(config.getint("488nm", "intensity")))
except:
pass
print("488nm laser textbox set")
except Exception as e:
print("488nm laser textbox not set")
try:
self.verticalSlider_3.setValue(config.getint("515nm", "intensity"))
except:
pass
print("515nm laser set")
except Exception as e:
print("515nm laser not set")
try:
self.lineEdit_3.setText(str(config.getint("515nm", "intensity")))
except:
pass
print("515nm laser textbox set")
except Exception as e:
print("515nm laser textbox not set")
try:
self.verticalSlider_4.setValue(config.getint("561nm", "intensity"))
except:
pass
print("561nm laser set")
except Exception as e:
print("561nm laser not set")
try:
self.lineEdit_4.setText(str(config.getint("561nm", "intensity")))
except:
pass
print("561nm laser textbox set")
except Exception as e:
print("561nm laser textbox not set")
try:
self.verticalSlider_5.setValue(config.getint("594nm", "intensity"))
except:
pass
print("594nm laser set")
except Exception as e:
print("594nm laser not set")
try:
self.lineEdit_5.setText(str(config.getint("594nm", "intensity")))
except:
pass
print("594nm laser textbox set")
except Exception as e:
print("594nm laser textbox not set")
try:
self.verticalSlider_6.setValue(config.getint("638nm", "intensity"))
except:
pass
print("638nm laser set")
except Exception as e:
print("638nm laser not set")
try:
self.lineEdit_6.setText(str(config.getint("638nm", "intensity")))
except:
pass
print("638nm laser textbox set")
except Exception as e:
print("638nm laser textbox not set")
else:
pass
print("Config file does not exist")
getConfig()
def printValue(self):
......@@ -702,10 +714,6 @@ class Ui_Laser(object):
if __name__ == "__main__":
import sys
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
app = QtWidgets.QApplication(sys.argv)
Laser = QtWidgets.QMainWindow()
ui = Ui_Laser()
......
......@@ -6,17 +6,17 @@ from mqtt2 import *
class Laser(Actor):
async def handle_message(self, message: Message):
print("Laser")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
# 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.
......
laser
laser
laser
laser
laser
laser
laser
laser
laser
laser
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
laser
cameras
......@@ -5,17 +5,17 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Motorized_galvo_wheel(Actor):
async def handle_message(self, message: Message):
print("Motorized galvo wheel")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
# await asyncio.sleep(2)
print("Initialising")