From 33b7c95df3df330ab2ffb7b325e94164db1159fd Mon Sep 17 00:00:00 2001 From: James Myatt Date: Fri, 9 Sep 2016 20:53:22 +0100 Subject: [PATCH] Update examples added since PR#98 (#109) Signed-off-by: James Myatt --- examples/{logger.py => client_logger.py} | 28 +++++------------- examples/client_pub-wait.py | 37 ++++++++++-------------- examples/client_session_present.py | 28 +++++++----------- 3 files changed, 32 insertions(+), 61 deletions(-) rename examples/{logger.py => client_logger.py} (55%) diff --git a/examples/logger.py b/examples/client_logger.py similarity index 55% rename from examples/logger.py rename to examples/client_logger.py index 3b0e7b1..eb30468 100644 --- a/examples/logger.py +++ b/examples/client_logger.py @@ -1,38 +1,24 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2016 James Myatt # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # James Myatt - initial implementation -# This shows a simple example of standard logging with an MQTT subscriber. +# This shows a simple example of standard logging with an MQTT subscriber client. -import sys -import logging - -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - - cmd_subfolder = os.path.realpath( - os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt +import logging logging.basicConfig(level=logging.DEBUG) # If you want to use a specific client id, use diff --git a/examples/client_pub-wait.py b/examples/client_pub-wait.py index 1840409..3847be5 100644 --- a/examples/client_pub-wait.py +++ b/examples/client_pub-wait.py @@ -15,40 +15,33 @@ # Copyright (c) 2010,2011 Roger Light # All rights reserved. -# This shows a simple example of an MQTT subscriber. - -import sys -import time -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0], "../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +# This shows a simple example of waiting for a message to be published. + +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt + def on_connect(mqttc, obj, flags, rc): - print("rc: "+str(rc)) + print("rc: " + str(rc)) + def on_message(mqttc, obj, msg): - print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) + def on_publish(mqttc, obj, mid): - print("mid: "+str(mid)) + print("mid: " + str(mid)) pass + def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: "+str(mid)+" "+str(granted_qos)) + print("Subscribed: " + str(mid) + " " + str(granted_qos)) + def on_log(mqttc, obj, level, string): print(string) + # If you want to use a specific client id, use # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client @@ -59,7 +52,7 @@ mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages -#mqttc.on_log = on_log +# mqttc.on_log = on_log mqttc.connect("localhost", 1883, 60) mqttc.loop_start() diff --git a/examples/client_session_present.py b/examples/client_session_present.py index bf40c58..63bab41 100644 --- a/examples/client_session_present.py +++ b/examples/client_session_present.py @@ -17,20 +17,9 @@ # This demonstrates the session present flag when connecting. -import sys -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt + def on_connect(mqttc, obj, flags, rc): if obj == 0: @@ -39,23 +28,26 @@ def on_connect(mqttc, obj, flags, rc): print("Second connection:") elif obj == 2: print("Third connection (with clean session=True):") - print(" Session present: "+str(flags['session present'])) - print(" Connection result: "+str(rc)) + print(" Session present: " + str(flags['session present'])) + print(" Connection result: " + str(rc)) mqttc.disconnect() + def on_disconnect(mqttc, obj, rc): - mqttc.user_data_set(obj+1) + mqttc.user_data_set(obj + 1) if obj == 0: mqttc.reconnect() + def on_log(mqttc, obj, level, string): print(string) + mqttc = mqtt.Client(client_id="asdfj", clean_session=False) mqttc.on_connect = on_connect mqttc.on_disconnect = on_disconnect # Uncomment to enable debug messages -#mqttc.on_log = on_log +# mqttc.on_log = on_log mqttc.user_data_set(0) mqttc.connect("test.mosquitto.org", 1883, 60) -- GitLab