' . 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(); if ( stream_select( $read, $array, $array, 0 )) { logger(DEBUG, _("socket ready to read"), __FILE__ . ":" . __LINE__); $spawn = stream_socket_accept($read[0]); if ($spawn !== false) { logger(DEBUG, _("socket accepted"), __FILE__ . ":" . __LINE__); $input = fgets($spawn, 4096); logger(DEBUG, $input, __FILE__ . ":" . __LINE__); if (!empty($input)) { $input = substr($input,5); $input = explode(" ", $input); // suppress text $argTmp = explode("&", urldecode($input[0])); foreach($argTmp as $tmp) { 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"]); logger(DEBUG, _("command is ") . $command); switch($command) { case "get": 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"); }else { 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"]); } 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"; htmlSend($spawn, $response); }else { $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); } break; case "dump": case "print": logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); $var = $GLOBALS[$argList["object"]]; if (isset($argList["topic"])) { $topic = $argList["topic"]; } if (isset($argList["address"])) { $var = $var[$argList["address"]]; }elseif (isset($argList["fn"])) { if(!empty($topic)) { $var = getDevice($topic, $argList["fn"]); }else { $str = _("topic is not defining: add &topic=\nThese topics are availables: "); foreach($topics as $key => $value) { $str .= $key . EOL; } logger(ERROR, $str, __FILE__ . ":" . __LINE__); } } $error = error_get_last(); if($error !== null) { $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); } 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"); htmlSend($spawn, $response); }else { $response = _("notify command have been set"); $monitored[] = new watch($argList["topic"], $argList["fn"], $argList["property"], $argList["condition"], $argList["value"]); htmlSend($spawn, $response); } logger(DEBUG, print_r($monitored, true)); break; default: webBrowse($spawn, $argList); } }else { webBrowse($spawn,$argList); } }else { webDashboard($spawn); } } } } ?>