forked from nebulous/infinitude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinitude
executable file
·447 lines (390 loc) · 13.4 KB
/
infinitude
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.10';
use FindBin;
use lib "$FindBin::Bin/lib";
use Mojolicious::Lite;
use Mojo::JSON qw/encode_json decode_json/;
use Carp qw/shortmess/;
use CHI;
use DateTime;
use Path::Tiny;
use Time::HiRes qw/time/;
use Try::Tiny;
use XML::Simple::Minded;
my $store = CHI->new(
driver => 'File',
root_dir => 'state',
depth => 0,
max_key_length => 256,
namespace => ''
);
$store->set(started=>time);
our $config;
our $config_file = path("infinitude.json");
if (my $config_json = $store->get('infinitude.json')) { #migrate older format config
$config_file->spew($config_json);
$store->remove('infinitude.json');
} else {
# Default configuration if no infinitude.json file is found
# customize config by editing infinitude.json
try { $config = decode_json($config_file->slurp) };
$config //= {
serial_tty => '/dev/ttyUSB0', #RS485 serial tty interface
serial_socket => '127.0.0.1:23', #RS485 socket relay
app_secret => 'Pogotudinal', #Mojolicious cookie signature
pass_reqs => 60*17 #Pass requests to Carrier webservice every 17 minutes (set to 0 to disable)
};
$config_file->spew(encode_json($config));
}
$config->{serial_tty} = $ENV{SERIAL_TTY} || $config->{serial_tty};
$config->{serial_socket} = $ENV{SERIAL_SOCKET} || $config->{serial_socket};
$config->{app_secret} = $ENV{APP_SECRET} || $config->{app_secret};
$config->{pass_reqs} = $ENV{PASS_REQS} || $config->{pass_reqs};
if (defined($config->{serial_tty}) or defined($config->{serial_socket})) {
require CarBus;
if (-e $config->{serial_tty}) {
require IO::Termios;
warn "Using $config->{serial_tty} serial interface\n";
} else {
warn "Can't find serial device: $config->{serial_tty}. Serial monitoring disabled.\n" if $config->{serial_tty};
delete $config->{serial_tty};
}
if ($config->{serial_socket}) {
require IO::Socket::IP;
warn "Using $config->{serial_socket} for serial interface\n";
}
}
app->secrets([$config->{app_secret}]);
push (@{app->static->paths}, ('development' eq ($ENV{MOJO_MODE}//'')) ? 'public/app' : 'public/dist');
hook before_dispatch => sub {
my $c = shift;
my $url = $c->req->url;
if ($url->to_abs->host =~ /(bryant|carrier|ioncomfort)/i) { # request from stat
my $nk = $url->path->to_string;
$nk =~ s/\//-/g;
$nk =~ s/^-//;
$c->stash->{action_key} = $nk;
#say "action_key: $nk";
if ($config->{pass_reqs} and !$store->get('changes') and ($store->get('carrier_changes') or !$store->get($nk))) {
$store->set($nk, time, $config->{pass_reqs});
my $o_req = $c->req->clone;
my $o_url = Mojo::URL->new($o_req->url->to_abs->to_string);
$o_url->scheme('https');
$o_req->url($o_url);
$c->app->log->info("No cache for $nk. Make Carrier request");
$store->set("req-$nk.txt", $o_req->to_string);
#say "REQUEST:\n".$o_req->to_string."\n------------";
my $ua = Mojo::UserAgent->new;
my $tx = $ua->start(Mojo::Transaction::HTTP->new(req=>$o_req));
$store->set("res-$nk.txt", $tx->res->to_string);
#say "RESPONSE:\n".$store->get("res-$nk.txt");
$c->stash->{pass_res} = $tx->res->body;
$c->stash->{format} = 'xml' if ($tx->res->headers->content_type//'') =~ /xml/;
$store->set("$nk.xml", $tx->res->body)
if ($tx->res->headers->content_type//'' =~ /xml/);
} else {
$c->app->log->info("$nk cached or passthru disabled");
}
#Mangle Internet-bound request to self
my $appurl = $c->url_for('/');
$url->base($appurl->base);
$c->app->log->info($url);
#Stuff data into store
if (my $data = $c->req->param('data')) {
my $xml;
try {
$xml = XML::Simple::Minded->new($data);
my ($store_key) = $url->path =~ /.*\/(.*)$/;
$c->app->log->info("Saving $store_key");
$store->set("$store_key.raw", $data);
$store->set("$store_key.xml" , $xml.'');
$store->set("$store_key.json", $xml->_as_json);
$c->stash(store_key=>$store_key);
} catch {
$c->stash('error','true');
$c->app->log->error($url, shortmess(), "Caught error: $_".$@);
$store->set("error-".time() => 'url:'.$url."\nerror: $_\ndata:".$data);
};
}
}
};
sub qtr_hr {
my ($hour,$minute) = @_;
if ($hour =~ /\d:\d/) {
($hour,$minute) = $hour =~ /(\d+)\:(\d+)/;
}
$hour||=(localtime)[2];
$minute||=(localtime)[1];
$minute = 15*int(0.5+($minute/15));
if ($minute==60) {
$hour+=1;
$minute=0;
}
return sprintf("%02d:%02d",$hour,$minute);
}
# Quick and dirty automation support. Ideally this will turn REST'y
# but we want to ensure automation devices eg VeraLite will work first
any '/api/:zone_id/hold' => sub {
my $c = shift;
my $xml = XML::Simple::Minded->new($store->get('systems.xml'));
my $zone;
my $idx = $c->stash('zone_id')||1;
if ($idx eq 'wholeHouse') {
$zone = $xml->system->config->wholeHouse;
} else {
$idx--;
$zone = $xml->system->config->zones->zone->[$idx];
}
my $setting = {};
my $hold = $c->req->param('hold') || '';
$setting->{hold} = $hold eq 'off' ? 'off' : 'on';
$zone->hold([$setting->{hold}]);
my $activities = { home=>'home', away=>'away', sleep=>'sleep', wake=>'wake', manual=>'manual' };
$setting->{activity} = $activities->{lc($c->req->param('activity'))} || 'home';
$zone->holdActivity([$setting->{activity}]);
# default to about an hour from now
my $until = $c->req->param('until') || ((localtime)[2] + 1);
$setting->{until} = $until eq 'forever' ? {} : &qtr_hr($until);
$zone->otmr([$setting->{until}]);
$store->set('systems.xml', $xml.'');
$store->set('systems.json', $xml->_as_json());
$store->set(changes => 'true');
$c->render(text=>$xml, format=>'xml');
#$c->render(json=>$setting);
};
sub api_stat {
my $c = shift;
my $status = decode_json($store->get('status.json')||'{}');
my $zone_id = $c->stash('zone_id')||'';
if ($zone_id =~ /^\d+$/) {
my $idx = ($zone_id||1) - 1;
$status = $status->{status}[0]{zones}[0]{zone}[$idx];
} else {
$status = $status->{status}[0];
}
my $prop = $c->stash('prop') || $zone_id;
if ($prop =~ /^[A-Z]/i) {
my $val = $status->{$prop};
$val = $val->[0] if ref($val) eq 'ARRAY' && scalar(@$val)==1;
$val *=1 if defined($val) and $val =~ /^[\.\d]+$/;
$status = { $prop=>$val };
}
$c->render(json=>$status);
}
get '/api/status/:zone_id/:prop' => \&api_stat;
get '/api/status/:zone_id/' => \&api_stat;
get '/api/status/' => \&api_stat;
any '/api/:zone_id/activity/:activity_id' => sub {
my $c = shift;
my $xml = XML::Simple::Minded->new($store->get('systems.xml'));
my $idx = ($c->stash('zone_id')||1) - 1;
my $zone = $xml->system->config->zones->zone->[$idx];
my $setting = {};
foreach my $activity (@{$zone->activities->activity}) {
if ($activity->id eq $c->stash('activity_id')) {
foreach my $set (qw/clsp htsp fan/) {
$activity->$set([$c->req->param($set)]) if $c->req->param($set);
}
$setting = decode_json($activity->_as_json);
$store->set('systems.xml', $xml.'');
$store->set('systems.json', $xml->_as_json());
$store->set(changes => 'true');
last;
}
}
$c->render(json=>$setting);
};
get '/api/state_keys' => sub {
my $c = shift;
$c->render(json=>[sort $store->get_keys] );
};
# access simple system config variables by path
any '/api/*variable_path' => sub {
my $c = shift;
my $xml = XML::Simple::Minded->new($store->get('systems.xml'));
my $search = $xml->system();
foreach my $node (split('/',$c->stash('variable_path'))) {
$search = ($node =~ /^\d+$/) ? $search->[$node]
: $search->$node;
}
my $setting = { status=>'success' };
try {
if (ref($search)) {
# path returns a structure. Can modify.
my %sets = %{$c->req->params->to_hash()};
foreach my $key (keys %sets) {
$search->$key([$sets{$key}]);
}
$setting->{data} = { %{$search->TO_JSON()} };
$store->set('systems.xml', $xml.'');
$store->set('systems.json', $xml->_as_json());
if (('POST' eq $c->req->method) or ('PUT' eq $c->req->method)) { # Only set changes if it's a POST
$store->set(changes => 'true');
}
} else {
#path is a scalar. return only.
$setting->{data} = $search;
}
} catch {
$setting->{status} = 'fail';
};
$c->render(json=>$setting);
};
get '/' => sub {
my $c = shift;
$c->reply->static('index.html');
};
get '/Alive' => sub {
my $c = shift;
$c->render(text=>'alive', format=>'txt');
};
get '/time' => sub {
my $c = shift;
my $date = DateTime->now();
my $xml = XML::Simple::Minded->new({
"time" => {
version => '1.9',
utc => [$date->strftime("%FT%TZ")]
}
});
$c->render(text=>$xml, format=>'xml');
};
get '/releaseNotes/#id' => sub {
my $c = shift;
my $text = $store->get('releaseNotes-'.$c->stash('id'));
$c->render(text=>"WARNING: installing new firmware may cause Infinitude to stop working!\n\n$text", format=>'txt');
};
post '/systems/:id' => sub {
my $c = shift;
if ($c->stash('id') eq 'infinitude') { # Data is being saved from web client
my $xml = XML::Simple::Minded->new($c->req->json);
$store->set('systems.xml' => $xml.'');
$store->set('systems.json' => $xml->_as_json);
$store->set(changes => 'true');
} else { # Data from thermostat.
if (!$c->stash('error')) {
if (my $key = $c->stash('store_key')) {
$store->set( 'systems.xml' => $store->get("$key.xml") );
$store->set( 'systems.json' => $store->get("$key.json") );
}
}
}
$c->render(text=>'',format=>'txt');
};
# stat fetches config if server has changes
get '/systems/:id/config' => sub {
my $c = shift;
if ($store->get('carrier_changes') and $c->stash->{pass_res}) {
$store->set(carrier_changes => '' );
$store->set(changes => time+60); # Force a followup stat->infinitude push/pull cycle after 1m
$c->render(text=>$c->stash->{pass_res}, format=>'xml');
} else {
my $xml = XML::Simple::Minded->new($store->get('systems.xml'));
my $config = XML::Simple::Minded->new({config=>$xml->system->config()});
$c->render(text=>$config, format=>'xml');
}
};
get '/systems/:id' => sub {
my $c = shift;
my $xml = $store->get('systems.xml');
$c->app->log->info("-------- Getting systems.xml (".$c->stash('id').") from infinitude.");
$xml
? $c->render(text=>$xml.'', format=>'xml')
: $c->render(text=>'', format=>'txt');
};
post '/systems/:system_id/status' => sub {
my $c = shift;
my $changes = $store->get('changes') || '';
if ($changes =~ /\d+/) {
$changes = (time>$changes) ? 'true' : ''
}
$changes = 'true' unless $store->get('systems.xml'); #Force a change cycle if we have no stat config
my $xml = XML::Simple::Minded->new({
status => {
version => '1.37',
pingRate => [$changes eq 'true' ? 20 : 12],
serverHasChanges => [$changes || 'false'],
configHasChanges => [$changes || 'false'],
}
});
# if infinitude has no changes, and this is a carrier passthru request, return carrier's response
if (!$changes and $c->stash->{pass_res}) {
$c->app->log->info("********** Check Carrier/Bryant change flags ****************");
$xml = XML::Simple::Minded->new($c->stash->{pass_res});
$xml->status->pingRate([12]);
$changes = $xml->status->serverHasChanges eq 'true' ? 1 : 0;
$store->set(carrier_changes => time, 120) if $changes; # open a window to Carrier passthru, max of 2 minutes
}
if ($changes) {
$c->app->log->info("********** There are changes. ****************");
$store->set(changes=>'');
}
$c->render(text=>$xml, format=>'xml');
};
any '/systems/:system_id/:part' => sub {
my $c = shift;
$c->app->log->info('Unimplemented request: ' . $c->stash->{'part'});
$c->render(text=>'', format=>'txt');
};
websocket '/serial' => sub {
my $c = shift;
my $handle;
if ($config->{serial_socket} or ($config->{serial_tty} and -e $config->{serial_tty})) {
if ($config->{serial_socket}) {
$c->app->log->info('Attempting to access '.$config->{serial_socket});
my ($host,$port) = split(':',$config->{serial_socket});
$handle = IO::Socket::IP->new(
PeerHost => $host//'localhost',
PeerPort => $port//'telnet',
);
} else {
$c->app->log->info('Attempting to access '.$config->{serial_tty});
$handle = IO::Termios->open($config->{serial_tty},"38400,8,n,1");
}
}
unless ($handle) {
$c->app->log->info("Websocket opened, but no streaming source found");
return;
}
my $carbus = CarBus->new(async=>1);
Mojo::IOLoop->recurring(0.0625 => sub {
my $buffer;
$handle->sysread($buffer, 128);
$carbus->push_stream($buffer);
my $frame = $carbus->get_frame;
unless ($frame->{error}) {
my $fstruc = $frame->frame_hash;
$fstruc->{timestamp} = time;
$c->send({json=>$fstruc});
#print $frame->frame_hex."\n";
}
});
$c->on('finish' => sub {
my ($c, $code) = @_;
$c->app->log->info("Websocket Closed: $code");
$handle->close;
});
};
any '/*catchall' => sub {
my $c = shift;
my $store_key = $c->stash('catchall');
$store_key =~ s/\//-/g;
my $text = $c->stash->{pass_res}
|| $store->get($store_key)
|| $store->get("$store_key.xml")
|| $store->get("$store_key.json")
// '';
my $format = $c->stash('format') ? $c->stash('format')
: $store_key =~ /\.json$/ ? 'json'
: $text =~ /^\s*{/ ? 'json'
: $store_key =~ /\.xml$/ ? 'xml'
: $text =~ /^\s*</ ? 'xml'
: 'txt';
$text = '{}' if ($format eq 'json' and !$text);
$text = "Infinitude doesn't know how to ".$c->req->method." $store_key" unless ($text or $store->is_valid($store_key));
$c->render(text=>$text, format=>$format);
};
app->log->level($ENV{LOGLEVEL}//app->log->level);
app->start;