Subversion Repositories HomeAutomation

Rev

Rev 1880 | Blame | Last modification | View Log | SVN | RSS feed

  1. <?
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. function atomd_connect($host, $port)
  6. {
  7.   $socket = pfsockopen($host, $port, $errno, $errstr);
  8.  
  9.   if (!$socket)
  10.   {
  11.     throw new Exception("$errstr ($errno)");
  12.   }
  13.  
  14.   return $socket;
  15. }
  16.  
  17.  
  18. function atomd_read_packet($socket)
  19. {
  20.   if (feof($socket))
  21.   {
  22.     throw new Exception("socket not open");
  23.   }
  24.  
  25.   $command = fread($socket, 4);
  26.  
  27.   if ($command === FALSE)
  28.   {
  29.     throw new Exception("got EOF while reading command from socket");
  30.   }
  31.  
  32.   $payload_length = fread($socket, 4);
  33.  
  34.   if ($payload_length === FALSE)
  35.   {
  36.     throw new Exception("got EOF while reading payload_length from socket");
  37.   }
  38.  
  39.   $payload = "";
  40.  
  41.   if ($payload_length > 0)
  42.   {
  43.     $payload = fread($socket, intval($payload_length));
  44.    
  45.     if ($payload === FALSE)
  46.     {
  47.       throw new Exception("got EOF while reading payload from socket");
  48.     }
  49.   }
  50.  
  51.   return $command . $payload_length . $payload;
  52. }
  53.  
  54. function atomd_write_packet($socket, $command, $payload)
  55. {
  56.   if (feof($socket))
  57.   {
  58.     throw new Exception("socket not open");
  59.   }
  60.  
  61.   $packet = $command . str_pad(strlen($payload) + 1, 4, "0", STR_PAD_LEFT) . $payload . "\0";
  62.  
  63.   if (fwrite($socket, $packet) === FALSE)
  64.   {
  65.      throw new Exception("got error while writing payload to socket");
  66.   }
  67. }
  68.  
  69. function atomd_data_available($socket)
  70. {
  71.   $position = ftell($socket);
  72.  
  73.   fseek($socket, -1, SEEK_END);
  74.  
  75.   $has_data = ftell($socket) > $position;
  76.  
  77.   fseek($socket, $position, SEEK_SET);
  78.  
  79.   return $has_data;
  80. }
  81.  
  82.  
  83.  
  84. function atomd_initialize($host, $port)
  85. {
  86.   $socket = atomd_connect($host, $port);
  87.  
  88.   while (atomd_data_available($socket))
  89.   {
  90.     $packet = atomd_read_packet($socket); // Read initial prompt
  91.   }
  92.  
  93.   return $socket;
  94. }
  95.  
  96. function atomd_send_command($socket, $command)
  97. {
  98.   atomd_write_packet($socket, "RESP", $command);
  99. }
  100.  
  101. function atomd_read_command_response($socket)
  102. {
  103.   $response = "";
  104.  
  105.   while (true)
  106.   {
  107.     $packet = atomd_read_packet($socket);
  108.    
  109.     if (substr($packet, 0, 4) != "TEXT")
  110.     {
  111.       break;
  112.     }
  113.    
  114.     $response .= substr($packet, 8) . "\n";
  115.   }
  116.  
  117.   return $response;
  118. }
  119.  
  120. try
  121. {
  122.   $socket = atomd_initialize("127.0.0.1", 1202);
  123.  
  124.   atomd_send_command($socket, "Module_GetLastValue bokhylla");
  125.  
  126.   echo atomd_read_command_response($socket);
  127. }
  128. catch (Exception $e)
  129. {
  130.   die($e);
  131. }
  132.  
  133.  
  134. /*
  135. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  136.  
  137. if ($socket === false)
  138. {
  139.   die("Socket Creation Failed");
  140. }
  141.  
  142. $result = socket_connect($socket, "127.0.0.1", 1202);
  143.  
  144. if ($result === false)
  145. {
  146.   die("Connection Failed");
  147. }
  148.  
  149.  
  150. $command = "help";
  151. $length = strlen($command) + 1;
  152.  
  153. $data = "RESP" . str_pad($length, 4, "0", STR_PAD_LEFT) . $command . "\0\n";
  154.  
  155. echo "Sending:" . $data . "<br/>";
  156.  
  157. socket_write($socket, $data, strlen($data));
  158.  
  159. do
  160. {
  161.   $line = socket_read($socket, 1024, PHP_BINARY_READ);
  162.   $output .= $line;
  163. }
  164. while ($line != "");
  165.  
  166. echo $output . "<br/>";
  167. $output = "";
  168.  
  169. echo "Done first read!<br/>";
  170.  
  171. do
  172. {
  173.   $line = socket_read($socket, 1024, PHP_BINARY_READ);
  174.   $output .= $line;
  175. }
  176. while ($line != "");
  177.  
  178. echo $output . "<br/>";
  179. $output = "";
  180.  
  181. echo "Done!<br/>";
  182.  
  183. socket_close($socket);
  184. */
  185. ?>
  186.