Changeset 8fd7454 for src/webapp


Ignore:
Timestamp:
Dec 10, 2017, 4:32:57 PM (9 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, debian/jessie
Children:
ce5244e
Parents:
c810990 (diff), 8ef59a1 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'debian/jessie' of ​https://svn.engsas.de/gnuboat into debian/jessie

Location:
src/webapp
Files:
26 added
3 deleted
12 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/webapp/build.xml

    rc810990 r8fd7454  
    2828                        <exclude name="mbtiles/**"/>
    2929                        <exclude name="tests/**"/>
     30                        <exclude name="config/**"/>
    3031                </fileset>
    3132                <fileset dir="./webservice/mbtiles" id="filesWebserviceMaps">
    … …  
    4445
    4546
    46         <target name="dist" depends="buildwebservice,buildwebclient">
     47        <target name="dist" depends="buildwebservice,buildwebclient,test">
    4748                <echo msg="Creating archive"/>
    48         <!--    <tar destfile="${builddir}/..//gnuboat-webapp.tar.gz" compression="gzip">
     49                <tar destfile="${builddir}/..//gnuboat-webapp.tar.gz" compression="gzip">
    4950                        <fileset refid="filesWebApp"/>
    50                 </tar>-->
     51                </tar>
    5152                <echo msg="Files copied and compressed in build directory weblcient.tar.gz OK!" />
    5253        </target>
    53         <target name="testdist" depends="buildwebservice,buildwebclient">
     54
     55        <target name="testdist" depends="buildwebservice,buildwebclient,test">
    5456                <echo msg="Install project into ${user.home}/public_html/gnuboat"/>
     57                <fileset id="filesConfig" dir="./webservice/config">
     58                        <include name="config.php"/>
     59                </fileset>
     60                <fileset id="filesLogs" dir="./webservice/tests/logs">
     61                        <include name="*.log"/>
     62                </fileset>
     63       
    5564                <copy todir="${user.home}/public_html/gnuboat">
    5665                        <fileset refid="filesWebApp"/>
    5766                </copy>
     67                <copy todir="${user.home}/public_html/gnuboat/webservice/tests/logs">
     68                        <fileset refid="filesLogs"/>
     69                </copy>
     70                <copy todir="${user.home}/public_html/gnuboat/webservice/config">
     71                        <fileset refid="filesConfig"/>
     72                </copy>
    5873        </target>
    5974
    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> -->
     75        <target name="test">
     76                <exec command="php phpunit-4.8.9.phar" passthru="true" checkreturn="true"/>
     77                <!--<phpunit pharlocation="phpunit-4.8.9.phar" configuration="phpunit.xml" haltonfailure="true" haltonerror="true" printsummary="true" codecoverage="false">
     78                        <formatter type="plain" usefile="false"/> -->
     79                        <!--<formatter type="JUnit" usefile="true"/>-->
     80<!--                    <batchtest>
     81                                <fileset dir="webservice">
     82                                        <include name="tests/*Test.php"/>
     83                                </fileset>
     84                        </batchtest>
     85                </phpunit>-->
     86        </target>
    6887</project>
  • src/webapp/webclient/css/GnuBoat.css

    rc810990 r8fd7454  
    2828.l-box {
    2929        padding: 1em;
     30}
     31
     32.header h1 img {
     33        height: 2em;
     34        width: auto;
    3035}
    3136
    … …  
    9196}
    9297
     98.viewportGeometry {
     99/*      width: 100%; */
     100        height: 24em;
     101}
     102
    93103#mapViewport {
    94         width: 100%;
    95         height: 24em;
    96104        border: 1px solid #347C7C;
    97105        margin-top: 1em;
    … …  
    107115}
    108116
    109 #shutdownMessage {
     117#message .error {
    110118        font-weight: bold;
    111119        color: #8C1C00;
     120}
     121
     122#message .info {
     123        font-weight: bold;
     124        color: #45A545;
    112125}
    113126
    … …  
    116129        background:url('../resources/view-refresh.svg') no-repeat left center;
    117130}
     131
     132.hidden {
     133        display: none;
     134}
     135
     136#mapLgend table {
     137        margin-top: 0.25em;
     138        margin-left: 1em;
     139}
     140
     141#mapLegend table tr td.level {
     142        text-align: right;
     143}
  • src/webapp/webclient/index.html

    rc810990 r8fd7454  
    3030                <link rel="stylesheet" content="text/css" href="lib/css/pure/grids-responsive-min.css">
    3131                <!--<![endif]-->
    32                 <link rel="stylesheet" content="text/css" href="lib/css/leaflet.css"/>
     32                <link rel="stylesheet" content="text/css" href="lib/css/leaflet/leaflet.css"/>
    3333               
    34                 <script language="javascript" src="lib/js/jquery-3.2.1.min.js" type="text/javascript"></script>
     34                <script src="lib/js/jquery-3.2.1.min.js" type="text/javascript"></script>
    3535                <script src="lib/js/jquery.validate.min.js" type="text/javascript"></script>
    3636                <script src="lib/js/additional-methods.min.js" type="text/javascript"></script>
    37                 <script language="javascript\" src="lib/js/leaflet.js" type="text/javascript"></script>
     37                <script src="lib/js/leaflet.js" type="text/javascript"></script>
     38                <script src="lib/js/leaflet.hotline.min.js" type="text/javascript"></script>
     39                <script src="lib/js/three.min.js"></script>
     40                <script src="lib/js/three.OrbitControls.js"></script>
    3841               
    39                 <link rel="stylesheet" href="css/main.css" type="text/css"/>
     42                <link rel="stylesheet" href="css/GnuBoat.css" type="text/css"/>
    4043                <link rel="stylesheet" href="css/side-menu.css" type="text/css"/>
    41                 <script language="javascript" src="js/side-menu.js" type="text/javascript"></script>
    42                 <script language="javascript" src="js/main.js" type="text/javascript"></script>
     44                <script language="javascript" src="js/GnuBoat.js" type="text/javascript"></script>
     45                <script language="javascript" type="text/javascript">
     46                        var active = 'active';
     47                        var layout = null;
     48                        var menu = null;
     49                        var menuLink = null;
     50                        var content = null;
     51
     52                        function toggleAll() {
     53                                layout.toggleClass(active);
     54                                menu.toggleClass(active);
     55                                menuLink.toggleClass(active);
     56                        }
     57
     58                        $(document).ready(function () {
     59                                layout   = $("#layout");
     60                                menu     = $("#menu");
     61                                menuLink = $("#menuLink");
     62                                content  = $("#main");
     63
     64                                menuLink.click(function () {
     65                                        toggleAll();
     66                                });
     67
     68                                content.click(function() {
     69                                        if (menu.hasClass(active)) {
     70                                                toggleAll();
     71                                        }
     72                                });
     73
     74                                GnuBoat.initialize();
     75                                GnuBoat.setServer("../webservice");
     76                                GnuBoat.start();
     77                        });
     78                </script>
    4379        </head>
    4480        <body>
    … …  
    5187                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#display">Display</a></li>
    5288                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#map">Map</a></li>
     89                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#rendering">3D Rendering</a></li>
    5390                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#logfiles">Logfiles</a></li>
     91                                                <li class="pure-menu-item"><a class="pure-menu-link" href="#opencpn">OpenCPN</a></li>
    5492                                                <li class="pure-menu-item"><button type="button" class="pure-button shutdown-button" id="shutdownButton">SHUTDOWN</button></li>
    5593                                        </ul>
    … …  
    91129                                                <div class="pure-u-1 pure-u-md-1-2 pure-u-lg-1-2">
    92130                                                        <div id="shutdown" class="l-box">
    93                                                                 <a name="shutdown"></a>
    94                                                                 <p id="shutdownMessage"/>
     131                                                                <a name="messages"></a>
     132                                                                <p id="message"/>
    95133<!--                                                            <button type="button" class="pure-button shutdown-button" id="shutdownButton">SHUTDOWN</button> -->
    96134                                                        </div>
    … …  
    101139                                        <div id="map">
    102140                                                <a name="map"></a>
    103                                                 <form id="mapSelectForm" class="pure-form"><select id="mapSelect" class="pure-input-1" size="1"></select></form>
    104                                                 <div id="mapViewport"></div>
     141                                                <form id="mapSelectForm" class="pure-form pure-form-aligned">
     142                                                        <div class="pure-control-group">
     143                                                                <label for="mapModusSelect">Map Modus:</label>
     144                                                                <select id="mapModusSelect" size="1" class="pure-input-1-2"></select>
     145                                                        </div>
     146                                                        <div class="pure-control-group">
     147                                                                <label for="mapSelect">Map Selection:</label>
     148                                                                <select id="mapSelect" size="1" class="pure-input-1-2"></select>
     149                                                        </div>
     150                                                </form>
     151                                                <div class="pure-g">
     152                                                        <div class="pure-u-1 pure-u-md-4-5 pure-u-lg-4-5">
     153                                                                <div id="mapViewport" class="viewportGeometry"></div>
     154                                                        </div>
     155                                                        <div class="pure-u-1 pure-u-md-1-5 pure-u-lg-1-5">
     156                                                                <div id="mapLegend"></div>
     157                                                        </div>
     158                                                </div>
    105159                                        </div>
    106160                                        <form id="mapTileUpload" class="pure-form pure-form-aligned" enctype="multipart/form-data">
    … …  
    118172                                                </fieldset>
    119173                                        </form>
    120                                                                        
     174
     175                                        <h2 class="content-subhead">3D Rendering of Water Depth</h2>
     176                                        <div>
     177                                                <a name="rendering"></a>
     178                                                <form id="renderForm" class="pure-form">
     179                                                        <label>Scale Depth Axis:</label><input id="renderScaleZ" type="range" name="renderScaleZ" min="0.5" max="1000" step="1" value="10" class="pure-input-1">
     180                                                </form>
     181                                                <div id="rendering" class="viewportGeometry"></div>
     182                                        </div>
     183
    121184                                        <h2 class="content-subhead">
    122185                                                Logfiles
    … …  
    127190                                                <form id="logfilesForm" class="pure-form" method="POST" action="../webservice/?request=logfiles"></form>
    128191                                        </div>
     192                                       
     193                                        <h2 class="content-subhead">OpenCPN</h2>
     194                                        <div>
     195                                                <a name="opencpn"></a>
     196                                                <div id="opencpnHint"></div>
     197                                        </div>
    129198                                </div>
    130199                        </div>
  • src/webapp/webservice/inc/GnuBoat/Config.php

    rc810990 r8fd7454  
    2626        public function __construct()
    2727        {
    28                 $configFile = __DIR__ ."/../config/config.php";
     28                $configFile = __DIR__ ."/../../config/config.php";
    2929                if(file_exists($configFile)){
    3030                        include($configFile);
    … …  
    5151        {
    5252                if(array_key_exists("datadirectory", $this->_config))
    53                         return addTrailingSlash($this->_config["datadirectory"]);
     53                        return $this->addTrailingSlash($this->_config["datadirectory"]);
    5454                return "/var/lib/gnuboat/";
    5555        }
    … …  
    5858        {
    5959                if(array_key_exists("logdirectory", $this->_config))
    60                         return addTrailingSlash($this->_config["logdirectory"]);
     60                        return $this->addTrailingSlash($this->_config["logdirectory"]);
    6161                return "/var/log/gnuboat/";
    6262        }
  • src/webapp/webservice/inc/GnuBoat/Service.php

    rc810990 r8fd7454  
    4444                $this->process();
    4545        }
     46       
     47        public static function errorStatic($message, $additionalContent = array(), $debug)
     48        {
     49                error_log($message);
     50                if($debug > 0)
     51                        print $message ."\n";
     52                $result = ["error" => 1, "errormessage" => $message];
     53                Service::outputStatic(array_merge($result, $additionalContent), $debug);
     54        }
     55       
     56        public static function outputStatic($content, $debug)
     57        {
     58                header('Content-Type: application/json');
     59                if($debug > 0){
     60                        echo json_encode($content,  JSON_PRETTY_PRINT) ."\n";
     61                }
     62                else{
     63                        echo json_encode($content) ."\n";
     64                }
     65        }
     66       
     67        protected function error($message, $additionalContent = array())
     68        {
     69                Service::errorStatic($message, $additionalContent, $this->debug());
     70        }
     71       
     72        protected function output($content)
     73        {
     74                Service::outputStatic($content, $this->debug());
     75        }
    4676
    4777        /**
  • src/webapp/webservice/inc/GnuBoat/ServiceData.php

    rc810990 r8fd7454  
    2929                $this->_configRound = array
    3030                (
    31                         "latitude" => 5,
    32                         "longitude" => 5,
     31                        "latitude" => 4,
     32                        "longitude" => 4,
    3333                        "speedthroughwater" => 1,
    3434                        "speedoverground" => 1,
     35                        "courseoverground" => 1,
    3536                        "waterdepth" => 1,
    3637                        "watertemperature" => 1,
    … …  
    4849                $result["track"] = array();
    4950
    50                 if(!file_exists($dbFile)){
    51                         echo json_encode("");
    52                         if($debug > 0)
    53                                 echo "Database file ". $dbFile ." does not exist!";
    54                         error_log("Database file ". $dbFile ." does not exist");
    55                         exit;
    56                 }
     51                if(!file_exists($dbFile))
     52                        return $this->error("Database file ". $dbFile ." does not exist");
     53
    5754                // Fetch entries of the last 30s with maximum (available) resolution of 2s
    5855                $this->addRrdData($dbFile, "2", "240", $result, $lastValid);
    59                 if($lastValid){
    60                         $result["lastvalid"] = $lastValid;
     56                if($lastValid !== false){
     57                        $result["lastvalid"] = $lastValid->toArray();
    6158                }
    6259                // Fetch entries of the last 1h with maximum (available) resolution of 10s
    … …  
    6562                $this->addRrdData($dbFile, "60", "86400", $result, $lastTime);
    6663
    67                 if($debug > 0){
    68                         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    69                 }
    70                 else{
    71                         header('Content-Type: application/json');
    72                         echo json_encode($result) ."\n";
    73                 }
     64                return $this->output($result);
    7465        }
    7566
    … …  
    8273                $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", $startTime, "--end", $endTime));
    8374                // $data = rrd_fetch($dbFile, array("AVERAGE", "--resolution", $interval, "--start", "-". $period, "--end", "start+". $period, "--align-start"));
    84                 if($data === false){
    85                         echo json_encode($result);
    86                         $message = "Failure reading rrd database ". $dbFile .": ". rrd_error();
    87                         if($debug > 0)
    88                                 echo $message;
    89                         else
    90                                 error_log($message);
    91                         return;
    92                 }
     75                if($data === false)
     76                        return $this->error("Failure reading rrd database ". $dbFile .": ". rrd_error(), $result);
    9377
    9478                if($debug > 2){
    95                         echo "<pre>";
    9679                        var_dump($data);
    97                         echo "</pre>";
    9880                }
    9981
    … …  
    10284                // transform them into json format
    10385                $i = 0;
    104                 $lastValid = -1;
     86                $lastValid = false;
    10587                $keys = array_keys($data);
    10688                $keyTimeframes = $keys[0];
    10789                $itemCount = count($keys);
    108                 if($itemCount < 0 ){
    109                         error_log("No keys in fetched rrd data from ". $dbFile);
    110                         echo json_encode("");
    111                         exit;
    112                 }
     90                if($itemCount < 0 )
     91                        return $this->error("No keys in fetched rrd data from ". $dbFile);
     92
    11393                $timeframesCount = count($data[$keyTimeframes]);
    114                 if($debug > 0){
    115                         echo "<p>Variables: ". $itemCount ."; Timeframes: ". $timeframesCount ."</p>\n";
     94                if($debug > 1){
     95                        error_log("Variables: ". $itemCount ."; Timeframes: ". $timeframesCount);
    11696                }
    11797                foreach($data[$keys[0]] as $timeframe => $value){
    118                         $allValid = true;
    119                         $gpsFix = false;
    120                         $tmp = array();
     98                        $tmp = new TrackItem($timeframe);
    12199                        foreach($keys as $key){
    122                                 if($key == "gpsfix"){
    123                                         switch($data[$key][$timeframe]){
    124                                                 case 2:
    125                                                         $tmp[$key] = "2D";
    126                                                         $gpsFix = true;
    127                                                         break;
    128                                                 case 3:
    129                                                         $tmp[$key] = "3D";
    130                                                         $gpsFix = true;
    131                                                         break;
    132                                                 default:
    133                                                         $tmp[$key] = "-";
    134                                         }
    135                                 }
    136                                 else{
    137                                         $tmp[$key] = $data[$key][$timeframe];
    138                                         if(!is_numeric($tmp[$key]) || is_nan($tmp[$key])){
    139                                                 $allValid = false;
    140                                                 if($debug > 1){
    141                                                         echo "Invalid value ". $key ." (". $tmp[$key] .") at time ". $timeframe ."<br/>";
    142                                                 }
    143                                         }
    144                                         elseif(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
    145                                                 $tmp[$key] = round($tmp[$key], $this->_configRound[$key]);
    146                                 }
     100                                $value = $data[$key][$timeframe];
     101                                if(array_key_exists($key, $this->_configRound) && is_integer($this->_configRound[$key]))
     102                                        $value = round($value, $this->_configRound[$key]);
     103                                if(!$tmp->setProperty($key, $value) && $debug > 1)
     104                                        error_log("Coult not set property ". $key ." to value ". $value ." at ". $timeframe);
    147105                        }
    148106                        // only add the timeframe to the track, if the values are valid and we had a gps fix at that time
    149                         if($allValid){
     107                        if($tmp->isValid()){
    150108                                $lastValid = $tmp;
    151                                 if($gpsFix){
     109                                if($tmp->gpsFixed()){
    152110                                        if(!array_key_exists($timeframe, $result["track"])){
    153                                                 $result["track"][$timeframe] = $tmp;
     111                                                $result["track"][$timeframe] = $tmp->toArray();
    154112                                        }
    155113                                }
  • src/webapp/webservice/inc/GnuBoat/ServiceLogFiles.php

    rc810990 r8fd7454  
    3232                $debug = $this->debug();
    3333                $config = new Config();
     34                if($debug > 1)
     35                        print "Log directory: ". $config->logDirectory() ."\n";
    3436
    3537                if(!array_key_exists("logfilesSelected", $_POST)){
    … …  
    3739                        $files = glob($config->logDirectory() ."*.log");
    3840                        array_walk($files, 'GnuBoat\ServiceLogFiles::stripLogDirectory');
     41                        return $this->output($files);
     42                }
    3943
    40                         header('Content-Type: application/json');
    41                         if($debug > 0){
    42                                 echo json_encode($files,  JSON_PRETTY_PRINT) ."\n";
     44                // prepare a download of available log files
     45                $files = $_POST["logfilesSelected"];
     46                if(count($files) < 1)
     47                        return $this->error("No logfile selected");
     48                elseif(count($files) == 1){
     49                        $content = file_get_contents($config->logDirectory() . $files[0]);
     50                        if($content === false){
     51                                echo "<html><head></head><body>Failed to read logfile ". $files[0] ."</body></html>";
    4352                        }
    44                         else{
    45                                 echo json_encode($files) ."\n";
    46                         }
     53               
     54                        header('Content-Type: text/plain');
     55                        header('Content-disposition: attachment; filename="'. $files[0] .'"');
     56                        header('Content-Length: ' . filesize($config->logDirectory() . $files[0]));
     57                        echo $content;
    4758                }
    4859                else{
    49                         // prepare a download of available log files
    50                         $files = $_POST["logfilesSelected"];
    51                         if(count($files) < 1){
     60                        $date = new \DateTime('NOW');
     61                        $archiveName = "gnuboat-logs-". $date->format("Y-m-d");
     62               
     63                        // provide files as zip file
     64                        $tmpArchive = tempnam(sys_get_temp_dir(), $archiveName);
     65                        $zip = new \ZipArchive();
     66                        if($zip->open($tmpArchive, \ZipArchive::CREATE) !== TRUE){
     67                                echo "<html><head></head><body>Failed to create zip file</body></html>";
    5268                                return;
    5369                        }
    54                         elseif(count($files) == 1){
    55                                 $content = file_get_contents($config->logDirectory() . $files[0]);
    56                                 if($content === false){
    57                                         echo "<html><head></head><body>Failed to read logfile ". $files[0] ."</body></html>";
    58                                 }
    59                        
    60                                 header('Content-Type: text/plain');
    61                                 header('Content-disposition: attachment; filename="'. $files[0] .'"');
    62                                 header('Content-Length: ' . filesize($config->logDirectory() . $files[0]));
    63                                 echo $content;
    64                         }
    65                         else{
    66                                 $date = new \DateTime('NOW');
    67                                 $archiveName = "gnuboat-logs-". $date->format("Y-m-d");
    68                        
    69                                 // provide files as zip file
    70                                 $tmpArchive = tempnam(sys_get_temp_dir(), $archiveName);
    71                                 $zip = new \ZipArchive();
    72                                 if($zip->open($tmpArchive, \ZipArchive::CREATE) !== TRUE){
    73                                         echo "<html><head></head><body>Failed to create zip file</body></html>";
     70                        foreach($files as $file){
     71                                if($zip->addFile($config->logDirectory() . $file, $archiveName ."/". $file) !== true){
     72                                        $zip->close();
     73                                        echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
    7474                                        return;
    7575                                }
    76                                 foreach($files as $file){
    77                                         if($zip->addFile($config->logDirectory() . $file, $archiveName ."/". $file) !== true){
    78                                                 $zip->close();
    79                                                 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
    80                                                 return;
    81                                         }
    82                                 }
    83                                 if($zip->close() !== true){
    84                                                 echo "<html><head></head><body>Failed to save zip file</body></html>";
    85                                                 return;
    86                                 }
    87                                
    88                                 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
    89                                
    90                                 //Send zip folder
    91                                 while (ob_get_level()) {
    92                                         ob_end_clean();
    93                                 }
    94                                 ob_start();
    95                                 header('Content-Type: application/zip');
    96                                 header('Content-Transfer-Encoding: Binary');
    97                                 header('Content-disposition: attachment; filename=' . $archiveName . '.zip');
    98                                 header('Content-Length: ' . filesize($tmpArchive));
    99                                 ob_flush();
    100                                 ob_clean();
    101                                 readfile($tmpArchive);
    102                                 unlink($tmpArchive);
    103                                 return;
    10476                        }
     77                        if($zip->close() !== true){
     78                                        echo "<html><head></head><body>Failed to save zip file</body></html>";
     79                                        return;
     80                        }
     81                       
     82                        echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
     83                       
     84                        //Send zip folder
     85                        while (ob_get_level()) {
     86                                ob_end_clean();
     87                        }
     88                        ob_start();
     89                        header('Content-Type: application/zip');
     90                        header('Content-Transfer-Encoding: Binary');
     91                        header('Content-disposition: attachment; filename=' . $archiveName . '.zip');
     92                        header('Content-Length: ' . filesize($tmpArchive));
     93                        ob_flush();
     94                        ob_clean();
     95                        readfile($tmpArchive);
     96                        unlink($tmpArchive);
     97                        return;
    10598                }
    10699        }
  • src/webapp/webservice/inc/GnuBoat/ServiceMap.php

    rc810990 r8fd7454  
    3030        protected function process()
    3131        {
    32                 header('Content-type: application/json');
     32                if(!array_key_exists("mapName", $_POST))
     33                        return $this->error("Please specify map name (no value)");
    3334
    34                 $result["error"] = 1;
    35                 $result["errormessage"] = "Unknown Error";
    36 
    37                 if(!array_key_exists("mapName", $_POST)){
    38                         $result["error"] = 1;
    39                         $result["errormessage"] = "Please specify map name (no value)";
    40                         echo json_encode($result);
    41                         return;
    42                 }
    4335                $mapName = $_POST["mapName"];
    44                 if(empty($mapName) || $mapName == ""){
    45                         $result["error"] = 1;
    46                         $result["errormessage"] = "Please specifiy map name (empty value)";
    47                         echo json_encode($result);
    48                         return;
    49                 }
     36                if(empty($mapName) || $mapName == "")
     37                        return $this->error("Please specifiy map name (empty value)");
    5038
    5139                $mapDirectory = __DIR__ ."/../../maps";
    52                 if(!array_key_exists("mapFile", $_FILES)){
    53                         $result["error"] = 1;
    54                         $result["errormessage"] = "No map file recieved";
    55                         echo json_encode($result);
    56                         return;
    57                 }
    58                 if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error'])){
    59                         $result["error"] = 1;
    60                         $result["errormessage"] = "Invalid parameters";
    61                         echo json_encode($result);
    62                         return;
    63                 }
     40                if(!array_key_exists("mapFile", $_FILES))
     41                        return $this->error("No map file recieved");
     42
     43                if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error']))
     44                        return $this->error("Invalid parameters");
     45
    6446                switch ($_FILES['mapFile']['error']) {
    6547                        case UPLOAD_ERR_OK:
    6648                                break;
    6749                        case UPLOAD_ERR_NO_FILE:
    68                                 $result["error"] = 1;
    69                                 $result["errormessage"] = "No file sent";
    70                                 echo json_encode($result);
    71                                 return;
     50                                return $this->error("No file sent");
    7251                        case UPLOAD_ERR_INI_SIZE:
    7352                        case UPLOAD_ERR_FORM_SIZE:
    74                                 $result["error"] = 1;
    75                                 $result["errormessage"] = "Exceeded filesize limit.";
    76                                 echo json_encode($result);
    77                                 return;
     53                                return $this->error("Exceeded filesize limit.");
    7854                        default:
    79                                 $result["error"] = 1;
    80                                 $result["errormessage"] = "Unknown PHP Error";
    81                                 echo json_encode($result);
    82                                 return;
     55                                return $this->error("Unknown PHP Error");
    8356                }
    8457                $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
    8558                if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
    86                         if(!chmod($mapFileName, 0755)){
    87                                 $result["error"] = 1;
    88                                 $result["errormessage"] = "Could not change permissions of the map file";
    89                         }
    90                         else{
    91                                 // correct map metadata
    92                                 $resultRename = $this->setMapName($mapFileName, $mapName);
    93                                 if($resultRename[0] === true){
    94                                         $result["error"] = 0;
    95                                         $result["errormessage"] = "";
    96                                 }
    97                                 else{
    98                                         $result["error"] = 1;
    99                                         $result["errormessage"] = $resultRename[1];
    100                                 }
    101                         }
     59                        if(!chmod($mapFileName, 0755))
     60                                return $this->error("Could not change permissions of the map file");
     61                        // correct map metadata
     62                        $resultRename = $this->setMapName($mapFileName, $mapName);
     63                        if($resultRename[0] === true)
     64                                return $this->output(["error" => 0, "errormessage" => ""]);
     65                       
     66                        return $this->error($resultRename[1]);
    10267                }
    103                 else {
    104                         $result["error"] = 1;
    105                         $result["errormessage"] = "Could not move map in place";
    106                 }
    107                 echo json_encode($result);
     68                else
     69                        return $this->error("Could not move map in place");
    10870        }
    10971       
  • src/webapp/webservice/inc/GnuBoat/ServiceShutdown.php

    rc810990 r8fd7454  
    3636                if($handle === false){
    3737                        $message = "Could not create file (". $config->shutdownRequestFile() .") to indicate shutdown to the daemon.";
    38                         if($debug > 0)
    39                                 echo $message;
    40                         error_log($message);
    41                         $result["shutdownstart"] = -1;
    42                         $result["errormessage"] = $message;
    43                 }
    44                 else{
    45                         $shutdownTime = new \DateTime('NOW');
    46                         $shutdownTime->modify("+15 seconds");
    47                         fwrite($handle, $shutdownTime->format(\DateTime::ATOM));
    48                         $result["shutdownstart"] = $shutdownTime->format(\DateTime::ATOM);
     38                        return $this->error($message, ["shutdownstart" => -1]);
    4939                }
    5040
    51                 header('Content-Type: application/json');
    52                 if($debug > 0){
    53                         echo json_encode($result,  JSON_PRETTY_PRINT) ."\n";
    54                 }
    55                 else{
    56                         echo json_encode($result) ."\n";
    57                 }
     41                $shutdownTime = new \DateTime('NOW');
     42                $shutdownTime->modify("+15 seconds");
     43                fwrite($handle, $shutdownTime->format(\DateTime::ATOM));
     44                fclose($handle);
     45                $result["shutdownstart"] = $shutdownTime->format(\DateTime::ATOM);
     46                $this->output($result);
    5847        }
    5948}
  • src/webapp/webservice/inc/GnuBoat/Webservice.php

    rc810990 r8fd7454  
    2323class WebService
    2424{
    25         function __construct()
     25        function __construct($debug)
    2626        {
    2727                $this->_requests = array();
    … …  
    3030                $this->_requests["logfiles"] = "GnuBoat\ServiceLogFiles";
    3131                $this->_requests["map"] = "GnuBoat\ServiceMap";
     32                $this->_requests["replay"] = "GnuBoat\ServiceReplay";
     33               
     34                $this->_debug = $debug;
    3235        }
    3336
    3437        public function process()
    3538        {
    36                 $debug = 0;
    37                 if(is_array($_GET) && array_key_exists("debug", $_GET))
    38                         $debug = $_GET["debug"];
    39 
    4039                $request = array_keys($this->_requests)[0];
    4140                if(is_array($_GET) && array_key_exists("request", $_GET)){
    … …  
    4443
    4544                if(!array_key_exists($request, $this->_requests)){
    46                         $result["error"] = 1;
    47                         $result["errormessage"] = "Request ". $request ." not implemented";
    48                        
    49                         header('Content-Type: application/json');
    50                         echo json_encode($result) ."\n";
    51                         return;
     45                        return Service::errorStatic("Request ". $request ." not implemented", $this->_debug);
    5246                }
    53 
    54                 call_user_func(array(new $this->_requests[$request], "processRequest"), $debug);
     47               
     48                call_user_func(array(new $this->_requests[$request], "processRequest"), $this->_debug);
    5549        }
    5650
    5751        private $_requests;
     52        private $_debug;
    5853}
    5954
  • src/webapp/webservice/inc/autoloader.php

    rc810990 r8fd7454  
    2525}
    2626
     27function canClassBeLoaded($class) {
     28        $parts = explode('\\', $class);
     29        $fileName = __DIR__ ."/". join("/", $parts) .".php";
     30        return file_exists($fileName);
     31}
     32
    2733spl_autoload_register("gnuboatAutoloader");
    2834
  • src/webapp/webservice/index.php

    rc810990 r8fd7454  
    1919require_once(__DIR__ ."/inc/autoloader.php");
    2020
    21 $webservice = new GnuBoat\Webservice();
     21$debug = 0;
     22if(array_key_exists("debug", $_GET))
     23        $debug = $_GET["debug"];
     24
     25$webservice = new GnuBoat\Webservice($debug);
    2226$webservice->process();
    2327
  • src/webapp/webservice/tests/ServiceTest.php

    rc810990 r8fd7454  
    2020// declare(Strict_types=1);
    2121
     22namespace GnuBoat;
     23
    2224/**
    23   * @brief Covers index.php tests
     25  * @brief Covers Service.php tests
    2426  */
    25 final class ServiceTest extends PHPUnit_Framework_TestCase
     27final class ServiceTest extends \PHPUnit_Framework_TestCase
    2628{
    27         /**
    28           * @runInSeparateProcess
    29           */
    3029        public function testData()
    3130        {
    32                 $webservice = new GnuBoat\Webservice();
    33                 $this->assertInstanceOf(GnuBoat\Webservice::class, $webservice);
    34                 $_POST["request"] = "data";
     31                $webservice = new Webservice(0);
     32                $this->assertInstanceOf(Webservice::class, $webservice);
     33                $_GET["request"] = "data";
    3534                $webservice->process();
    3635        }
    3736
    38         /**
    39           * @runInSeparateProcess
    40           */
    4137        public function testLogFiles()
    4238        {
    43                 $webservice = new GnuBoat\Webservice();
    44                 $this->assertInstanceOf(GnuBoat\Webservice::class, $webservice);
    45                 $_POST["request"] = "logfiles";
     39                $webservice = new Webservice(0);
     40                $this->assertInstanceOf(Webservice::class, $webservice);
     41                $_GET["request"] = "logfiles";
    4642                $webservice->process();
    4743        }
    4844       
    49         /**
    50           * @runInSeparateProcess
    51           */
    5245        public function testShutdown()
    5346        {
    54                 $webservice = new GnuBoat\Webservice();
    55                 $this->assertInstanceOf(GnuBoat\Webservice::class, $webservice);
    56                 $_POST["request"] = "shutdown";
     47                $webservice = new Webservice(0);
     48                $this->assertInstanceOf(Webservice::class, $webservice);
     49                $_GET["request"] = "shutdown";
    5750                $webservice->process();
    5851        }
    59        
    60         /**
    61           * @runInSeparateProcess
    62           */
     52
    6353        public function testMap()
    6454        {
    65                 $webservice = new GnuBoat\Webservice();
    66                 $this->assertInstanceOf(GnuBoat\Webservice::class, $webservice);
    67                 $_POST["request"] = "map";
     55                $webservice = new Webservice(0);
     56                $this->assertInstanceOf(Webservice::class, $webservice);
     57                $_GET["request"] = "map";
    6858                $webservice->process();
     59        }
     60
     61        public function testReplay()
     62        {
     63                $webservice = new Webservice(0);
     64                $_GET["request"] = "replay";
     65                $_POST["logfile"] = "replay-test.log";
     66                $webservice->process();
     67                $output = $this->getActualOutput();
     68                $this->assertJsonStringEqualsJsonString('{"track":{"1503767412":{"latitude":1.001,"longitude":1.001,"speedthroughwater":"-","speedoverground":11,"courseoverground":21,"waterdepth":"-","watertemperature":"-","gpsfix":"-"},"1503767413":{"latitude":2.0186666666667001,"longitude":2.0186666666667001,"speedthroughwater":"-","speedoverground":12,"courseoverground":22,"waterdepth":"-","watertemperature":"-","gpsfix":"-"},"1503767414":{"latitude":3.1696666666666999,"longitude":3.1696666666666999,"speedthroughwater":"-","speedoverground":13,"courseoverground":23,"waterdepth":1,"watertemperature":21,"gpsfix":"-"},"1503767415":{"latitude":4.004,"longitude":4.004,"speedthroughwater":"-","speedoverground":14,"courseoverground":24,"waterdepth":2,"watertemperature":22,"gpsfix":"-"},"1503767416":{"latitude":5.005,"longitude":5.005,"speedthroughwater":"-","speedoverground":15,"courseoverground":25,"waterdepth":3,"watertemperature":23,"gpsfix":"-"}}}', $output);
    6969        }
    7070}
    7171
     72function error_log($msg)
     73{
     74        echo "ERR GnuBoat: ". $msg ."\n";
     75}
     76
    7277?>
Note: See TracChangeset for help on using the changeset viewer.