MqttWatchdog/Watchdog.py

51 lines
727 B
Python
Raw Normal View History

2015-06-19 22:27:46 +02:00
import os
import sys
from logger import Logger
import paho.mqtt.client as mqtt
import time
DEBUG = False
2016-06-25 15:52:45 +02:00
BACKGROUND = False
2015-06-19 22:27:46 +02:00
PID_FILE = "/tmp/watchdog.pid"
LOG_FILE = "/tmp/watchdog.log"
2016-06-25 15:52:45 +02:00
BROKER = "172.16.2.15"
2015-06-19 22:27:46 +02:00
if BACKGROUND:
pid = os.fork()
else:
pid = 0
if pid:
pidFile = file(PID_FILE , mode='w')
pidFile.write("%i\n" % pid)
pidFile.close()
sys.exit(0)
Logger.openlog(LOG_FILE)
if DEBUG:
Logger.debugEnable()
Logger.log("watchdog started")
mqttClient = mqtt.Client()
mqttClient.connect(BROKER, 1883, 60)
mqttClient.loop_start()
try:
while True:
time.sleep(1)
Logger.debug("WauWau!")
mqttClient.publish('IoT/Watchdog', 'WauWau!')
finally:
Logger.log("watchdog terminating")