Skip to content

Commit

Permalink
replace Python mosquitto by Paho-MQTT
Browse files Browse the repository at this point in the history
    	closes #2
  • Loading branch information
jpmens committed May 4, 2015
1 parent 1f3746e commit 559cf83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Requirements

- `watchdog <https://github.com/gorakhargosh/watchdog>`_, a Python
library to monitor file-system events.
- `Mosquitto <http://mosquitto.org>`_'s Python module
- `Paho-MQTT <https://pypi.python.org/pypi/paho-mqtt>`_'s Python module

Related utilities & Credits
---------------------------
Expand Down
23 changes: 12 additions & 11 deletions mqtt-watchdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
# POSSIBILITY OF SUCH DAMAGE.

__author__ = "Jan-Piet Mens"
__copyright__ = "Copyright (C) 2013 by Jan-Piet Mens"
__copyright__ = "Copyright (C) 2013-2015 by Jan-Piet Mens"

import os, sys
import signal
import time
import mosquitto
import paho.mqtt.client as paho
# https://github.com/gorakhargosh/watchdog
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
Expand Down Expand Up @@ -86,21 +86,22 @@
except Exception, e:
sys.exit("Can't import filter from file %s: %s" % (MQTTFILTER, e))

mqttc = mosquitto.Mosquitto()
clientid = 'mqtt-watchdir-%s' % os.getpid()
mqtt = paho.Client(clientid, clean_session=True)

def on_publish(mosq, userdata, mid):
pass
# print("mid: "+str(mid))

def on_disconnect(mosq, userdata, rc):
print "OOOOPS! disconnect"
print "disconnected"
time.sleep(5)

def signal_handler(signal, frame):
""" Bail out at the top level """

mqttc.loop_stop()
mqttc.disconnect()
mqtt.loop_stop()
mqtt.disconnect()

sys.exit(0)

Expand Down Expand Up @@ -171,7 +172,7 @@ def catch_all(self, event, op):
except Exception, e:
print "mfilter: %s" % (e)

mqttc.publish(topic, payload, qos=MQTTQOS, retain=MQTTRETAIN)
mqtt.publish(topic, payload, qos=MQTTQOS, retain=MQTTRETAIN)

def on_created(self, event):
self.catch_all(event, 'NEW')
Expand All @@ -184,12 +185,12 @@ def on_deleted(self, event):

def main():

mqttc.on_disconnect = on_disconnect
mqttc.on_publish = on_publish
mqtt.on_disconnect = on_disconnect
mqtt.on_publish = on_publish

mqttc.connect(MQTTHOST, MQTTPORT)
mqtt.connect(MQTTHOST, MQTTPORT)

mqttc.loop_start()
mqtt.loop_start()

signal.signal(signal.SIGINT, signal_handler)
while 1:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
install_requires=[
'watchdog',
'mosquitto',
'paho-mqtt',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit 559cf83

Please sign in to comment.