diff --git a/class/db.php b/class/db.php
index ede00f7..8ba93e8 100644
--- a/class/db.php
+++ b/class/db.php
@@ -75,7 +75,7 @@ class db extends mysqli
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
}
}
- logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), $value, $propertyTree, $device->friendlyName), __FILE__ . ":" . __LINE__);
+ logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), bool2string($value), $propertyTree, $device->friendlyName), __FILE__ . ":" . __LINE__);
}
}
}
diff --git a/class/hook_class.php b/class/hook_class.php
index 7bfa5d2..e2f37cb 100644
--- a/class/hook_class.php
+++ b/class/hook_class.php
@@ -53,7 +53,12 @@ class hook
logger(DEBUG, $this->hookName . _(" initialized"), __FILE__ . ":" . __LINE__);
//var_dump($this);
}
+ }else
+ {
+ $this->initialized = true;
+ logger(DEBUG, $this->hookName . _("hook is disabled"), __FILE__ . ":" . __LINE__);
}
+
return $result;
}
}
diff --git a/class/main.php b/class/main.php
index 5da4e90..9e014e6 100644
--- a/class/main.php
+++ b/class/main.php
@@ -34,6 +34,7 @@ class device
public $availability;
public $toConfirm;
public $triggerDevice;
+ public $properties = array();
public function __construct()
{
@@ -89,17 +90,17 @@ class watch
public function __construct($topic, $fn, $property, $condition, $value)
{
- global $conditions, $acceptedValues, $indexDevices;
+ global $conditions, $indexDevices;
logger(DEBUG, _("New Notification"), __FILE__ . ":" . __LINE__);
if (($device = getDevice($topic, $fn)) !== false)
{
$this->topic = $topic;
- if (property_exists($device, $property))
+ if (array_key_exists($property, $device->properties))
{
$this->property = $property;
- if (!is_numeric($value) and array_search($value, $this->acceptedValues) === false)
+ if (!is_numeric($value) or array_search($value, $this->acceptedValues) === false)
{
- logger(ERROR, _("Value is not numeric"), __FILE__ . ":" . __LINE__ );
+ logger(ERROR, _("Value is not numeric or not accepted"), __FILE__ . ":" . __LINE__ );
return false;
}
if (array_search($condition, $this->conditions))
@@ -112,7 +113,7 @@ class watch
logger(ERROR, sprintf(_("Condition %s is not one of the permitted : "), $condition) . print_r($conditions, true), __FILE__ . ":" . __LINE__ );
return false;
}
- $indexDevices[$device->ieeeAddress]->$property["functions"][] = array($this,"notifyCallback");
+ $indexDevices[$device->ieeeAddress]->properties[$property]["functions"][] = array($this,"notifyCallback");
}else
{
logger(ERROR, _("Property do not exists"), __FILE__ . ":" . __LINE__ );
@@ -132,11 +133,9 @@ class watch
if (eval($this->function))
{
logger(DEBUG, _("notifyCallback"));
- $msg = sprintf(_("Device '%s' have property '%s' value %s %s %s"), $device->friendlyName, $property, $value, $this->condition, $this->PropertyValue );
+ $msg = sprintf(_("Device '%s' have property '%s' value %s %s %s"), $device->friendlyName, $property, bool2string($value), $this->condition, bool2string($this->PropertyValue) );
notify($msg);
}
-
-
}
}
diff --git a/db_functions.php b/db_functions.php
index 53f9813..8440b59 100644
--- a/db_functions.php
+++ b/db_functions.php
@@ -46,12 +46,13 @@ function mkDevicesDB($topic, $json, $group=false)
}
$device["device"]->topic = $topic;
//$device["device"]->device = $jsonDevice;
- $device["device"]->friendlyName = $jsonDevice->friendly_name;
+ $device["device"]->friendlyName = $fn;
if ($group)
{
//print_r($device);
$device["device"]->groupID = $jsonDevice->id;
- $indexDevices[$device["device"]->groupID] = $jsonDevice->friendly_name;
+ $indexDevices[$device["device"]->groupID] = & $device["device"];
+ $indexFriendlyNames[$fn] = & $device["device"];
}else
{
addDevice($device, $fn, $jsonDevice);
@@ -68,7 +69,7 @@ function mkDevicesDB($topic, $json, $group=false)
function addDevice(& $device, $fn, $jsonDevice )
{
- global $listProperties, $listPropertiesKeys, $hooks, $indexDevices;
+ global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames;
$device["device"]->type = $jsonDevice->type;
$device["device"]->ieeeAddress = $jsonDevice->ieee_address;
if ( !empty($jsonDevice->power_source ) )
@@ -84,6 +85,7 @@ function addDevice(& $device, $fn, $jsonDevice )
//indexing device
$indexDevices[$device["device"]->ieeeAddress] = & $device["device"];
+ $indexFriendlyNames[$fn] = & $device["device"];
}
function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
@@ -99,8 +101,8 @@ function searchPropertyKey($fn, &$device, $object, $listPropertiesKeys)
if ( isset($value->property))
{
$string = $value->property;
- $device->{$string}["value"] = null;
- $device->$string["functions"] = array();
+ $device->properties[$string]["value"] = null;
+ $device->properties[$string]["functions"] = array();
}
}
//print_r($device);
@@ -139,7 +141,7 @@ function changeDevice($topic, $fn, &$device, $payloadArray)
function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="")
{
global $changed, $mohaDB, $testMode;
- $deviceType = (gettype($device) == "object"); // = true if object
+ $deviceType = (is_a($device, "device")); // = true if object
//echo "==================== New iterate =====================" .EOL;
//echo "devicetype = "; var_dump($deviceType); echo EOL;
//echo "device =>";print_r($device);echo EOL;
@@ -159,24 +161,24 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
if ($deviceType === true )
{
//echo "deviceType = true" . EOL;
- if (!property_exists($device, $key))
+ if (!array_key_exists($key, $device->properties))
{
//echo "Property do not exists" . EOL;
- $device->{$key} = new stdClass;
+ $device->properties[$key] = new stdClass;
}
//echo "iterating" . EOL;
//echo "===============>";
- iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
+ iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}else
{
//echo "is array";
- if (!array_key_exists($key, $device))
+ if (!array_key_exists($key, $device->properties))
{
- $device[$key] = new stdClass;
+ $device->properties[$key] = new stdClass;
}
//echo "iterating" . EOL;
- iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
+ iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}
}elseif ($valueType == "array")
@@ -184,12 +186,12 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$propertyTree .= $key . "/";
if ($deviceType === true )
{
- $device->$key = array();
- iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree);
+ $device->properties[$key] = array();
+ iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}else
{
- $device[$key] = array();
- iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree);
+ $device->properties[$key] = array();
+ iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree);
}
}else
@@ -208,7 +210,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
}
}else
{
- $device->{$key} = array("value" => null);
+ $device->$key = array("value" => null);
$device->$key["functions"] = array();
}
//echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
@@ -219,16 +221,15 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
//$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__);
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
- if (!empty($device->$key["functions"]))
- {
- logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
- foreach($device->$key["functions"] as $function)
+ if (!empty($device->$key["functions"]))
{
- $function($device, $key, $value);
+ logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__);
+ foreach($device->properties[$key]["functions"] as $function)
+ {
+ $function($device, $key, $value);
+ }
}
}
- }
-
//}
}
}
@@ -245,25 +246,24 @@ function getDevicesValues($topic)
logger(DEBUG, "device: " . $device->friendlyName);
$payload = "{";
$flag = false;
- $properties = array_slice($device, 12);
- logger(DEBUG, print_r($properties, true));
- foreach($properties as $property)
+ //$properties = array_slice($device, 12);
+ //logger(DEBUG, print_r($properties, true));
+ foreach($device->properties as $property => $value)
{
- if (flag)
+
+ if ($flag)
{
- $payload = ",";
+ $payload .= ",";
}else {
$flag = true;
}
- $payload .= $property . ':""';
+ $payload .= '"' . $property . '":""';
}
$device->payload = $payload . "}";
logger(DEBUG, $device->payload);
$device->get();
}
}
- exit(0);
}
-
?>
diff --git a/events.php b/events.php
index f59be59..49668bf 100644
--- a/events.php
+++ b/events.php
@@ -1,12 +1,12 @@
availability = $value;
- logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, $value), __FILE__ . ":" . __LINE__);
+ logger($log, sprintf(_("Device: %s/%s is %s"), $device->topic, $device->friendlyName, bool2string($value)), __FILE__ . ":" . __LINE__);
}
break;
}
diff --git a/hooks/scripts/etage_plan_travail_eclairage b/hooks/scripts/etage_plan_travail_eclairage.php
similarity index 91%
rename from hooks/scripts/etage_plan_travail_eclairage
rename to hooks/scripts/etage_plan_travail_eclairage.php
index ea56c45..8def8c8 100644
--- a/hooks/scripts/etage_plan_travail_eclairage
+++ b/hooks/scripts/etage_plan_travail_eclairage.php
@@ -3,10 +3,10 @@
class etage_plan_travail_eclairage extends hook
{
public $hookName = "etage_plan_travail_eclairage";
- public $active = true; //enable/disable hook (true => enabled)
+ public $active = false; //enable/disable hook (true => enabled)
public $delay = 3; // amount of time in $timeunit
public $timeUnit = "hour"; // unit of time for delay, second, minute, hour, day, week, month, year
- protected $devicelist = array("ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE" => array("state", false));
+ protected $devicelist = array(ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE => array("state", false));
// callback fonction. Is called with these 3 parameters
// $device -> calling device
diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php
index 09f93aa..8cbae24 100644
--- a/hooks/scripts/rdc_chambre_eclairage.php
+++ b/hooks/scripts/rdc_chambre_eclairage.php
@@ -30,7 +30,7 @@ class rdc_chambre_eclairage extends hook
{
$this->send(RDC_CHAMBRE_ECLAIRAGE, "ON", "OFF", AUTO);
}
- logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value), __FILE__ . ":" . __LINE__);
+ logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__);
}
private function send($device, $state, $delayState = false, $method = MANUAL)
diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php
index 35246f4..0ace81f 100644
--- a/hooks/scripts/rdc_salon_eclairage.php
+++ b/hooks/scripts/rdc_salon_eclairage.php
@@ -1,4 +1,5 @@
send("ON", null, AUTO);
- removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
+ removeEvent($device, "state", "OFF");
}elseif ($value == OFF)
{
logger(DEBUG, _("Value is OFF"), __FILE__ . ":" . __LINE__);
if (getValue(RDC_SALON_MVMT, "occupancy") == OFF and (getValue(RDC_SALON_MVMT2, "occupancy") == OFF))
{
logger(DEBUG, _("Setting to OFF"), __FILE__ . ":" . __LINE__);
- setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
+ setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
//$this->send("ON", "OFF", AUTO);
}
}
@@ -64,7 +65,10 @@ class rdc_salon_eclairage extends hook
logger(DEBUG, _("illuminace is > to max"), __FILE__ . ":" . __LINE__);
//$this->send("OFF", null, AUTO);
//removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
- setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
+ if (searchEvent($device, "state", "OFF") === false)
+ {
+ setDelay($device, $this->delay, $this->timeUnit, "state", "OFF", true);
+ }
}elseif ($value <= $this->luminance_min and (getValue(RDC_SALON_MVMT, "occupancy") == ON OR getValue(RDC_SALON_MVMT2,"occupancy") == ON))
{
logger(DEBUG, _("illuminance < min and movement detected"), __FILE__ . ":" . __LINE__);
@@ -77,7 +81,7 @@ class rdc_salon_eclairage extends hook
private function send($state, $delayState = false, $method = MANUAL)
{
- global $devices, $indexDevices;
+ global $indexDevices;
$device = & $indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU];
$msg = array("state" => $state);
if ($device->state["value"] != $state)
@@ -89,7 +93,6 @@ class rdc_salon_eclairage extends hook
}else
{
logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName), __FILE__ . ":" . __LINE__);
-
}
//echo 'delaystate = ' . var_dump($delayState);
if ($delayState !== false) setDelay($device, $this->delay, $this->timeUnit, "state", $delayState, true);
diff --git a/hooks/scripts/rdc_wc_eclairage.php b/hooks/scripts/rdc_wc_eclairage.php
index 79f30e2..96c0681 100644
--- a/hooks/scripts/rdc_wc_eclairage.php
+++ b/hooks/scripts/rdc_wc_eclairage.php
@@ -28,7 +28,7 @@ class rdc_wc_eclairage extends hook
}
break;
}
- logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value), __FILE__ . ":" . __LINE__);
+ logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, bool2string($value)), __FILE__ . ":" . __LINE__);
}
}
diff --git a/moha.php b/moha.php
index 1a9b96f..abc767a 100644
--- a/moha.php
+++ b/moha.php
@@ -10,7 +10,7 @@ $webServerIsActive = true;
require "constants.php";
$listProperties = array("powerSource" => "batterie");
-$listPropertiesKeys = array("property");
+$listPropertiesKeys = array("expose");
//global variables
$logLevel = ALL; // INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
@@ -18,7 +18,8 @@ $notificationLevel = ALERT | ERROR;
$topics = array(); // list of topics
$mids = array(); // list of message IDs
$devices = array(); // array of device objetcs
-$indexDevices = array(); // index devices by ieee_address
+$indexDevices = array(); // index of devices by ieee_address
+$indexFriendlyNames = array(); // index of devices by freindly name
$hooksList = array(); // list of hooks to be included
$hooks = array(); // array of hooks
$notificationMethods = array(); // array of notification methods objects
@@ -271,7 +272,7 @@ while (true)
}else
{
- if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized
+ if ($oneshot === false) // execute while the first loop :WARNING hooks can to be not initialized
{
logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false);
@@ -284,7 +285,7 @@ while (true)
{
if ($hook->initialized === false)
{
- logger(WARNING, _("Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
+ logger(WARNING, _("Initializing Hook not completely initialized :") . $hookName, __FILE__ . ":" . __LINE__);
$i &= $hook->installHooks();
}
}
@@ -293,7 +294,11 @@ while (true)
{
logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__);
$flagHooks = true;
- getDevicesValues();exit (0);
+
+ }else // executed when initialization finished
+ {
+ logger(DEBUG, _("looping"), __FILE__ . ":" . __LINE__);
+
}
checkEvents();
askWebServer($read);
diff --git a/webserver.php b/webserver.php
index 8d4238c..30cce41 100644
--- a/webserver.php
+++ b/webserver.php
@@ -12,6 +12,7 @@ $listenPort = 1025;
$Dashboards = array();
require_once $configDir . "/dashboard_conf.php";
+require_once "class/main.php";
// opening listening server
$socket = stream_socket_server("tcp://" . $listenHost . ":" . $listenPort, $error_code, $error_message) or logger(ERROR, _("Could not create socket") . EOL);
@@ -37,7 +38,7 @@ function webListTopics()
$response = "";
foreach ($topics as $name => $topic)
{
- $response .= '' . $name ."
";
+ $response .= '' . $name ."
";
}
return $response;
}
@@ -45,7 +46,6 @@ function webListTopics()
function webBrowse($socket, $argList)
{
global $topics, $devices;
- $response = "";
logger(DEBUG, _("Generic response to choose device and property"), __FILE__ . ":" . __LINE__);
//$response = "
" . nl2br(print_r($var, true)) . ""; + }elseif($command === "dump") + { + $response = "Dump" . EOL; + $response .= "" . nl2br(var_export($var, true)) . ""; + } + }else { - $response = $error["message"] . " file: " . $error["file"] . " line: " . $error["line"]; - } - if ($command === "print") - { - $response = print_r($var, true); - }elseif($command === "dump") - { - $response = "Dump" . EOL; - $response .= var_export($var, true); + $response = _("Object do not exists"); } + htmlSend($spawn, $response); break; case "notify": @@ -278,14 +299,14 @@ function askWebServer($read) $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); htmlSend($spawn, $response); } - logger(DEBUG, print_r($monitored, true)); + logger(DEBUG, print_r($monitored, true), __FILE__ . ":" . __LINE__); break; default: webBrowse($spawn, $argList); } }else { - webBrowse($spawn,$argList); + webDashboard($spawn); } }else { diff --git a/www/index.php b/www/index.php new file mode 100644 index 0000000..3f39399 --- /dev/null +++ b/www/index.php @@ -0,0 +1,10 @@ +