Changeset 04de4a4 for src/daemon


Ignore:
Timestamp:
Aug 23, 2016, 12:05:09 AM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
3f77ad0
Parents:
2925c4a (diff), 9be4f3f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of ​https://svn.falaba.de/gnuboat

Location:
src/daemon
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/daemon/gnuboat

    r2925c4a r04de4a4  
    3131import getopt
    3232import sys
    33 from EPD import EPD
    34 from PIL import Image
    35 from PIL import ImageDraw
    36 from PIL import ImageFont
     33
     34try:
     35  from EPD import EPD
     36  from PIL import Image
     37  from PIL import ImageDraw
     38  from PIL import ImageFont
     39  epdEnabled = True
     40except:
     41  epdEnabled = False
    3742
    3843WHITE=1
    … …  
    4247
    4348# default values for command line arguments
    44 rrdfile = "/var/www/html/webservice/data/log-webservice.rrd"
     49rrdfile = "/usr/share/gnuboat/webservice/data/log-webservice.rrd"
    4550shutdownfile = "/var/spool/gnuboat/shutdown.request"
    4651logperiod = 1
    … …  
    4853
    4954shutdownRequested = None
     55latitude = None
     56longitude = None
     57speedThroughWater = None
     58speedOverGround = None
     59waterDepth = None
     60waterTemperature = None
     61gpsFix = None
    5062
    5163# Copied from https://gist.github.com/wolfg1969/4653340
    … …  
    8193      pass
    8294
     95"""@class DisplayHandler
     96"""
     97class DisplayHandler(threading.Thread):
     98  def __init__(self, displayperiod):
     99    threading.Thread.__init__(self)
     100    self._epd = EPD()
     101    self._epd.clear()
     102    self._updatePeriod = displayperiod
     103    self._running = True
     104   
     105  def stop(self):
     106    self._running = False
     107   
     108  def run(self):
     109    global latitude
     110    global longitude
     111    global speedThroughWater
     112    global speedOverGround
     113    global waterDepth
     114    global waterTemperature
     115    global gpsFix
     116
     117    try:
     118      while self._running:
     119        self.displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix)
     120        time.sleep(self._updatePeriod)
     121    except StopIteration:
     122      pass
     123
     124  def displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix):
     125    if not epdEnabled:
     126      return
     127
     128    # requires dejavu package!!!!
     129    fontValues = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 34)
     130    fontTitles = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)
     131
     132    # initially set all white background
     133    image = Image.new('1', epd.size, WHITE)
     134    width, height = image.size
     135
     136    rowHeightTitles = 25
     137    rowHeightValues = float(height - 2 * rowHeightTitles - 3 * marginVertical) / 2.0
     138    columnWidthUnit = 30
     139    columnWidthValues = int(float(width - 3 * marginHorizontal - 2 * columnWidthUnit) / 2.0)
     140
     141    draw = ImageDraw.Draw(image)
     142    # draw.rectangle((0, 0, width, height), fill=WHITE, outline=WHITE)
     143
     144    x = marginHorizontal
     145    y = marginVertical
     146    draw.text((x, y), "STW", fill=BLACK, font=fontTitles)
     147    if speedThroughWater is not None:
     148      draw.text((x, y + rowHeightTitles), str(speedThroughWater), fill=BLACK, font=fontValues)
     149    draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
     150
     151    x = x + marginHorizontal + columnWidthValues + columnWidthUnit
     152    draw.text((x, y), "SOG", fill=BLACK, font=fontTitles)
     153    if speedOverGround is not None:
     154      draw.text((x, y + rowHeightTitles), str(speedOverGround), fill=BLACK, font=fontValues)
     155    draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
     156
     157    x = marginHorizontal
     158    y = y + rowHeightTitles + rowHeightValues + marginVertical
     159    draw.text((x, y), "Depth", fill=BLACK, font=fontTitles)
     160    if waterDepth is not None:
     161      draw.text((x, y + rowHeightTitles), str(waterDepth), fill=BLACK, font=fontValues)
     162    draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "m", fill=BLACK, font=fontTitles)
     163
     164    x = x + marginHorizontal + columnWidthValues + columnWidthUnit
     165    draw.text((x, y), "Water", fill=BLACK, font=fontTitles)
     166    if waterTemperature is not None:
     167      draw.text((x, y + rowHeightTitles), str(waterTemperature), fill=BLACK, font=fontValues)
     168    draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "C", fill=BLACK, font=fontTitles)
     169
     170    x = int((x + marginHorizontal + columnWidthValues) * 0.93)
     171    draw.text((x, y), "Fix", fill=BLACK, font=fontTitles)
     172    if gpsFix is not None:
     173      draw.text((x, y + rowHeightTitles), str(gpsFix), fill=BLACK, font=fontValues)
     174 
     175    epd.display(image)
     176    epd.update()
     177
    83178def createDatabase(databaseFile):
    84179  rrdDataSources = [ 'DS:latitude:GAUGE:15:-90:90',
    … …  
    104199                   rrdDataSources,
    105200                   rrdArchives)
    106 
    107 def displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix):
    108   # requires dejavu package!!!!
    109   fontValues = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 34)
    110   fontTitles = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)
    111 
    112   # initially set all white background
    113   image = Image.new('1', epd.size, WHITE)
    114   width, height = image.size
    115 
    116   rowHeightTitles = 25
    117   rowHeightValues = float(height - 2 * rowHeightTitles - 3 * marginVertical) / 2.0
    118   columnWidthUnit = 30
    119   columnWidthValues = int(float(width - 3 * marginHorizontal - 2 * columnWidthUnit) / 2.0)
    120 
    121   draw = ImageDraw.Draw(image)
    122   # draw.rectangle((0, 0, width, height), fill=WHITE, outline=WHITE)
    123 
    124   x = marginHorizontal
    125   y = marginVertical
    126   draw.text((x, y), "STW", fill=BLACK, font=fontTitles)
    127   draw.text((x, y + rowHeightTitles), str(speedThroughWater), fill=BLACK, font=fontValues)
    128   draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
    129 
    130   x = x + marginHorizontal + columnWidthValues + columnWidthUnit
    131   draw.text((x, y), "SOG", fill=BLACK, font=fontTitles)
    132   draw.text((x, y + rowHeightTitles), str(speedOverGround), fill=BLACK, font=fontValues)
    133   draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
    134 
    135   x = marginHorizontal
    136   y = y + rowHeightTitles + rowHeightValues + marginVertical
    137   draw.text((x, y), "Depth", fill=BLACK, font=fontTitles)
    138   draw.text((x, y + rowHeightTitles), str(waterDepth), fill=BLACK, font=fontValues)
    139   draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "m", fill=BLACK, font=fontTitles)
    140 
    141   x = x + marginHorizontal + columnWidthValues + columnWidthUnit
    142   draw.text((x, y), "Water", fill=BLACK, font=fontTitles)
    143   draw.text((x, y + rowHeightTitles), str(waterTemperature), fill=BLACK, font=fontValues)
    144   draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "C", fill=BLACK, font=fontTitles)
    145 
    146   x = int((x + marginHorizontal + columnWidthValues) * 0.93)
    147   draw.text((x, y), "Fix", fill=BLACK, font=fontTitles)
    148   draw.text((x, y + rowHeightTitles), str(gpsFix), fill=BLACK, font=fontValues)
    149  
    150   epd.display(image)
    151   epd.update()
    152201
    153202def checkShutdownRequest(shutdownFile):
    … …  
    181230  global logperiod
    182231  global displayperiod
     232  global latitude
     233  global longitude
     234  global speedThroughWater
     235  global speedOverGround
     236  global waterDepth
     237  global waterTemperature
     238  global gpsFix
    183239
    184240  try:                               
    … …  
    229285  createDatabase(rrdfile)
    230286
    231   epd = EPD()
    232   epd.clear()
     287  displayHandler = None
     288  if epdEnabled:
     289    displayHandler = DisplayHandler(displayperiod)
    233290
    234291  gpsp = GpsdPoller()
    … …  
    249306      # update rrd database for the webservice
    250307      rrdtool.update(rrdfile, "N:%s:%s:%s:%s:%s:%s:%s" % (latitude, longitude, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix))
    251       # update display, if needed
    252       if round(time.time(),0) % displayperiod == 0:
    253         displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix)
    254308      #check for shutdown request
    255309      checkShutdownRequest(shutdownfile)
    … …  
    258312    gpsp.stop()
    259313    gpsp.join() # wait for the thread to finish what it's doing
     314    if displayHandler is not None:
     315      displayHandler.stop()
     316      displayHandler.join() # wait for the thread to finish what it's doing
    260317
    261318if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.