Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- ##############################################################################################################################################################################
- #
- # Created by: Marcel Lemmen
- # Version: 1.0
- # Date: 04-01-2014
- # Contact: homewizard-php@daxy.net
- # Note: An HomeWizard is required (http://www.homewizard.nl)
- #
- # Changelog:
- # v1.0 - Initial release. Implemented Light switches and Scenes
- #
- # Open items:
- # - Dimmer support for lights
- # - Code cleanup (LOL... likely not going to happen)
- #
- #
- # Credits for borrowed code:
- # * hw_exec function is kindly borrowed from Felix: http://www.homewizards.nl/forum/viewtopic.php?f=7&t=15&start=20#p727
- # * Weather info is kindly collected from OpenWeatherMap: http://www.openweathermap.org
- # * iPhone size screenmap kindly borrowed from Tobias: http://stackoverflow.com/a/2963251
- # * iOS7 CSS kindly borrowed from Bandar: http://codepen.io/BandarRaffah/pen/ibwje
- # * iOS7 slider for dimmer kindly borrowed from Achim: http://codepen.io/hauptrolle/pen/yfejp
- # * jQuery for additional Javascript functionality: http://www.jquery.com
- # * Auto refresh of tables kindly borrowed from Robbert: http://stackoverflow.com/a/13324909
- # * Status images kindly collected from Clker: http://www.clker.com/
- #
- ##############################################################################################################################################################################
- # Basic Settings:
- $hw = "http://<HomeWizard IP>/<HomeWizard Password>"; // IP and password for your HomeWizard
- $base = "http://<websitehostname or IP>/<This File>"; // Exact URL where this script resides
- $settings['name'] = "HomeWizard"; // Name your HomeWizard, shown on page
- $settings['location'] = "Utrecht"; // Set your location for weather image, will display Amsterdam when city can't be found
- $settings['debug'] = 0; // Disables actual action, Enable debug-mode (HTML borders, shows additional lights and scenes for HTML debugging)
- $settings['lights'] = 1; // Enable lights section
- $settings['scenes'] = 1; // Enables scenes section
- $settings['dimvalue'] = 175; // Default value when dimmer is enabled. Value between 0-255
- // Limit amount of scenes and lights to display.
- $settings['scenes2show'] = array(0,2,3); // Limit amount of scenes to show (this example shows the 1st and 3rd scene only). Comment line out to show them all
- $settings['lights2show'] = array(0,1,2,3,4); // Limit amount of lights to show (this example shows the 1st and 3rd scene only). Comment line out to show them all
- ##############################################################################################################################################################################
- # NO EDITING SHOULD BE REQUIRED BELOW THIS LINE
- ##############################################################################################################################################################################
- function hw_exec($jsonurl) {
- $data = null;
- try {
- $json = file_get_contents($jsonurl); // get info
- $data = json_decode($json,true); // convert csv to array
- } catch (Exception $e) { // jic if no response
- print $e->getMessage();
- }
- return $data;
- }
- function decode_url($url){
- // $action = 0;
- if (!isset($_GET['a'])) { $action = "display";
- } else {
- $action = $_GET['a'];
- }
- return $action;
- }
- function display_weather_icon($settings){
- $loc = $settings['location'];
- $jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=$loc";
- $w_data = hw_exec($jsonurl);
- if(!isset($w_data['weather'][0]['icon'])) { $loc = "Amsterdam"; $jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=$loc"; $w_data = hw_exec($jsonurl);}
- $icon = $w_data['weather'][0]['icon'];
- print "<img SRC=\"http://openweathermap.org/img/w/$icon\"></IMG>\n";
- }
- function display_header($data,$settings){
- print "<html>\n";
- print "<head>\n";
- print "<meta name=\"viewport\" content=\"initial-scale = 1.0,maximum-scale = 1.0\" />\n";
- print "<title>" . $settings['name'] . "</title>\n";
- print "</head>\n";
- print "<body>\n";
- print "<script src=\"http://code.jquery.com/jquery-latest.js\"></script>\n";
- // ios7 style sheet
- print " <style>input[type=\"checkbox\"] { \n";
- print " position: absolute;\n";
- print " opacity: 0;\n";
- print "}\n";
- print "\n";
- print "/* Normal Track */\n";
- print "input[type=\"checkbox\"].ios-switch + div {\n";
- print " vertical-align: middle;\n";
- print " width: 40px; height: 20px;\n";
- print " border: 1px solid rgba(0,0,0,.4);\n";
- print " border-radius: 999px;\n";
- print " background-color: rgba(0, 0, 0, 0.1);\n";
- print " -webkit-transition-duration: .4s;\n";
- print " -webkit-transition-property: background-color, box-shadow;\n";
- print " box-shadow: inset 0 0 0 0px rgba(0,0,0,0.4);\n";
- print " margin: 15px 1.2em 15px 2.5em;\n";
- print "}\n";
- print "\n";
- print "/* Checked Track (Blue) */\n";
- print "input[type=\"checkbox\"].ios-switch:checked + div {\n";
- print " width: 40px;\n";
- print " background-position: 0 0;\n";
- print " background-color: #3b89ec;\n";
- print " border: 1px solid #0e62cd;\n";
- print " box-shadow: inset 0 0 0 10px rgba(59,137,259,1);\n";
- print "}\n";
- print "\n";
- print "/* Green Track */\n";
- print "input[type=\"checkbox\"].green.ios-switch:checked + div {\n";
- print " background-color: #00e359;\n";
- print " border: 1px solid rgba(0, 162, 63,1);\n";
- print " box-shadow: inset 0 0 0 10px rgba(0,227,89,1);\n";
- print "}\n";
- print "\n";
- print "/* Normal Knob */\n";
- print "input[type=\"checkbox\"].ios-switch + div > div {\n";
- print " float: left;\n";
- print " width: 18px; height: 18px;\n";
- print " border-radius: inherit;\n";
- print " background: #ffffff;\n";
- print " -webkit-transition-timing-function: cubic-bezier(.54,1.85,.5,1);\n";
- print " -webkit-transition-duration: 0.4s;\n";
- print " -webkit-transition-property: transform, background-color, box-shadow;\n";
- print " -moz-transition-timing-function: cubic-bezier(.54,1.85,.5,1);\n";
- print " -moz-transition-duration: 0.4s;\n";
- print " -moz-transition-property: transform, background-color;\n";
- print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(0, 0, 0, 0.4);\n";
- print " pointer-events: none;\n";
- print " margin-top: 1px;\n";
- print " margin-left: 1px;\n";
- print "}\n";
- print "\n";
- print "/* Checked Knob (Blue Style) */\n";
- print "input[type=\"checkbox\"].ios-switch:checked + div > div {\n";
- print " -webkit-transform: translate3d(20px, 0, 0);\n";
- print " -moz-transform: translate3d(20px, 0, 0);\n";
- print " background-color: #ffffff;\n";
- print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(8, 80, 172,1);\n";
- print "}\n";
- print "\n";
- print "/* Green Knob */\n";
- print "input[type=\"checkbox\"].green.ios-switch:checked + div > div {\n";
- print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(0, 162, 63,1);\n";
- print "}\n";
- print "\n";
- print "/* Needless Page Decoration */\n";
- print "body {-webkit-user-select: none; cursor: default; font: 18px \"Helvetica Neue\";color: rgba(0, 0, 0, 0.77);font-weight: 200;padding-left: 30px;padding-top: 0px;background: -webkit-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: -moz-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: -ms-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: linear-gradient(to bottom, #f2fbff 0%, #ffffff 64%) no-repeat;} h1 {font-weight: 100;font-size: 40px;color: #135ae4;} h2 { font-weight: 200; font-size: 22px; color: #03b000;} h3 {font-weight: 200; font-size: 18px; color: rgba(0, 0, 0, 0.77); margin-top: 50px;} a:link {text-decoration:none; color: #f06;} a:visited {text-decoration:none; color: #f06;} a:hover {text-decoration:underline;} a:active {text-decoration:underline;}</style>\n";
- print "\n";
- print " <script>\n";
- print " window.open = function(){};\n";
- print " window. print = function(){};\n";
- print " // Support hover state for mobile.\n";
- print " if (false) {\n";
- print " window.ontouchstart = function(){};\n";
- print " }\n";
- print " </script>\n";
- //auto refresh light status//
- print "<script>\n";
- print "$(function() {\n";
- print " startRefresh()\n";
- print "});\n";
- print " function startRefresh() {\n";
- print "setTimeout(startRefresh,20000);\n";
- print " $.get('$base?a=lightstatus', function(data) {\n";
- print " $('#lightsRefresh').html(data);\n";
- print " });\n";
- print " }\n";
- print "</script>\n";
- // Display slider
- print "<script>\n";
- print "var inputs = document.getElementsByTagName('input');\n";
- print "inputs = Array.splice(inputs, 0);\n";
- print "inputs.forEach(function (item) {\n";
- print " if (item.type === 'range') {\n";
- print " item.onchange = function () {\n";
- print " value = (item.value - item.min)/(item.max - item.min)\n";
- print " item.style.backgroundImage = [\n";
- print " '-webkit-gradient(',\n";
- print " 'linear, ',\n";
- print " 'left top, ',\n";
- print " 'right top, ',\n";
- print " 'color-stop(' + value + ', blue), ',\n";
- print " 'color-stop(' + value + ', red)',\n";
- print " ')'\n";
- print " ].join('');\n";
- print " };\n";
- print " }\n";
- print "});\n";
- print "</script>\n";
- if($data['status'] = "ok"){
- $status = "http://www.clker.com/cliparts/P/L/S/h/3/O/small-green-check-mark-th.png";
- } else {
- $status = "http://www.clker.com/cliparts/Q/A/W/c/p/q/small-red-x-mark-th.png";
- }
- print "<div class=\"wrap\">\n";
- print "<h1> " . $settings['name'] . "</h1>\n";
- if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
- print "<table $v_debug width=\"100%\">\n";
- print " <tr>\n";
- print "<td>Status: </td>\n";
- print "<td>Weer: </td>\n";
- print "<td>Temperatuur: </td>\n";
- print "</tr><tr>\n";
- print "<td align=\"center\"><img src=\"$status\" height=\"20\" width=\"20\"></img></td>\n";
- print "<td>";
- display_weather_icon($settings);
- print "</td>\n";
- print "<td>\n";
- display_thermometers_data($data,$settings);
- print "</td>\n";
- print "</tr></table>\n";
- print "<hr width=\"100%\">\n";
- }
- function display_footer($data,$settings){
- print "<hr width=\"100%\">\n";
- print "<h3>HomeWizard Version: " . $data['version'] . "</h3>\n";
- print "</body>\n</html>\n";
- }
- function display_thermometers_data($data,$settings){
- if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
- print "<table $v_debug>\n";
- for($i=0; $i<count($data['response']['thermometers']); $i++){
- print "<tr><td width=100>";
- print $data['response']['thermometers'][$i]['name'] . "";
- print "</td><td>";
- print $data['response']['thermometers'][$i]['te'] . "C";
- print "</td></tr>\n";
- }
- print "</table>\n";
- }
- function display_lights_data($data,$base,$settings){
- print "Light switches: <BR>\n";
- if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
- $v_count=count($data['response']['switches']) -1 ; // Minus one due to count starting at one, not zero
- if(!is_array($settings['lights2show'])) { $v_tmp = range(0,$v_count); } else { $v_tmp = $settings['lights2show']; }
- if($settings['debug'] == 1){ print "Lights to display: "; print_r($v_tmp); }
- print "<table $v_debug width=25%>\n";
- foreach ($v_tmp as &$i) {
- //for($i=0; $i<count($data['response']['switches']); $i++){
- print "<tr><td width=75%>";
- print $data['response']['switches'][$i]['name'] . "";
- print "</td><td>";
- // print $data['response']['switches'][$i]['status'] . "";
- // print "</td><td>";
- if ($data['response']['switches'][$i]['status'] == "on"){
- $checked = " checked ";
- $onoff = "off";
- } else {
- $checked = "";
- $onoff = "on";
- }
- if ($data['response']['switches'][$i]['type'] == "dimmer"){
- // print "<input type=range min=1 max=255 step=1 value=200>\n"; // Dimmer not yet implemented
- }
- print "<label>\n";
- print "<form action=\"$base\" method=\"get\">\n";
- print "<input type=\"hidden\" name=\"type\" value=\"sw\">\n";
- print "<input type=\"hidden\" name=\"a\" value=\"set\">\n";
- print "<input type=\"hidden\" name=\"id\" value=\"$i\">\n";
- print "<input type=\"hidden\" name=\"ae\" value=\"$onoff\">\n";
- print "<input type=\"checkbox\" class=\"ios-switch\" $checked onclick=\"this.form.submit();\" /><div><div></div></div>\n";
- print "</form>\n";
- print "</label>\n";
- print "</td></tr>\n";
- }
- print "</table>\n";
- }
- function display_scenes_data($data,$base,$settings){
- print "Scene switches:<BR>\n";
- if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
- $v_count=count($data['response']['scenes']) -1 ; // Minus one due to count starting at one, not zero
- if(!is_array($settings['scenes2show'])) { $v_tmp = range(0,$v_count); } else { $v_tmp = $settings['scenes2show']; }
- if($settings['debug'] == 1){ print "Scenes to display: "; print_r($v_tmp); }
- print "<table $v_debug width=25%>\n";
- foreach ($v_tmp as &$i) {
- print "<tr><td width=75%>";
- print $data['response']['scenes'][$i]['name'] . "";
- print "</td><td>";
- if ($data['response']['scenes'][$i]['status'] == "on"){
- $checked = " checked ";
- $onoff = "off";
- } else {
- $checked = "";
- $onoff = "on";
- }
- print "<label>\n";
- print "<form action=\"$base\" method=\"get\">\n";
- print "<input type=\"hidden\" name=\"type\" value=\"gp\">\n";
- print "<input type=\"hidden\" name=\"a\" value=\"set\">\n";
- print "<input type=\"hidden\" name=\"id\" value=\"$i\">\n";
- print "<input type=\"hidden\" name=\"ae\" value=\"$onoff\">\n";
- print "<input type=\"checkbox\" class=\"ios-switch\" $checked onclick=\"this.form.submit();\" /><div><div></div></div>\n";
- print "</form>\n";
- print "</label>\n";
- print "</td></tr>\n";
- }
- print "</table>\n";
- }
- function build_page($data,$base,$settings){
- // Display Header
- display_header($data,$settings);
- // Display Scenes
- if($settings['scenes'] == 1){ display_scenes_data($data,$base,$settings); }
- // Display Light Switches
- if($settings['lights'] == 1){ print "<div id=\"lightsRefresh\">\n</div>\n"; }
- // Display light switches and scenes for test (HTML debug only)
- if($settings['debug'] == 1 && $settings['lights'] == 1){ display_lights_data($data,$base,$settings); }
- if($settings['debug'] == 1 && $settings['scenes'] == 1){ display_scenes_data($data,$base,$settings); }
- display_footer($data,$settings);
- }
- ### Start the actual processing
- $action = decode_url($_GET);
- $url = "$hw/get-sensors";
- $data = hw_exec($url);
- switch ($action){
- case "lightstatus":
- display_lights_data($data,$base,$settings);
- break;
- case "scenestatus":
- display_scenes_data($data,$base,$settings);
- break;
- case "display":
- build_page($data,$base,$settings);
- break;
- case "set":
- $v_id = $_GET[id];
- $v_ae = $_GET[ae];
- $v_type = $_GET[type];
- $v_dimmer = $settings['dimvalue'];
- if($v_ae == "on" && $v_type == "gp" ){
- $url = "$hw/$v_type/on";
- }
- if($v_ae == "on" && $v_type == "sw" && $data['response']['switches'][$v_id][type] == "dimmer") {
- $url = "$hw/$v_type/dim/$v_id/$v_dimmer";
- } else {
- $url = "$hw/$v_type/$v_id/$v_ae";
- }
- if($settings['debug'] == 0){ $data = hw_exec($url); } else { print $url; }
- $url = "$hw/get-sensors";
- $data = hw_exec($url);
- build_page($data,$base,$settings);
- break;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement