-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp_server.php
79 lines (67 loc) · 1.83 KB
/
esp_server.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
<?php
require_once(__DIR__.'/espdb.php');
class esp_server {
private $espdb;
private $debug = 1;
public function __construct()
{
$this->espdb = new espdb;
if ($this->check_post() == false) {
return;
}
}
public function check_post() {
if (!isset($_GET)) return false;
$device = "";
$value = -1000;
$sensorID = -1000;
$valueType = "undef";
$bootcount = -1;
$mytime = time();
if (isset($_GET['device']) && $this->alphaNumericCheck($_GET['device'])) {
// usually wifi MAC address of the device
$device = $_GET['device'];
} else return;
if (isset($_GET['addtemp']) ) {
$value = $_GET['addtemp'];
}
if (isset($_GET['sensorid']) && $this->alphaNumericCheck($_GET['sensorid'])) {
$sensorID = $_GET['sensorid'];
}
if (isset($_GET['type']) && $this->alphaNumericCheck($_GET['type'])) {
// what type, eg. level, temp
$valueType = $_GET['type'];
} else return;
if (isset($_GET['time'])) {
// exact time of measurement
$mytime = $_GET['time'];
}
if (isset($_GET['bc']) && is_numeric($_GET['bc'])) {
// bootcount
$bootcount = $_GET['bc'];
}
if (isset($_GET['value']) && is_numeric($_GET['value'])) {
// value of measurement
$value = $_GET['value'];
}
$this->lok("Device: $device, Value: $value, bootcount: $bootcount");
echo $this->espdb->addNewMeasurementToDB($valueType, $device, $value, $mytime, $bootcount, $sensorID);
echo "Ding";
return;
}
private function alphaNumericCheck($arg = null) {
if ($arg === null) return;
return preg_match('/^[a-zA-Z0-9]*$/', $arg);
}
# debug print string
private function dp($arg = null) {
if ($arg === null || $this->debug == 0) return;
echo $arg . "\n";
}
private function lok($arg = null) {
if ($arg === null) return;
file_put_contents(__DIR__.'/log.log', $arg, FILE_APPEND);
}
}
$pum = new esp_server();
?>