From cac05ce2f2d07edc07605dc361613958a261d08a Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 6 Oct 2017 23:29:44 +0200 Subject: [PATCH 1/2] 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 --- boswatch.py | 71 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/boswatch.py b/boswatch.py index df1e5e6..b00792a 100755 --- a/boswatch.py +++ b/boswatch.py @@ -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: From 4028ffd6a1993ca6d20f57b6f4ad3f01f475d84f Mon Sep 17 00:00:00 2001 From: Florian Date: Sat, 7 Oct 2017 22:38:11 +0200 Subject: [PATCH 2/2] Update boswatch.py - daemonize observing thread - csv-dir depending on installation-dir - cleaning up the code --- boswatch.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/boswatch.py b/boswatch.py index b00792a..6cdbeeb 100755 --- a/boswatch.py +++ b/boswatch.py @@ -74,39 +74,39 @@ except: # # define a function for observing csv-files # -def csv_watch(): +def csv_watch(dir): wm = pyinotify.WatchManager() mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY t = threading.currentThread() + logging.debug("CSV-Watch-Dir: %s", dir) class EventHandler(pyinotify.ProcessEvent): def process_IN_CREATE(self, event): try: - logging.debug("+++++++++++ Reloaded csv...") + logging.debug("Reloading 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) + logging.error("cannot reload description lists") + logging.debug("cannot reload description lists", exc_info=True) def process_IN_MODIFY(self, event): try: - logging.debug("++++++++++++ Reloaded csv...") + logging.debug("Reloading 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) + logging.error("cannot reload description lists") + logging.debug("cannot reload description lists", exc_info=True) handler = EventHandler() notifier = pyinotify.Notifier(wm, handler) - wdd = wm.add_watch('/opt/boswatch_develop/csv', mask, rec=True) + wdd = wm.add_watch(dir, mask, rec=True) - while getattr(t, "run", True): - notifier.loop() + notifier.loop() # # Main program @@ -201,11 +201,12 @@ try: # start a new oberserver.thread # try: - thread = threading.Thread(target = csv_watch) + thread = threading.Thread(target = csv_watch, args = (globalVars.script_path+'/csv/',)) + thread.daemon = True # start it as daemon to avoid trouble when exiting Boswatch 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]) + logging.error("Unable to start thread to observe csv-directory.", exc_info=True) # # For debug display/log args @@ -432,7 +433,7 @@ try: logging.info("Testdata: %s", testData.rstrip(' \t\n\r')) from includes import decoder decoder.decode(freqConverter.freqToHz(args.freq), testData) -# time.sleep(10) + time.sleep(5) logging.debug("test finished") except KeyboardInterrupt: @@ -469,8 +470,6 @@ 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: