Subversion Repositories HomeAutomation

Rev

Rev 1872 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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