提交 165a9b5f 编写于 作者: J James Myatt

Use common "context" module to ensure that paho is in the Python path for the examples

Signed-off-by: NJames Myatt <james@jamesmyatt.co.uk>
上级 8cc73ba7
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2013 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -18,45 +19,43 @@
import sys
import getopt
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.
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
final_mid = 0
def on_connect(mqttc, userdata, flags, rc):
if userdata == True:
print("rc: "+str(rc))
print("rc: " + str(rc))
def on_message(mqttc, userdata, msg):
global final_mid
if msg.retain == 0:
pass
#sys.exit()
# sys.exit()
else:
if userdata == True:
print("Clearing topic "+msg.topic)
print("Clearing topic " + msg.topic)
(rc, final_mid) = mqttc.publish(msg.topic, None, 1, True)
def on_publish(mqttc, userdata, mid):
global final_mid
if mid == final_mid:
sys.exit()
def on_log(mqttc, userdata, level, string):
print(string)
def print_usage():
print("mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic")
print(
"mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic")
def main(argv):
debug = False
......@@ -70,7 +69,8 @@ def main(argv):
verbose = False
try:
opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v", ["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"])
opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v",
["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"])
except getopt.GetoptError as s:
print_usage()
sys.exit(2)
......@@ -114,6 +114,6 @@ def main(argv):
mqttc.subscribe(topic)
mqttc.loop_forever()
if __name__ == "__main__":
main(sys.argv[1:])
#!/usr/bin/python3.5
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
#
# 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:
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# 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:
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2013 Roger Light <roger@atchoo.org>
#
......@@ -14,20 +15,8 @@
# This example shows how you can use the MQTT client in a class.
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
class MyMQTTClass(mqtt.Client):
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -15,37 +16,29 @@
# This shows a simple example of an MQTT subscriber using a per-subscription message handler.
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_message_msgs(mosq, obj, msg):
# This callback will only be called for messages with topics that match
# $SYS/broker/messages/#
print("MESSAGES: "+msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print("MESSAGES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_message_bytes(mosq, obj, msg):
# This callback will only be called for messages with topics that match
# $SYS/broker/bytes/#
print("BYTES: "+msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print("BYTES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_message(mosq, obj, msg):
# This callback will be called for messages that we receive that do not
# match any patterns defined in topic specific callbacks, i.e. in this case
# those messages that do not have topics $SYS/broker/messages/# nor
# $SYS/broker/bytes/#
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
mqttc = mqtt.Client()
......@@ -57,4 +50,3 @@ mqttc.connect("iot.eclipse.org", 1883, 60)
mqttc.subscribe("$SYS/#", 0)
mqttc.loop_forever()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -16,20 +17,8 @@
# This shows a simple example of an MQTT subscriber using connect_srv method.
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):
print("Connected to %s:%s" % (mqttc._host, mqttc._port))
......
#!/usr/bin/python3
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -16,21 +17,8 @@
# This shows a simple example of an MQTT subscriber.
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):
print("rc: "+str(rc))
......@@ -61,6 +49,4 @@ mqttc.on_log = on_log
mqttc.connect("test.mosquitto.org", 8080, 60)
mqttc.subscribe("$SYS/broker/version", 0)
mqttc.loop_forever()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -16,36 +17,30 @@
# This shows a simple example of an MQTT subscriber.
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):
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))
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
......@@ -56,10 +51,8 @@ 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("m2m.eclipse.org", 1883, 60)
mqttc.subscribe("$SYS/#", 0)
mqttc.loop_forever()
# -*- coding: utf-8 -*-
# Ensure can import paho package
try:
import paho
except ImportError:
# This part is only required to run the examples from within the examples
# directory when the module itself is not installed.
import sys
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -14,20 +15,8 @@
# This shows an example of using the publish.multiple helper function.
import sys
try:
import paho.mqtt.publish as publish
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.publish"
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.publish as publish
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.publish as publish
msgs = [{'topic':"paho/test/multiple", 'payload':"multiple 1"}, ("paho/test/multiple", "multiple 2", 0, False)]
msgs = [{'topic': "paho/test/multiple", 'payload': "multiple 1"}, ("paho/test/multiple", "multiple 2", 0, False)]
publish.multiple(msgs, hostname="test.mosquitto.org")
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -14,19 +15,7 @@
# This shows an example of using the publish.single helper function.
import sys
try:
import paho.mqtt.publish as publish
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.publish"
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.publish as publish
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.publish as publish
publish.single("paho/test/single", "boo", hostname="test.mosquitto.org")
#!/usr/bin/python
# vim: set fileencoding=utf-8
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -15,21 +15,9 @@
# This shows an example of using the publish.single helper function with unicode topic and payload.
import sys
try:
import paho.mqtt.publish as publish
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.publish"
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.publish as publish
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.publish as publish
t=u"paho/test/single/ô"
publish.single(t, u"bôô", hostname="test.mosquitto.org")
topic = u"paho/test/single/ô"
payload = u"bôô"
publish.single(topic, payload, hostname="test.mosquitto.org")
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -14,21 +15,8 @@
# This shows an example of using the subscribe.callback helper function.
import sys
try:
import paho.mqtt.subscribe as subscribe
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.subscribe"
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.subscribe as subscribe
import paho.mqtt.client
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.subscribe as subscribe
def print_msg(client, userdata, message):
print("%s : %s" % (message.topic, message.payload))
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Roger Light <roger@atchoo.org>
#
# 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:
......@@ -14,21 +15,8 @@
# This shows an example of using the subscribe.simple helper function.
import sys
try:
import paho.mqtt.subscribe as subscribe
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.subscribe"
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.subscribe as subscribe
import paho.mqtt.client
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.subscribe as subscribe
topics = ['#']
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册