Subversion Repositories HomeAutomation

Rev

Rev 1883 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1883 Rev 2051
Line 11... Line 11...
11
    throw new Exception("$errstr ($errno)");
11
    throw new Exception("$errstr ($errno)");
12
  }
12
  }
13
 
13
 
14
  return $socket;
14
  return $socket;
15
}
15
}
16
 
-
 
17
 
16
 
18
function atomd_read_packet($socket)
17
function atomd_read_packet($socket)
19
{
18
{
20
  if (feof($socket))
19
  if (feof($socket))
21
  {
20
  {
22
    throw new Exception("socket not open");
21
    throw new Exception("socket not open");
23
  }
22
  }
24
 
23
 
25
  $command = fread($socket, 4);
24
  $command = fread($socket, 4);
26
 
25
 
27
  if ($command === FALSE)
26
  if ($command === FALSE)
28
  {
27
  {
29
    throw new Exception("got EOF while reading command from socket");
28
    throw new Exception("got EOF while reading command from socket");
30
  }
29
  }
31
 
30
 
32
  $payload_length = fread($socket, 4);
31
  $payload_length = fread($socket, 4);
33
 
32
 
34
  if ($payload_length === FALSE)
33
  if ($payload_length === FALSE)
35
  {
34
  {
Line 52... Line 51...
52
}
51
}
53
 
52
 
54
function atomd_write_packet($socket, $command, $payload)
53
function atomd_write_packet($socket, $command, $payload)
55
{
54
{
56
  if (feof($socket))
55
  if (feof($socket))
57
  {
56
  {
58
    throw new Exception("socket not open");
57
    throw new Exception("socket not open");
59
  }
58
  }
60
 
59
 
61
  $packet = $command . str_pad(strlen($payload) + 1, 4, "0", STR_PAD_LEFT) . $payload . "\0";
60
  $packet = $command . str_pad(strlen($payload) + 1, 4, "0", STR_PAD_LEFT) . $payload . "\0";
62
 
61
 
63
  if (fwrite($socket, $packet) === FALSE)
62
  if (fwrite($socket, $packet) === FALSE)
64
  {
63
  {
65
     throw new Exception("got error while writing payload to socket");
64
     throw new Exception("got error while writing payload to socket");
66
  }
65
  }
67
}
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
/*
68
 
95
 
69
function atomd_data_available($socket)
96
function atomd_data_available($socket)
70
{
97
{
71
  $position = ftell($socket);
98
  $position = ftell($socket);
72
 
99
 
73
  fseek($socket, -1, SEEK_END);
100
  fseek($socket, -1, SEEK_END);
74
 
101
 
75
  $has_data = ftell($socket) > $position;
102
  $has_data = ftell($socket) > $position;
76
 
103
 
77
  fseek($socket, $position, SEEK_SET);
104
  fseek($socket, $position, SEEK_SET);
78
 
105
 
79
  return $has_data;
106
  return $has_data;
80
}
107
}
81
 
-
 
82
 
108
*/
83
 
109
 
84
function atomd_initialize($host, $port)
110
function atomd_initialize($host, $port)
85
{
111
{
86
  $socket = atomd_connect($host, $port);
112
  $socket = atomd_connect($host, $port);
87
 
113
 
Line 92... Line 118...
92
 
118
 
93
  return $socket;
119
  return $socket;
94
}
120
}
95
 
121
 
96
function atomd_send_command($socket, $command)
122
function atomd_send_command($socket, $command)
97
{
123
{
98
  atomd_write_packet($socket, "RESP", $command);
124
  atomd_write_packet($socket, "RESP", $command);
99
}
125
}
-
 
126
 
100
 
127
 
101
function atomd_read_command_response($socket)
128
function atomd_read_command_response($socket)
102
{
129
{
103
  $response = "";
130
  $response = "";
104
 
131
 
Line 113... Line 140...
113
   
140
   
114
    $response .= substr($packet, 8) . "\n";
141
    $response .= substr($packet, 8) . "\n";
115
  }
142
  }
116
 
143
 
117
  return $response;
144
  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
}
145
}
132
 
146
 
133
 
147
 
134
/*
148
/*
135
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
149
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);