Changes in / [c84f87e:19ac332]


Ignore:
Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/daemon/gnuboat

    rc84f87e r19ac332  
    3131import getopt
    3232import sys
    33 import socket
    34 import json
    3533
    3634try:
    … …  
    4947
    5048# default values for command line arguments
    51 rrdfile = "/var/lib/gnuboat/log-webservice.rrd"
     49rrdfile = "/usr/share/gnuboat/webservice/data/log-webservice.rrd"
    5250shutdownfile = "/var/spool/gnuboat/shutdown.request"
    53 logperiod = 2
     51logperiod = 1
    5452displayperiod = 5
    5553
    … …  
    6765  def __init__(self):
    6866    threading.Thread.__init__(self)
    69     try:
    70       self._session = gps.gps(mode=gps.WATCH_ENABLE)
    71     except:
    72       self._session = None
    73       self._running = False
    74       return
     67    self._session = gps.gps(mode=gps.WATCH_ENABLE)
    7568    self._currentValue = None
    7669    self._running = True
    7770
    7871  def latitude(self):
    79     if self._session == None:
    80       return 0
    8172    return self._session.fix.latitude
    8273   
    8374  def longitude(self):
    84     if self._session == None:
    85       return 0
    8675    return self._session.fix.longitude
    8776
    8877  def speedOverGround(self):
    89     if self._session == None:
    90       return 0
    9178    return self._session.fix.speed
    9279   
    9380  def gpsFix(self):
    94     if self._session == None:
    95       return 0
    9681    return self._session.fix.mode
    9782   
    … …  
    10489      while self._running:
    10590        self._currentValue = self._session.next()
    106         time.sleep(float(logperiod)/2.0)
    107     except StopIteration:
    108       pass
    109 
    110 """@class N2kdPoller
    111 """
    112 class N2kdPoller(threading.Thread):
    113   def __init__(self):
    114     threading.Thread.__init__(self)
    115     self._serverHost = "raspi"
    116     self._serverPort = 2597
    117     self._timeout = 1.5
    118     self._speedThroughWater = 0
    119     self._distanceLog = 0
    120     self._distanceLogTrip = 0
    121     self._waterDepth = 0
    122     self._waterTemperature = 0
    123     self._running = True
    124 
    125   def speedThroughWater(self):
    126     return self._speedThroughWater
    127    
    128   def distanceLog(self):
    129     return self._distanceLog
    130 
    131   def distanceLogTrip(self):
    132     return self._distanceLogTrip
    133 
    134   def waterDepth(self):
    135     return self._waterDepth
    136    
    137   def waterTemperature(self):
    138     return self._waterTemperature
    139 
    140   def _loadData(self):
    141     n2kdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    142     n2kdSocket.connect((self._serverHost, self._serverPort))
    143     response = ""
    144     while 1:
    145       data = n2kdSocket.recv(1024)
    146       if not data:
    147         break
    148       response += data
    149     n2kdSocket.close()
    150     data = json.loads(response)
    151    
    152     descriptions = [ "Water Depth", "Speed", "Environmental Parameters", "Distance Log" ]
    153    
    154     for key in data:
    155       entryKeys = data[key].keys()
    156       if len(entryKeys) != 2:
    157         print "Malformed json n2kd response? %s" % response
    158       else:
    159         if data[key]["description"] in descriptions:
    160           entryKey = entryKeys[1]
    161           lineKeys = data[key][entryKey].keys()
    162           if "fields" not in lineKeys:
    163             print "No fields entry. Malformed json n2kd response in %s" % data[key][entryKey]
    164           else:
    165             fields = data[key][entryKey]["fields"]
    166             # try water depth (NEED TO APPLY OFFSET?)
    167             if "Depth" in fields:
    168               if data[key][entryKey]["description"] == "Water Depth":
    169                 self._waterDepth = fields["Depth"]
    170             # try speed
    171             if "Speed Water Referenced" in fields:
    172               self._speedThroughWater = fields["Speed Water Referenced"]
    173             # try sea temperature
    174             if "Temperature Source" in fields and "Temperature" in fields:
    175               if fields["Temperature Source"] == "Sea Temperature":
    176                 self._waterTemperature = fields["Temperature"]
    177             # try log
    178             if "Log" in fields:
    179               self._distanceLog = fields["Log"]
    180             if "Trip Log" in fields:
    181               self._distanceLogTrip = fields["Trip Log"]
    182 
    183   def _serverUrl(self):
    184     return "http://%s:%d/" % (self._serverHost, self._serverPort)
    185    
    186   def stop(self):
    187     self._running = False
    188    
    189   def run(self):
    190     global logperiod
    191     try:
    192       while self._running:
    193         self._loadData()
    19491        time.sleep(float(logperiod)/2.0)
    19592    except StopIteration:
    … …  
    283180    epd.update()
    284181
    285 def rraRows(duration, resolution, step):
    286     return int(round(float(duration) / rraSteps(resolution, float(step)) / float(step)))
    287 
    288 def rraSteps(resolution, step):
    289     return int(round(float(resolution) / float(step)))
    290 
    291182def createDatabase(databaseFile):
    292183  rrdDataSources = [ 'DS:latitude:GAUGE:15:-90:90',
    … …  
    298189                     'DS:gpsfix:GAUGE:15:-999:3' ]
    299190
    300   step = logperiod
    301   # durations: each archive should contain values for in seconds
    302   archives = [
    303     {"duration": 240, "resolution": 2},
    304     {"duration": 3600, "resolution": 10},
    305     {"duration": 86400, "resolution": 60},
    306   ]
    307   rrdArchives = []
    308   for i in range(0, len(archives)):
    309     rrdArchives += ['RRA:AVERAGE:0.5:%d:%d' % (rraSteps(archives[i]["resolution"], step), rraRows(archives[i]["duration"], archives[i]["resolution"], step))]
     191  rrdArchives = [ 'RRA:AVERAGE:0.5:1:60',
     192                  'RRA:AVERAGE:0.5:5:720',
     193                  'RRA:AVERAGE:0.5:15:1440',
     194                  'RRA:AVERAGE:0.5:30:1440',
     195                  'RRA:AVERAGE:0.5:60:1440' ]
    310196
    311197  if not os.path.isfile(databaseFile):
    … …  
    314200                   "N",
    315201                   "--step",
    316                    str(step),
     202                   "1",
    317203                   rrdDataSources,
    318204                   rrdArchives)
    … …  
    408294
    409295  gpsp = GpsdPoller()
    410   n2kd = N2kdPoller()
    411296  try:
    412297    gpsp.start()
    413     n2kd.start()
    414298    # gpsp now polls every .2 seconds for new data, storing it in self.current_value
    415299    while True:
    … …  
    418302      latitude = gpsp.latitude()
    419303      longitude = gpsp.longitude()
    420       speedThroughWater = n2kd.speedThroughWater()
     304      speedThroughWater = random.randrange(0, 10)
    421305      speedOverGround = gpsp.speedOverGround()
    422       waterDepth = n2kd.waterDepth()
    423       waterTemperature = n2kd.waterTemperature();
     306      waterDepth = random.randrange(1, 30) + 0.5;
     307      waterTemperature = random.randrange(15, 25) + 0.5;
    424308      gpsFix = gpsp.gpsFix()
    425309     
    … …  
    432316    gpsp.stop()
    433317    gpsp.join() # wait for the thread to finish what it's doing
    434     n2kd.stop()
    435     n2kd.join()
    436318    if displayHandler is not None:
    437319      displayHandler.stop()
  • src/webapp/webclient/javascript/main.js

    rc84f87e r19ac332  
    11/*
    22
    3     Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     3    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    44
    55    This library is free software; you can redistribute it and/or
    … …  
    122122$(document).ready(function() {
    123123        displayDataGet();
    124         setInterval(displayDataGet, 10000);
    125 
     124        setInterval(displayDataGet, 15000);
     125       
    126126        $.getJSON("../webservice/tileserver.php?/map.json", mapSetup);
    127127       
  • src/webapp/webservice/inc/Config.php

    rc84f87e r19ac332  
    11<?php
    22/*
    3     Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     3    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    44
    55    This program is free software: you can redistribute it and/or modify
    … …  
    4747                if(array_key_exists("datadirectory", $this->_config))
    4848                        return addTrailingSlash($this->_config["datadirectory"]);
    49                 return "/var/lib/gnuboat/";
     49                return __DIR__ ."/../data/";
    5050        }
    5151
  • src/webapp/webservice/index.php

    rc84f87e r19ac332  
    11<?php
    22/*
    3     Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     3    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    44
    55    This program is free software: you can redistribute it and/or modify
    … …  
    3838);
    3939
    40 function addRrdData($interval, $period, &$result, &$lastValid){
     40function addRrdData($interval, $period, &$result, &$lastTime){
    4141        global $dbFile;
    4242        global $debug;
    4343        global $configRound;
    44         $lastValid = false;
    4544
    46         $endTime = floor(time() / $interval) * $interval;
    47         $startTime = $endTime - $period;
    48         $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    49         // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
     45        $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period));
    5046        if($data === false){
    5147                echo json_encode($result);
    … …  
    5551                else
    5652                        error_log($message);
    57                 return;
    5853        }
    5954
    … …  
    8176                echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
    8277        }
    83         foreach($data[$keys[0]] as $timeframe => $value){
     78        $keysTimeframes = array_keys($data[$keyTimeframes]);
     79        for($i = 0; $i < $timeframesCount; $i++){
     80                $time = $keysTimeframes[$i];
    8481                $allValid = true;
    85                 $gpsFix = false;
    8682                $tmp = array();
    8783                foreach($keys as $key){
    8884                        if($key == "gpsfix"){
    89                                 switch($data[$key][$timeframe]){
     85                                switch($data[$key][$time]){
     86                                        case 1:
     87                                                $tmp[$key] = "2D";
     88                                                break;
    9089                                        case 2:
    91                                                 $tmp[$key] = "2D";
    92                                                 $gpsFix = true;
    93                                                 break;
    94                                         case 3:
    9590                                                $tmp[$key] = "3D";
    96                                                 $gpsFix = true;
    9791                                                break;
    9892                                        default:
    … …  
    10195                        }
    10296                        else{
    103                                 $tmp[$key] = $data[$key][$timeframe];
     97                                $tmp[$key] = $data[$key][$time];
    10498                                if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    10599                                        $allValid = false;
    … …  
    114108                // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    115109                if($allValid){
    116                         $lastValid = $tmp;
    117                         if($gpsFix){
    118                                 if(!array_key_exists($timeframe, $result["track"])){
    119                                         $result["track"][$timeframe] = $tmp;
    120                                 }
    121                         }
     110                        if(!array_key_exists($time, $result["track"])){
     111                                $result["track"][$time] = $tmp;
     112                        }
     113                        $lastTime = $time;
    122114                }
    123115                else{
    … …  
    129121                }
    130122        }
     123
    131124        ksort($result["track"]);
    132125}
    … …  
    172165                exit;
    173166        }
    174         // Fetch entries of the last 30s with maximum (available) resolution of 2s
    175         addRrdData("2", "240", $result, $lastValid);
    176         if($lastValid){
    177                 $result["lastvalid"] = $lastValid;
     167        // Fetch entries of the last 30 seconds
     168        addRrdData("5s", "30s", $result, $lastTime);
     169        if($lastTime > -1){
     170                $result["lastvalid"] = $result["track"][$lastTime];
    178171        }
    179         // Fetch entries of the last 1h with maximum (available) resolution of 10s
    180         addRrdData("10", "36000", $result, $lastTime);
    181         // Fetch entries of the last 24h with maximum (available) resolution of 60s
    182         addRrdData("60", "86400", $result, $lastTime);
     172        addRrdData("15s", "5minutes", $result, $lastTime);
     173        addRrdData("30s", "12h", $result, $lastTime);
    183174}
    184175
    185176if($debug > 0){
    186         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
     177        echo json_encode($result,  JSON_PRETTY_PRINT);
    187178}
    188179else{
    189         echo json_encode($result) ."\n";
     180        echo json_encode($result);
    190181}
    191182
Note: See TracChangeset for help on using the changeset viewer.