Commit 2cba05fb authored by Spencer Du's avatar Spencer Du

new code

parent f0e3b193
......@@ -17,7 +17,22 @@ class MainWindow(QtWidgets.QMainWindow):
self.setMinimumSize(QSize(800, 600))
self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com")
client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message
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":{"cmd": "get all devices"}}, indent=2))
time.sleep(1)
client.loop_stop()
pybutton = QPushButton('Add device', self)
pybutton.clicked.connect(self.importbutton)
......@@ -25,75 +40,37 @@ class MainWindow(QtWidgets.QMainWindow):
pybutton.move(100, 400)
pybutton.resize(150, 32)
self.textbox = QLineEdit(self)
self.textbox.move(100,350)
self.textbox.resize(100, 32)
self.fileName_UI = ""
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):
self.fileName_UI = self.textbox.text()
self.fileName_UI = self.combo.currentText()
self.loadGUI()
def getGUIFilename(self):
return self.fileName_UI
def loadGUI(self):
print("\n" + "Searching file", self.fileName_UI)
try:
module = __import__(self.fileName_UI)
my_class = getattr(module, "SubWindow")
sub = QMdiSubWindow()
sub.setWidget(my_class())
sub.setWindowTitle("New GUI: " + self.fileName_UI)
self.mdi.addSubWindow(sub)
sub.show()
client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message
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": "device", "payload":{"name": self.fileName_UI}}, indent=2))
time.sleep(1)
client.loop_stop()
except:
client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_start()
print("Connecting 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": "device", "payload":{"name": self.fileName_UI}}, indent=2))
time.sleep(1)
client.loop_stop()
print(self.fileName_UI + ".py " + "file doesn't exist")
print("Device not added")
module = __import__(self.fileName_UI.rstrip("\n"))
my_class = getattr(module, "SubWindow")
sub = QMdiSubWindow()
sub.setWidget(my_class())
sub.setWindowTitle("New GUI: " + self.fileName_UI)
self.mdi.addSubWindow(sub)
sub.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
try:
mainWin.show()
os.remove("list_of_devices_currently_active.txt")
print("Awaiting devices to be launched")
except:
print("Awaiting devices to be launched")
publishedMessage = mainWin.getGUIFilename()
# publishedMessage = mainWin.getGUIFilename()
sys.exit(app.exec_())
\ No newline at end of file
......@@ -5,17 +5,19 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Cameras(Actor):
# Here we override the handle_message method to send a `DataMessage` with the data "Hello World!".
async def handle_message(self, message: Message):
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 camera!", sender=self))
print("Camera")
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 camera!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with Cameras() as cameras:
......
import paho.mqtt.client as mqtt
from mqtt2 import *
import os
import time
import json
try:
os.remove("list_of_devices_currently_active.txt")
print("Awaiting devices to be activated")
except:
print("Awaiting devices to be activated")
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)
client.loop_stop()
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")
print("\n" + "List of devices currently active:")
client.loop_forever()
\ No newline at end of file
def readFile(fname):
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")
\ No newline at end of file
......@@ -4,17 +4,19 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class FW(Actor):
async def handle_message(self, message: Message):
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 filter wheel!", sender=self))
print("Filter wheel")
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 filter wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with FW() as fw:
......
......@@ -4,7 +4,8 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Laser(Actor):
async def handle_message(self, message: Message):
print("Laser actor")
print("Laser")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
......@@ -15,7 +16,7 @@ class Laser(Actor):
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a laser!", sender=self))
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:
......
......@@ -4,17 +4,19 @@ from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Motorized_galvo_wheel(Actor):
async def handle_message(self, message: Message):
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 motorized galvo wheel!", sender=self))
print("Motorized galvo wheel")
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 motorized galvo wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with Motorized_galvo_wheel() as motorized_galvo_wheel:
......
import logging
import paho.mqtt.client as mqtt
import json
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
def on_connect(client, userdata, flags, rc):
pass
......@@ -16,7 +19,14 @@ def on_message(client, userdata, msg):
print("message qos=", msg.qos)
print("message retain flag=", msg.retain)
m_in = json.loads(m_decode)
if m_in["payload"]["name"] == "laser":
print("Laser added" + "\n")
if m_in["payload"]["cmd"] == "laser turning on":
print("Laser turned on")
\ No newline at end of file
if m_in["payload"]["cmd"] == "get all devices":
print("List of devices currently active:")
def readFile(fname):
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")
\ No newline at end of file
......@@ -14,13 +14,13 @@ class device(mqtt.Client):
print("message qos=", message.qos)
print("message retain flag=", message.retain)
m_in = json.loads(m_decode)
if m_in["payload"]["name"] == "laser":
print("Laser added" + "\n")
# if m_in["payload"]["name"] == devices:
# print("Laser added" + "\n")
if m_in["payload"]["cmd"] == "laser turning on":
laser = ("laserEmbedded")
laserImport = __import__(laser)
print("Laser turned on" + "\n")
# if m_in["payload"]["cmd"] == "laser turning on":
# laser = ("laserEmbedded")
# laserImport = __import__(laser)
# print("Laser turned on" + "\n")
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
\ No newline at end of file
x = map(str,input("Devices to be activated ").split(","))
for item in x:
device = (item + "Embedded")
deviceImport = __import__(device)
\ No newline at end of file
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 = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message
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":{"cmd": "get all devices"}}, indent=2))
time.sleep(1)
client.loop_stop()
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):
self.fileName_UI = self.combo.currentText()
self.loadGUI()
def loadGUI(self):
module = __import__(self.fileName_UI.rstrip("\n"))
my_class = getattr(module, "SubWindow")
sub = QMdiSubWindow()
sub.setWidget(my_class())
sub.setWindowTitle("New GUI: " + 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
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
class Cameras(Actor):
# Here we override the handle_message method to send a `DataMessage` with the data "Hello World!".
async def handle_message(self, message: Message):
print("Camera")
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 camera!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with Cameras() as cameras:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(cameras, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())
\ No newline at end of file
import paho.mqtt.client as mqtt
from mqtt2 import *
import os
import time
import json
try:
os.remove("list_of_devices_currently_active.txt")
print("Awaiting devices to be activated")
except:
print("Awaiting devices to be activated")
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)
client.loop_stop()
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")
print("\n" + "List of devices currently active:")
def readFile(fname):
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")
\ No newline at end of file
import random
import asyncio
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)
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 filter wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it.
async with FW() as fw:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(fw, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())
\ 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 *
import json
import time
class SubWindow(QWidget):
def __init__(self, parent = None):
super(SubWindow, self).__init__(parent)
self.setMinimumSize(QSize(300, 200))
label = QLabel("Laser", self)
self.Button = QtWidgets.QPushButton("ON",self)
self.Button.clicked.connect(self.on)
self.Button.setGeometry(QtCore.QRect(10, 40, 81, 23))
self.Button2 = QtWidgets.QPushButton("OFF",self)
self.Button2.clicked.connect(self.off)
self.Button2.setGeometry(QtCore.QRect(10, 40, 81, 23))
self.Button2.move(120,40)
self.fileName_UI = "laser"
def on(self):
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.hivemq.com",1883,60)
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": "device", "payload":{"name": self.fileName_UI, "cmd": "laser turning on"}}, indent=2))
time.sleep(1)
self.Button.setStyleSheet("background-color: green")
listofdevices = []
listofdevices.append(self.fileName_UI)
with open("list_of_devices_currently_active.txt", "a+") as myfile:
for item in listofdevices:
myfile.write(item + "\n")
print("\n" + item + " added to device list" + "\n" + "\n" + "List of devices currently active:")
def readFile(fname):
try:
with open(fname, "r") as f:
for item in f:
print(item)
except:
print("No devices added yet")
readFile("list_of_devices_currently_active.txt")
def off(self):
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.hivemq.com",1883,60)
client.loop_start()
time.sleep(1)
print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")
client.subscribe("microscope/light_sheet_microscope/UI/state")
print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state")
client.publish("Publishing message to topic", "microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "laser", "state": "turning off"}}))
print("Device turned off")
self.Button.setStyleSheet("background-color:")
with open("list_of_devices_currently_active.txt", "r") as f:
lines = f.readlines()
with open("list_of_devices_currently_active.txt", "w") as f:
for line in lines:
if line.strip("\n") != "laser":
f.write(line)
print("Laser removed from device list" + "\n\n" + "List of devices currently active")
def readFile(fname):
try:
with open(fname, "r") as f:
for item in f:
print(item)