Refactor mqtt sender script #299
This commit is contained in:
parent
68a92b2daf
commit
fb52b59ad9
1 changed files with 11 additions and 32 deletions
|
|
@ -1,7 +1,6 @@
|
|||
import paho.mqtt.client as mqtt
|
||||
import yaml
|
||||
import ssl
|
||||
from threading import Thread
|
||||
from daemonize import Daemonize
|
||||
import redis
|
||||
import time
|
||||
import signal
|
||||
|
|
@ -10,24 +9,18 @@ import os
|
|||
import logging
|
||||
|
||||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
#logging.warning('Interrupted')
|
||||
sys.exit(0)
|
||||
os._exit(0)
|
||||
|
||||
# TODO: yaml configuration file for redis and mqtt settings
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
#logging.info("Connected with result code "+str(rc))
|
||||
client.subscribe("$SYS/#")
|
||||
|
||||
logging.info("Connected with result code "+str(rc))
|
||||
#client.subscribe("$SYS/#")
|
||||
|
||||
|
||||
def on_publish(client, userdata, mid):
|
||||
pass
|
||||
|
||||
|
||||
def getRedis(i, client, logger):
|
||||
def redis_listen(client, logger):
|
||||
logger.info("Listening in redis events")
|
||||
r = redis.StrictRedis(host='localhost', port=6379, db=0)
|
||||
while 1:
|
||||
|
|
@ -40,9 +33,6 @@ def getRedis(i, client, logger):
|
|||
|
||||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
sys.exit(0)
|
||||
|
||||
def get_logger():
|
||||
logger = logging.getLogger("mqtt_logger")
|
||||
logger.setLevel(logging.INFO)
|
||||
|
|
@ -64,25 +54,14 @@ def main():
|
|||
client.on_connect = on_connect
|
||||
client.on_publish = on_publish
|
||||
|
||||
client.tls_set(
|
||||
"/etc/letsencrypt/live/resqaws.jankstudio.com/fullchain.pem", cert_reqs=ssl.CERT_NONE,
|
||||
tls_version=ssl.PROTOCOL_TLSv1)
|
||||
# configure mqtt broker to accept localhost
|
||||
client.connect("localhost", 1883, 60)
|
||||
|
||||
client.connect("resqaws.jankstudio.com", 8883, 60)
|
||||
client.loop_start()
|
||||
redis_listen(client, logger)
|
||||
client.loop_end()
|
||||
|
||||
logger.info("Starting redis thread")
|
||||
t = Thread(target=getRedis, args=(1, client, logger))
|
||||
|
||||
t.start()
|
||||
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
client.loop_forever()
|
||||
#client.loop_forever()
|
||||
|
||||
|
||||
#logging.basicConfig(filename='/tmp/mqtt_sender.log', level=logging.INFO)
|
||||
#logging.info('Started mqtt_sender')
|
||||
|
||||
#pid = "/tmp/mqtt_sender.pid"
|
||||
#daemon = Daemonize(app="mqtt_sender", pid=pid, action=main)
|
||||
#daemon.start()
|
||||
main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue