some changes
This commit is contained in:
parent
ebb18e3d66
commit
6d415e481c
@ -26,7 +26,7 @@ class MongoWriter(threading.Thread):
|
|||||||
mongoClient = pymongo.MongoClient(self.dbhost)
|
mongoClient = pymongo.MongoClient(self.dbhost)
|
||||||
db = mongoClient[self.database]
|
db = mongoClient[self.database]
|
||||||
res = db[self.collection].insert_one(msg)
|
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:
|
except pymongo.errors.ServerSelectionTimeoutError, e:
|
||||||
Logger.log("Exception %s in MongoWriter, run" % str(e))
|
Logger.log("Exception %s in MongoWriter, run" % str(e))
|
||||||
Logger.log("Msg dropped: %s" % msg)
|
Logger.log("Msg dropped: %s" % msg)
|
||||||
|
@ -53,7 +53,7 @@ class MonitorPublisher(threading.Thread):
|
|||||||
self.nameMap[name]['cnt'] = 0;
|
self.nameMap[name]['cnt'] = 0;
|
||||||
self.nameMap[name]['oldbody'] = self.nameMap[name]['body']
|
self.nameMap[name]['oldbody'] = self.nameMap[name]['body']
|
||||||
message = str(self.nameMap[name]['slot']) + " " + self.nameMap[name]['header'] + " " + 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)
|
Logger.log("MonitorPublisher has sent: " + message)
|
||||||
else:
|
else:
|
||||||
self.nameMap[name]['cnt'] += 1
|
self.nameMap[name]['cnt'] += 1
|
||||||
|
@ -2,13 +2,14 @@ from time import gmtime, strftime
|
|||||||
|
|
||||||
class Logger(object):
|
class Logger(object):
|
||||||
debugFlag = False
|
debugFlag = False
|
||||||
|
debugToStdoutFlag = False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def log(cls, data):
|
def log(cls, data):
|
||||||
t = strftime("%d %b %Y %H:%M:%S", gmtime())
|
t = strftime("%d %b %Y %H:%M:%S", gmtime())
|
||||||
with open(cls.logfile, 'a') as f:
|
with open(cls.logfile, 'a') as f:
|
||||||
f.write("%s %s\n" % (t, data))
|
f.write("%s %s\n" % (t, data))
|
||||||
if cls.debugFlag:
|
if cls.debugFlag and cls.debugToStdoutFlag:
|
||||||
print data
|
print data
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -20,6 +21,10 @@ class Logger(object):
|
|||||||
def debugEnable(cls):
|
def debugEnable(cls):
|
||||||
cls.debugFlag = True
|
cls.debugFlag = True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def debugToStdoutEnable(cls):
|
||||||
|
cls.debugToStdoutFlag = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def debugDisable(cls):
|
def debugDisable(cls):
|
||||||
cls.debugFlag = False
|
cls.debugFlag = False
|
||||||
|
8
mqtt2mongo.py
Normal file → Executable file
8
mqtt2mongo.py
Normal file → Executable file
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
'''
|
'''
|
||||||
Created on 20.05.2015
|
Created on 20.05.2015
|
||||||
|
|
||||||
@ -16,7 +17,8 @@ import sys
|
|||||||
from logger import Logger
|
from logger import Logger
|
||||||
import time
|
import time
|
||||||
|
|
||||||
DEBUG = False
|
DEBUG = True
|
||||||
|
DEBUG_TO_STDOUT = False
|
||||||
BACKGROUND = True
|
BACKGROUND = True
|
||||||
PID_FILE = "/opt/logs/mqtt2mongo.pid"
|
PID_FILE = "/opt/logs/mqtt2mongo.pid"
|
||||||
LOG_FILE = "/opt/logs/mqtt2mongo.log"
|
LOG_FILE = "/opt/logs/mqtt2mongo.log"
|
||||||
@ -43,6 +45,10 @@ Logger.openlog(LOG_FILE)
|
|||||||
if DEBUG:
|
if DEBUG:
|
||||||
Logger.debugEnable()
|
Logger.debugEnable()
|
||||||
|
|
||||||
|
if DEBUG_TO_STDOUT:
|
||||||
|
Logger.debugToStdoutEnable()
|
||||||
|
|
||||||
|
|
||||||
Logger.log("mqtt2mongo starting")
|
Logger.log("mqtt2mongo starting")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user