Commit 83ac7644 authored by Spencer Du's avatar Spencer Du

modifying pub/sub and mqtt

parent 3bbfd5ba
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
class device(mqtt.Client):
def on_connect(self, mqtt, obj, flags, rc):
pass
def on_subscribe(client, userdata, mid, granted_qos):
print("I've subscribed to topic")
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 on_message(client, userdata, msg):
m_decode = str(msg.payload.decode("utf-8"))
print("message recieved= " + m_decode)
# print("File which you want to import(with .py extension)")
print("message topic=", msg.topic)
print("message qos=", msg.qos)
print("message retain flag=", msg.retain)
m_in = json.loads(m_decode)
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
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
\ No newline at end of file
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)
# 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")
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
\ No newline at end of file
......@@ -4,7 +4,7 @@ import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
from mqtt2 import *
import json
import time
......@@ -18,10 +18,8 @@ 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 = device()
client.run()
client.loop_start()
print("Connected to broker")
......@@ -32,6 +30,16 @@ class MainWindow(QtWidgets.QMainWindow):
client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2))
time.sleep(1)
client.loop_stop()
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")
pybutton = QPushButton('Add device', self)
......
......@@ -25,10 +25,8 @@ class SubWindow(QWidget):
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 = device()
client.run()
client.loop_start()
print("Connected to broker")
......@@ -57,17 +55,17 @@ class SubWindow(QWidget):
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 = device()
client.run()
client.loop_start()
print("Connected to broker")
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"}}))
time.sleep(1)
print("Device turned off")
self.Button.setStyleSheet("background-color:")
......
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
class device(mqtt.Client):
def on_connect(self, mqtt, obj, flags, rc):
pass
def on_subscribe(client, userdata, mid, granted_qos):
print("I've subscribed to topic")
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)
# 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")
def on_message(client, userdata, msg):
m_decode = str(msg.payload.decode("utf-8"))
print("message recieved= " + m_decode)
# print("File which you want to import(with .py extension)")
print("message topic=", msg.topic)
print("message qos=", msg.qos)
print("message retain flag=", msg.retain)
m_in = json.loads(m_decode)
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
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
\ No newline at end of file
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)
# 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")
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment