-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite_sd.php
More file actions
96 lines (89 loc) · 2.68 KB
/
Copy pathwrite_sd.php
File metadata and controls
96 lines (89 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
function appendToFile($filename, $content) {
shell_exec("sudo remount");
if(file_exists($filename)) {
$decoded = (array) json_decode(file_get_contents($filename), false);
array_push($decoded, $content);
$content = $decoded;
} else {
$content = [$content];
}
file_put_contents($filename, json_encode($content));
shell_exec("sudo remount ro");
}
function deleteFile($filename) {
shell_exec("sudo remount");
unlink($filename);
shell_exec("sudo remount ro");
}
function getFileContents($filename) {
if(!file_exists($filename)) {
return "{}";
}
return file_get_contents($filename);
}
if(!empty($_GET["type"])) {
switch($_GET["type"]) {
case "adblue":
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
$adblueFillList = getFileContents("adblue.json");
if(empty($_GET["mileage"])) {
header("Content-Type: application/json");
echo $adblueFillList == "{}" ? "[]" : $adblueFillList;
} else {
$consumption = 1;
$adblueFillList = json_decode($adblueFillList, false);
if(count((array)$adblueFillList) < 1) {
echo json_encode(["error" => 1]);
return;
}
if(count($adblueFillList) > 2) {
$lastFourFills = array_slice($adblueFillList, -4, 4, false);
$consumptionList = [];
foreach($lastFourFills as $index => $fill) {
if($index == count($lastFourFills) - 1) {
break;
}
$consumptionForTrip = 1;
if(((array)$lastFourFills[$index+1])["liters"] != null) {
$mileageBetween = ((array)$lastFourFills[$index+1])["mileage"] - ((array)$lastFourFills[$index])["mileage"];
$consumptionForTrip = ((array)$lastFourFills[$index+1])["liters"] / ($mileageBetween/1000);
}
array_push($consumptionList, $consumptionForTrip);
}
sort($consumptionList);
$consumption = $consumptionList[(int)count($consumptionList)/2];
}
$sinceLastFill = ($_GET["mileage"] - ((array)$adblueFillList[count($adblueFillList)-1])["mileage"]);
$remaining = 17 - ($sinceLastFill / 1000) * $consumption;
$output = round($remaining, 2);
echo $output;
/*$output = [
"sinceLastFill" => round($sinceLastFill),
"remaining" => round($remaining, 2),
"consumption" => round($consumption, 2)
];
header("Content-Type: application/json");
echo json_encode($output);*/
}
return;
case 'POST':
$toWrite = [
"mileage" => $_POST["mileage"],
"liters" => $_POST["liters"],
];
appendToFile("adblue.json", $toWrite);
echo '{}';
return;
case 'DELETE':
deleteFile("adblue.json");
echo '{}';
return;
}
return;
}
}
echo "{}";
return;
?>