How to create list for stuff inside mqtt and GUI?.

2019-09-01 Thread Spencer Du via Python-list
Hi 

I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
GUI file if the file exists in current directory. I want to add the file name 
to a list when a file is imported and for each subsequent file that is imported 
I want the file name to be imported to the same list and print the list or 
create a new list but with the imported file named added to list which has the 
existing file names that have already been imported. I was wondering how I do 
this. By the way run GUI.py to test this.

GUI.py

import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt
import importlib
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
import json

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")

pybutton = QPushButton('Add device', self)

pybutton.clicked.connect(self.importbutton)

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 = ""

def importbutton(self):
self.fileName_UI = self.textbox.text()
self.loadGUI()

def getGUIFilename(self):
return self.fileName_UI

def loadGUI(self):
print("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()

print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
device_message = self.fileName_UI
time.sleep(2)
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": 
"adding device"}}, indent=2))
time.sleep(1)  # wait
client.loop_stop()  # stop the loop
print("Device added" + "\n")
listofdevice = []
listofdevice.append(self.fileName_UI)
print(listofdevice) 
except:
print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
device_message = self.fileName_UI
time.sleep(2)
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(2)  # wait
client.loop_stop()  # stop the loop
print(device_message + ".py " + "file doesn't exist")
print("Device not added")
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
publishedMessage = mainWin.getGUIFilename()
sys.exit(app.exec_())

MQTT.py
import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt
import importlib
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic

class device(mqtt.Client):
def on_connect(self, mqttc, obj, flags, rc):
if rc == 0:
print("Connected to broker")
else:
print("Connection failed")

# mqttc.subscribe("microscope/light_sheet_microscope/UI")

def on_message(self, mqttc, userdata, message):
msg = str(message.payload.decode("utf-8"))
print("message recieved= " + msg)
# 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)

def run(self):
self.connect("broker.hivemq.com", 1883, 60)

Tha

How do I decouple these two modules?

2019-08-28 Thread Spencer Du via Python-list
Hi

I have code for a GUI and MQTT. How do I make both of these modules decoupled 
because currently they rely on each other to some extent.

GUI.py

import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt
import importlib
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *

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")

pybutton = QPushButton('Add device', self)

pybutton.clicked.connect(self.importbutton)

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 = ""

def importbutton(self):
self.fileName_UI = self.textbox.text()
self.loadGUI()

def getGUIFilename(self):
return self.fileName_UI

def loadGUI(self):
print("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()

print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
device_message = self.fileName_UI
time.sleep(2)
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI", " Searching file ", device_message+".py 
...")
client.publish("microscope/light_sheet_microscope/UI", 
device_message)
time.sleep(2)  # wait
client.loop_stop()  # stop the loop
print("File imported")
except:
print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
device_message = self.fileName_UI
time.sleep(2)
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI", " Searching file ", device_message+".py 
...")
client.publish("microscope/light_sheet_microscope/UI", 
device_message)
time.sleep(2)  # wait
client.loop_stop()  # stop the loop
print(device_message + ".py " + "file doesn't exist")

if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
publishedMessage = mainWin.getGUIFilename()
sys.exit(app.exec_())


MQTT.py

import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt
import importlib
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic

class device(mqtt.Client):
def on_connect(self, mqttc, obj, flags, rc):
if rc == 0:
print("Connected to broker")
else:
print("Connection failed")

mqttc.subscribe("microscope/light_sheet_microscope/UI")

def on_message(self, mqttc, userdata, message):
msg = str(message.payload.decode("utf-8"))
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)

def run(self):
self.connect("broker.hivemq.com", 1883, 60)

Thanks
Spencer
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do i execute some code when I have subscribed to a topic with a message payload for mqtt in python?

2019-08-08 Thread Spencer Du via Python-list
On Thursday, 8 August 2019 22:48:11 UTC+2, Spencer Du  wrote:
> Ok so here is some code below. How do I write an if code block to execute 
> some commands when I subscribe to the topic: 
> microscope/light_sheet_microscope/UI and which has a message which is a 
> device type published to it. I want to execute some code to check if the 
> device has a python file in the currently directory because each device also 
> relates to a python file. If the file exists then import the file into the 
> python program else print an error message. If I need to use on_subscribe 
> callback then tell and show me how the code is to be written and if another 
> method then also tell me and show me how to write the code. I really 
> appreciate all the help you can give me because this is really important for 
> me and I am currently really struggling with MQTT because I need help with 
> writing MQTT code because I am working on MQTT at work.
> 
> import logging
> from datetime import timedelta
> import time
> from thespian.actors import *
> from transitions import Machine
> import paho.mqtt.client as mqtt
> 
> class device(mqtt.Client):
> def on_connect(self, mqttc, obj, flags, rc):
>  if rc == 0:
>   print("Connected to broker") 
>  else:
>   print("Connection failed")
>  mqttc.subscribe("microscope/light_sheet_microscope/UI")
> def on_message(self, mqttc, userdata, message):
> print("message received " ,str(message.payload.decode("utf-8")))
> print("message topic=",message.topic)
> print("message qos=",message.qos)
> print("message retain flag=",message.retain)
> def on_publish(self, mqttc, obj, mid):
> print("mid: "+str(mid))
> def on_subscribe(self, mqttc, obj, mid, granted_qos):
> print("Subscribed: "+str(mid)+" "+str(granted_qos))
> def run(self):
> self.connect("broker.hivemq.com", 1883, 60)
> print("creating new instance laser")
> client = device("Device")
> client.run()
> client.loop_start() #start the loop
> device = "laser"
> time.sleep(2)
> print("Publishing message to topic","microscope/light_sheet_microscope/UI")
> client.publish("microscope/light_sheet_microscope/UI",device)
> time.sleep(2) # wait
> print("subscribing ")
> client.subscribe("microscope/light_sheet_microscope/UI")
> client.loop_stop() #stop the loop
> 
> Thanks
> Spencer

-- 
https://mail.python.org/mailman/listinfo/python-list


How do i execute some code when I have subscribed to a topic with a message payload for mqtt in python?

2019-08-08 Thread Spencer Du via Python-list
Ok so here is some code below. How do I write an if code block to execute some 
commands when I subscribe to the topic: microscope/light_sheet_microscope/UI 
and which has a message which is a device type published to it. I want to 
execute some code to check if the device has a python file in the currently 
directory because each device also relates to a python file. If the file exists 
then import the file into the python program else print an error message. If I 
need to use on_subscribe callback then tell and show me how the code is to be 
written and if another method then also tell me and show me how to write the 
code. I really appreciate all the help you can give me because this is really 
important for me and I am currently really struggling with MQTT because I need 
help with writing MQTT code because I am working on MQTT at work.

import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt

class device(mqtt.Client):
def on_connect(self, mqttc, obj, flags, rc):
 if rc == 0:
  print("Connected to broker") 
 else:
  print("Connection failed")
 mqttc.subscribe("microscope/light_sheet_microscope/UI")
def on_message(self, mqttc, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
def on_publish(self, mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(self, mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
print("creating new instance laser")
client = device("Device")
client.run()
client.loop_start() #start the loop
device = "laser"
time.sleep(2)
print("Publishing message to topic","microscope/light_sheet_microscope/UI")
client.publish("microscope/light_sheet_microscope/UI",device)
time.sleep(2) # wait
print("subscribing ")
client.subscribe("microscope/light_sheet_microscope/UI")
client.loop_stop() #stop the loop

Thanks
Spencer
-- 
https://mail.python.org/mailman/listinfo/python-list