Changeset c455d7b for src


Ignore:
Timestamp:
Aug 10, 2017, 6:05:33 PM (9 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
0.5, app-cordova, debian/bullseye, debian/jessie, master
Children:
6fec060
Parents:
82b4b49
Message:
  • Webservice now leverages PHP classes and autoloader functionality. All webservice tasks are combined in index.php and served by Webservice.php
  • Webclient lists available log files and webservice provides them to download
Location:
src/webapp
Files:
10 added
1 deleted
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/webapp/build.xml

    r82b4b49 rc455d7b  
    2727                <fileset dir="./webservice" id="filesWebservice">
    2828                        <exclude name="mbtiles/**"/>
     29                        <exclude name="tests/**"/>
    2930                </fileset>
    3031                <fileset dir="./webservice/mbtiles" id="filesWebserviceMaps">
    … …  
    4546        <target name="dist" depends="buildwebservice,buildwebclient">
    4647                <echo msg="Creating archive"/>
    47                 <tar destfile="${builddir}/..//gnuboat-webapp.tar.gz" compression="gzip">
     48        <!--    <tar destfile="${builddir}/..//gnuboat-webapp.tar.gz" compression="gzip">
    4849                        <fileset refid="filesWebApp"/>
    49                 </tar>
    50                 <echo msg="Files copied and compressed in build directory weblcient.tar.gz OK!" />
     50                </tar>-->
     51                <echo msg="Files copied and compressed in build directory weblcient.tar.gz OK!" />
    5152        </target>
    5253        <target name="testdist" depends="buildwebservice,buildwebclient">
    … …  
    5657                </copy>
    5758        </target>
     59
     60        <!-- <phpunit bootstrap="webservice/inc/autoloader.php" pharlocation="phpunit-4.8.9.phar" haltonfailure="true" haltonerror="true" printsummary="true">
     61                <formatter type="plain" usefile="false"/>
     62                <batchtest>
     63                        <fileset dir="webservice">
     64                                <include name="tests/ServiceTest.php"/>
     65                        </fileset>
     66                </batchtest>
     67        </phpunit> -->
    5868</project>
  • src/webapp/webclient/css/main.css

    r82b4b49 rc455d7b  
    102102        color: white;
    103103        font-weight: bold;
    104         font-size: 24pt;
    105104        height: 3em;
     105        width: 100%;
     106        margin-top: 3em;
    106107}
     108
     109#shutdownMessage {
     110        font-weight: bold;
     111        color: #8C1C00;
     112}
     113
     114.logfiles-reload-button, .logfiles-reload-button:hover, .logfiles-reload-button:active, .logfiles-reload-button:focus {
     115        float: right;
     116        background:url('../resources/view-refresh.svg') no-repeat left center;
     117}
  • src/webapp/webclient/index.html

    r82b4b49 rc455d7b  
    4949                                        <a class="pure-menu-heading" href="#">GNUBoat</a>
    5050                                        <ul class="pure-menu-list">
    51                                                 <li class="pure-menu-item"><a class="pure-menu-link" href="#display">Home</a></li>
     51                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#display">Display</a></li>
    5252                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#map">Map</a></li>
    53                                                 <li class="pure-menu-item"><a class="pure-menu-link" href="#download">Download</a></li>
     53                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#logfiles">Logfiles</a></li>
     54                                                <li class="pure-menu-item"><button type="button" class="pure-button shutdown-button" id="shutdownButton">SHUTDOWN</button></li>
    5455                                        </ul>
    5556                                </div>
    … …  
    9293                                                                <a name="shutdown"></a>
    9394                                                                <p id="shutdownMessage"/>
    94                                                                 <button type="button" class="pure-button shutdown-button" id="shutdownButton">SHUTDOWN</button>
     95<!--                                                            <button type="button" class="pure-button shutdown-button" id="shutdownButton">SHUTDOWN</button> -->
    9596                                                        </div>
    9697                                                </div>
    … …  
    114115                                                                <input id="mapFileInput" type="file" name="mapFile" value="" placeholder="Path to MAPTile file" required><progress id="mapTileUploadProgress"></progress>
    115116                                                        </div>
    116                                                         <input type="submit" class="pure-button pure-button-primary" name="buttonSendMap" value="Upload"/>
     117                                                        <input type="submit" class="pure-button pure-button-primary" name="mapUploadButton" value="Upload"/>
    117118                                                </fieldset>
    118119                                        </form>
     120                                                                       
     121                                        <h2 class="content-subhead">
     122                                                Logfiles
     123                                                <button id="logfilesReload" class="pure-button logfiles-reload-button"></button>
     124                                        </h2>
     125                                        <div>
     126                                                <a name="logfiles"></a>
     127                                                <form id="logfilesForm" class="pure-form" method="POST" action="../webservice/?request=logfiles"></form>
     128                                        </div>
    119129                                </div>
    120130                        </div>
  • src/webapp/webclient/js/main.js

    r82b4b49 rc455d7b  
    167167                var shutdownHint = "Shutting down in "+ Math.floor((shutdownDate - currentDate)/1000) +"s at "+ shutdownTime.toLocaleString();
    168168                if(shutdownTime == -1){
    169                         shutdownHint = "Failed to initiate shutdown!";
     169                        shutdownHint = "Failed to initiate shutdown: "+ data["errormessage"];
    170170                }
    171171                $("#shutdownMessage").text(shutdownHint);
    172172        });
     173        location.hash = "#shutdown";
    173174}
    174175
    175176function uploadMap(form)
    176177{
    177         var formData = new FormData($("#mapTileUpload")[0]);
    178 //      formData.append("mapFile", $("#mapFileInput")[0].files[0]);
    179        
    180178        $("#mapFileInput").hide();
    181179        $("#mapTileUploadProgress").show();
    182180        $.ajax({
    183181                type: 'POST',
    184                 url: '../webservice/mapupload.php',
    185                 data: formData,
     182                url: '../webservice/?request=map',
     183                data: new FormData($("#mapTileUpload")[0]),
    186184                contentType: false,
    187185                processData: false,
    … …  
    208206}
    209207
     208function logfilesLoad()
     209{
     210        $("#logfilesForm").empty();
     211        $.getJSON("../webservice/?request=logfiles", function(data){
     212                if(!Array.isArray(data) || data.length < 1){
     213                        $("<p>No Logfiles found!</p>").appendTo("#logfilesForm");
     214                        console.log(data);
     215                        return;
     216                }
     217                if(("error" in data) && data["error"] == 1){
     218                        alert("Failed to upload map: "+ data["errormessage"]);
     219                        $("#mapTileUploadProgress").hide();
     220                        $("#mapFileInput").show();
     221                        return;
     222                }
     223                $("<select id=\"logfilesSelect\" name=\"logfilesSelected[]\" class=\"pure-input-1\" multiple></select>").appendTo("#logfilesForm");
     224                $("<input type=\"submit\" class=\"pure-button pure-button-primary\" name=\"logfilesDownload\" value=\"Download\"/>").appendTo("#logfilesForm");
     225                data.forEach(function(value, index){
     226                        $("<option value=\""+ value +"\">"+ value +"</option>").appendTo("#logfilesSelect");
     227                });
     228        });
     229}
    210230
    211231$(document).ready(function() {
    212232        $("#mapTileUploadProgress").hide();
    213         $("shutdown").click(shutdown);
    214 
    215                 $("#mapTileUpload").validate({
     233        $("#logfilesReload").click(logfilesLoad);
     234        $("#shutdownButton").click(shutdown);
     235
     236        $("#mapTileUpload").validate({
    216237                rules: {
    217238                        mapFile: {
    … …  
    226247                }
    227248        });
     249       
     250        $("#logfilesForm").validate({
     251                rules: {
     252                        logfilesSelect: {
     253                                required: true
     254                        },
     255                }
     256        });
    228257
    229258        getMapInfos();
    230259        displayDataGet();
    231260        setInterval(displayDataGet, 10000);
     261        logfilesLoad();
    232262});
  • src/webapp/webservice/inc/GnuBoat/Config.php

    r82b4b49 rc455d7b  
    1717*/
    1818
    19 $configFile = __DIR__ ."/../config/config.php";
    20 if(file_exists($configFile))
    21         include($configFile);
    22 else{
    23         error_log("No config file ". $configFile ." not found!");
    24         $config = array();
    25 }
     19namespace GNUBoat;
    2620
    2721/**
    2822 * @brief Loads the configuration from webservice/config/config.php (if file exists) and provides the settings for usage
    2923 */
    30 class Config {
    31         public function __construct(){
    32                 global $config;
    33                 $this->_config = $config;
     24class Config
     25{
     26        public function __construct()
     27        {
     28                $configFile = __DIR__ ."/../config/config.php";
     29                if(file_exists($configFile)){
     30                        include($configFile);
     31                        $this->_config = $config;
     32                }
     33                else{
     34                        $this->_config = array();
     35                }
    3436        }
    3537
    36         public function databaseFile(){
     38        public function databaseFile()
     39        {
    3740                return $this->dataDirectory() ."gnuboat.rrd";
    3841        }
    3942
    40         public function shutdownRequestFile(){
     43        public function shutdownRequestFile()
     44        {
    4145                if(array_key_exists("shutdownfile", $this->_config))
    4246                        return $this->_config["shutdownfile"];
    … …  
    4448        }
    4549
    46         public function dataDirectory(){
     50        public function dataDirectory()
     51        {
    4752                if(array_key_exists("datadirectory", $this->_config))
    4853                        return addTrailingSlash($this->_config["datadirectory"]);
    … …  
    5055        }
    5156
    52         private static function addTrailingSlash($path){
     57        public function logDirectory()
     58        {
     59                if(array_key_exists("logdirectory", $this->_config))
     60                        return addTrailingSlash($this->_config["logdirectory"]);
     61                return "/var/log/gnuboat/";
     62        }
     63
     64        private static function addTrailingSlash($path)
     65        {
    5366                $length = strlen($path);
    5467                if(substr($path, $length - 1, 1) != "/"){
  • src/webapp/webservice/index.php

    r82b4b49 rc455d7b  
    1717*/
    1818
    19 require_once(__DIR__ ."/inc/Config.php");
    20 $config = new Config();
    21 $dbFile = $config->databaseFile();
     19require_once(__DIR__ ."/inc/autoloader.php");
    2220
    23 define("REQUEST_DATA", "data");
    24 define("REQUEST_SHUTDOWN", "shutdown");
    25 
    26 
    27 // configure roundings per value (key) and precision (value)
    28 $configRound = array
    29 (
    30         "latitude" => 5,
    31         "longitude" => 5,
    32         "speedthroughwater" => 1,
    33         "speedoverground" => 1,
    34         "waterdepth" => 1,
    35         "watertemperature" => 1,
    36 );
    37 
    38 function addRrdData($interval, $period, &$result, &$lastValid){
    39         global $dbFile;
    40         global $debug;
    41         global $configRound;
    42         $lastValid = false;
    43 
    44         $endTime = floor(time() / $interval) * $interval;
    45         $startTime = $endTime - $period;
    46         $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    47         // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
    48         if($data === false){
    49                 echo json_encode($result);
    50                 $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
    51                 if($debug > 0)
    52                         echo $message;
    53                 else
    54                         error_log($message);
    55                 return;
    56         }
    57 
    58         if($debug > 2){
    59                 echo "<pre>";
    60                 var_dump($data);
    61                 echo "</pre>";
    62         }
    63 
    64         $data = $data["data"];
    65 
    66         // transform them into json format
    67         $i = 0;
    68         $lastValid = -1;
    69         $keys = array_keys($data);
    70         $keyTimeframes = $keys[0];
    71         $itemCount = count($keys);
    72         if($itemCount < 0 ){
    73                 error_log("No keys in fetched rrd data from ". $dbFile);
    74                 echo json_encode("");
    75                 exit;
    76         }
    77         $timeframesCount = count($data[$keyTimeframes]);
    78         if($debug > 0){
    79                 echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
    80         }
    81         foreach($data[$keys[0]] as $timeframe => $value){
    82                 $allValid = true;
    83                 $gpsFix = false;
    84                 $tmp = array();
    85                 foreach($keys as $key){
    86                         if($key == "gpsfix"){
    87                                 switch($data[$key][$timeframe]){
    88                                         case 2:
    89                                                 $tmp[$key] = "2D";
    90                                                 $gpsFix = true;
    91                                                 break;
    92                                         case 3:
    93                                                 $tmp[$key] = "3D";
    94                                                 $gpsFix = true;
    95                                                 break;
    96                                         default:
    97                                                 $tmp[$key] = "-";
    98                                 }
    99                         }
    100                         else{
    101                                 $tmp[$key] = $data[$key][$timeframe];
    102                                 if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    103                                         $allValid = false;
    104                                         if($debug > 1){
    105                                                 echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $time ."<br/>";
    106                                         }
    107                                 }
    108                                 elseif(array_key_exists($key, $configRound) && is_integer($configRound[$key]))
    109                                         $tmp[$key] = round($tmp[$key], $configRound[$key]);
    110                         }
    111                 }
    112                 // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    113                 if($allValid){
    114                         $lastValid = $tmp;
    115                         if($gpsFix){
    116                                 if(!array_key_exists($timeframe, $result["track"])){
    117                                         $result["track"][$timeframe] = $tmp;
    118                                 }
    119                         }
    120                 }
    121                 else{
    122                         if($debug > 1){
    123                                 echo "Failed <pre>";
    124                                 var_dump($tmp);
    125                                 echo "</pre>\n";
    126                         }
    127                 }
    128         }
    129         ksort($result["track"]);
    130 }
    131 
    132 $debug = 0;
    133 if(array_key_exists("debug", $_GET))
    134         $debug = $_GET["debug"];
    135 
    136 $request = REQUEST_DATA;
    137 if(array_key_exists("request", $_GET)){
    138         $request = $_GET["request"];
    139 }
    140 
    141 if($debug == 0)
    142         header('Content-Type: application/json');
    143 
    144 $result = array();
    145 if($request == REQUEST_SHUTDOWN){
    146         $handle = @fopen($config->shutdownRequestFile(), "w");
    147         if($handle === false){
    148                 $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon (Timezone: ". date_default_timezone_get() .")";
    149                 if($debug > 0)
    150                         echo $message;
    151                 error_log($message);
    152                 $result["shutdownstart"] = -1;
    153         }
    154         else{
    155                 $shutdownTime = new DateTime('NOW');
    156                 $shutdownTime->modify("+15 seconds");
    157                 fwrite($handle, $shutdownTime->format(DateTime::ISO8601));
    158                 $result["shutdownstart"] = $shutdownTime->format(DateTime::ISO8601);
    159         }
    160 }
    161 elseif($request == REQUEST_DATA){
    162         $result["lastvalid"] = array();
    163         $result["track"] = array();
    164 
    165         if(!file_exists($dbFile)){
    166                 echo json_encode("");
    167                 if($debug > 0)
    168                         echo "Database file ". $dbFile ." does not exist!";
    169                 error_log("Database file ". $dbFile ." does not exist");
    170                 exit;
    171         }
    172         // Fetch entries of the last 30s with maximum (available) resolution of 2s
    173         addRrdData("2", "240", $result, $lastValid);
    174         if($lastValid){
    175                 $result["lastvalid"] = $lastValid;
    176         }
    177         // Fetch entries of the last 1h with maximum (available) resolution of 10s
    178         addRrdData("10", "36000", $result, $lastTime);
    179         // Fetch entries of the last 24h with maximum (available) resolution of 60s
    180         addRrdData("60", "86400", $result, $lastTime);
    181 }
    182 
    183 if($debug > 0){
    184         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    185 }
    186 else{
    187         echo json_encode($result) ."\n";
    188 }
     21$webservice = new GnuBoat\Webservice();
     22$webservice->process();
    18923
    19024?>
Note: See TracChangeset for help on using the changeset viewer.