Changeset 10edeb0 for src


Ignore:
Timestamp:
Jul 20, 2016, 8:36:05 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
7549de9
Parents:
73c34f3
Message:
  • Improved the gnuboat daemon
  • Added gnutboat-splashscreen program for epd
Location:
src/daemon
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/daemon/gnuboat

    r73c34f3 r10edeb0  
    11#!/usr/bin/python
     2# -*- coding: utf-8 -*-
    23'''
    34    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    … …  
    2223import time
    2324import threading
    24 import os.path
     25import re
     26import os
     27import dateutil
     28from EPD import EPD
     29from PIL import Image
     30from PIL import ImageDraw
     31from PIL import ImageFont
    2532
    26 rrdFile = "/home/jlan11/public_html/gnuboat/webservice/data/log-webservice.rrd"
     33WHITE=1
     34BLACK=0
     35marginVertical = 10
     36marginHorizontal = marginVertical
     37rrdFile = "/home/joachim/public_html/gnuboat/webservice/data/log-webservice.rrd"
     38shutdownFile = "/var/spool/gnuboat/shutdown.request"
    2739logPeriod = 1
     40displayPeriod = 5
     41
     42shutdownRequested = None
    2843
    2944# Copied from https://gist.github.com/wolfg1969/4653340
    … …  
    5873      pass
    5974
    60 def createDatabase(databaseFile)
     75def createDatabase(databaseFile):
    6176  rrdDataSources = [ 'DS:latitude:GAUGE:15:-90:90',
    6277                     'DS:longitude:GAUGE:15:-180:180',
    … …  
    8297                   rrdArchives)
    8398
     99def 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
     145def 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
    84158if __name__ == '__main__':
    85159  createDatabase(rrdFile)
     160
     161  epd = EPD()
     162  epd.clear()
    86163
    87164  gpsp = GpsdPoller()
    88165  try:
    89166    gpsp.start()
    90     print "Collecting data..."
    91167    # gpsp now polls every .2 seconds for new data, storing it in self.current_value
    92168    while True:
    93       # In the main thread, every 5 seconds print the current value
     169      # In the main thread, every logPeriod seconds print the current value
    94170      time.sleep(logPeriod)
    95171      latitude = gpsp.latitude()
    … …  
    100176      waterTemperature = random.randrange(15, 25) + 0.5;
    101177      gpsFix = gpsp.gpsFix()
     178     
     179      # update rrd database for the webservice
    102180      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()
    103186       
    104187  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    105     print "\nKilling Thread..."
    106188    gpsp.stop()
    107189    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.