Changes in / [7549de9:0c7eb83]


Ignore:
Location:
src/daemon
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • src/daemon/gnuboat

    r7549de9 r0c7eb83  
    11#!/usr/bin/python
    2 # -*- coding: utf-8 -*-
    32'''
    43    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    … …  
    2322import time
    2423import threading
    25 import re
    26 import os
    27 import dateutil
    28 from EPD import EPD
    29 from PIL import Image
    30 from PIL import ImageDraw
    31 from PIL import ImageFont
     24import os.path
    3225
    33 WHITE=1
    34 BLACK=0
    35 marginVertical = 10
    36 marginHorizontal = marginVertical
    37 rrdFile = "/home/joachim/public_html/gnuboat/webservice/data/log-webservice.rrd"
    38 shutdownFile = "/var/spool/gnuboat/shutdown.request"
     26rrdFile = "/home/jlan11/public_html/gnuboat/webservice/data/log-webservice.rrd"
    3927logPeriod = 1
    40 displayPeriod = 5
    41 
    42 shutdownRequested = None
    4328
    4429# Copied from https://gist.github.com/wolfg1969/4653340
    … …  
    7358      pass
    7459
    75 def createDatabase(databaseFile):
     60def createDatabase(databaseFile)
    7661  rrdDataSources = [ 'DS:latitude:GAUGE:15:-90:90',
    7762                     'DS:longitude:GAUGE:15:-180:180',
    … …  
    9782                   rrdArchives)
    9883
    99 def displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix):
    100   # requires dejavu package!!!!
    101   fontValues = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 34)
    102   fontTitles = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)
    103 
    104   # initially set all white background
    105   image = Image.new('1', epd.size, WHITE)
    106   width, height = image.size
    107 
    108   rowHeightTitles = 25
    109   rowHeightValues = float(height - 2 * rowHeightTitles - 3 * marginVertical) / 2.0
    110   columnWidthUnit = 30
    111   columnWidthValues = int(float(width - 3 * marginHorizontal - 2 * columnWidthUnit) / 2.0)
    112 
    113   draw = ImageDraw.Draw(image)
    114   # draw.rectangle((0, 0, width, height), fill=WHITE, outline=WHITE)
    115 
    116   x = marginHorizontal
    117   y = marginVertical
    118   draw.text((x, y), "STW", fill=BLACK, font=fontTitles)
    119   draw.text((x, y + rowHeightTitles), str(speedThroughWater), fill=BLACK, font=fontValues)
    120   draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
    121 
    122   x = x + marginHorizontal + columnWidthValues + columnWidthUnit
    123   draw.text((x, y), "SOG", fill=BLACK, font=fontTitles)
    124   draw.text((x, y + rowHeightTitles), str(speedOverGround), fill=BLACK, font=fontValues)
    125   draw.text((x + columnWidthValues, y + rowHeightTitles * 1.4), "kts", fill=BLACK, font=fontTitles)
    126 
    127   x = marginHorizontal
    128   y = y + rowHeightTitles + rowHeightValues + marginVertical
    129   draw.text((x, y), "Depth", fill=BLACK, font=fontTitles)
    130   draw.text((x, y + rowHeightTitles), str(waterDepth), fill=BLACK, font=fontValues)
    131   draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "m", fill=BLACK, font=fontTitles)
    132 
    133   x = x + marginHorizontal + columnWidthValues + columnWidthUnit
    134   draw.text((x, y), "Water", fill=BLACK, font=fontTitles)
    135   draw.text((x, y + rowHeightTitles), str(waterTemperature), fill=BLACK, font=fontValues)
    136   draw.text((x + int(float(columnWidthValues) / 1.5), y + rowHeightTitles * 1.4), "C", fill=BLACK, font=fontTitles)
    137 
    138   x = int((x + marginHorizontal + columnWidthValues) * 0.93)
    139   draw.text((x, y), "Fix", fill=BLACK, font=fontTitles)
    140   draw.text((x, y + rowHeightTitles), str(gpsFix), fill=BLACK, font=fontValues)
    141  
    142   epd.display(image)
    143   epd.update()
    144 
    145 def checkShutdownRequest():
    146   global shutdownRequest
    147   # have look for new shutdown requests
    148   if os.path.isfile(shutdownFile):
    149     with open(shutdownFile, "r") as fileHandler:
    150       shutdownStart = fileHandler.read().strip()
    151     os.remove(shutdownFile)
    152     shutdownRequested = dateutil.parser.parse(shutdownStart)
    153   # check existing shutdown requests
    154   if shutdownRequested is not None:
    155     if (datetime.now() - shutdownRequested) > timedelta(seconds = 0):
    156       os.system("sudo shutdown -r now")
    157 
    15884if __name__ == '__main__':
    15985  createDatabase(rrdFile)
    160 
    161   epd = EPD()
    162   epd.clear()
    16386
    16487  gpsp = GpsdPoller()
    16588  try:
    16689    gpsp.start()
     90    print "Collecting data..."
    16791    # gpsp now polls every .2 seconds for new data, storing it in self.current_value
    16892    while True:
    169       # In the main thread, every logPeriod seconds print the current value
     93      # In the main thread, every 5 seconds print the current value
    17094      time.sleep(logPeriod)
    17195      latitude = gpsp.latitude()
    … …  
    176100      waterTemperature = random.randrange(15, 25) + 0.5;
    177101      gpsFix = gpsp.gpsFix()
    178      
    179       # update rrd database for the webservice
    180102      rrdtool.update(rrdFile, "N:%s:%s:%s:%s:%s:%s:%s" % (latitude, longitude, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix))
    181       # update display, if needed
    182       if time.time() % displayPeriod == 0:
    183         displayValues(epd, speedThroughWater, speedOverGround, waterDepth, waterTemperature, gpsFix)
    184       #check for shutdown request
    185       checkShutdownRequest()
    186103       
    187104  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
     105    print "\nKilling Thread..."
    188106    gpsp.stop()
    189107    gpsp.join() # wait for the thread to finish what it's doing
     108  print "Done.\nExiting."
Note: See TracChangeset for help on using the changeset viewer.