forked from emilbunk/UNICEF_VACCINE_MONITOR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarmModule.php
61 lines (47 loc) · 1.49 KB
/
alarmModule.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
<?php
include '/home/pi/UNICEF_VACCINE_MONITOR/hilink_smsGateway.php';
// Global variables
$fridgeMax = 7.5;
$fridgeLow = 2.5;
$freezerMax = -15;
$freezerLow = -25;
$db = new mysqli('localhost', 'root', 'raspberry', 'emoncms');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$alarms = "";
// Check Power source
$input = $db->query("SELECT * FROM feeds WHERE tag = 'power-source'");
while($row = $input -> fetch_assoc()) {
if($row['value'] == '0') {
$time = strtotime($row['time']);
if(time()-$time < 10 * 60) { // if reading is less than 10 min old
$alarms = $alarms."The power-grid seems to be down! - ";
}
}
}
// Check Fridges
$input = $db->query("SELECT * FROM feeds WHERE tag = 'fridge'");
while($row = $input -> fetch_assoc()) {
$name = $row['name'];
$val = floatval($row['value']);
$time = strtotime($row['time']);
if(time()-$time < 10 * 60) { // if reading is less than 10 min old
if($val > $fridgeMax OR $val < $fridgeLow) {
$alarms = $alarms."[".$name."] ".$row['value'].", ";
}
}
}
if(strlen($alarms) > 0) {
$mutetime = time() - 30 * 60;
$users = $db -> query("SELECT * FROM event WHERE lasttime < '$mutetime'");
while($row = $users -> fetch_assoc()) {
$phoneNumber = $row['setphonenumber'];
$message = "ALARM: ".substr($alarms, 0, -2);
echo $message;
sendMessage($phoneNumber, $message);
$id = $row['id'];
$updateTime = time();
$db -> query("UPDATE event SET lasttime = '$updateTime' WHERE id = '$id'");
}
}?>