diff --git a/class/main.php b/class/main.php index 9a9e4dc..5da4e90 100644 --- a/class/main.php +++ b/class/main.php @@ -17,6 +17,7 @@ class topic { public $extensions; public $config; public $callback; + public $getOnStart = false; } class device @@ -47,7 +48,7 @@ class device public function get() { - publish($this->topic . "/" . $this->friendlyNames, $this->payload, "get"); //, $event); + publish($this->topic . "/" . $this->friendlyName, $this->payload, "get"); //, $event); } } diff --git a/config/dashboard_conf.php b/config/dashboard_conf.php new file mode 100644 index 0000000..1600074 --- /dev/null +++ b/config/dashboard_conf.php @@ -0,0 +1,10 @@ + "state", + RDC_SDB_WC_ECLAIRAGE => "state_l1", + RDC_SDB_WC_ECLAIRAGE => "state_l2", + RDC_CHAMBRE_AMBIANCE => "state", + RDC_CHAMBRE_ECLAIRAGE => "state_l1" + ); + +?> diff --git a/constants.php b/constants.php index c9f6876..e809814 100644 --- a/constants.php +++ b/constants.php @@ -1,7 +1,8 @@ topic == $topic) + { + logger(DEBUG, "device: " . $device->friendlyName); + $payload = "{"; + $flag = false; + $properties = array_slice($device, 12); + logger(DEBUG, print_r($properties, true)); + foreach($properties as $property) + { + if (flag) + { + $payload = ","; + }else { + $flag = true; + } + $payload .= $property . ':""'; + } + $device->payload = $payload . "}"; + logger(DEBUG, $device->payload); + $device->get(); + } + } + exit(0); } + ?> diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php index f52bdef..35246f4 100644 --- a/hooks/scripts/rdc_salon_eclairage.php +++ b/hooks/scripts/rdc_salon_eclairage.php @@ -13,7 +13,7 @@ class rdc_salon_eclairage extends hook public $delay = 3; // amount of time in $timeunit public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year - public $luminance_min = 70; + public $luminance_min = 60; public $luminance_max = 3000; diff --git a/moha.php b/moha.php index e3d35f4..1a9b96f 100644 --- a/moha.php +++ b/moha.php @@ -34,6 +34,8 @@ $curlErr = 0; // Number of errors returned by curl $configDir = "./config"; // default config dir (production value is /etc/moha/) $hooksInitialized = 0; // are all hooks initialized ? false/true $flagHooks = false; + + if ($testMode) { $mqttServerIp = "192.168.1.253"; // IP address of mqttserver in test mode @@ -45,7 +47,7 @@ if ($testMode) $mqttServerIp = "127.0.0.1"; // IP address of mqttserver in production mode $dataPath = "/usr/share/moha/"; $logFile = "/var/log/moha.log"; // Path of log file - $configDir = "/etc/config"; // default config dir (production value is /etc/moha/) + $configDir = "/etc/moha"; // default config dir (production value is /etc/moha/) } @@ -88,11 +90,7 @@ function logger($level, $log, $pos = false, $notif = true) { logger(INFO, _("Notification not sent"), __FILE__ . ":" . __LINE__, false); } - }else - { - logger(INFO, _("Notification not sent because of testMode"), __FILE__ . ":" . __LINE__, false); } - } function init() @@ -134,7 +132,10 @@ function endMoha() $x = 0; logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__); - storeDB($devices, $dataPath . "moha.db"); + if (storeDB($devices, $dataPath . "moha.db") === false) + { + logger(ERROR, _("Can not store db" ), __FILE__ . ":" . __LINE__); + } if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true)); if ($connected) { @@ -160,6 +161,25 @@ function connect2mqttServer() $client->onConnect('connectResponse'); } logger(WARNING, _("starting moha"), __FILE__ . ":" . __LINE__); + + +logger(DEBUG, _("requiring config files -> devices_constants.php"), __FILE__ . ":" . __LINE__); +//include predefined file witch define constants for devices +if (is_readable($configDir . "/devices_constants.php")) +{ + include $configDir . "/devices_constants.php"; + //echo "hooklist"; print_r($hooksList); echo EOL; + logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); +}elseif(is_readable("config/devices_constants.php")) +{ + include "config/devices_constants.php"; + //echo "hooklist"; print_r($hooksList); echo EOL; + logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); +}else +{ + logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); +} + logger(DEBUG, _("requiring php modules"), __FILE__ . ":" . __LINE__); require "class/main.php"; require "class/db.php"; @@ -171,11 +191,11 @@ require "events.php"; require "db_functions.php"; require "webserver.php"; -//logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false); -//$client = new Mosquitto\Client(); - logger(DEBUG, _("Loading stored devices datas"), __FILE__ . ":" . __LINE__); -loadDB($devices, "moha.db"); +if (loadDB($devices, $dataPath . "moha.db") === false) +{ + logger(ERROR, _("Can not load device db"), __FILE__ . ":" . __LINE__); +} // topics definition @@ -189,22 +209,6 @@ if (!empty($hooksList)) } } -logger(DEBUG, _("requiring config files -> devices_constants.php"), __FILE__ . ":" . __LINE__); -//include predefined file witch define constants for devices -if (is_readable($configDir . "/" . "devices_constants.php")) -{ - include $configDir . "/" . "devices_constants.php"; - //echo "hooklist"; print_r($hooksList); echo EOL; - logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); -}elseif(is_readable("config/devices_constants.php")) -{ - include "config/devices_constants.php"; - //echo "hooklist"; print_r($hooksList); echo EOL; - logger(INFO, sprintf(_("%s/devices_constants.define found, so it has been included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); -}else -{ - logger(WARNING, sprintf(_("%s/devices_constants.define not found, so not included :-)"), $configDir), __FILE__ . ":" . __LINE__, false); -} // making the list of hooks to include listHooks("./hooks", $hooksList); @@ -252,13 +256,13 @@ while (true) if (! $included) // hooks not already included { logger(DEBUG, _("Making hooks list"), __FILE__ . ":" . __LINE__, false); - getDevicesValues(); // TODO get the values of devices + if (!empty($hooksList)) // some hooks to include if hooklist is not empty { - foreach ($hooksList as $hook) // loop to include hooks in hookslist + foreach ($hooksList as $hookFile) // loop to include hooks in hookslist { - logger(INFO, _("Including ") . $hook, __FILE__ . ":" . __LINE__, false); - include $hook; + logger(INFO, _("Including ") . $hookFile, __FILE__ . ":" . __LINE__, false); + include $hookFile; } file_put_contents($dataPath . "moha.devices", var_export($devices, true)); // debugging : save device list $included = true; @@ -270,11 +274,12 @@ while (true) if ($oneshot === false) // execute once initialization finished :WARNING hooks can to be not initialized { logger(DEBUG, _("Oneshot part of loop"), __FILE__ . ":" . __LINE__, false); + $oneshot = true; } if($hooksInitialized == 0) // all hooks are not initialized { - $i = 1; + //$i = 1; foreach($hooks as $hookName => $hook) { if ($hook->initialized === false) @@ -288,6 +293,7 @@ while (true) { logger(DEBUG, _("All hooks initialized"), __FILE__ . ":" . __LINE__); $flagHooks = true; + getDevicesValues();exit (0); } checkEvents(); askWebServer($read); diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index c707ea9..5842d94 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -15,6 +15,7 @@ $callback = function($topic, $message) $topics[$topic[0]]->devices = json_decode($message->payload); fwrite($logFh, var_export($topics[$topic[0]]->devices, true)); mkDevicesDB($topic[0], $topics[$topic[0]]->devices); + getDevicesValues($topic[0]); break; case "groups": logger(DEBUG,_("Inserting zigbee groups in DB"), __FILE__ . ":" . __LINE__); diff --git a/webserver.php b/webserver.php index 30a874c..8d4238c 100644 --- a/webserver.php +++ b/webserver.php @@ -4,17 +4,147 @@ set_time_limit(0); ob_implicit_flush(); + $error_message = null; $error_code = null; $listenHost = "0.0.0.0"; $listenPort = 1025; +$Dashboards = array(); + +require_once $configDir . "/dashboard_conf.php"; + +// opening listening server $socket = stream_socket_server("tcp://" . $listenHost . ":" . $listenPort, $error_code, $error_message) or logger(ERROR, _("Could not create socket") . EOL); stream_set_blocking($socket, false); $read = array( $socket ); +function htmlSend($socket, $text) +{ + $httpHeader = "HTTP/1.1 200 OK" . EOLR . + "Date: " . date("r") . EOLR . + "Connection: close" . EOLR . + "Content-Type: text/html; charset=UTF-8" . EOLR . EOLR; + $response = $httpHeader . '' . EOL . '' . EOL . '' . EOL . '' . EOL . 'Moha' . EOL . '' . $text . ""; + + stream_socket_sendto($socket, $response); +} + +function webListTopics() +{ + global $topics; + logger(DEBUG, _("webListTopics function")); + $response = ""; + foreach ($topics as $name => $topic) + { + $response .= '' . $name ."
"; + } + return $response; +} + +function webBrowse($socket, $argList) +{ + global $topics, $devices; + $response = ""; + logger(DEBUG, _("Generic response to choose device and property"), __FILE__ . ":" . __LINE__); + //$response = "
" . _("unknown command") . ""; + $response = ""; + $flag = false; + if (array_key_exists("topic", $argList)) + { + if (array_key_exists($argList["topic"], $topics)) + { + logger(DEBUG, _("Topic exists") , __FILE__ . ":" . __LINE__); + $topicRef = ' $value) + { + if ($key != "device" and is_array($value)) + { + print_r($value); + if (array_key_exists("value", $value)) + { + $response .= $key . ' = ' . bool2string($value["value"]) . "
"; + } + } + } + }else + { + foreach($device as $key => $value) + { + $response .= $topicRef . "&fn=" . $fn . "/" . $key . '">' . $key . "

"; + } + } + logger(DEBUG, _("response: ") . EOL . $response , __FILE__ . ":" . __LINE__); + + //htmlSend($socket, $response); + }else + { + foreach($devices[$argList["topic"]] as $key => $value) + { + logger(DEBUG, _("devices de topic: ") . $key , __FILE__ . ":" . __LINE__); + $response .= $topicRef . "&fn=" . $key . '">' . $key . "
"; + logger(DEBUG, _("response: ") . $response , __FILE__ . ":" . __LINE__); + } + } + }else + { + $response = webListTopics(); + } + }else + { + $response = webListTopics(); + } + htmlSend($socket, $response); +} + +function webDashboard($socket, $n = 0) +{ + global $dashboards, $indexDevices; + logger(DEBUG, _("webDashboard function")); + $response = ""; + if(array_key_exists($n, $dashboards)) + { + foreach ($dashboards[$n] as $ieeeAddress => $property) + { + logger(DEBUG, _($indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($indexDevices[$ieeeAddress]->$property["value"]))); + $response .= $indexDevices[$ieeeAddress]->friendlyName . " => " . bool2string($indexDevices[$ieeeAddress]->$property["value"]) . "
"; + } + }else + { + $response = _("dashboard not found"); + } + + htmlSend($socket, $response); +} + function askWebServer($read) { + global $topics, $indexDevices, $devices; $array = array(); $argList =array(); @@ -31,16 +161,32 @@ function askWebServer($read) { $input = substr($input,5); $input = explode(" ", $input); // suppress text - $argTmp = explode("&", $input[0]); + + $argTmp = explode("&", urldecode($input[0])); foreach($argTmp as $tmp) { - //logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); - $argList[strchr($tmp, "=", true)] = substr(strchr($tmp, "="), 1); - //logger(DEBUG, $argList[0] . " ==========> " . $argList[1]); - //print_r($array); - //if (isset($array[1])) $argList[$array[0]] = $array[1]; + logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__); + if(strpos($tmp, "=") === false) + { + logger(DEBUG, _("no =")); + $argList[trim($tmp)] = ""; + }else + { + $argList[trim(strchr($tmp, "=", true))] = trim(substr(strchr($tmp, "="), 1)); + } } logger(DEBUG, print_r($argList, true)); + + if (array_key_exists("browse", $argList)) + { + logger(DEBUG, _("Browsing")); + webBrowse($spawn, $argList); + return true; + }elseif(array_key_exists("dashboard", $argList)) + { + webDashboard($spawn, $argList["dashboard"]); + return true; + } if(array_key_exists("cmd", $argList)) { $command = strtolower($argList["cmd"]); @@ -51,25 +197,29 @@ function askWebServer($read) logger(DEBUG, _("GET reached"), __FILE__ . ":" . __LINE__); if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList)) { - $response = "
GET: " . _("no parameters passed, need topic, fn and property") . ""; + $response = "GET: " . _("no parameters passed, need topic, fn and property"); }else { - $device = getDevice($argList["topic"], $argList["fn"]); + if (($device = getDevice($argList["topic"], $argList["fn"])) === false) + { + htmlSend($spawn, sprintf(_('"%s" or "%s" does not exists'), $argList["topic"], $argList["fn"])); + return false; + } $property = $argList["property"]; - $response = "
GET: " . bool2string($device->$property["value"]) . ""; + $response = "GET: " . bool2string($device->$property["value"]); } - fwrite($spawn, $response); + htmlSend($spawn, $response); break; case "set": logger(DEBUG, _("SET reached"), __FILE__ . ":" . __LINE__); if(!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("value", $argList)) { - $response = "
SET: " . _("no parameters passed, need topic, fn, property and value") . "passed"; - fwrite($spawn, $response); + $response = "SET: " . _("no parameters passed, need topic, fn, property and value") . "passed"; + htmlSend($spawn, $response); }else { - $response = "
setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"] . ""; - fwrite($spawn, $response); + $response = "setting property " . $argList["property"] . " of " . $argList["fn"] . " to value: " . $argList["value"]; + htmlSend($spawn, $response); $payload = array($argList["property"] => $argList["value"]); publish(Z2M . "/" . $argList["fn"], $payload); } @@ -104,7 +254,7 @@ function askWebServer($read) $error = error_get_last(); if($error !== null) { - $response = "
" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . ""; + $response = $error["message"] . " file: " . $error["file"] . " line: " . $error["line"]; } if ($command === "print") { @@ -114,28 +264,32 @@ function askWebServer($read) $response = "Dump" . EOL; $response .= var_export($var, true); } - fwrite($spawn, $response); + htmlSend($spawn, $response); break; case "notify": logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); if (!array_key_exists("topic", $argList) or !array_key_exists("fn", $argList) or !array_key_exists("property", $argList) or !array_key_exists("condition", $argList) or !array_key_exists("value", $argList)) { - $response = "
" . _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value") . ""; - fwrite($spawn, $response); + $response = _("Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value"); + htmlSend($spawn, $response); }else { - $response = "
" . _("notify command have been set") . ""; + $response = _("notify command have been set"); $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); - fwrite($spawn, $response); + htmlSend($spawn, $response); } logger(DEBUG, print_r($monitored, true)); break; default: - logger(DEBUG, _("unknown command"), __FILE__ . ":" . __LINE__); - $response = "
" . _("unknown command") . ""; - fwrite($spawn, $response); + webBrowse($spawn, $argList); } + }else + { + webBrowse($spawn,$argList); } + }else + { + webDashboard($spawn); } } }