Subversion Repositories HomeAutomation

Rev

Rev 1872 | Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1. <?
  2. $aliases = array("sovrum", "bardisk");
  3.  
  4.  
  5. function atomd_connect($host, $port)
  6. {
  7.   $socket = pfsockopen("127.0.0.1", 1202, $errno, $errstr);
  8.  
  9.   if (!$socket)
  10.   {
  11.     throw new Exception("$errstr ($errno)");
  12.   }
  13.  
  14.   return $socket;
  15. }
  16.  
  17. function atomd_read_packet($socket)
  18. {
  19.   if (feof($socket))
  20.   {
  21.     throw new Exception("socket not open");
  22.   }
  23.  
  24.   $command = fread($socket, 4);
  25.  
  26.   if ($command === FALSE)
  27.   {
  28.     throw new Exception("got EOF while reading command from socket");
  29.   }
  30.  
  31.   $payload_length = fread($socket, 4);
  32.  
  33.   if ($payload_length === FALSE)
  34.   {
  35.     throw new Exception("got EOF while reading payload_length from socket");
  36.   }
  37.  
  38.   $payload = "";
  39.  
  40.   if ($payload_length > 0)
  41.   {
  42.     $payload = fread($socket, intval($payload_length));
  43.    
  44.     if ($payload === FALSE)
  45.     {
  46.       throw new Exception("got EOF while reading payload from socket");
  47.     }
  48.   }
  49.  
  50.   return $command . $payload_length . $payload;
  51. }
  52.  
  53. function atomd_write_packet($socket, $command, $payload)
  54. {
  55.   if (feof($socket))
  56.   {
  57.     throw new Exception("socket not open");
  58.   }
  59.  
  60.   $packet = $command . str_pad(strlen($payload) + 1, 4, "0", STR_PAD_LEFT) . $payload . "\0";
  61.  
  62.   if (fwrite($socket, $packet) === FALSE)
  63.   {
  64.      throw new Exception("got error while writing payload to socket");
  65.   }
  66. }
  67.  
  68. function atomd_data_available($socket)
  69. {
  70.   $read   = array($socket);
  71.   $write  = NULL;
  72.   $except = NULL;
  73.  
  74.   if (false === ($num_changed_streams = stream_select($read, $write, $except, 0)))
  75.   {
  76.     throw new Exception("could not do select on socket");
  77.   }
  78.  
  79.   return $num_changed_streams > 0;
  80.   /*
  81.  
  82.  
  83.   $position = ftell($socket);
  84.  
  85.   fseek($socket, -1, SEEK_END);
  86.  
  87.   $has_data = ftell($socket) > $position;
  88.  
  89.   fseek($socket, $position, SEEK_SET);
  90.  
  91.   return $has_data;*/
  92. }
  93.  
  94.  
  95.  
  96. function atomd_initialize($host, $port)
  97. {
  98.   $socket = atomd_connect("127.0.0.1", 1202);
  99.  
  100.   while (atomd_data_available($socket))
  101.   {
  102.     $packet = atomd_read_packet($socket); // Read initial prompt
  103.   }
  104.  
  105.   return $socket;
  106. }
  107.  
  108. function atomd_send_command($socket, $command)
  109. {
  110.   atomd_write_packet($socket, "RESP", $command);
  111. }
  112.  
  113. function atomd_read_command_response($socket)
  114. {
  115.   $response = "";
  116.  
  117.   while (true)
  118.   {
  119.     $packet = atomd_read_packet($socket);
  120.    
  121.     if (substr($packet, 0, 4) != "TEXT")
  122.     {
  123.       break;
  124.     }
  125.    
  126.     $response .= substr($packet, 8) . "\n";
  127.   }
  128.  
  129.   return $response;
  130. }
  131.  
  132.  
  133.  
  134. function GetCurrentValue($alias2)
  135. {
  136.   $level2 = 0;
  137.   $buffer = "";
  138.  
  139.   try
  140.   {
  141.     $socket = atomd_initialize("127.0.0.1", 1202);
  142.  
  143.     atomd_send_command($socket, "Module_GetLastValue $alias2");
  144.    
  145.     $buffer = atomd_read_command_response($socket);
  146.   }
  147.   catch (Exception $e)
  148.   {
  149.     die($e);
  150.   }
  151.  
  152.   $output = explode("\n", $buffer);
  153.  
  154.   foreach ($output as $line)
  155.   {
  156.     if (strpos($line, "Level: ") !== FALSE)
  157.     {
  158.       list($dummy1, $level2) = explode(" ", $line);
  159.      
  160.       $start = strpos($level2, "m") + 1;
  161.      
  162.       $level2 = substr($level2, $start, strpos($level2, "[", $start) - $start);
  163.     }
  164.   }
  165.  
  166.   return intval($level2);
  167. }
  168.  
  169. function SetCurrentValue($alias2, $level)
  170. {
  171.   $level2 = 0;
  172.   $buffer = "";
  173.  
  174.   try
  175.   {
  176.     $socket = atomd_initialize("127.0.0.1", 1202);
  177.  
  178.     atomd_send_command($socket, "Dimmer_AbsoluteFade $alias2 255 $level");
  179.    
  180.     $buffer = atomd_read_command_response($socket);
  181.   }
  182.   catch (Exception $e)
  183.   {
  184.     die($e);
  185.   }
  186. }
  187.  
  188. $response = array();
  189.  
  190.  
  191. if (isset($_GET["strength"]))
  192. {
  193.   if (intval($_GET["strength"]) < 10)
  194.   {
  195.     $_GET["strength"] = 0;
  196.   }
  197.  
  198.   SetCurrentValue($_GET["alias"], $_GET["strength"]);
  199.  
  200.   echo json_encode($response);
  201.   exit(0);
  202. }
  203. else if (isset($_GET["getvalue"]))
  204. {
  205.   foreach ($aliases as $alias)
  206.   {
  207.     $response[$alias] = GetCurrentValue($alias);
  208.   }
  209.  
  210.   echo json_encode($response);
  211.   exit(0);
  212. }
  213.  
  214.  
  215.  
  216.  
  217. ?>
  218.  
  219.  
  220. <!DOCTYPE html>
  221. <html>
  222. <head>
  223.   <meta charset="utf-8">
  224.   <meta name="viewport" content="width=device-width, initial-scale=1">
  225.   <title>Light switch</title>
  226.   <link rel="stylesheet"  href="jquery.mobile-1.0b1.css" />
  227.   <script src="jquery-1.6.1.min.js"></script>
  228.   <script src="jquery.mobile-1.0b1.min.js"></script>
  229.  
  230.   <script>
  231.  
  232.     $(document).bind("pagecreate", function(event, ui)
  233.     {
  234.       <?
  235.       foreach ($aliases as $alias)
  236.       {
  237.       ?>
  238.         $('#slider<?=$alias?>' ).bind('change', function(event, ui){ makeAjaxChange("<?=$alias?>"); });
  239.       <?
  240.       }
  241.       ?>
  242.       delayed_start_of_poll();
  243.     });
  244.  
  245.     var current_value = {};
  246.     var next_value = {};
  247.    
  248.     <?
  249.     foreach ($aliases as $alias)
  250.     {
  251.     ?>
  252.       current_value['<?=$alias?>'] = 0;
  253.       next_value['<?=$alias?>'] = -1;
  254.     <?
  255.     }
  256.     ?>
  257.     var updating_current = false;
  258.     var timer = null;
  259.  
  260.     function checkCurrentValue()
  261.     {
  262.       updating_current = true;
  263.       jQuery.getJSON("?getvalue=1", "", function(data)
  264.       {
  265.        
  266.        
  267.         jQuery.each(data, function(alias, value)
  268.         {
  269.           current_value[alias] = value;
  270.           $('#slider' + alias).val(value).slider("refresh");
  271.         });
  272.        
  273.         updating_current = false;
  274.       });
  275.     }
  276.  
  277.     function makeAjaxChange(alias)
  278.     {
  279.       if (updating_current)
  280.       {
  281.         return;
  282.       }
  283.    
  284.       if ($('#slider' + alias).val() == current_value)
  285.       {
  286.         return;
  287.       }
  288.      
  289.       if (next_value[alias] == -1) // Nothing in progress, do a call
  290.       {
  291.         do_call(alias, $('#slider' + alias).val());
  292.       }
  293.       else
  294.       {
  295.         next_value[alias] = $('#slider' + alias).val();
  296.       }
  297.       // console.log($('#slider').val());
  298.     }
  299.    
  300.     function do_call(alias, strength)
  301.     {
  302.       next_value[alias] = strength;
  303.      
  304.       //clearInterval(timer);
  305.       timer = null;
  306.      
  307.       jQuery.get("?alias=" + alias + "&strength=" + strength, "", function()
  308.       {
  309.         current_value[alias] = strength;
  310.        
  311.         if (next_value[alias] != current_value[alias])
  312.         {
  313.           do_call(alias, next_value[alias]);
  314.         }
  315.         else
  316.         {
  317.           //timer = setTimeout(delayed_start_of_poll, 3000);
  318.           next_value[alias] = -1;
  319.         }
  320.       });
  321.     }
  322.    
  323.     function delayed_start_of_poll()
  324.     {
  325.       timer = setTimeout(checkCurrentValue, 1000);
  326.     }
  327.  
  328.   </script>
  329.  
  330. </head>
  331. <body>
  332. <div data-role="page" id="jqm-home" class="type-home" data-theme="a">
  333.   <div data-role="content">
  334.    
  335.      <?
  336.     foreach ($aliases as $alias)
  337.     {
  338.     ?>
  339.       <div data-role="fieldcontain" id="slider-container">
  340.         <label for="slider<?=$alias?>"><?=ucfirst($alias)?>:</label>
  341.         <input type="range" name="slider<?=$alias?>" id="slider<?=$alias?>" value="<?=intval($level)?>" min="0" max="255"  />
  342.       </div>
  343.     <?
  344.     }
  345.     ?>
  346.    
  347.  
  348.   </div>
  349.  
  350. </div>
  351. </body>
  352. </html>