-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenhab_monitor.php
executable file
·204 lines (179 loc) · 7.21 KB
/
openhab_monitor.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
#!/usr/bin/php
<?php
$INI_LOCATION='/etc/openhab_monitor.ini';
function parse_ini_advanced($array) {
$returnArray = array();
if (is_array($array)) {
foreach ($array as $key => $value) {
$e = explode(':', $key);
if (!empty($e[1])) {
$x = array();
foreach ($e as $tk => $tv) {
$x[$tk] = trim($tv);
}
$x = array_reverse($x, true);
foreach ($x as $k => $v) {
$c = $x[0];
if (empty($returnArray[$c])) {
$returnArray[$c] = array();
}
if (isset($returnArray[$x[1]])) {
$returnArray[$c] = array_merge($returnArray[$c], $returnArray[$x[1]]);
}
if ($k === 0) {
$returnArray[$c] = array_merge($returnArray[$c], $array[$key]);
}
}
} else {
$returnArray[$key] = $array[$key];
}
}
}
return $returnArray;
}
function recursive_parse($array)
{
$returnArray = array();
if (is_array($array)) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = recursive_parse($value);
}
$x = explode('.', $key);
if (!empty($x[1])) {
$x = array_reverse($x, true);
if (isset($returnArray[$key])) {
unset($returnArray[$key]);
}
if (!isset($returnArray[$x[0]])) {
$returnArray[$x[0]] = array();
}
$first = true;
foreach ($x as $k => $v) {
if ($first === true) {
$b = $array[$key];
$first = false;
}
$b = array($v => $b);
}
$returnArray[$x[0]] = array_merge_recursive($returnArray[$x[0]], $b[$x[0]]);
} else {
$returnArray[$key] = $array[$key];
}
}
}
return $returnArray;
}
$array = parse_ini_file($INI_LOCATION, true);
$INI_ARRAY = recursive_parse(parse_ini_advanced($array));
$DEBUG = $INI_ARRAY['defaults']['DEBUG'];
$OPENHAB_ROOT = $INI_ARRAY['defaults']['OPENHAB_ROOT'];
$APRON_DATABASE_LOCATION = $INI_ARRAY['defaults']['APRON_DATABASE_LOCATION'];
$SLEEP_INTERVAL = $INI_ARRAY['defaults']['SLEEP_INTERVAL'];
$FORCE_INTERVAL = $INI_ARRAY['defaults']['FORCE_INTERVAL'];
$ZIGBEE_DEVICES = array();
$ZWAVE_DEVICES = array();
$zigbee_attr_ids = array();
$zwave_attr_ids = array();
foreach($INI_ARRAY['zigbee'] as $device_id => $zigbee_device) {
$ZIGBEE_DEVICES[$device_id] = array();
foreach ($zigbee_device as $attribute_id => $attributes) {
array_push($zigbee_attr_ids, $attribute_id);
$ZIGBEE_DEVICES[$device_id][$attribute_id] = array();
$ZIGBEE_DEVICES[$device_id][$attribute_id]['name'] = $attributes['name'];
$ZIGBEE_DEVICES[$device_id][$attribute_id]['convert'] = isset($attributes['convert']) ? $attributes['convert'] : '';
$ZIGBEE_DEVICES[$device_id][$attribute_id]['last_status'] = '';
}
}
foreach($INI_ARRAY['zwave'] as $device_id => $zwave_device) {
$ZWAVE_DEVICES[$device_id] = array();
foreach ($zwave_device as $attribute_id => $attributes) {
array_push($zwave_attr_ids, $attribute_id);
$ZWAVE_DEVICES[$device_id][$attribute_id] = array();
$ZWAVE_DEVICES[$device_id][$attribute_id]['name'] = $attributes['name'];
$ZWAVE_DEVICES[$device_id][$attribute_id]['convert'] = isset($attributes['convert']) ? $attributes['convert'] : '';
$ZWAVE_DEVICES[$device_id][$attribute_id]['last_status'] = '';
}
}
$ZIGBEE_ATTRS = "(" . implode(',', array_unique($zigbee_attr_ids)) . ")";
$ZWAVE_ATTRS = "(" . implode(',', array_unique($zwave_attr_ids )) . ")";
$DEVICES = array(
'zigbee' => $ZIGBEE_DEVICES,
'zwave' => $ZWAVE_DEVICES
);
function query_db($device_type) {
global $APRON_DATABASE_LOCATION, $DEBUG, $ZIGBEE_ATTRS, $ZWAVE_ATTRS;
if($device_type == 'zigbee'){
$cmd = 'sqlite3 ' . $APRON_DATABASE_LOCATION . ' "SELECT masterId, attributeId, value_get FROM zigbeeDeviceState LEFT JOIN zigbeeDevice ON zigbeeDeviceState.globalID = zigbeeDevice.globalID WHERE attributeId IN ' . $ZIGBEE_ATTRS . ';"';
} elseif ($device_type == 'zwave') {
$cmd = 'sqlite3 ' . $APRON_DATABASE_LOCATION . ' "SELECT masterId, attributeId, value_get FROM zwaveDeviceState LEFT JOIN zwaveDevice ON zwaveDeviceState.nodeId = zwaveDevice.nodeId WHERE attributeId IN ' . $ZWAVE_ATTRS . ';"';
};
unset($out);
if($DEBUG) echo "$cmd\n";
exec($cmd,$out,$retval);
if($retval == 0) {
return $out;
} else {
echo "SQLITE FAIL!\n";
echo $out;
}
}
function curl_openhab($path, $state) {
global $OPENHAB_ROOT, $DEBUG;
$cmd = 'curl --silent --show-error --header "Content-Type: text/plain" --request PUT --data "' . $state . '" ' . $OPENHAB_ROOT . $path . "/state \n";
if($DEBUG) echo $cmd;
unset($out);
exec($cmd,$out,$retval);
}
function query_and_check($device_type, $force = false) {
global $DEVICES, $DEBUG;
$out = query_db($device_type);
foreach($out as $line) {
list($apron_id, $attribute_id, $val) = explode("|", $line);
if(array_key_exists($apron_id, $DEVICES[$device_type])) {
if(array_key_exists($attribute_id, $DEVICES[$device_type][$apron_id])) {
if($DEVICES[$device_type][$apron_id][$attribute_id]['last_status'] == $val && !$force) {
# Nothing changed and we're not forcing, move along..,
} else {
$DEVICES[$device_type][$apron_id][$attribute_id]['last_status'] = $val;
# Convert from 0-255 to 0-100...
if($DEVICES[$device_type][$apron_id][$attribute_id]['convert'] == 'number'){
$val = intval($val/255 * 100);
} elseif($DEVICES[$device_type][$apron_id][$attribute_id]['convert'] == 'switch'){
if($val == 'TRUE') $val = 'ON';
if($val == 'FALSE') $val = 'OFF';
} elseif($DEVICES[$device_type][$apron_id][$attribute_id]['convert'] == 'contact'){
if($val == 'TRUE') $val = 'CLOSED';
if($val == 'FALSE') $val = 'OPEN';
}
if($val != '') {
curl_openhab($DEVICES[$device_type][$apron_id][$attribute_id]['name'], $val);
}
};
} else {
echo "--- skip attribute $attribute_id!\n";
};
} else {
if($DEBUG) echo "Skipping $apron_id because it's not in the $device_type list.\n";
};
};
};
if($argc > 1 && $argv[1] == 'now') {
if(count($DEVICES['zigbee']) > 0) query_and_check('zigbee', TRUE);
if(count($DEVICES['zwave']) > 0) query_and_check('zwave', TRUE);
} else {
$i = 0;
while(true) {
if ($i > $FORCE_INTERVAL) {
if(count($DEVICES['zigbee']) > 0) query_and_check('zigbee', TRUE);
if(count($DEVICES['zwave']) > 0) query_and_check('zwave', TRUE);
$i = 0;
} else {
if(count($DEVICES['zigbee']) > 0) query_and_check('zigbee');
if(count($DEVICES['zwave']) > 0) query_and_check('zwave');
$i = $i + 1;
}
sleep($SLEEP_INTERVAL);
}
}
?>