Skip to content
Permalink
Browse files

Updates

* Simple Devices: Sensors now can be blocked for some time (blockSensor/unblockSensor methods)
* Core: processSubscription now can be used to inject code into HTML-templates
* Patterns: user's context fix
  • Loading branch information...
sergejey committed Sep 16, 2019
1 parent c76f526 commit 55953986527bf4f047e05bc86e776fe2a6a821fb
Showing with 632 additions and 345 deletions.
  1. +2 −0 admin.php
  2. +2 −0 index.php
  3. +2 −0 languages/default.php
  4. +3 −0 languages/ru.php
  5. +1 −0 lib/common.class.php
  6. +40 −24 lib/context.inc.php
  7. +19 −16 lib/hooks.inc.php
  8. +4 −0 lib/utils/postprocess_general.inc.php
  9. +3 −4 lib/utils/postprocess_result.inc.php
  10. +26 −0 lib/utils/postprocess_subscriptions.inc.php
  11. +188 −172 modules/dashboard/dashboard.class.php
  12. +11 −8 modules/devices/SDevices_statusUpdated.php
  13. +13 −0 modules/devices/SLeak_blockSensor.php
  14. +5 −0 modules/devices/SLeak_statusUpdated.php
  15. +7 −0 modules/devices/SLeak_unblockSensor.php
  16. +13 −0 modules/devices/SMotions_blockSensor.php
  17. +5 −0 modules/devices/SMotions_motionDetected.php
  18. +7 −0 modules/devices/SMotions_unblockSensor.php
  19. +13 −0 modules/devices/SOpenClose_blockSensor.php
  20. +4 −0 modules/devices/SOpenClose_statusUpdated.php
  21. +7 −0 modules/devices/SOpenClose_unblockSensor.php
  22. +13 −0 modules/devices/SSensors_blockSensor.php
  23. +7 −0 modules/devices/SSensors_unblockSensor.php
  24. +16 −12 modules/devices/SSensors_valueUpdated.php
  25. +13 −0 modules/devices/SSmoke_blockSensor.php
  26. +5 −0 modules/devices/SSmoke_statusUpdated.php
  27. +7 −0 modules/devices/SSmoke_unblockSensor.php
  28. +10 −10 modules/devices/devices_links_actions.inc.php
  29. +17 −2 modules/devices/devices_structure.inc.php
  30. +11 −0 modules/panel.class.php
  31. +10 −6 modules/patterns/patterns.class.php
  32. +5 −0 objects/index.php
  33. +56 −7 templates/panel.html
  34. +87 −84 templates/panel_default.html
@@ -63,6 +63,8 @@
$result = str_replace("nf.php", "admin.php", $result);
require(ROOT.'lib/utils/postprocess_general.inc.php');
require(ROOT.'lib/utils/postprocess_subscriptions.inc.php');
//require(ROOT.'lib/utils/postprocess_result.inc.php');
@@ -80,6 +80,8 @@
$result = $cached_result;
}
require(ROOT.'lib/utils/postprocess_general.inc.php');
require(ROOT.'lib/utils/postprocess_subscriptions.inc.php');
require(ROOT.'lib/utils/postprocess_result.inc.php');
/**
@@ -1390,6 +1390,8 @@
'PREVIOUS' => 'Prev',
'NEXT' => 'Next',
'LOCATED_IN_ROOM' => 'located in',
'BLOCK_SENSOR' => 'Block sensor',
'UNBLOCK_SENSOR' => 'Un-block sensor',
'TEST' => 'test'
@@ -1388,6 +1388,9 @@
'LOCATED_IN_ROOM' => 'расположеный в комнате',
'BLOCK_SENSOR' => 'Блокировать датчик',
'UNBLOCK_SENSOR' => 'Разблокировать датчик',
/* end module names */
@@ -707,6 +707,7 @@ function getURLBackground($url, $cache = 0, $username = '', $password = '')
{
//DebMes("URL: ".$url,'debug1');
getURL($url, $cache, $username, $password, true);
return true;
}
/**
@@ -6,14 +6,17 @@
*/
function context_getuser()
{
global $context_user_id;
if ($context_user_id) {
return $context_user_id;
}
global $session;
if ($session->data['SITE_USER_ID'])
{
return $session->data['SITE_USER_ID'];
if ($session->data['SITE_USER_ID']) {
return (int)$session->data['SITE_USER_ID'];
}
$user = SQLSelectOne("SELECT ID FROM users WHERE IS_DEFAULT=1");
$session->data['SITE_USER_ID'] = $user['ID'];
return (int)$user['ID'];
@@ -48,10 +51,11 @@ function context_getcurrent($from_user_id = 0)
* Summary of context_get_history
* @return mixed
*/
function context_get_history()
function context_get_history($user_id = 0)
{
$user_id = context_getuser();
if (!$user_id) {
$user_id = context_getuser();
}
$sqlQuery = "SELECT ID, ACTIVE_CONTEXT_ID, ACTIVE_CONTEXT_EXTERNAL, ACTIVE_CONTEXT_HISTORY
FROM users
WHERE ID = '" . (int)$user_id . "'";
@@ -68,10 +72,13 @@ function context_get_history()
* Summary of context_clear
* @return void
*/
function context_clear()
function context_clear($user_id=0)
{
$user_id = context_getuser();
if (!$user_id) {
$user_id = context_getuser();
}
$user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int)$user_id . "'");
$user['ACTIVE_CONTEXT_ID'] = 0;
@@ -89,9 +96,11 @@ function context_clear()
* @param mixed $history History (default '')
* @return void
*/
function context_activate($id, $no_action = 0, $history = '')
function context_activate($id, $no_action = 0, $history = '', $user_id = 0)
{
$user_id = context_getuser();
if (!$user_id) {
$user_id = context_getuser();
}
$user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int)$user_id . "'");
$user['ACTIVE_CONTEXT_ID'] = $id;
@@ -125,7 +134,7 @@ function context_activate($id, $no_action = 0, $history = '')
}
else
{
context_clear();
context_clear($user_id);
clearTimeOut('user_' . $user_id . '_contexttimeout');
}
}
@@ -138,9 +147,12 @@ function context_activate($id, $no_action = 0, $history = '')
* @param mixed $timeout_context_id Timeout context id (default 0)
* @return void
*/
function context_activate_ext($id, $timeout = 0, $timeout_code = '', $timeout_context_id = 0)
function context_activate_ext($id, $timeout = 0, $timeout_code = '', $timeout_context_id = 0, $user_id = 0)
{
$user_id = context_getuser();
if (!$user_id) {
$user_id = context_getuser();
}
$user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int)$user_id . "'");
$user['ACTIVE_CONTEXT_ID'] = $id;
@@ -174,7 +186,7 @@ function context_activate_ext($id, $timeout = 0, $timeout_code = '', $timeout_co
}
else
{
context_clear();
context_clear($user_id);
clearTimeOut('user_' . $user_id . '_contexttimeout');
}
}
@@ -185,18 +197,22 @@ function context_activate_ext($id, $timeout = 0, $timeout_code = '', $timeout_co
* @param mixed $user_id User ID
* @return void
*/
function context_timeout($id, $user_id)
function context_timeout($id, $user_id = 0)
{
global $session;
$user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int)$user_id . "'");
$session->data['SITE_USER_ID'] = $user['ID'];
if (!$user_id) {
$user_id = context_getuser();
} else {
global $context_user_id;
$context_user_id = $user_id;
}
//global $session;
//$user = SQLSelectOne("SELECT * FROM users WHERE ID = '" . (int)$user_id . "'");
//$session->data['SITE_USER_ID'] = $user['ID'];
$context = SQLSelectOne("SELECT * FROM patterns WHERE ID = '" . (int)$id . "'");
if (!$context['TIMEOUT_CONTEXT_ID'])
context_activate(0);
if (!$context['TIMEOUT_CONTEXT_ID']) context_activate(0,0,'',$user_id);
if ($context['TIMEOUT_SCRIPT'])
{
@@ -219,7 +235,7 @@ function context_timeout($id, $user_id)
}
if ($context['TIMEOUT_CONTEXT_ID'])
context_activate((int)$context['TIMEOUT_CONTEXT_ID']);
context_activate((int)$context['TIMEOUT_CONTEXT_ID'],0,'',$user_id);
}
/**
@@ -95,40 +95,39 @@ function processSubscriptionsSafe($event_name, $details = '')
* @param mixed $details Details (default '')
* @return int|void
*/
function processSubscriptions($event_name, $details = '')
function processSubscriptions($event_name, $details = '', $return_output = false)
{
//DebMes("New event: ".$event_name,'process_subscription');
postToWebSocketQueue($event_name, $details, 'PostEvent');
if (!$return_output) {
postToWebSocketQueue($event_name, $details, 'PostEvent');
}
//DebMes("Post websocket event done: ".$event_name,'process_subscription');
if (!defined('SETTINGS_HOOK_EVENT_' . strtoupper($event_name))) {
DebMes("Processing not defined for " . 'SETTINGS_HOOK_EVENT_' . strtoupper($event_name), 'process_subscription');
//DebMes("Processing not defined for " . 'SETTINGS_HOOK_EVENT_' . strtoupper($event_name), 'process_subscription');
return 0;
}
$data = json_decode(constant('SETTINGS_HOOK_EVENT_' . strtoupper($event_name)), true);
//DebMes("Subscription data for ".'SETTINGS_HOOK_EVENT_' . strtoupper($event_name).": ".serialize($data),'process_subscription');
if (!is_array($data)) {
DebMes("Incorrect data for " . 'SETTINGS_HOOK_EVENT_' . strtoupper($event_name) . ':' . constant('SETTINGS_HOOK_EVENT_' . strtoupper($event_name)), 'process_subscription');
//DebMes("Incorrect data for " . 'SETTINGS_HOOK_EVENT_' . strtoupper($event_name) . ':' . constant('SETTINGS_HOOK_EVENT_' . strtoupper($event_name)), 'process_subscription');
} else {
$data2 = array();
foreach ($data as $k => $v) {
$data2[] = array('module' => $k, 'filter' => $v['filter'], 'priority' => (int)$v['priority']);
}
usort($data2, function ($a, $b) {
if ($a['priority'] == $b['priority']) return 0;
return ($a['priority'] > $b['priority']) ? -1 : 1;
});
$output = '';
$total = count($data2);
for ($i = 0; $i < $total; $i++) {
$module_name = $data2[$i]['module'];
$filter_details = $data2[$i]['filter'];
//DebMes("Post event ".$event_name." to module ".$module_name. " (details: ".json_encode($details).")",'process_subscription');
$modulePath = DIR_MODULES . $module_name . '/' . $module_name . '.class.php';
if (file_exists($modulePath)) {
@@ -138,30 +137,34 @@ function processSubscriptions($event_name, $details = '')
//DebMes("$module_name.processSubscription ($event_name)",'process_subscription');
verbose_log("Processing subscription to [" . $event_name . "] by [" . $module_name . "] (" . (is_array($details) ? json_encode($details) : '') . ")");
try {
$module_object->processSubscription($event_name, $details);
$output .= $module_object->processSubscription($event_name, $details);
} catch (Exception $e) {
DebMes('Error in processing "%s": ' . $e->getMessage(), 'process_subscription');
//DebMes('Error in processing "%s": ' . $e->getMessage(), 'process_subscription');
}
//DebMes("$module_name.processSubscription ($event_name) DONE",'process_subscription');
} else {
DebMes("$module_name.processSubscription error (method not found)", 'process_subscription');
//DebMes("$module_name.processSubscription error (method not found)", 'process_subscription');
}
if (!isset($details['BREAK'])) {
$details['BREAK'] = false;
}
if ($details['BREAK']) break;
} else {
DebMes("$module_name.processSubscription error (module class not found)", 'process_subscription');
//DebMes("$module_name.processSubscription error (module class not found)", 'process_subscription');
}
}
if (!isset($details['PROCESSED'])) {
$details['PROCESSED'] = false;
}
/*
if (!$details['PROCESSED'] && $event_name == 'COMMAND') { sayReplySafe(LANG_DEVICES_UNKNOWN_COMMAND,2);}
*/
return (int)$details['PROCESSED'];
//if (!$details['PROCESSED'] && $event_name == 'COMMAND') { sayReplySafe(LANG_DEVICES_UNKNOWN_COMMAND,2);}
if ($return_output) {
return $output;
} else {
return (int)$details['PROCESSED'];
}
}
return 0;
@@ -0,0 +1,4 @@
<?php
$result = preg_replace('/<!--\s+(\w)/','<!--$1',$result);
$result = preg_replace('/([\w\]])\s+-->/','$1-->',$result);
@@ -2,13 +2,13 @@
// BEGIN: begincut endcut placecut
if (preg_match_all('/<!-- placecut (\w+?) -->/is', $result, $matches))
if (preg_match_all('/<!--placecut (\w+?)-->/is', $result, $matches))
{
$matchesCount = count($matches[1]);
for ($i = 0; $i < $matchesCount; $i++)
{
$block = $matches[1][$i];
if (preg_match('/<!-- begincut ' . $block . ' -->(.*?)<!-- endcut ' . $block . ' -->/is', $result, $matches2))
if (preg_match('/<!--begincut ' . $block . ' -->(.*?)<!--endcut ' . $block . '-->/is', $result, $matches2))
{
$result = str_replace($matches[0][$i], $matches2[1], $result);
$result = str_replace($matches2[0], '', $result);
@@ -20,7 +20,7 @@
// BEGIN: filter output
if (isset($filterblock) && $filterblock != '')
{
$matchPattern = '/<!-- begin_data \[' . $filterblock . '\] -->(.*?)<!-- end_data \[' . $filterblock . '\] -->/is';
$matchPattern = '/<!--begin_data \[' . $filterblock . '\]-->(.*?)<!--end_data \[' . $filterblock . '\]-->/is';
preg_match($matchPattern, $result, $match);
$result = $match[1];
}
@@ -115,7 +115,6 @@
}
}
// END GLOBALS
// BEGIN: language constants
@@ -0,0 +1,26 @@
<?php
// BEGIN: process subscriptions
if (preg_match_all('/<!--processSubscription (\w+?)-->/is', $result, $matches))
{
$total = count($matches[0]);
for($i=0;$i<$total;$i++) {
$result = str_replace($matches[0][$i],'<span id="subscription'.$matches[1][$i].'"></span>',$result);
}
$result.="<script type='text/javascript'>";
$result.="function postLoadSubscription(name) {
var url=\"".ROOTHTML."objects/?processSubscriptionsOutput=1&event=\"+name;
$.ajax({
url: url
}).done(function(data) {
$('#subscription'+name).html(data);
});
}";
$result.="\$(document).ready(function() {";
foreach($matches[1] as $name) {
$result.="postLoadSubscription('$name');";
}
$result.="});";
$result.="</script>";
}
// END: process subscriptions

0 comments on commit 5595398

Please sign in to comment.
You can’t perform that action at this time.