Changeset c53f31a for src/daemon


Ignore:
Timestamp:
Jul 20, 2016, 9:15:08 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
0272b16
Parents:
7549de9
Message:

Fixed gnuboat daemon and added getopt command line options

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/daemon/gnuboat

    r7549de9 rc53f31a  
    2626import os
    2727import dateutil
     28import getopt
     29import sys
    2830from EPD import EPD
    2931from PIL import Image
    … …  
    3537marginVertical = 10
    3638marginHorizontal = marginVertical
    37 rrdFile = "/home/joachim/public_html/gnuboat/webservice/data/log-webservice.rrd"
    38 shutdownFile = "/var/spool/gnuboat/shutdown.request"
    39 logPeriod = 1
    40 displayPeriod = 5
     39
     40# default values for command line arguments
     41rrdfile = "/var/www/gnuboat/webservice/data/log-webservice.rrd"
     42shutdownfile = "/var/spool/gnuboat/shutdown.request"
     43logperiod = 1
     44displayperiod = 5
    4145
    4246shutdownRequested = None
    … …  
    6670   
    6771  def run(self):
     72    global logperiod
    6873    try:
    6974      while self._running:
    7075        self._currentValue = self._session.next()
    71         time.sleep(float(logPeriod)/2.0)
     76        time.sleep(float(logperiod)/2.0)
    7277    except StopIteration:
    7378      pass
    … …  
    8994
    9095  if not os.path.isfile(databaseFile):
    91     rrdtool.create(rrdFile,
     96    rrdtool.create(databaseFile,
    9297                   "--start",
    9398                   "N",
    … …  
    143148  epd.update()
    144149
    145 def checkShutdownRequest():
    146   global shutdownRequest
     150def checkShutdownRequest(shutdownFile):
     151  global shutdownRequested
    147152  # have look for new shutdown requests
    148153  if os.path.isfile(shutdownFile):
    … …  
    156161      os.system("sudo shutdown -r now")
    157162
    158 if __name__ == '__main__':
    159   createDatabase(rrdFile)
     163def usage():
     164  print "Usage of gnuboat daemon:"
     165  print "  gnuboat -r PATH_TO_RRD_DATABAE_FILE -s PATH_TO_SHUTDOWN_REQUEST_FILE [-l [LOG_UPDATE_PERIOD] [-d DISPLAY_UPDATE_PERIOD]"
     166  print ""
     167  print "  -h --help              Display this help"
     168  print "  -r --rrdfile           Path to the rrd database used by the webservice to serve the clients"
     169  print "  -s --shutdownfile      Path to the shutdown request file, used by the webservice to request a shutdown of the system running gnuboat"
     170  print "  -l --logperiod         Time in seconds to update the logs"
     171  print "  -d --displayperiod     Timein seconds to update the display"
     172
     173def main(argv):
     174  global rrdfile
     175  global shutdownfile
     176  global logperiod
     177  global displayperiod
     178
     179  try:                               
     180    opts, args = getopt.getopt(argv, "hr:s:l:d:", ["help", "rrdfile=", "shutdownfile=", "logperiod=", "displayperiod="])
     181  except getopt.GetoptError:           
     182    usage()                         
     183    sys.exit(2)
     184
     185  for option, argument in opts:
     186    if option == ("-h", "--help"):
     187      usage()
     188      sys.exit()
     189    elif option in ("-r", "--rrdfile"):
     190       rrdfile = argument
     191    elif option in ("-s", "--shutdownfile"):
     192      shutdownfile = argument
     193    elif option in ("-l", "--logperiod"):
     194      logperiod = argument
     195    elif option in ("-d", "--displayperiod"):
     196      displayperiod = argument
     197    else:
     198      assert False, "unhandled option"
     199
     200  if not os.path.exists(os.path.dirname(rrdfile)):
     201    print "rrd database directory not found %s" % os.path.dirname(rrdfile)
     202    print ""
     203    usage()
     204    sys.exit(2)
     205
     206  if not os.path.exists(os.path.dirname(shutdownfile)):
     207    print "Shutdown request directory not found %s" % os.path.dirname(shutdownfile)
     208    print ""
     209    usage()
     210    sys.exit(2)
     211
     212  if logperiod < 1:
     213    print "Log period must be >= 1"
     214    print ""
     215    usage()
     216    sys.exit(2)
     217
     218  if displayperiod < 1 or displayperiod < logperiod:
     219    print "Display period must be >= 1 and could not be faster than log period"
     220    print ""
     221    usage()
     222    sys.exit(2)
     223
     224  createDatabase(rrdfile)
    160225
    161226  epd = EPD()
    … …  
    168233    while True:
    169234      # In the main thread, every logPeriod seconds print the current value
    170       time.sleep(logPeriod)
     235      time.sleep(logperiod)
    171236      latitude = gpsp.latitude()
    172237      longitude = gpsp.longitude()
    … …  
    178243     
    179244      # update rrd database for the webservice
    180       rrdtool.update(rrdFile, "N:%s:%s:%s:%s:%s:%s:%s" % (latitude, longitude, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix))
     245      rrdtool.update(rrdfile, "N:%s:%s:%s:%s:%s:%s:%s" % (latitude, longitude, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix))
    181246      # update display, if needed
    182       if time.time() % displayPeriod == 0:
     247      if round(time.time(),0) % displayperiod == 0:
    183248        displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix)
    184249      #check for shutdown request
    185       checkShutdownRequest()
     250      checkShutdownRequest(shutdownfile)
    186251       
    187252  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    188253    gpsp.stop()
    189254    gpsp.join() # wait for the thread to finish what it's doing
     255
     256if __name__ == "__main__":
     257  main(sys.argv[1:])
Note: See TracChangeset for help on using the changeset viewer.