-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhal_helpers.php
More file actions
156 lines (134 loc) · 4.49 KB
/
hal_helpers.php
File metadata and controls
156 lines (134 loc) · 4.49 KB
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
<?php
function timeline_graph_data($from, $to){
$mysqli = get_mysqli();
if($stmt = $mysqli->prepare("SELECT sensor, UNIX_TIMESTAMP(start_at), UNIX_TIMESTAMP(progress_at), UNIX_TIMESTAMP(end_at) FROM haldor WHERE start_at BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?) AND (end_at IS NULL OR end_at BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?))")){
$stmt->bind_param('iiii', $from, $to, $from, $to);
$stmt->execute();
if($res = $stmt->get_result()){
$data = $res->fetch_all();
return $data;
}
}
return null;
}
function timeline_graph_json($from, $to){
$data = timeline_graph_data($from, $to);
if($data){
$response = [];
foreach($data as $value){
$name = str_replace('_', ' ', $value[0]);
$end_at = $value[1];
if($value[2] && $value[2] > $end_at){
$end_at = $value[2];
}
if($value[3] && $value[3] > $end_at){
$end_at = $value[3];
}
array_push($response, "['${name}', '', new Date(${value[1]}000), new Date(${end_at}000)]");
}
$response = implode(",\n", $response);
return "[${response}]";
}
# [Name, Subname, from, to]
return "[]";
}
function get_latest($sensor){
$mysqli = get_mysqli();
if($stmt = $mysqli->prepare("SELECT UNIX_TIMESTAMP(progress_at), UNIX_TIMESTAMP(end_at), UNIX_TIMESTAMP(mark_at), last_value FROM haldor WHERE sensor = ? ORDER BY progress_at DESC LIMIT 1")){
$stmt->bind_param('s', str_replace(' ', '_', $sensor));
$stmt->execute();
if($res = $stmt->get_result()){
$data = $res->fetch_all()[0];
return $data;
}
}
return null;
}
function latest_changes(){
$change_items = array(
'Open Switch' => [],
'Front Door' => [],
'Main Door' => [],
'Office Motion' => [],
'Shop Motion' => [],
'Temperature' => []
);
$now = time();
$last_update_time = 0;
// TODO: May be better to manually run each one to avoid extra strpos calls
foreach($change_items as $sensor => &$value){
$data = get_latest($sensor);
if($data == null){
array_push($value, 'No Data');
array_push($value, null);
array_push($value, false);
continue;
}
# Doors are either open or closed. Easy
if(strpos($sensor, 'Door') !== false){
if($data[1] == null && $data[2] == null){
array_push($value, 'Open');
array_push($value, $data[0]);
array_push($value, true);
} else {
array_push($value, 'Closed');
array_push($value, $data[1] | $data[2]);
array_push($value, false);
}
}
if(strpos($sensor, 'Motion') !== false){
if($data[1] == null && $data[2] == null){
array_push($value, 'Moving');
array_push($value, $data[0]);
array_push($value, true);
} elseif($now - 20*60 < $data[0] && ($data[1] or $data[2])){
array_push($value, 'Moving');
array_push($value, $data[0]);
array_push($value, true);
} else {
array_push($value, 'No Movement since');
array_push($value, $data[0]);
array_push($value, false);
}
}
if(strpos($sensor, 'Switch') !== false){
if($data[1] == null && $data[2] == null){
array_push($value, 'Flipped ON');
array_push($value, $data[0]);
array_push($value, true);
} else {
array_push($value, 'Flipped OFF');
array_push($value, $data[1] | $data[2]);
array_push($value, false);
}
}
if($sensor == 'Temperature'){
array_push($value, '' . sprintf("%.2f °C/ %.2f °F", (($data[3] | 0) / 1000), ((($data[3] | 0) / 1000)*1.8 + 32)));
array_push($value, $data[2] | $data[1] | $data[0]);
}
if($value[1] and $value[1] > $last_update_time){
$last_update_time = $value[1];
}
}
$change_items['Page Loaded'] = ['at', $now];
$change_items['LastUpdate'] = ['at', $last_update_time];
return $change_items;
}
function is_maglabs_open($latest){
# Space is open when:
# a) a door is open (main or front)
# b) one of the following is true: open switch ON, movement in last 20 minutes
$movement = $latest['Open Switch'][2] | $latest['Office Motion'][2] | $latest['Shop Motion'][2];
$doors = $latest['Front Door'][2] | $latest['Main Door'][2];
return $movement && $doors;
}
function is_tech_bad(&$latest){
$last_update_time = $latest['LastUpdate'][1];
unset($latest['LastUpdate']);
$now = time();
if($last_update_time <= $now-(60*20)){
return true; # No response in 20 minutes is bad
} else {
return false;
}
}