new
This commit is contained in:
commit
038e58bb50
50
Watchdog.py
Normal file
50
Watchdog.py
Normal 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
31
logger.py
Normal 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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user