Changeset 5519ced for src/webapp


Ignore:
Timestamp:
Jul 20, 2016, 5:18:34 PM (10 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
d7ee6ae
Parents:
a250eb4
Message:
  • Started to implement shutdown functionality
    • Tried to fix webservice rrd_fetch with to many values
Location:
src/webapp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/webapp/webclient/css/main.css

    ra250eb4 r5519ced  
    1818body {
    1919        margin: 1em;
     20        width: 58.3mm;
    2021}
    2122
    … …  
    3536#display {
    3637        background-color: #E6E6E6;
    37         width: 58.3mm;
     38        width: 100%;
    3839        height: 38.2mm;
    3940        font-size: 12pt;
    … …  
    9495
    9596#map {
    96         width: 58.3mm;
     97        width: 100%;
    9798        height: 76mm;
    9899        border: 1px solid black;
    … …  
    133134
    134135}
     136
     137#shutdown {
     138        width: 100%;
     139        margin-top: 3em;
     140}
     141
     142#shutdown button {
     143        background-color: #8C1C00;
     144        color: white;
     145        font-weight: bold;
     146        font-size: 24pt;
     147        width: 100%;
     148}
  • src/webapp/webclient/javascript/main.js

    ra250eb4 r5519ced  
    102102}
    103103
     104function shutdown(){
     105        $.getJSON("../webservice/?request=shutdown", function(data){
     106                var shutdownTime = data["shutdownstart"];
     107                var shutdownDate = new Date(shutdownTime);
     108                var currentDate = new Date();
     109                var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
     110                if(shutdownTime == -1){
     111                        shutdownHint = "Failed to initiate shutdown!";
     112                }
     113                $("#shutdownMessage").text(shutdownHint);
     114        });
     115}
     116
    104117$(document).ready(function() {
    105118        displayDataGet();
    … …  
    107120       
    108121        $.getJSON("../webservice/tileserver.php?/map.json", mapSetup);
     122       
     123        $("#shutdownButton").click(shutdown);
    109124});
  • src/webapp/webclient/root/index.php

    ra250eb4 r5519ced  
    100100                </div>
    101101        </form>
     102        <div id="shutdown">
     103                <p id="shutdownMessage"/>
     104                <form>
     105                        <button type="button" id="shutdownButton">SHUTDOWN</button>
     106                </form>
     107        </div>
    102108<?php
    103109require_once("inc/footer.php");
  • src/webapp/webservice/index.php

    ra250eb4 r5519ced  
    2121$dbFile = $config->databaseFile();
    2222
     23define("REQUEST_DATA", "data");
     24define("REQUEST_SHUTDOWN", "shutdown");
     25
     26
    2327// configure roundings per value (key) and precision (value)
    2428$configRound = array
    … …  
    3640        $debug = $_GET["debug"];
    3741
     42$request = REQUEST_DATA;
     43if(array_key_exists("request", $_GET)){
     44        $request = $_GET["request"];
     45}
     46
    3847if($debug == 0)
    3948        header('Content-Type: application/json');
    4049
    41 $result["lastvalid"] = array();
    42 $result["track"] = array();
     50$result = array();
     51if($request == REQUEST_SHUTDOWN){
     52        $handle = @fopen("/var/spool/gnuboat/gnuboat_shutdown", "w");
     53        if($handle === false){
     54                $message = "Could not create file to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
     55                if($debug > 0)
     56                        echo $message;
     57                error_log($message);
     58                $result["shutdownstart"] = -1;
     59                $shutdownTime = new DateTime('NOW');
     60                $shutdownTime->modify("+60 seconds");
     61                $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
     62        }
     63        else{
     64                $shutdownTime = new DateTime('NOW');
     65                $shutdownTime>modify("+15 seconds");
     66                fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
     67                $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
     68        }
     69}
     70elseif($request == REQUEST_DATA){
     71        $result["lastvalid"] = array();
     72        $result["track"] = array();
    4373
    44 // Fetch entries of the last 12hours
    45 $data = rrd_fetch($dbFile, array( "AVERAGE", "--start", "-12h"));
    46 if($data === false){
    47         echo json_encode($result);
    48         $message = "Could not open rrd database ". $dbFile;
    49         if($debug > 0)
    50                 echo $message;
    51         else
    52                 error_log($message);
    53         exit;
    54 }
     74        if(!file_exists($dbFile)){
     75                echo json_encode("");
     76                if($debug > 0)
     77                        echo "Database file ". $dbFile ." does not exist!";
     78                error_log("Database file ". $dbFile ." does not exist");
     79                exit;
     80        }
     81        // Fetch entries of the last 12hours
     82        $data = rrd_fetch($dbFile, array( "AVERAGE", "--resolution", "5", "--start", "-12h"));
     83        if($data === false){
     84                echo json_encode($result);
     85                $message = "Could not open rrd database ". $dbFile;
     86                if($debug > 0)
     87                        echo $message;
     88                else
     89                        error_log($message);
     90                exit;
     91        }
    5592
    56 if($debug > 2){
    57         echo "<pre>";
    58         var_dump($data);
    59         echo "</pre>";
    60 }
     93        if($debug > 2){
     94                echo "<pre>";
     95                var_dump($data);
     96                echo "</pre>";
     97        }
    6198
    62 $data = $data["data"];
     99        $data = $data["data"];
    63100
    64 // transform them into json format
    65 $i = 0;
    66 $lastTime = -1;
    67 $keys = array_keys($data);
    68 if(count($keys) < 0 ){
    69         error_log("No keys in fetched rrd data from ". $dbFile);
    70         echo json_encode("");
    71         exit;
    72 }
    73 for($i = 0; $i < count($data[$keys[0]]); $i++){
    74         $time = array_keys($data[$keys[0]])[$i];
    75         $allValid = true;
    76         $tmp = array();
    77         foreach($keys as $key){
    78                 if($key == "gpsfix"){
    79                         switch($data[$key][$time]){
    80                                 case 1:
    81                                         $tmp[$key] = "2D";
    82                                         break;
    83                                 case 2:
    84                                         $tmp[$key] = "3D";
    85                                         break;
    86                                 default:
    87                                         $tmp[$key] = "-";
     101        // transform them into json format
     102        $i = 0;
     103        $lastTime = -1;
     104        $keys = array_keys($data);
     105        if(count($keys) < 0 ){
     106                error_log("No keys in fetched rrd data from ". $dbFile);
     107                echo json_encode("");
     108                exit;
     109        }
     110        for($i = 0; $i < count($data[$keys[0]]); $i++){
     111                $time = array_keys($data[$keys[0]])[$i];
     112                $allValid = true;
     113                $tmp = array();
     114                foreach($keys as $key){
     115                        if($key == "gpsfix"){
     116                                switch($data[$key][$time]){
     117                                        case 1:
     118                                                $tmp[$key] = "2D";
     119                                                break;
     120                                        case 2:
     121                                                $tmp[$key] = "3D";
     122                                                break;
     123                                        default:
     124                                                $tmp[$key] = "-";
     125                                }
     126                        }
     127                        else{
     128                                $tmp[$key] = $data[$key][$time];
     129                                if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
     130                                        $allValid = false;
     131                                }
     132                                elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
     133                                        $tmp[$key] = round($tmp[$key], $configRound[$key]);
    88134                        }
    89135                }
     136                // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
     137                if($allValid && $tmp["gpsfix"] != "-"){
     138                        $result["track"][$time] = $tmp;
     139                        $lastTime = $time;
     140                }
    90141                else{
    91                         $tmp[$key] = $data[$key][$time];
    92                         if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    93                                 $allValid = false;
     142                        if($debug > 0){
     143                                echo "Failed <pre>";
     144                                var_dump($tmp);
     145                                echo "</pre>\n";
    94146                        }
    95                         elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
    96                                 $tmp[$key] = round($tmp[$key], $configRound[$key]);
    97147                }
    98148        }
    99         // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    100         if($allValid && $tmp["gpsfix"] != "-"){
    101                 $result["track"][$time] = $tmp;
    102                 $lastTime = $time;
     149        if($lastTime > -1){
     150                $result["lastvalid"] = $result["track"][$lastTime];
    103151        }
    104         else{
    105                 if($debug > 0){
    106                         echo "Failed <pre>";
    107                         var_dump($tmp);
    108                         echo "</pre>\n";
    109                 }
    110         }
    111 }
    112 if($lastTime > -1){
    113         $result["lastvalid"] = $result["track"][$lastTime];
    114152}
    115153
Note: See TracChangeset for help on using the changeset viewer.