Advertisement
daxy01

HomeWizard iPhone PHP script

Jan 4th, 2014
2,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.32 KB | None | 0 0
  1. <?
  2.  
  3. ##############################################################################################################################################################################
  4. #
  5. # Created by: Marcel Lemmen
  6. # Version: 1.0
  7. # Date: 04-01-2014
  8. # Contact: homewizard-php@daxy.net
  9. # Note: An HomeWizard is required (http://www.homewizard.nl)
  10. #
  11. # Changelog:
  12. # v1.0 - Initial release. Implemented Light switches and Scenes
  13. #
  14. # Open items:
  15. # - Dimmer support for lights
  16. # - Code cleanup (LOL... likely not going to happen)
  17. #
  18. #
  19. # Credits for borrowed code:
  20. # * hw_exec function is kindly borrowed from Felix: http://www.homewizards.nl/forum/viewtopic.php?f=7&t=15&start=20#p727
  21. # * Weather info is kindly collected from OpenWeatherMap: http://www.openweathermap.org
  22. # * iPhone size screenmap kindly borrowed from Tobias: http://stackoverflow.com/a/2963251
  23. # * iOS7 CSS kindly borrowed from Bandar: http://codepen.io/BandarRaffah/pen/ibwje
  24. # * iOS7 slider for dimmer kindly borrowed from Achim: http://codepen.io/hauptrolle/pen/yfejp
  25. # * jQuery for additional Javascript functionality: http://www.jquery.com
  26. # * Auto refresh of tables kindly borrowed from Robbert: http://stackoverflow.com/a/13324909
  27. # * Status images kindly collected from Clker: http://www.clker.com/
  28. #
  29. ##############################################################################################################################################################################
  30.  
  31. # Basic Settings:
  32.  
  33. $hw = "http://<HomeWizard IP>/<HomeWizard Password>"; // IP and password for your HomeWizard
  34. $base = "http://<websitehostname or IP>/<This File>"; // Exact URL where this script resides
  35. $settings['name'] = "HomeWizard"; // Name your HomeWizard, shown on page
  36. $settings['location'] = "Utrecht"; // Set your location for weather image, will display Amsterdam when city can't be found
  37. $settings['debug'] = 0; // Disables actual action, Enable debug-mode (HTML borders, shows additional lights and scenes for HTML debugging)
  38. $settings['lights'] = 1; // Enable lights section
  39. $settings['scenes'] = 1; // Enables scenes section
  40. $settings['dimvalue'] = 175; // Default value when dimmer is enabled. Value between 0-255
  41.  
  42. // Limit amount of scenes and lights to display.
  43. $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
  44. $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
  45.  
  46. ##############################################################################################################################################################################
  47. # NO EDITING SHOULD BE REQUIRED BELOW THIS LINE
  48. ##############################################################################################################################################################################
  49.  
  50. function hw_exec($jsonurl) {
  51. $data = null;
  52. try {
  53. $json = file_get_contents($jsonurl); // get info
  54. $data = json_decode($json,true); // convert csv to array
  55. } catch (Exception $e) { // jic if no response
  56. print $e->getMessage();
  57. }
  58.  
  59. return $data;
  60. }
  61.  
  62. function decode_url($url){
  63. // $action = 0;
  64. if (!isset($_GET['a'])) { $action = "display";
  65. } else {
  66. $action = $_GET['a'];
  67. }
  68. return $action;
  69. }
  70.  
  71. function display_weather_icon($settings){
  72. $loc = $settings['location'];
  73. $jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=$loc";
  74. $w_data = hw_exec($jsonurl);
  75.  
  76. 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);}
  77. $icon = $w_data['weather'][0]['icon'];
  78. print "<img SRC=\"http://openweathermap.org/img/w/$icon\"></IMG>\n";
  79. }
  80.  
  81. function display_header($data,$settings){
  82. print "<html>\n";
  83. print "<head>\n";
  84. print "<meta name=\"viewport\" content=\"initial-scale = 1.0,maximum-scale = 1.0\" />\n";
  85. print "<title>" . $settings['name'] . "</title>\n";
  86. print "</head>\n";
  87. print "<body>\n";
  88. print "<script src=\"http://code.jquery.com/jquery-latest.js\"></script>\n";
  89.  
  90. // ios7 style sheet
  91. print " <style>input[type=\"checkbox\"] { \n";
  92. print " position: absolute;\n";
  93. print " opacity: 0;\n";
  94. print "}\n";
  95. print "\n";
  96. print "/* Normal Track */\n";
  97. print "input[type=\"checkbox\"].ios-switch + div {\n";
  98. print " vertical-align: middle;\n";
  99. print " width: 40px; height: 20px;\n";
  100. print " border: 1px solid rgba(0,0,0,.4);\n";
  101. print " border-radius: 999px;\n";
  102. print " background-color: rgba(0, 0, 0, 0.1);\n";
  103. print " -webkit-transition-duration: .4s;\n";
  104. print " -webkit-transition-property: background-color, box-shadow;\n";
  105. print " box-shadow: inset 0 0 0 0px rgba(0,0,0,0.4);\n";
  106. print " margin: 15px 1.2em 15px 2.5em;\n";
  107. print "}\n";
  108. print "\n";
  109. print "/* Checked Track (Blue) */\n";
  110. print "input[type=\"checkbox\"].ios-switch:checked + div {\n";
  111. print " width: 40px;\n";
  112. print " background-position: 0 0;\n";
  113. print " background-color: #3b89ec;\n";
  114. print " border: 1px solid #0e62cd;\n";
  115. print " box-shadow: inset 0 0 0 10px rgba(59,137,259,1);\n";
  116. print "}\n";
  117. print "\n";
  118. print "/* Green Track */\n";
  119. print "input[type=\"checkbox\"].green.ios-switch:checked + div {\n";
  120. print " background-color: #00e359;\n";
  121. print " border: 1px solid rgba(0, 162, 63,1);\n";
  122. print " box-shadow: inset 0 0 0 10px rgba(0,227,89,1);\n";
  123. print "}\n";
  124. print "\n";
  125. print "/* Normal Knob */\n";
  126. print "input[type=\"checkbox\"].ios-switch + div > div {\n";
  127. print " float: left;\n";
  128. print " width: 18px; height: 18px;\n";
  129. print " border-radius: inherit;\n";
  130. print " background: #ffffff;\n";
  131. print " -webkit-transition-timing-function: cubic-bezier(.54,1.85,.5,1);\n";
  132. print " -webkit-transition-duration: 0.4s;\n";
  133. print " -webkit-transition-property: transform, background-color, box-shadow;\n";
  134. print " -moz-transition-timing-function: cubic-bezier(.54,1.85,.5,1);\n";
  135. print " -moz-transition-duration: 0.4s;\n";
  136. print " -moz-transition-property: transform, background-color;\n";
  137. print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(0, 0, 0, 0.4);\n";
  138. print " pointer-events: none;\n";
  139. print " margin-top: 1px;\n";
  140. print " margin-left: 1px;\n";
  141. print "}\n";
  142. print "\n";
  143. print "/* Checked Knob (Blue Style) */\n";
  144. print "input[type=\"checkbox\"].ios-switch:checked + div > div {\n";
  145. print " -webkit-transform: translate3d(20px, 0, 0);\n";
  146. print " -moz-transform: translate3d(20px, 0, 0);\n";
  147. print " background-color: #ffffff;\n";
  148. print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(8, 80, 172,1);\n";
  149. print "}\n";
  150. print "\n";
  151. print "/* Green Knob */\n";
  152. print "input[type=\"checkbox\"].green.ios-switch:checked + div > div {\n";
  153. print " box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(0, 162, 63,1);\n";
  154. print "}\n";
  155. print "\n";
  156. print "/* Needless Page Decoration */\n";
  157. 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";
  158. print "\n";
  159. print " <script>\n";
  160. print " window.open = function(){};\n";
  161. print " window. print = function(){};\n";
  162. print " // Support hover state for mobile.\n";
  163. print " if (false) {\n";
  164. print " window.ontouchstart = function(){};\n";
  165. print " }\n";
  166. print " </script>\n";
  167.  
  168. //auto refresh light status//
  169. print "<script>\n";
  170. print "$(function() {\n";
  171. print " startRefresh()\n";
  172. print "});\n";
  173. print " function startRefresh() {\n";
  174. print "setTimeout(startRefresh,20000);\n";
  175. print " $.get('$base?a=lightstatus', function(data) {\n";
  176. print " $('#lightsRefresh').html(data);\n";
  177. print " });\n";
  178. print " }\n";
  179. print "</script>\n";
  180.  
  181. // Display slider
  182. print "<script>\n";
  183. print "var inputs = document.getElementsByTagName('input');\n";
  184. print "inputs = Array.splice(inputs, 0);\n";
  185. print "inputs.forEach(function (item) {\n";
  186. print " if (item.type === 'range') {\n";
  187. print " item.onchange = function () {\n";
  188. print " value = (item.value - item.min)/(item.max - item.min)\n";
  189. print " item.style.backgroundImage = [\n";
  190. print " '-webkit-gradient(',\n";
  191. print " 'linear, ',\n";
  192. print " 'left top, ',\n";
  193. print " 'right top, ',\n";
  194. print " 'color-stop(' + value + ', blue), ',\n";
  195. print " 'color-stop(' + value + ', red)',\n";
  196. print " ')'\n";
  197. print " ].join('');\n";
  198. print " };\n";
  199. print " }\n";
  200. print "});\n";
  201. print "</script>\n";
  202.  
  203. if($data['status'] = "ok"){
  204. $status = "http://www.clker.com/cliparts/P/L/S/h/3/O/small-green-check-mark-th.png";
  205. } else {
  206. $status = "http://www.clker.com/cliparts/Q/A/W/c/p/q/small-red-x-mark-th.png";
  207. }
  208.  
  209. print "<div class=\"wrap\">\n";
  210. print "<h1> " . $settings['name'] . "</h1>\n";
  211.  
  212. if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
  213. print "<table $v_debug width=\"100%\">\n";
  214. print " <tr>\n";
  215. print "<td>Status: </td>\n";
  216. print "<td>Weer: </td>\n";
  217. print "<td>Temperatuur: </td>\n";
  218. print "</tr><tr>\n";
  219. print "<td align=\"center\"><img src=\"$status\" height=\"20\" width=\"20\"></img></td>\n";
  220. print "<td>";
  221. display_weather_icon($settings);
  222. print "</td>\n";
  223. print "<td>\n";
  224. display_thermometers_data($data,$settings);
  225. print "</td>\n";
  226. print "</tr></table>\n";
  227. print "<hr width=\"100%\">\n";
  228.  
  229. }
  230.  
  231. function display_footer($data,$settings){
  232. print "<hr width=\"100%\">\n";
  233. print "<h3>HomeWizard Version: " . $data['version'] . "</h3>\n";
  234. print "</body>\n</html>\n";
  235. }
  236.  
  237. function display_thermometers_data($data,$settings){
  238. if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
  239. print "<table $v_debug>\n";
  240. for($i=0; $i<count($data['response']['thermometers']); $i++){
  241. print "<tr><td width=100>";
  242. print $data['response']['thermometers'][$i]['name'] . "";
  243. print "</td><td>";
  244. print $data['response']['thermometers'][$i]['te'] . "C";
  245. print "</td></tr>\n";
  246. }
  247. print "</table>\n";
  248.  
  249. }
  250.  
  251. function display_lights_data($data,$base,$settings){
  252. print "Light switches: <BR>\n";
  253. if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
  254.  
  255. $v_count=count($data['response']['switches']) -1 ; // Minus one due to count starting at one, not zero
  256. if(!is_array($settings['lights2show'])) { $v_tmp = range(0,$v_count); } else { $v_tmp = $settings['lights2show']; }
  257.  
  258. if($settings['debug'] == 1){ print "Lights to display: "; print_r($v_tmp); }
  259.  
  260. print "<table $v_debug width=25%>\n";
  261. foreach ($v_tmp as &$i) {
  262. //for($i=0; $i<count($data['response']['switches']); $i++){
  263. print "<tr><td width=75%>";
  264. print $data['response']['switches'][$i]['name'] . "";
  265. print "</td><td>";
  266. // print $data['response']['switches'][$i]['status'] . "";
  267. // print "</td><td>";
  268. if ($data['response']['switches'][$i]['status'] == "on"){
  269. $checked = " checked ";
  270. $onoff = "off";
  271. } else {
  272. $checked = "";
  273. $onoff = "on";
  274. }
  275. if ($data['response']['switches'][$i]['type'] == "dimmer"){
  276. // print "<input type=range min=1 max=255 step=1 value=200>\n"; // Dimmer not yet implemented
  277. }
  278. print "<label>\n";
  279. print "<form action=\"$base\" method=\"get\">\n";
  280. print "<input type=\"hidden\" name=\"type\" value=\"sw\">\n";
  281. print "<input type=\"hidden\" name=\"a\" value=\"set\">\n";
  282. print "<input type=\"hidden\" name=\"id\" value=\"$i\">\n";
  283. print "<input type=\"hidden\" name=\"ae\" value=\"$onoff\">\n";
  284. print "<input type=\"checkbox\" class=\"ios-switch\" $checked onclick=\"this.form.submit();\" /><div><div></div></div>\n";
  285. print "</form>\n";
  286. print "</label>\n";
  287. print "</td></tr>\n";
  288. }
  289. print "</table>\n";
  290. }
  291.  
  292. function display_scenes_data($data,$base,$settings){
  293. print "Scene switches:<BR>\n";
  294. if($settings['debug'] == 1){ $v_debug = " border=1 ";} else { $v_debug = " border=0 ";}
  295.  
  296. $v_count=count($data['response']['scenes']) -1 ; // Minus one due to count starting at one, not zero
  297. if(!is_array($settings['scenes2show'])) { $v_tmp = range(0,$v_count); } else { $v_tmp = $settings['scenes2show']; }
  298.  
  299. if($settings['debug'] == 1){ print "Scenes to display: "; print_r($v_tmp); }
  300.  
  301. print "<table $v_debug width=25%>\n";
  302. foreach ($v_tmp as &$i) {
  303. print "<tr><td width=75%>";
  304. print $data['response']['scenes'][$i]['name'] . "";
  305. print "</td><td>";
  306. if ($data['response']['scenes'][$i]['status'] == "on"){
  307. $checked = " checked ";
  308. $onoff = "off";
  309. } else {
  310. $checked = "";
  311. $onoff = "on";
  312. }
  313. print "<label>\n";
  314. print "<form action=\"$base\" method=\"get\">\n";
  315. print "<input type=\"hidden\" name=\"type\" value=\"gp\">\n";
  316. print "<input type=\"hidden\" name=\"a\" value=\"set\">\n";
  317. print "<input type=\"hidden\" name=\"id\" value=\"$i\">\n";
  318. print "<input type=\"hidden\" name=\"ae\" value=\"$onoff\">\n";
  319. print "<input type=\"checkbox\" class=\"ios-switch\" $checked onclick=\"this.form.submit();\" /><div><div></div></div>\n";
  320. print "</form>\n";
  321. print "</label>\n";
  322. print "</td></tr>\n";
  323. }
  324. print "</table>\n";
  325.  
  326. }
  327.  
  328.  
  329. function build_page($data,$base,$settings){
  330.  
  331. // Display Header
  332. display_header($data,$settings);
  333.  
  334. // Display Scenes
  335. if($settings['scenes'] == 1){ display_scenes_data($data,$base,$settings); }
  336.  
  337. // Display Light Switches
  338. if($settings['lights'] == 1){ print "<div id=\"lightsRefresh\">\n</div>\n"; }
  339.  
  340. // Display light switches and scenes for test (HTML debug only)
  341. if($settings['debug'] == 1 && $settings['lights'] == 1){ display_lights_data($data,$base,$settings); }
  342. if($settings['debug'] == 1 && $settings['scenes'] == 1){ display_scenes_data($data,$base,$settings); }
  343.  
  344. display_footer($data,$settings);
  345. }
  346.  
  347.  
  348. ### Start the actual processing
  349.  
  350. $action = decode_url($_GET);
  351. $url = "$hw/get-sensors";
  352. $data = hw_exec($url);
  353.  
  354. switch ($action){
  355. case "lightstatus":
  356. display_lights_data($data,$base,$settings);
  357. break;
  358. case "scenestatus":
  359. display_scenes_data($data,$base,$settings);
  360. break;
  361. case "display":
  362. build_page($data,$base,$settings);
  363. break;
  364. case "set":
  365. $v_id = $_GET[id];
  366. $v_ae = $_GET[ae];
  367. $v_type = $_GET[type];
  368. $v_dimmer = $settings['dimvalue'];
  369.  
  370. if($v_ae == "on" && $v_type == "gp" ){
  371. $url = "$hw/$v_type/on";
  372. }
  373. if($v_ae == "on" && $v_type == "sw" && $data['response']['switches'][$v_id][type] == "dimmer") {
  374. $url = "$hw/$v_type/dim/$v_id/$v_dimmer";
  375. } else {
  376. $url = "$hw/$v_type/$v_id/$v_ae";
  377. }
  378.  
  379. if($settings['debug'] == 0){ $data = hw_exec($url); } else { print $url; }
  380. $url = "$hw/get-sensors";
  381. $data = hw_exec($url);
  382. build_page($data,$base,$settings);
  383. break;
  384. }
  385.  
  386. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement