Skip to content

Commit 0f1304e

Browse files
committed
Updates
* API update (more details for getting objects by class) * Simple Devices -- ablity to disable linked actions * Code editor -- tips line with ablity to switch to fullscreen and save code
1 parent 4d285f1 commit 0f1304e

14 files changed

+660
-561
lines changed

api.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,24 @@
265265
$result['event_id']=registerEvent($event_name,$result['params']);
266266
$result['result'] = true;
267267
} elseif (strtolower($request[0]) == 'objects') {
268+
$class_name = '';
268269
if (isset($request[1])) {
269-
$objects=getObjectsByClass($request[1]);
270+
$class_name = $request[1];
271+
$objects=getObjectsByClass($class_name);
272+
$properties = getClassProperties($class_name);
273+
$total = count($objects);
274+
for($i=0;$i<$total;$i++) {
275+
$objects[$i]['object']=$objects[$i]['TITLE'];
276+
$objects[$i]['id']=$objects[$i]['ID'];
277+
unset($objects[$i]['TITLE']);
278+
unset($objects[$i]['ID']);
279+
foreach($properties as $property) {
280+
$property_title = $property['TITLE'];
281+
$objects[$i][$property_title]=getGlobal($objects[$i]['object'].'.'.$property_title);
282+
}
283+
}
270284
} else {
271-
$objects=SQLSelect("SELECT ID, TITLE FROM objects ORDER BY TITLE");
285+
$objects=SQLSelect("SELECT ID, TITLE, TITLE as OBJECT FROM objects ORDER BY TITLE");
272286
}
273287
$result['objects'] = $objects;
274288
} elseif (strtolower($request[0]) == 'data' && !isset($request[1]) && is_array($input['properties']) && $method=='POST') {
@@ -404,6 +418,8 @@
404418
}
405419

406420
if (!headers_sent()) {
421+
header("Access-Control-Allow-Origin: *");
422+
//header("Access-Control-Allow-Methods: \"GET, HEAD\"");
407423
header("Content-type:application/json");
408424
}
409425
echo json_encode($result);

lib/objects.class.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,14 @@ function addLinkedProperty($object, $property, $module)
338338
}
339339
}
340340

341-
/**
342-
* Summary of removeLinkedProperty
343-
* @param mixed $object Object
344-
* @param mixed $property Property
345-
* @param mixed $module Module
346-
* @return int
347-
*/
341+
function removeLinkedPropertyIfNotUsed($table_name, $object, $property, $module)
342+
{
343+
$tmp = SQLSelectOne("SELECT ID FROM " . DBSafe($table_name) . " WHERE LINKED_OBJECT='" . DBSafe($object) . "' AND LINKED_PROPERTY='" . DBSafe($property) . "'");
344+
if (!isset($tmp['ID'])) {
345+
removeLinkedProperty($object, $property, $module);
346+
}
347+
}
348+
348349
function removeLinkedProperty($object, $property, $module)
349350
{
350351
$sqlQuery = "SELECT *
@@ -353,27 +354,21 @@ function removeLinkedProperty($object, $property, $module)
353354

354355
$value = SQLSelectOne($sqlQuery);
355356

356-
if ($value['ID']) {
357+
if (isset($value['ID'])) {
357358
if (!$value['LINKED_MODULES']) {
358359
$tmp = array();
359360
} else {
360361
$tmp = explode(',', $value['LINKED_MODULES']);
361362
}
362-
363363
if (in_array($module, $tmp)) {
364364
$total = count($tmp);
365365
$res = array();
366-
367366
for ($i = 0; $i < $total; $i++) {
368367
if ($tmp[$i] != $module) {
369368
$res[] = $tmp[$i];
370369
}
371370
}
372-
373-
$tmp = $res;
374-
375-
$value['LINKED_MODULES'] = implode(',', $tmp);
376-
371+
$value['LINKED_MODULES'] = implode(',', $res);
377372
SQLUpdate('pvalues', $value);
378373
}
379374
} else {

lib/python.inc.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function python_run_code($code, $params = '', $object = '') {
2121
}
2222
$filePath = DOC_ROOT . '/cms/python/' . $fileName;
2323
$cmd = $python_path." ".$filePath.' \''.json_encode($params).'\' 2>&1';
24-
2524
if (file_exists($filePath)) {
2625
$currentMd5=md5(LoadFile($filePath));
2726
} else {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$this->callMethod('keepAlive');
4+
5+
$currentTemperature = $this->getProperty('value');
6+
7+
//if ($need_action) {
8+
include_once(dirname(__FILE__).'/devices.class.php');
9+
$dv=new devices();
10+
$dv->checkLinkedDevicesAction($this->object_title, $currentTemperature);
11+
$this->callMethod('logicAction');
12+
//}

modules/devices/devices.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,7 @@ function dbInstall($data = '')
15341534
devices: ROLES varchar(100) NOT NULL DEFAULT ''
15351535
15361536
devices_linked: ID int(10) unsigned NOT NULL auto_increment
1537+
devices_linked: IS_ACTIVE int(3) unsigned NOT NULL DEFAULT 1
15371538
devices_linked: DEVICE1_ID int(10) unsigned NOT NULL DEFAULT 0
15381539
devices_linked: DEVICE2_ID int(10) unsigned NOT NULL DEFAULT 0
15391540
devices_linked: LINK_TYPE varchar(100) NOT NULL DEFAULT ''

modules/devices/devices_links.inc.php

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,121 @@
11
<?php
22

3-
if ($this->edit_mode=='delete_link') {
3+
if ($this->edit_mode == 'delete_link') {
44
global $link_id;
5-
SQLExec("DELETE FROM security_rules WHERE OBJECT_TYPE='sdevice' AND OBJECT_ID=".(int)$link_id);
6-
SQLExec("DELETE FROM devices_linked WHERE ID=".(int)$link_id);
7-
$this->redirect("?id=".$rec['ID']."&tab=".$this->tab."&view_mode=".$this->view_mode);
5+
SQLExec("DELETE FROM security_rules WHERE OBJECT_TYPE='sdevice' AND OBJECT_ID=" . (int)$link_id);
6+
SQLExec("DELETE FROM devices_linked WHERE ID=" . (int)$link_id);
7+
$this->redirect("?id=" . $rec['ID'] . "&tab=" . $this->tab . "&view_mode=" . $this->view_mode);
88
}
99

10-
if ($this->edit_mode=='edit_link') {
10+
if ($this->edit_mode == 'edit_link') {
1111
global $link_name;
1212
global $link_id;
1313

14-
$link_details=$this->getLinkDetails($link_name);
14+
$link_details = $this->getLinkDetails($link_name);
1515

16-
$link_rec=SQLSelectOne("SELECT * FROM devices_linked WHERE ID=".(int)$link_id);
16+
$link_rec = SQLSelectOne("SELECT * FROM devices_linked WHERE ID=" . (int)$link_id);
1717
if (isset($link_rec['ID'])) {
18-
$out['DEVICE2_ID']=$link_rec['DEVICE2_ID'];
19-
$out['LINK_ID']=$link_rec['ID'];
20-
$out['COMMENT']=$link_rec['COMMENT'];
21-
$settings=unserialize($link_rec['LINK_SETTINGS']);
18+
$out['DEVICE2_ID'] = $link_rec['DEVICE2_ID'];
19+
$out['LINK_ID'] = $link_rec['ID'];
20+
$out['COMMENT'] = $link_rec['COMMENT'];
21+
$out['IS_ACTIVE'] = $link_rec['IS_ACTIVE'];
22+
$settings = unserialize($link_rec['LINK_SETTINGS']);
2223
if (is_array($link_details['PARAMS'])) {
23-
foreach($link_details['PARAMS'] as &$p) {
24-
if (isset($settings[$p['PARAM_NAME']])) {
25-
$p['VALUE']=$settings[$p['PARAM_NAME']];
26-
if ($p['PARAM_TYPE']=='duration') {
27-
$p['VALUE'] = gmdate("H:i:s", $p['VALUE']);
24+
foreach ($link_details['PARAMS'] as &$p) {
25+
if (isset($settings[$p['PARAM_NAME']])) {
26+
$p['VALUE'] = $settings[$p['PARAM_NAME']];
27+
if ($p['PARAM_TYPE'] == 'duration') {
28+
$p['VALUE'] = gmdate("H:i:s", $p['VALUE']);
29+
}
2830
}
2931
}
3032
}
31-
}
33+
} else {
34+
$out['IS_ACTIVE']=1;
3235
}
3336

34-
if ($this->mode=='update') {
35-
$ok=1;
36-
$link_rec['DEVICE1_ID']=$rec['ID'];
37-
$link_rec['LINK_TYPE']=$link_name;
37+
if ($this->mode == 'update') {
38+
$ok = 1;
39+
$link_rec['DEVICE1_ID'] = $rec['ID'];
40+
$link_rec['LINK_TYPE'] = $link_name;
3841
global $device2_id;
39-
$link_rec['DEVICE2_ID']=(int)$device2_id;
42+
$link_rec['DEVICE2_ID'] = (int)$device2_id;
4043
if (!$link_rec['DEVICE2_ID']) {
41-
$ok=0;
44+
$ok = 0;
4245
}
4346

44-
global $comment;
45-
$link_rec['COMMENT']=$comment;
4647

47-
$params=$link_details['PARAMS'];
48+
$link_rec['COMMENT'] = gr('comment');
49+
$link_rec['IS_ACTIVE'] = gr('is_active', 'int');
50+
51+
$params = $link_details['PARAMS'];
4852

49-
$config=array();
53+
$config = array();
5054
$total = count($params);
5155
for ($i = 0; $i < $total; $i++) {
5256
//global ${$params[$i]['PARAM_NAME'].'_value'};
5357
//$config[$params[$i]['PARAM_NAME']]=${$params[$i]['PARAM_NAME'].'_value'};
54-
$config[$params[$i]['PARAM_NAME']] = gr($params[$i]['PARAM_NAME'].'_value');
55-
if ($params[$i]['PARAM_TYPE']=='duration' && preg_match('/(\d+):(\d+):(\d+)/',$config[$params[$i]['PARAM_NAME']],$m)) {
56-
$config[$params[$i]['PARAM_NAME']] = $m[1]*60*60+$m[2]*60+$m[3];
58+
$config[$params[$i]['PARAM_NAME']] = gr($params[$i]['PARAM_NAME'] . '_value');
59+
if ($params[$i]['PARAM_TYPE'] == 'duration' && preg_match('/(\d+):(\d+):(\d+)/', $config[$params[$i]['PARAM_NAME']], $m)) {
60+
$config[$params[$i]['PARAM_NAME']] = $m[1] * 60 * 60 + $m[2] * 60 + $m[3];
5761
}
5862
}
59-
$link_rec['LINK_SETTINGS']=serialize($config);
63+
$link_rec['LINK_SETTINGS'] = serialize($config);
6064
if ($ok) {
6165
if ($link_rec['ID']) {
62-
SQLUpdate('devices_linked',$link_rec);
66+
SQLUpdate('devices_linked', $link_rec);
6367
} else {
64-
$link_rec['ID']=SQLInsert('devices_linked',$link_rec);
68+
$link_rec['ID'] = SQLInsert('devices_linked', $link_rec);
6569
}
6670
}
67-
$this->redirect("?id=".$rec['ID']."&tab=".$this->tab."&view_mode=".$this->view_mode);
71+
$this->redirect("?id=" . $rec['ID'] . "&tab=" . $this->tab . "&view_mode=" . $this->view_mode);
6872
}
6973

70-
foreach($link_details as $k=>$v) {
71-
$out['LINK_DETAILS_'.$k]=$v;
74+
foreach ($link_details as $k => $v) {
75+
$out['LINK_DETAILS_' . $k] = $v;
7276
}
73-
$target_classes=explode(',',$link_details['TARGET_CLASS']);
74-
$target_classes=array_map('trim',$target_classes);
75-
$second_devices=array();
77+
$target_classes = explode(',', $link_details['TARGET_CLASS']);
78+
$target_classes = array_map('trim', $target_classes);
79+
$second_devices = array();
7680

77-
$other_devices=SQLSelect("SELECT * FROM devices WHERE ID!=".(int)$rec['ID']." ORDER BY TITLE");
81+
$other_devices = SQLSelect("SELECT * FROM devices WHERE ID!=" . (int)$rec['ID'] . " ORDER BY TITLE");
7882
$total = count($other_devices);
7983
for ($i = 0; $i < $total; $i++) {
80-
$type_details=$this->getTypeDetails($other_devices[$i]['TYPE']);
81-
if (in_array($type_details['CLASS'],$target_classes) || in_array($type_details['PARENT_CLASS'],$target_classes)) {
82-
$second_devices[]=$other_devices[$i];
84+
$type_details = $this->getTypeDetails($other_devices[$i]['TYPE']);
85+
if (in_array($type_details['CLASS'], $target_classes) || in_array($type_details['PARENT_CLASS'], $target_classes)) {
86+
$second_devices[] = $other_devices[$i];
8387
}
8488
}
85-
$out['SECOND_DEVICES']=$second_devices;
89+
$out['SECOND_DEVICES'] = $second_devices;
8690

8791
//print_r($link_details);exit;
8892
}
8993

90-
$links=SQLSelect("SELECT devices_linked.*, devices.TITLE FROM devices_linked LEFT JOIN devices ON devices.ID=DEVICE2_ID WHERE (DEVICE1_ID=".(int)$rec['ID']." OR DEVICE2_ID=".(int)$rec['ID'].") ORDER BY ID");
94+
$links = SQLSelect("SELECT devices_linked.*, devices.TITLE FROM devices_linked LEFT JOIN devices ON devices.ID=DEVICE2_ID WHERE (DEVICE1_ID=" . (int)$rec['ID'] . " OR DEVICE2_ID=" . (int)$rec['ID'] . ") ORDER BY ID");
9195
if (isset($links[0]['ID'])) {
9296
$total = count($links);
9397
for ($i = 0; $i < $total; $i++) {
94-
$device1=SQLSelectOne("SELECT ID, TITLE FROM devices WHERE ID=".(int)$links[$i]['DEVICE1_ID']);
95-
$links[$i]['DEVICE1_TITLE']=$device1['TITLE'];
96-
if ($links[$i]['LINK_SETTINGS']!='') {
97-
$settings=unserialize($links[$i]['LINK_SETTINGS']);
98-
$new_settings='';
99-
foreach($settings as $k=>$v) {
100-
if ($v=='' || $v=='0') continue;
101-
$new_settings.=$k.': <i>'.$v.'</i>; ';
98+
$device1 = SQLSelectOne("SELECT ID, TITLE FROM devices WHERE ID=" . (int)$links[$i]['DEVICE1_ID']);
99+
$links[$i]['DEVICE1_TITLE'] = $device1['TITLE'];
100+
if ($links[$i]['LINK_SETTINGS'] != '') {
101+
$settings = unserialize($links[$i]['LINK_SETTINGS']);
102+
$new_settings = '';
103+
foreach ($settings as $k => $v) {
104+
if ($v == '' || $v == '0') continue;
105+
$new_settings .= $k . ': <i>' . $v . '</i>; ';
102106
}
103-
$links[$i]['LINK_SETTINGS']=$new_settings;
107+
$links[$i]['LINK_SETTINGS'] = $new_settings;
104108
}
105-
$rule=SQLSelectOne("SELECT ID FROM security_rules WHERE OBJECT_TYPE='sdevice' AND OBJECT_ID=". $links[$i]['ID']);
109+
$rule = SQLSelectOne("SELECT ID FROM security_rules WHERE OBJECT_TYPE='sdevice' AND OBJECT_ID=" . $links[$i]['ID']);
106110
if ($rule['ID']) {
107-
$links[$i]['HAS_RULE']=1;
111+
$links[$i]['HAS_RULE'] = 1;
108112
}
109113
}
110-
$out['LINKS']=$links;
114+
$out['LINKS'] = $links;
111115
}
112116

113-
$avail_links=$this->getTypeLinks($rec['TYPE']);
117+
$avail_links = $this->getTypeLinks($rec['TYPE']);
114118
if (isset($avail_links[0])) {
115-
$out['AVAIL_LINKS']=$avail_links;
119+
$out['AVAIL_LINKS'] = $avail_links;
116120
}
117121

modules/devices/devices_links_actions.inc.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
if (!$device1['SYSTEM_DEVICE'] && !$device1['ARCHIVED'] && $this->isHomeBridgeAvailable()) {
1010
// send updated status to HomeKit
11-
require DIR_MODULES.'devices/homebridgeSendUpdate.inc.php';
11+
require DIR_MODULES . 'devices/homebridgeSendUpdate.inc.php';
1212
}
1313
endMeasure('homebridge_update');
1414

1515
startMeasure('checkingLinks');
1616
$value = (float)gg($device1['LINKED_OBJECT'] . '.value');
1717
$status = (float)gg($device1['LINKED_OBJECT'] . '.status');
1818

19-
$links = SQLSelect("SELECT devices_linked.*, devices.LINKED_OBJECT FROM devices_linked LEFT JOIN devices ON devices_linked.DEVICE2_ID=devices.ID WHERE DEVICE1_ID=" . (int)$device1['ID']);
19+
$links = SQLSelect("SELECT devices_linked.*, devices.LINKED_OBJECT FROM devices_linked LEFT JOIN devices ON devices_linked.DEVICE2_ID=devices.ID WHERE devices_linked.IS_ACTIVE=1 AND DEVICE1_ID=" . (int)$device1['ID']);
2020
$total = count($links);
2121
for ($i = 0; $i < $total; $i++) {
2222
if (!checkAccess('sdevice', $links[$i]['ID'])) continue;
23-
if ($device1['TYPE']=='motion' && !$status) continue;
24-
if ($device1['TYPE']=='button' && !$status) continue;
23+
if ($device1['TYPE'] == 'motion' && !$status) continue;
24+
if ($device1['TYPE'] == 'button' && !$status) continue;
2525
$link_type = $links[$i]['LINK_TYPE'];
2626
$object = $links[$i]['LINKED_OBJECT'];
2727
$settings = unserialize($links[$i]['LINK_SETTINGS']);
@@ -83,15 +83,15 @@
8383
$action_string = 'callMethodSafe("' . $object . '.close' . '",array("link_source"=>"' . $device1['LINKED_OBJECT'] . '"));';
8484
}
8585

86-
if ($settings['source_value_type']!='') {
86+
if ($settings['source_value_type'] != '') {
8787
$period = (int)$settings['source_value_time'];
88-
if ($period<1) $period=1;
88+
if ($period < 1) $period = 1;
8989
if ($settings['source_value_type'] == 'avg') {
90-
$value = getHistoryAvg($device1['LINKED_OBJECT'] . '.value',(-1)*$period);
90+
$value = getHistoryAvg($device1['LINKED_OBJECT'] . '.value', (-1) * $period);
9191
} elseif ($settings['source_value_type'] == 'min') {
92-
$value = getHistoryMin($device1['LINKED_OBJECT'] . '.value',(-1)*$period);
92+
$value = getHistoryMin($device1['LINKED_OBJECT'] . '.value', (-1) * $period);
9393
} elseif ($settings['source_value_type'] == 'max') {
94-
$value = getHistoryMax($device1['LINKED_OBJECT'] . '.value',(-1)*$period);
94+
$value = getHistoryMax($device1['LINKED_OBJECT'] . '.value', (-1) * $period);
9595
}
9696
}
9797

@@ -110,26 +110,23 @@
110110
} elseif ($link_type == 'thermostat_switch') {
111111
$set_value = 0;
112112
$current_relay_status = gg($device1['LINKED_OBJECT'] . '.relay_status');
113-
$ncno = gg($device1['LINKED_OBJECT'] . '.ncno');
114-
if ($ncno == 'no' && $current_relay_status) {
115-
$current_relay_status = 0;
116-
} elseif ($ncno == 'no' && !$current_relay_status) {
117-
$current_relay_status = 1;
118-
}
119113
$current_target_status = gg($object . '.status');
120-
//echo "status: $current_relay_status / $current_target_status<Br/>";
121-
if (!$settings['invert_status'] && $current_relay_status) { // NC
122-
$set_value = 1;
123-
} elseif ($settings['invert_status'] && !$current_relay_status) {
124-
$set_value = 1;
114+
if ($settings['invert_status']) {
115+
$set_value = $current_relay_status ? 0 : 1;
116+
} else {
117+
$set_value = $current_relay_status;
125118
}
119+
DebMes("Set value: $set_value, Current status: $current_target_status", $link_type);
120+
126121
if ($set_value && !$current_target_status) {
127122
// turn on
128123
$action_string = 'callMethodSafe("' . $object . '.turnOn' . '",array("link_source"=>"' . $device1['LINKED_OBJECT'] . '"));';
129124
} elseif (!$set_value && $current_target_status) {
130125
// turn off
131126
$action_string = 'callMethodSafe("' . $object . '.turnOff' . '",array("link_source"=>"' . $device1['LINKED_OBJECT'] . '"));';
132127
}
128+
129+
DebMes("Action string: $action_string", $link_type);
133130
}
134131

135132
$addons_dir = dirname(__FILE__) . '/addons';

0 commit comments

Comments
 (0)