-
Notifications
You must be signed in to change notification settings - Fork 3
/
smsRequest.php
184 lines (158 loc) · 5.05 KB
/
smsRequest.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
include '/home/pi/UNICEF_VACCINE_MONITOR/hilink_smsGateway.php';
// Retrive inbox
$Messages = getList(1);
if(!empty($Messages)){
$db = new mysqli('localhost', 'root', 'raspberry', 'emoncms');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
foreach ($Messages->Message as $mes) {
$content = $mes -> Content;
echo $content."\n";
$keyword = explode(" ", strtolower($content));
echo $keyword[0];
$sender = $mes -> Phone;
$index = $mes -> Index;
switch ($keyword[0]) {
case "api":
switch ($keyword[1]) {
case "get":
$result = $db->query("SELECT * FROM raspberrypi");
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row['remoteapikey']."\n";
sendMessage($sender, "Current remote API-key: ".$row['remoteapikey']);
break;
case "set":
$apikey = $keyword[2];
if(strlen($apikey) == 32){
$db->query("UPDATE raspberrypi SET remoteapikey = '$apikey'");
sendMessage($sender, "The remote API-key has been changed to: ".$apikey);
} else {
sendMessage($sender, "Error, the API-key should be 32 characters long");
}
break;
}
break;
case "domain":
switch ($keyword[1]) {
case "get":
$result = $db->query("SELECT * FROM raspberrypi");
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row['remotedomain']."\n";
sendMessage($sender, "Current remote domain: ".$row['remotedomain']);
break;
case "set":
$domain = $keyword[2];
$db->query("UPDATE raspberrypi SET remotedomain = '$domain'");
sendMessage($sender, "The remote domain has been changed to: ".$domain);
break;
}
break;
case "path":
switch ($keyword[1]) {
case "get":
$result = $db->query("SELECT * FROM raspberrypi");
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row['remotepath']."\n";
sendMessage($sender, "Current remote path: ".$row['remotepath']);
break;
case "set":
$path = $keyword[2];
$db->query("UPDATE raspberrypi SET remotepath = '$path'");
sendMessage($sender, "The remote path has been changed to: ".$path);
break;
}
break;
case "protocol":
switch ($keyword[1]) {
case "get":
$result = $db->query("SELECT * FROM raspberrypi");
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row['remoteprotocol']."\n";
sendMessage($sender, "Current remote protocol: ".$row['remoteprotocol']);
break;
case "set":
$protocol = $keyword[2];
$db->query("UPDATE raspberrypi SET remoteprotocol = '$protocol'");
sendMessage($sender, "The remote domain has been changed to: ".$protocol);
break;
}
break;
case "alarm": // Alarm
$check = $db->query("SELECT * FROM event WHERE setphonenumber = '$sender'");
if($check->num_rows > 0) {
$db->query("DELETE FROM event WHERE setphonenumber = '$sender'");
sendMessage($sender, "You have been taken off the alarm list");
} else {
$db->query("INSERT INTO event (userid, eventfeed, eventtype, action, setphonenumber, lasttime, mutetime) VALUES ('1','0','7', '5', '$sender','0','300')");
sendMessage($sender, "You have been signed up for alarms");
}
break;
case "reboot":
sendMessage($sender, "System will now reboot");
deleteMessage($index);
exec('sudo reboot');
break;
case "git": // git pull repository
exec('cd /home/pi/UNICEF_VACCINE_MONITOR && sudo git pull && cd -');
sendMessage($sender, "Newest repository has been been pulled, system will now reboot");
deleteMessage($index);
exec('sudo reboot');
break;
case "sensor":
$sensor = $keyword[2];
echo $sensor;
$result = $db->query("SELECT * FROM feeds WHERE name = '$sensor'");
if($result->num_rows < 1) {
sendMessage($sender, "could not find sensor: ".$sensor);
break;
}
switch ($keyword[1]) {
case "get":
$row = $result -> fetch_assoc();
sendMessage($sender, "Sensor: ".$sensor.", [".$row['tag'].", ".$row['value']."]");
break;
case "set":
switch ($keyword[3]) {
case "0":
// Disabled sensor, ignored in the alarm module
$tag = "none";
break;
case "1":
// fridge
$tag = "fridge";
break;
case "2":
// freezer
$tag = "freezer";
break;
case "3":
// outdoor
$tag = "outdoor";
break;
case "4":
// power-source
$tag = "power-source";
break;
}
$db -> query("UPDATE feeds SET tag = '$tag' WHERE name = '$sensor'");
sendMessage($sender, "Sensor: ".$sensor.", has changed tag to: ".$tag);
break;
}
break;
default:
sendMessage($sender, "\"".$content."\" does not match any known keywords, try again.");
}
deleteMessage($index);
}
}
// Empty Outbox
$Messages = getList(2);
if(!empty($Messages)){
foreach ($Messages->Message as $mes) {
$index = $mes -> Index;
deleteMessage($index);
}
}
?>