$id,
"lastPos" => $pos,
"lastLocationUpdate" => $lst,
"isRentable" => $rnt,
"state" => $act,
"name" => $nam,
"title" => $tit,
"desc" => $des);
$id++;
}
return $latestData; //return array
}
// 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
//TODO make historical json file..
//see: https://stackoverflow.com/questions/7895335/append-data-to-a-json-file-with-php/7895384
//add something like:
// $inp = file_get_contents('./latestData.json');
// $tempArray = json_decode($inp);
// $tempArray[] .= $latestData;
// $jsonData = json_encode($tempArray);
// file_put_contents('./latestData.json', $jsonData);
function makeMarker(int $id){ // generate appropriate markers based on latest JSON
$readJSON = file_get_contents('./latestData.json'); //read latest data
$arr = json_decode($readJSON, true);
$pos = $arr[$id]["lastPos"]; // get last known position
$lst = $arr[$id]["lastLocationUpdate"]; // get time of last position
$rnt = $arr[$id]["isRentable"]; // check if scooter is rentable,
$act = $arr[$id]["state"]; // check if scooter is ACTIVE or MAINTAINANCE
// access values from the multidimensional $scooters array
$nam = $arr[$id]["name"]; // get the artist name
$tit = $arr[$id]["title"]; // get the works title
$des = $arr[$id]["desc"]; // get the works description
//check scooter status and construct marker with corresponding icon
//check if scooter is rentable
if($rnt == true && $act === 'ACTIVE'){
$marker = 'var scooter'.$id.' = L.marker(['.$pos.'], {time: "'.$lst.'"}).addTo(map)
.bindPopup("'.$tit.'
'.$des.'
Last updated: '.$lst.'
Status: Rentable, Gallery Offline")
.bindTooltip("'.$nam.'").setIcon(availableIcon);';
} elseif ($rnt == false && $act === 'ACTIVE') {
$marker = 'var scooter'.$id.' = L.marker(['.$pos.'], {time: "'.$lst.'"}).addTo(map)
.bindPopup("'.$tit.'
'.$des.'
Last updated: '.$lst.'
Status: Rented Out, Gallery Online")
.bindTooltip("'.$nam.'").setIcon(isRentedIcon);';
};
//check if scooter is in maintenace
if($act !== "ACTIVE"){
$marker = 'var scooter'.$id.' = L.marker(['.$pos.'], {time: "'.$lst.'"}).addTo(map)
.bindPopup("'.$tit.'
'.$des.'
Last updated: '.$lst.'")
.bindTooltip("'.$nam.'").setIcon(isMaintenanceIcon);';
};
return $marker;
}
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------
///..mmm one big one? seperate ones, timestamped ones?
// 60 second update interval
// make timestamp file like original python one! to check for updates
// make new custom json:
// scooter 1/marker time1 { lat long }
// time2 { lat long }
// scooter 2/marker time1 { lat long }
// time2 { lat long }
//
//
// for each
// check if lastlocation is different...if update > latest file (map file..)
// check if is rentable
// TODO make two files
// 1: current position and state of scooter, latlong, id, status
// 2: historical json file, contains all scooters, updated everytime php file is loaded (or every 60 sec). No state changes, only lat long and time. even if time does not change
// Custom functions
function callAPI($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'x-api-key: bpEUTJEBTf74oGRWxaIcW7aeZMzDDODe1yBoSxi2',
'Content-Type: application/json',
)); // set the api key
curl_setopt($curl, CURLOPT_USERAGENT,'ProductionRelease/3.8.2 (app.tier.sharing; build:3.8.2.0; iOS 12.4.4) Alamofire/4.9.1'); // set user-agent to that of the Tier.app, sniffed by burpsuite
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}
?>