From 7838aee0552ad2cc7e1bcbc0488ffbb39a1add15 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 14 Oct 2020 21:17:39 +0200 Subject: [PATCH 1/2] check if latestjson is younger than 60s, else update curl.php --- website/curl.php | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/website/curl.php b/website/curl.php index 4c57c84..7d5d5b1 100644 --- a/website/curl.php +++ b/website/curl.php @@ -27,7 +27,6 @@ $scooters = array( - // for each scooter in array get the json (TODO, for each, safe to file/database?) $scooters is a multidimensional array: $scooter[0][0] = the first data (api url) of the first scooter, $scooter[1][0] = the first data (api url) of the second scooter, $scooter[0][1] = the artist name of the first scooter etcetc. //$id = 0; //get the first scooter for now @@ -71,11 +70,8 @@ global $scooters; //"import" the global variable into the function's scope } // THIS ONLY NEEDS TO RUN EVERY 1 or 2 minutes, possibly in the background, als this should construct historical json as per tom martins post here: https://stackoverflow.com/questions/7895335/append-data-to-a-json-file-with-php/7895384 -$arrayToEcho = saveLatestJSON(); - $latestData = json_encode($arrayToEcho, JSON_FORCE_OBJECT); // force object accepts 0 as a key for the array and not NULL - - $file = './latestData.json'; - file_put_contents($file, $latestData, LOCK_EX); + + //TODO make historical json file.. @@ -164,11 +160,6 @@ function makeMarker(int $id){ // generate appropriate markers based on latest JS // Custom functions -function checkModified($file, $expiration){ //check if file is older than expiration time - -} - - function callAPI($url){ $curl = curl_init($url); @@ -260,14 +251,34 @@ function callAPI($url){ //for each scooter in $scooters array, request status and construct marker 60) { + $arrayToEcho = saveLatestJSON(); + $latestData = json_encode($arrayToEcho, JSON_FORCE_OBJECT); // force object accepts 0 as a key for the array and not NULL + file_put_contents($filename, $latestData, LOCK_EX); + + + $id = 0; //start id foreach($scooters as $item) { - - // echo makeMarker($id); echo makeMarker($id); + $id++; + } + } else { // file younger than 60 seconds + + $id = 0; //start id + foreach($scooters as $item) { + echo makeMarker($id); $id++; } + } + + + + ?> // time slider From 92006102acc61bf05e5aa18a86660c69db31daeb Mon Sep 17 00:00:00 2001 From: user Date: Wed, 14 Oct 2020 21:44:05 +0200 Subject: [PATCH 2/2] added historical data capturing curl.php --- website/curl.php | 51 ++++++++++++++++++++++++++++++++++--- website/historicalData.json | 1 + 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100755 website/historicalData.json diff --git a/website/curl.php b/website/curl.php index 7d5d5b1..d3a97ab 100644 --- a/website/curl.php +++ b/website/curl.php @@ -127,7 +127,46 @@ function makeMarker(int $id){ // generate appropriate markers based on latest JS } +function appendJSON($filename, $data){ // create historical json data for documentation over time + // read the file if present + $handle = @fopen($filename, 'r+'); + + // create the file if needed + if ($handle === null) + { + $handle = fopen($filename, 'w+'); + } + + if ($handle) + { + // seek to the end + fseek($handle, 0, SEEK_END); + + // are we at the end of is the file empty + if (ftell($handle) > 0) + { + // move back a byte + fseek($handle, -1, SEEK_END); + + // add the trailing comma + fwrite($handle, ',', 1); + + // add the new json string + fwrite($handle, json_encode($data) . ']'); + } + else + { + // write the first event inside an array + fwrite($handle, json_encode(array($data))); + } + + // close the handle on the file + fclose($handle); + } + + +} //// make date time file------ @@ -249,14 +288,18 @@ function callAPI($url){ map.addLayer(Stamen_Toner); - //for each scooter in $scooters array, request status and construct marker + //the magic: for each scooter in $scooters array, request status and construct marker 60) { - $arrayToEcho = saveLatestJSON(); + if (time()-filemtime($filename) > 60) { //if older than 60 seconds + $arrayToEcho = saveLatestJSON(); + //append to historical json + appendJSON($filename_history, $arrayToEcho); + $latestData = json_encode($arrayToEcho, JSON_FORCE_OBJECT); // force object accepts 0 as a key for the array and not NULL file_put_contents($filename, $latestData, LOCK_EX); diff --git a/website/historicalData.json b/website/historicalData.json new file mode 100755 index 0000000..0fbaf3b --- /dev/null +++ b/website/historicalData.json @@ -0,0 +1 @@ +[[{"ID":0,"lastPos":"52.57619, 13.358061","lastLocationUpdate":"2020-10-09T08:00:29Z","isRentable":true,"state":"ACTIVE","name":"artist name 0","title":"title 0","desc":"desciption 0"},{"ID":1,"lastPos":"52.55619, 13.358061","lastLocationUpdate":"2020-10-09T08:00:29Z","isRentable":true,"state":"ACTIVE","name":"artist name 1","title":"title 1","desc":"desciption 1"},{"ID":2,"lastPos":"52.53619, 13.358061","lastLocationUpdate":"2020-10-09T08:00:29Z","isRentable":true,"state":"ACTIVE","name":"artist name 2","title":"title 2","desc":"desciption 2"}]] \ No newline at end of file