def btn_停止订阅被单击(): client.disconnect() client.loop_stop() print("已停止订阅并断开与服务器的连接") w.Scrolledtext1.insert(tk.END, "已停止订阅并断开与服务器的连接\n") w.TButton1.configure(state=tk.TRUE) w.TButton2.configure(state=tk.DISABLED)
def btn_停止订阅被单击(): client.disconnect() client.loop_stop() print("已断开与服务器的连接") w.TButton_connect.configure(state=tk.TRUE) w.TButton_disconnect.configure(state=tk.DISABLED) w.TButton_send.configure(state=tk.DISABLED) showinfo("连接提示", "已断开与服务的连接!")
def disconnect(text=""): global client, connect_flag btn_clearsub() client.disconnect() client.loop_stop() print("已断开与服务器的连接") w.Button_connect.configure(text='''开始连接''') w.Button_connect.configure(background="#00BFFF") w.Button_publish.configure(state="disabled") w.Button_addsub.configure(state="disabled") w.Button_cancelsub.configure(state="disabled") w.Button_clearsub.configure(state="disabled") showinfo("连接提示", "已断开与服务的连接!\n%s" % text) connect_flag = False
async def publish(queue): client = mqtt.Client(client_id=id) client.connect('202.139.192.75') client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect client.loop_start() print('Start MQTT publisher') while True: val = await queue.get() print('Forwarding', val) client.publish(topic, json.dumps(val), qos=1) await asyncio.sleep(1) mqtt.loop_stop()
async def publish(queue): client = mqtt.Client(client_id=id) client.username_pw_set(token) client.connect(MQTTbroker) client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect client.loop_start() print('Start MQTT publisher') while True: val = await queue.get() print('Forwarding', val) client.publish(topic, json.dumps(val), qos=0) await asyncio.sleep(1) mqtt.loop_stop()
def publish(): client = mqtt.Client(client_id='Jotaro') client.connect('202.139.192.75') client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect client.loop_start() print('Start MQTT publisher') while True: while len(queue) == 0: print('waiting for queue') sleep(1) print('Forwarding', 'Hello world') client.publish(topic, json.dumps(queue.pop(0)), qos=1) sleep(1) mqtt.loop_stop()
def _write_topic(self, topic, value): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c,d,f,r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() client.on_message = lambda c,d,msg: notifications.append(msg) client.publish(topic, json.dumps({'value': value})) time.sleep(1) client.loop_stop(True) self.assertTrue(len(notifications) > 0) topic = 'N' + topic[1:] v = None for n in notifications: if n.topic == topic: v = int(json.loads(n.payload)['value']) self.assertEqual(v, value)
def _write_topic(self, topic, value): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c, d, f, r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() client.on_message = lambda c, d, msg: notifications.append(msg) client.publish(topic, json.dumps({'value': value})) time.sleep(1) client.loop_stop(True) self.assertTrue(len(notifications) > 0) topic = 'N' + topic[1:] v = None for n in notifications: if n.topic == topic: v = int(json.loads(n.payload)['value']) self.assertEqual(v, value)
def test_notifications(self): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c,d,f,r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() time.sleep(2) # wait for retained messages client.on_message = lambda c,d,msg: notifications.append(msg) time.sleep(2) client.loop_stop(True) self.assertTrue(len(notifications) > 0) for n in notifications: action, system_id, service_type, device_instance, path = n.topic.split('/', 4) self.assertEqual(action, 'N') self.assertEqual(system_id, TestPortalId) # If the statements below raise an exception, the test will fail di = int(device_instance) v = json.loads(n.payload)['value']
def test_notifications(self): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c, d, f, r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() time.sleep(2) # wait for retained messages client.on_message = lambda c, d, msg: notifications.append(msg) time.sleep(2) client.loop_stop(True) self.assertTrue(len(notifications) > 0) for n in notifications: action, system_id, service_type, device_instance, path = n.topic.split( '/', 4) self.assertEqual(action, 'N') self.assertEqual(system_id, TestPortalId) # If the statements below raise an exception, the test will fail di = int(device_instance) v = json.loads(n.payload)['value']
def test_request(self): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c,d,f,r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() time.sleep(2) # wait for retained messages client.on_message = lambda c,d,msg: notifications.append(msg) topic = 'R/{}/settings/0/Settings/Vrmlogger/LogInterval'.format(TestPortalId) client.publish(topic, '') time.sleep(1) client.loop_stop(True) self.assertTrue(len(notifications) > 0) topic = 'N' + topic[1:] for n in notifications: if n.topic == topic: v = int(json.loads(n.payload)['value']) break else: raise Exception('Topic not found')
def test_request(self): client = paho.mqtt.client.Client() notifications = [] client.on_connect = lambda c, d, f, r: c.subscribe('N/#', 0) client.connect(TestHost) client.loop_start() time.sleep(2) # wait for retained messages client.on_message = lambda c, d, msg: notifications.append(msg) topic = 'R/{}/settings/0/Settings/Vrmlogger/LogInterval'.format( TestPortalId) client.publish(topic, '') time.sleep(1) client.loop_stop(True) self.assertTrue(len(notifications) > 0) topic = 'N' + topic[1:] for n in notifications: if n.topic == topic: v = int(json.loads(n.payload)['value']) break else: raise Exception('Topic not found')
def cleanup(ser, mqtt): """Called on exit to close serial and disconnect MQTT""" print("Ending and cleaning up") ser.close() mqtt.loop_stop()
def stop_pub():#断开与mq服务器的连接 client.disconnect() client.loop_stop() print("已断开与服务器的连接")
def stop(client): client.disconnect() client.loop_stop() sys.exit()
def stop(): log.info("Stop MQTT CAN receiver") client.loop_stop()
# settings logging.basicConfig(level=logging.INFO) # start of program # Load environment variables logging.info("starting program") load_dotenv("example.env") mqtt_host = get_env("MQTT_HOST") mqtt_port = int(get_env("MQTT_PORT")) mqtt_username = get_env("MQTT_USERNAME") mqtt_password = get_env("MQTT_PASSWORD") # Create new MQTT client client = paho.mqtt.client.Client() client.username_pw_set(mqtt_username, mqtt_password) client.on_publish = on_publish_callback # Establish connection to broker client.connect(mqtt_host, mqtt_port) logging.info("connected to ags broker") # Publish to broker client.publish("mqtt/hello-world", "hello world - 1", 1) client.publish("mqtt/hello-world", "hello world - 2", 1) # Wait for 3 seconds (to allow callback to run) before exiting client.loop_start() time.sleep(3) client.loop_stop()
class Whitelist(Resource): def get(self): parser = reqparse.RequestParser() parser.add_argument("Authorization", location='headers') args = parser.parse_args() return get_whitelist(args["Authorization"]) def post(self): parser = reqparse.RequestParser() parser.add_argument("Authorization", location='headers') parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files') parser.add_argument("name") args = parser.parse_args() return store_whitelist(args["Authorization"], args["name"], args["picture"]) app = Flask(__name__) api = Api(app) api.add_resource(Pairing, "/pair") api.add_resource(ManualUnlock, "/unlock") api.add_resource(Whitelist, "/whitelist") app.run("127.0.0.1", 5050, use_reloader=False, debug=True) mqtt_client.loop_stop()
sensor = sensoren[name] messdaten = aktuelleTemperatur(sensor) zeitstempel = datetime.datetime.now().timestamp() if messdaten != None: protokoll = str(zeitstempel) + ": " + name + ": " + str(messdaten) print(protokoll) with open("messdaten.log", "a") as file: file.write(protokoll + "\n") client.publish("haus/har/temperatur/"+name, messdaten) else: print(name + ': Messfehler') print("finished") def pegel(url): my_pegel_request = requests.get(url) my_pegel_text = my_pegel_request.text my_pegel_json = json.loads(my_pegel_text) my_pegel = float(my_pegel_json["timeseries"][0]["currentMeasurement"]["value"]) return my_pegel mqtt = mqttVerbindungsaufbau() while True: messen(mqtt) mqtt.publish("haus/pegel/oberwinter", pegel(oberwinter)) time.sleep(900) mqtt.loop_stop() mqtt.disconnect()
def stop(): log.info("Stop") client.loop_stop()