This commit is contained in:
Wolfgang Hottgenroth 2015-06-19 22:27:46 +02:00
commit 038e58bb50
3 changed files with 84 additions and 0 deletions

3
.hgignore Normal file
View File

@ -0,0 +1,3 @@
syntax: glob
*.pyc

50
Watchdog.py Normal file
View File

@ -0,0 +1,50 @@
import os
import sys
from logger import Logger
import paho.mqtt.client as mqtt
import time
DEBUG = False
BACKGROUND = True
PID_FILE = "/tmp/watchdog.pid"
LOG_FILE = "/tmp/watchdog.log"
BROKER = "mqttbroker"
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")

31
logger.py Normal file
View File

@ -0,0 +1,31 @@
from time import gmtime, strftime
class Logger(object):
debugFlag = False
@classmethod
def log(cls, data):
t = strftime("%d %b %Y %H:%M:%S", gmtime())
with open(cls.logfile, 'a') as f:
f.write("%s %s\n" % (t, data))
if cls.debugFlag:
print data
@classmethod
def debug(cls, data):
if cls.debugFlag:
cls.log(data)
@classmethod
def debugEnable(cls):
cls.debugFlag = True
@classmethod
def debugDisable(cls):
cls.debugFlag = False
@classmethod
def openlog(cls, logfile):
cls.logfile = logfile