Update boswatch.py

- create a function to watch for changes in csv-dir
- import pyinotify (use pip to install it if necessary) and threading
- starting thread to work parallel
- end thread while exiting boswatch
This commit is contained in:
Florian 2017-10-06 23:29:44 +02:00 committed by GitHub
parent 18425e2be2
commit cac05ce2f2

View file

@ -18,12 +18,15 @@ GitHUB: https://github.com/Schrolli91/BOSWatch
import logging
import logging.handlers
import argparse # for parse the args
import ConfigParser # for parse the config file
import os # for log mkdir
import sys # for py version
import time # for time.sleep()
import subprocess # for starting rtl_fm and multimon-ng
import argparse # for parse the args
import ConfigParser # for parse the config file
import os # for log mkdir
import time # for time.sleep()
import subprocess # for starting rtl_fm and multimon-ng
## New for reloading csv
import pyinotify
import threading
from includes import globalVars # Global variables
from includes import MyTimedRotatingFileHandler # extension of TimedRotatingFileHandler
@ -68,6 +71,42 @@ except:
print "ERROR: cannot parsing the arguments"
exit(1)
#
# define a function for observing csv-files
#
def csv_watch():
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY
t = threading.currentThread()
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
try:
logging.debug("+++++++++++ Reloaded csv...")
if globalVars.config.getboolean("FMS","idDescribed") or globalVars.config.getboolean("ZVEI","idDescribed") or globalVars.config.getboolean("POC","idDescribed"):
from includes import descriptionList
descriptionList.loadDescriptionLists()
except:
# It's an error, but we could work without that stuff...
logging.error("cannot load description lists")
logging.debug("cannot load description lists", exc_info=True)
def process_IN_MODIFY(self, event):
try:
logging.debug("++++++++++++ Reloaded csv...")
if globalVars.config.getboolean("FMS","idDescribed") or globalVars.config.getboolean("ZVEI","idDescribed") or globalVars.config.getboolean("POC","idDescribed"):
from includes import descriptionList
descriptionList.loadDescriptionLists()
except:
# It's an error, but we could work without that stuff...
logging.error("cannot load description lists")
logging.debug("cannot load description lists", exc_info=True)
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('/opt/boswatch_develop/csv', mask, rec=True)
while getattr(t, "run", True):
notifier.loop()
#
# Main program
@ -158,14 +197,22 @@ try:
logging.error("cannot clear Logfiles")
logging.debug("cannot clear Logfiles", exc_info=True)
#
# start a new oberserver.thread
#
try:
thread = threading.Thread(target = csv_watch)
thread.start()
logging.debug("Thread for csv-watch started")
except:
logging.error("Unable to start thread to observe csv-directory: %s",sys.exc_info()[0])
#
# For debug display/log args
#
try:
logging.debug("SW Version: %s",globalVars.versionNr)
logging.debug("Branch: %s",globalVars.branch)
logging.debug("Build Date: %s",globalVars.buildDate)
logging.debug("Python Vers: %s",sys.version)
logging.debug("BOSWatch given arguments")
if args.test:
logging.debug(" - Test-Mode!")
@ -223,9 +270,6 @@ try:
configHandler.checkConfig("FMS")
configHandler.checkConfig("ZVEI")
configHandler.checkConfig("POC")
configHandler.checkConfig("Plugins")
configHandler.checkConfig("Filters")
#NMAHandler is outputed below
except:
# we couldn't work without config -> exit
logging.critical("cannot read config file")
@ -246,7 +290,6 @@ try:
logging.error("cannot set loglevel of fileHandler")
logging.debug("cannot set loglevel of fileHandler", exc_info=True)
#
# Add NMA logging handler
#
@ -389,7 +432,7 @@ try:
logging.info("Testdata: %s", testData.rstrip(' \t\n\r'))
from includes import decoder
decoder.decode(freqConverter.freqToHz(args.freq), testData)
#time.sleep(1)
# time.sleep(10)
logging.debug("test finished")
except KeyboardInterrupt:
@ -426,6 +469,8 @@ finally:
if globalVars.config.getboolean("BOSWatch","processAlarmAsync") == True:
logging.debug("waiting 3s for threads...")
time.sleep(3)
thread.run = False
thread.join()
logging.info("BOSWatch exit()")
logging.shutdown()
if nmaHandler: