-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_fanspeed.php
127 lines (101 loc) · 2.99 KB
/
api_fanspeed.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
if(!isset($api))
{
die("Direct call to api_PHP.php is not allowed!");
}
//$data = array();
$dbFanSpeed ="/etc/pihole/fanspeed.db";
if (isset($_GET['getFanSpeed24hrs']) && $auth)
{
$data = array_merge($data, getFanSpeedData24hrs($dbFanSpeed));
}
if (isset($_GET['getLastFanSpeedResult']) && $auth)
{
$data = array_merge($data, getLastFanSpeedResult($dbFanSpeed));
}
if (isset($_GET['getAllFanSpeedData']) && $auth)
{
$data = array_merge($data, getAllFanSpeedData($dbFanSpeed));
}
function getAllFanSpeedData($dbFanSpeed)
{
$data = getFanSpeedData($dbFanSpeed,-1);
if(isset($data['errr']))
return [];
$newarr = array();
foreach ($data as $array) {
array_push($newarr,array_values($array));
}
return array('data' => $newarr );
}
function getLastFanSpeedResult($dbFanSpeed){
if(!file_exists($dbFanSpeed)){
// create db of not exists
copy('scripts/pihole/fanspeed/fanspeed.db', '/etc/pihole/fanspeed.db'); return array();
}
$db = new SQLite3($dbFanSpeed);
if(!$db) {
return array("error"=>"Unable to open DB");
} else {
// return array("status"=>"success");
}
$curdate = date('Y-m-d H:i:s');
$date = new DateTime();
$date->modify('-'.$durationdays.' day');
$start_date =$date->format('Y-m-d H:i:s');
$sql ="SELECT * from fanspeed order by id DESC limit 1";
$dbResults = $db->query($sql);
$dataFromFanSpeedDB= array();
if(!empty($dbResults)){
while($row = $dbResults->fetchArray(SQLITE3_ASSOC) ) {
array_push($dataFromFanSpeedDB, $row);
}
return($dataFromFanSpeedDB);
}
else{
return array("error"=>"No Results");
}
$db->close();
}
function getFanSpeedData($dbFanSpeed,$durationdays="1")
{
if(!file_exists($dbFanSpeed)){
// create db of not exists
copy('scripts/pihole/fanspeed/fanspeed.db', '/etc/pihole/fanspeed.db');
return array();
}
$db = new SQLite3($dbFanSpeed);
if(!$db) {
return array("error"=>"Unable to open DB");
} else {
// return array("status"=>"success");
}
$curdate = date('Y-m-d H:i:s');
$date = new DateTime();
$date->modify('-'.$durationdays.' day');
$start_date =$date->format('Y-m-d H:i:s');
if($durationdays == -1)
{
$sql ="SELECT * from fanspeed order by id asc";
}
else{
$sql ="SELECT * from fanspeed where start_time between '${start_date}' and '${curdate}' order by id asc;";
}
$dbResults = $db->query($sql);
$dataFromFanSpeedDB= array();
if(!empty($dbResults)){
while($row = $dbResults->fetchArray(SQLITE3_ASSOC) ) {
array_push($dataFromFanSpeedDB, $row);
}
return($dataFromFanSpeedDB);
}
else{
return array("error"=>"No Results");
}
$db->close();
}
function getFanSpeedData24hrs($dbFanSpeed){
global $log;
$dataFromFanSpeedDB = getFanSpeedData($dbFanSpeed);
return $dataFromFanSpeedDB;
}