some changes

This commit is contained in:
Wolfgang Hottgenroth 2016-10-31 16:27:39 +01:00
parent ebb18e3d66
commit 6d415e481c
4 changed files with 16 additions and 5 deletions

View File

@ -26,7 +26,7 @@ class MongoWriter(threading.Thread):
mongoClient = pymongo.MongoClient(self.dbhost)
db = mongoClient[self.database]
res = db[self.collection].insert_one(msg)
Logger.debug("MongoWriter inserts: %s" % res.inserted_id)
Logger.log("MongoWriter inserts: %s" % res.inserted_id)
except pymongo.errors.ServerSelectionTimeoutError, e:
Logger.log("Exception %s in MongoWriter, run" % str(e))
Logger.log("Msg dropped: %s" % msg)

View File

@ -53,7 +53,7 @@ class MonitorPublisher(threading.Thread):
self.nameMap[name]['cnt'] = 0;
self.nameMap[name]['oldbody'] = self.nameMap[name]['body']
message = str(self.nameMap[name]['slot']) + " " + self.nameMap[name]['header'] + " " + self.nameMap[name]['body']
client.publish("IoT/Monitor/Message", message)
client.publish("IoT/Monitor/Message", message, retain=True)
Logger.log("MonitorPublisher has sent: " + message)
else:
self.nameMap[name]['cnt'] += 1

View File

@ -2,13 +2,14 @@ from time import gmtime, strftime
class Logger(object):
debugFlag = False
debugToStdoutFlag = 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:
if cls.debugFlag and cls.debugToStdoutFlag:
print data
@classmethod
@ -20,6 +21,10 @@ class Logger(object):
def debugEnable(cls):
cls.debugFlag = True
@classmethod
def debugToStdoutEnable(cls):
cls.debugToStdoutFlag = True
@classmethod
def debugDisable(cls):
cls.debugFlag = False

8
mqtt2mongo.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/python
'''
Created on 20.05.2015
@ -16,7 +17,8 @@ import sys
from logger import Logger
import time
DEBUG = False
DEBUG = True
DEBUG_TO_STDOUT = False
BACKGROUND = True
PID_FILE = "/opt/logs/mqtt2mongo.pid"
LOG_FILE = "/opt/logs/mqtt2mongo.log"
@ -43,6 +45,10 @@ Logger.openlog(LOG_FILE)
if DEBUG:
Logger.debugEnable()
if DEBUG_TO_STDOUT:
Logger.debugToStdoutEnable()
Logger.log("mqtt2mongo starting")