-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdemandshaper_run.php
365 lines (298 loc) · 15.7 KB
/
demandshaper_run.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
define("MAX",1);
define("MIN",0);
// default user when mqtt multiuser disabled
$default_userid = 1;
define('EMONCMS_EXEC', 1);
$fp = fopen("/var/lock/demandshaper.lock", "w");
if (! flock($fp, LOCK_EX | LOCK_NB)) { echo "Already running\n"; die; }
$pid = getmypid();
chdir("/var/www/emoncms");
require "process_settings.php";
require "Lib/EmonLogger.php";
require "core.php";
require "$linked_modules_dir/demandshaper/lib/scheduler2.php";
require "$linked_modules_dir/demandshaper/lib/misc.php";
set_error_handler('exceptions_error_handler');
$log = new EmonLogger(__FILE__);
$log->info("Starting demandshaper service");
// -------------------------------------------------------------------------
// MYSQL, REDIS, MQTT
// -------------------------------------------------------------------------
$mysqli = @new mysqli(
$settings["sql"]["server"],
$settings["sql"]["username"],
$settings["sql"]["password"],
$settings["sql"]["database"],
$settings["sql"]["port"]
);
if ( $mysqli->connect_error ) {
$log->error("Can't connect to database, please verify credentials/configuration in settings.php");
if ( $display_errors ) $log->error("Error message: ".$mysqli->connect_error);
die();
}
$redis = new Redis();
if (!$redis->connect($settings['redis']['host'], $settings['redis']['port'])) { $log->error("Can't connect to redis"); die; }
if (!empty($settings['redis']['prefix'])) $redis->setOption(Redis::OPT_PREFIX, $settings['redis']['prefix']);
if (!empty($settings['redis']['auth']) && !$redis->auth($settings['redis']['auth'])) {
$log->error("Can't connect to redis, autentication failed"); die;
}
$mqtt_client = new Mosquitto\Client();
$connected = false;
$mqtt_client->onConnect('connect');
$mqtt_client->onDisconnect('disconnect');
$mqtt_client->onMessage('message');
// -------------------------------------------------------------------------
// Load user module used to fetch user timezone
require("Modules/user/user_model.php");
$user = new User($mysqli,$redis);
require_once "Modules/device/device_model.php";
$device = new Device($mysqli,$redis);
require "Modules/demandshaper/demandshaper_model.php";
$demandshaper = new DemandShaper($mysqli,$redis,$device);
$forecast_list = $demandshaper->get_forecast_list();
$device_class = $demandshaper->load_device_classes($mqtt_client,$settings['mqtt']['basetopic']);
require_once "Modules/input/input_model.php";
$input = new Input($mysqli,$redis,false);
$redis->del("demandshaper:trigger");
// -------------------------------------------------------------------------
// Control Loop
// -------------------------------------------------------------------------
$last_retry = 0;
$timer = array();
$update_interval = 60;
$last_state_check = 0;
$schedules = array();
$firstrun = true;
$lasttime = time();
while(true)
{
$now = time();
// demandshaper trigger
$trigger = $redis->llen("demandshaper:trigger");
if ($trigger>0) {
$log->info("trigger");
}
// ---------------------------------------------------------------------
// Control Loop
// ---------------------------------------------------------------------
if ($now%$update_interval==0 || $trigger || $firstrun) {
$firstrun = false;
$users = array();
for ($i=0; $i<$trigger; $i++) $users[] = $redis->lpop("demandshaper:trigger");
// Get list of users to process
if (!$trigger) {
$result = $mysqli->query("SELECT `userid` FROM demandshaper");
while ($row = $result->fetch_object()) {
if ($row->userid) $users[] = $row->userid;
}
}
foreach($users as $userid)
{
$log->info("processing:$userid");
// Get time of start of day
$timezone = $user->get_timezone($userid);
$date = new DateTime();
$date->setTimezone(new DateTimeZone($timezone));
$date->setTimestamp($now);
$date->modify("midnight");
$daystart = $date->getTimestamp();
$second_in_day = $now - $daystart;
// Schedule definition
$schedules = $demandshaper->get($userid);
foreach ($schedules as $sid=>$schedule)
{
$device = $schedule->settings->device;
$device_type = $schedule->settings->device_type;
if (isset($settings['mqtt']['multiuser']) && $settings['mqtt']['multiuser']) {
$device_class[$device_type]->set_basetopic($settings['mqtt']['basetopic']."/".$userid);
}
$log->info(date("Y-m-d H:i:s")." Schedule:$device ".$schedule->settings->ctrlmode);
$log->info(" end timestamp: ".$schedule->settings->end_timestamp." ".date("Y-m-d H:i:s",$schedule->settings->end_timestamp));
// -----------------------------------------------------------------------
// Work out if schedule is running, status and decrease timeleft
// -----------------------------------------------------------------------
$status = 0;
$active_period = 0;
if ($schedule->runtime->timeleft>0 || $schedule->settings->ctrlmode=="timer") {
foreach ($schedule->runtime->periods as $pid=>$period) {
$start = $period->start[0];
$end = $period->end[0];
if ($now>=$start && $now<$end) {
$status = 1;
$active_period = $pid;
}
}
}
if ($schedule->settings->ctrlmode=="on") $status = 1;
if ($schedule->settings->ctrlmode=="off") $status = 0;
if ($status) {
$log->info(" status: ON");
$schedule->runtime->started = true;
$time_elapsed = $now - $lasttime;
$log->info(" time elapsed: $time_elapsed");
$schedule->runtime->timeleft -= $time_elapsed; // $update_interval;
$log->info(" timeleft: ".$schedule->runtime->timeleft."s");
if ($schedule->runtime->timeleft<0) $schedule->runtime->timeleft = 0;
} else {
$log->info(" status: OFF");
$log->info(" timeleft: ".$schedule->runtime->timeleft."s");
}
// -----------------------------------------------------------------------
// Publish control commands
// -----------------------------------------------------------------------
if ($connected) {
// Timezone correction e.g conversion to UTC for applicable devices
$timeOffset = $device_class[$device_type]->get_time_offset($timezone);
// ----------------------------------------------------------------------------
// Set Timer
// ----------------------------------------------------------------------------
$s1 = 0.0; $e1 = 0.0; $s2 = 0.0; $e2 = 0.0;
// Smart or regular timer
if ($schedule->settings->ctrlmode=="smart" || $schedule->settings->ctrlmode=="timer") {
if (count($schedule->runtime->periods)) {
$s1 = time_offset($schedule->runtime->periods[$active_period]->start[1],-$timeOffset);
$e1 = time_offset($schedule->runtime->periods[$active_period]->end[1],-$timeOffset);
$device_class[$device_type]->timer($device,$s1,$e1,$s2,$e2);
}
}
else if ($schedule->settings->ctrlmode=="on") $device_class[$device_type]->on($device);
else if ($schedule->settings->ctrlmode=="off") $device_class[$device_type]->off($device);
if (isset($schedule->settings->flowT)) {
$device_class[$device_type]->set_flowT($device,$schedule->settings->flowT);
}
if (isset($schedule->settings->divert_mode)) {
$device_class[$device_type]->set_divert_mode($device,$schedule->settings->divert_mode);
}
}
// -----------------------------------------------------------------------
// Recalculate schedule
// -----------------------------------------------------------------------
// If we are beyond the end_timestamp, set the next end timestamp +1 day, reset the runtime and remove the started flag
if ($now>$schedule->settings->end_timestamp) {
$date->setTimestamp($schedule->settings->end_timestamp);
$date->modify("+1 day");
$schedule->settings->end_timestamp = $date->getTimestamp();
$schedule->runtime->timeleft = $schedule->settings->period * 3600;
unset($schedule->runtime->started);
schedule_log("$device schedule complete");
if ($schedule->settings->ctrlmode=="smart") {
if (in_array($schedule->settings->on_completion,array("on","off","smart"))) {
$schedule->settings->ctrlmode = $schedule->settings->on_completion;
schedule_log("$device next ctrlmode: ".$schedule->settings->on_completion);
}
}
}
if ($schedule->settings->ctrlmode=="smart") {
// If the schedule has not yet started it is ok to recalculate the schedule periods to find a more optimum time
if (!isset($schedule->runtime->started) || $schedule->settings->interruptible) {
// Automatic update of time left for schedule e.g take into account updated battery SOC of electric car, home battery, device
$schedule = $device_class[$device_type]->auto_update_timeleft($schedule);
// 1. Compile combined forecast
$combined = $demandshaper->get_combined_forecast($schedule->settings->forecast_config,$timezone);
// 2. Calculate forecast min/max
$combined = forecast_calc_min_max($combined);
// 3. Calculate schedule
if ($schedule->settings->interruptible) {
$schedule->runtime->periods = schedule_interruptible($combined,$schedule->runtime->timeleft,$schedule->settings->end_timestamp,$timezone);
} else {
$schedule->runtime->periods = schedule_block($combined,$schedule->runtime->timeleft,$schedule->settings->end_timestamp,$timezone);
}
$schedule = json_decode(json_encode($schedule));
$log->info(" reschedule ".json_encode($schedule->runtime->periods));
}
}
else if ($schedule->settings->ctrlmode=="timer") {
// 1. Compile combined forecast, not really needed here but we need forecast start and end times
$combined = $demandshaper->get_combined_forecast($schedule->settings->forecast_config,$timezone);
// 2. Calculate periods from timers
$schedule->runtime->periods = schedule_timer($combined->start,$combined->end,$schedule->settings->timer,$timezone);
// 3. Log output for debug
$log->info(" timer schedule ".json_encode($schedule->runtime->periods));
}
$schedules->$sid = $schedule;
} // foreach schedules
$demandshaper->set($userid,$schedules);
} // user list
sleep(1.0);
$lasttime = $now;
} // 10s update
// -----------------------------------------------------------------------
// Send a request for device state every 5 mins
// -----------------------------------------------------------------------
if ($connected && (time()-$last_state_check)>300) {
$last_state_check = time();
$result = $mysqli->query("SELECT `userid` FROM demandshaper");
while ($row = $result->fetch_object()) {
$userid = $row->userid;
$schedules = $demandshaper->get($userid);
foreach ($schedules as $schedule) {
if ($schedule->settings->device) {
$device_type = $schedule->settings->device_type;
if (isset($settings['mqtt']['multiuser']) && $settings['mqtt']['multiuser']) {
$device_class[$device_type]->set_basetopic($settings['mqtt']['basetopic']."/".$userid);
}
$device_class[$device_type]->send_state_request($schedule->settings->device);
}
}
}
}
// MQTT Connect or Reconnect
if (!$connected && (time()-$last_retry)>5.0) {
$last_retry = time();
try {
$mqtt_client->setCredentials($settings['mqtt']['user'],$settings['mqtt']['password']);
$mqtt_client->connect($settings['mqtt']['host'], $settings['mqtt']['port'], 5);
} catch (Exception $e) { }
}
try { $mqtt_client->loop(); } catch (Exception $e) { }
// Dont loop to fast
usleep(100000);
}
function connect($r, $message) {
global $connected, $mqtt_client, $settings;
$connected = true;
$mqtt_client->subscribe($settings['mqtt']['basetopic']."/#",2);
}
function disconnect() {
global $connected; $connected = false;
}
// -------------------------------------------------------------------------
// Update demand shaper state with state from device
// -------------------------------------------------------------------------
function message($message)
{
global $demandshaper, $schedules, $log, $user, $settings, $default_userid, $device_class;
$basetopic = $settings['mqtt']['basetopic'];
$topic_parts = explode("/",$message->topic);
$userid = false;
$device = false;
if (isset($settings['mqtt']['multiuser']) && $settings['mqtt']['multiuser']) {
if (isset($topic_parts[1]) && isset($topic_parts[2])) {
$userid = $topic_parts[1];
$device = $topic_parts[2];
$basetopic .= "/".$userid;
}
} else {
if (isset($topic_parts[1])) {
$userid = $default_userid;
$device = $topic_parts[1];
}
}
if ($userid && $device && isset($schedules->$device)) {
$device_type = $schedules->$device->settings->device_type;
$result = $device_class[$device_type]->handle_state_response($schedules->$device,$message,$user->get_timezone($userid));
if ($result) {
$schedules->$device = $result;
$demandshaper->set($userid,$schedules);
}
}
}