Ignore:
Timestamp:
May 4, 2020, 8:34:21 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
82eff76
Parents:
97efbbf
Message:

Added deviceservice implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/gnuboat/deviceservice_impl/controllers/DeviceController.py

    r97efbbf r9b8dd22  
    2020import rrdtool
    2121import os
     22import datetime
    2223
    2324import gnuboat.common.device.rrdschema as rrdschema
    2425from gnuboat.common.webservice.HttpErrors import (
    25     GnuboatBadRequest, GnuboatUnauthorized,
    2626    GnuboatNotFound, GnuboatInternalServerError
    2727)
    … …  
    3333        result = rrdtool.lastupdate(rrdfile)
    3434    except Exception as e:
    35         raise GnuboatInternalServerError(detail='Could not query rrd database. Error: {}'.format(repr(e)))
     35        raise GnuboatInternalServerError(
     36            detail='Could not query rrd database. Error: {}'.format(repr(e)))
    3637    if 'ds' not in result:
    3738        raise GnuboatNotFound(detail='No databset found')
     39    if (datetime.datetime.now() - result['date']).total_seconds() > 10:
     40        raise GnuboatNotFound(detail='Last entry is older than 10s')
    3841    return {
    3942        'speedThroughWater': result['ds']['speedthroughwater'],
    … …  
    4952def track_get():
    5053    rrdfile = _rrdfile_get()
    51    
     54
    5255    track = {}
    5356    for archive in rrdschema.archives:
    54         result = _rrd_fetch(rrdfile, archive.resolution, '{}s'.format(archive.duration))
     57        result = _rrd_fetch(rrdfile, archive.resolution,
     58                            '{}s'.format(archive.duration))
    5559
    5660        start, end, interval = result[0]
    … …  
    6468        watertemperature = keys.index('watertemperature')
    6569        gpsfix = keys.index('gpsfix')
    66        
     70
    6771        i = 0
    6872        for dataset in result[2]:
    … …  
    7983                }
    8084            i += 1
    81    
     85
    8286    return track
    8387
    … …  
    111115def _rrd_fetch(rrdfile, interval, period):
    112116    try:
    113         return rrdtool.fetch(rrdfile, 'AVERAGE', '--resolution', str(interval), '--start', '-{}'.format(period), '--align-start')
     117        return rrdtool.fetch(
     118            rrdfile, 'AVERAGE', '--resolution', str(interval),
     119            '--start', '-{}'.format(period), '--align-start'
     120        )
    114121    except Exception as e:
    115         raise GnuboatInternalServerError(detail='Could not fetch from rrd database. Error: {}'.format(repr(e)))
    116    
     122        raise GnuboatInternalServerError(
     123            detail=('Could not fetch from rrd database. '
     124                    'Error: {}').format(repr(e)))
     125
    117126
    118127def gpsfix_to_enum(gpsfix):
    119128    if gpsfix == 2:
    120         return "2D";
     129        return "2D"
    121130    elif gpsfix == 3:
    122         return "3D";
     131        return "3D"
    123132    else:
    124         return "-";
     133        return "-"
Note: See TracChangeset for help on using the changeset viewer.