-
Notifications
You must be signed in to change notification settings - Fork 15
/
cron_schedule.php
267 lines (167 loc) · 8.19 KB
/
cron_schedule.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/* Connect to database
--------------------------------------------------------------------------- */
require("./lib/base.inc.php");
// Create DB-instance
$mysqli = new Mysqli($host, $username, $password, $db_name);
// Check for connection errors
if ($mysqli->connect_errno) {
die('Connect Error: ' . $mysqli->connect_errno);
}
// Set DB charset
mysqli_set_charset($mysqli, "utf8");
/* ##################################################################################################################### */
/* ######################################## SCRIPT RUNS BELOW THIS LINE ################################################ */
/* ##################################################################################################################### */
$query = "SELECT *
FROM ".$db_prefix."schedule
INNER JOIN ".$db_prefix."sensors ON ".$db_prefix."schedule.sensor_id = ".$db_prefix."sensors.sensor_id
INNER JOIN ".$db_prefix."users ON ".$db_prefix."schedule.user_id = ".$db_prefix."users.user_id
";
$result = $mysqli->query($query);
$numRows = $result->num_rows;
if ($numRows > 0) {
while($row = $result->fetch_array()) {
$scheduleRun = false; // Set default as false
/* PRINT DATA FOR DEBUG
echo "Notification ID: {$row['notification_id']} <br />";
echo "Sensorname: {$row['name']} <br />";
echo "Sensor ID: {$row['sensor_id']} <br />";
echo "Device ID: {$row['device']} <br />";
*/
/* Get last sensor values logged
--------------------------------------------------------------------------- */
// Check only for temp values newer than
$newerThan = (time() - 3600); // 3600 = 1 hour
$queryTemp = "SELECT * FROM ".$db_prefix."sensors_log WHERE sensor_id='{$row['sensor_id']}' AND time_updated < '$newerThan' ORDER BY time_updated DESC LIMIT 1";
$resultTemp = $mysqli->query($queryTemp);
$tempData = $resultTemp->fetch_array();
/* Check for warning last sent
--------------------------------------------------------------------------- */
$timeSinceLastWarning = (time() - $row['last_warning']);
// Check if notification-repeat-state
if (($row['repeat_alert'] * 60) < $timeSinceLastWarning) $repeatNotification = true;
else $repeatNotification = false;
/* Check for device action
--------------------------------------------------------------------------- */
/* Don't run if last action was less than SET-secounds
* This is to prevent sending data every time
* 15 minutes => 900 sec
* 30 minutes => 1800 sec
* 60 minutes => 3600 sec
*/
if (900 < $timeSinceLastWarning) $repeatDeviceState = true;
else $repeatDeviceState = false;
if (empty($row['device'])) $repeatDeviceState = false; // Check for device
// Connect to Telldus Live
else {
require_once 'HTTP/OAuth/Consumer.php';
$consumer = new HTTP_OAuth_Consumer(constant('PUBLIC_KEY'), constant('PRIVATE_KEY'), constant('TOKEN'), constant('TOKEN_SECRET'));
if ($row['device_set_state'] == 1) $deviceStateApiParameter = "turnOn";
else $deviceStateApiParameter = "turnOff";
}
/* Check for LESS THAN
--------------------------------------------------------------------------- */
if ($row['direction'] == "less") {
// CELSIUS
if ($row['type'] == "celsius") {
if ($tempData['temp_value'] < $row['warning_value']) {
//echo "<b>WARNING: Temperature is less</b><br />{$lang['Notification mail low temperature']}<br /><br />";
// Send notification
if ($repeatNotification) {
$scheduleRun = true;
// Get and replace variables in mail message
$mailSubject = "{$config['pagetitle']}: {$lang['Warning']}: {$lang['Low']} {$lang['Temperature']}";
$mailMessage = "{$lang['Notification mail low temperature']}";
$mailMessage = str_replace("%%sensor%%", $row['name'], $mailMessage);
$mailMessage = str_replace("%%value%%", $tempData['temp_value'], $mailMessage);
}
// Send state to device
if ($repeatDeviceState) {
$scheduleRun = true;
$params = array('id'=> $row['device']);
$response = $consumer->sendRequest(constant('REQUEST_URI').'/device/'. $deviceStateApiParameter, $params, 'GET');
}
}
}
// HUMIDITY
elseif ($row['type'] == "humidity") {
if ($tempData['humidity_value'] < $row['warning_value']) {
//echo "<b>WARNING: Humidity is less</b><br />";
if ($repeatNotification) {
$scheduleRun = true;
// Get and replace variables in mail message
$mailSubject = "{$config['pagetitle']}: {$lang['Warning']}: {$lang['Low']} {$lang['Humidity']}";
$mailMessage = "{$lang['Notification mail low humidity']}";
$mailMessage = str_replace("%%sensor%%", $row['name'], $mailMessage);
$mailMessage = str_replace("%%value%%", $tempData['humidity_value'], $mailMessage);
}
// Send state to device
if ($repeatDeviceState) {
$scheduleRun = true;
$params = array('id'=> $row['device']);
$response = $consumer->sendRequest(constant('REQUEST_URI').'/device/'. $deviceStateApiParameter, $params, 'GET');
}
}
}
}
/* Check for MORE THAN
--------------------------------------------------------------------------- */
if ($row['direction'] == "more") {
// CELSIUS
if ($row['type'] == "celsius") {
if ($tempData['temp_value'] > $row['warning_value']) {
//echo "<b>WARNING: Temperature is more</b><br />{$lang['Notification mail high temperature']}<br /><br />";
if ($repeatNotification) {
$scheduleRun = true;
// Get and replace variables in mail message
$mailSubject = "{$config['pagetitle']}: {$lang['Warning']}: {$lang['High']} {$lang['Temperature']}";
$mailMessage = "{$lang['Notification mail high temperature']}";
$mailMessage = str_replace("%%sensor%%", $row['name'], $mailMessage);
$mailMessage = str_replace("%%value%%", $tempData['temp_value'], $mailMessage);
}
// Send state to device
if ($repeatDeviceState) {
$scheduleRun = true;
$params = array('id'=> $row['device']);
$response = $consumer->sendRequest(constant('REQUEST_URI').'/device/'. $deviceStateApiParameter, $params, 'GET');
}
}
}
// HUMIDITY
elseif ($row['type'] == "humidity") {
if ($tempData['humidity_value'] > $row['warning_value']) {
//echo "<b>WARNING: Humidity is more</b><br />";
if ($repeatNotification) {
$scheduleRun = true;
// Get and replace variables in mail message
$mailSubject = "{$config['pagetitle']}: {$lang['Warning']}: {$lang['High']} {$lang['Humidity']}";
$mailMessage = "{$lang['Notification mail high humidity']}";
$mailMessage = str_replace("%%sensor%%", $row['name'], $mailMessage);
$mailMessage = str_replace("%%value%%", $tempData['humidity_value'], $mailMessage);
}
// Send state to device
if ($repeatDeviceState) {
$scheduleRun = true;
$params = array('id'=> $row['device']);
$response = $consumer->sendRequest(constant('REQUEST_URI').'/device/'. $deviceStateApiParameter, $params, 'GET');
}
}
}
}
/* IF warning = true
--------------------------------------------------------------------------- */
if ($scheduleRun) {
// Update sent timestamp
$queryTimestamp = "UPDATE ".$db_prefix."schedule SET last_warning='".time()."' WHERE notification_id='".$row['notification_id']."'";
$resultTimestamp = $mysqli->query($queryTimestamp);
// Send mail
if ($row['send_to_mail'] == 1 && $repeatNotification == true) {
// Use mail-function in /lib/php_functions/global.functions.inc.php
if (!empty($row['notification_mail_primary'])) sendMail($row['notification_mail_primary'], $mailSubject, $mailMessage);
if (!empty($row['notification_mail_secondary'])) sendMail($row['notification_mail_secondary'], $mailSubject, $mailMessage);
}
}
} //end-while
} //end-numRows
?>