Subversion Repositories HomeAutomation

Rev

Rev 1883 | Rev 2054 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1883 Rev 2051
Line 1... Line 1...
1
<?
1
<?php
2
$aliases = array("sovrum", "bardisk");
2
$aliasesDimmer = array("TakSovrum", "bardisk", "bardisk_tak", "bardisk_tv");
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);
3
$aliasesPower = array("Power", "PowerBRF");
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
   
4
 
180
    $buffer = atomd_read_command_response($socket);
5
require 'atom_interface.php';
181
  }
-
 
182
  catch (Exception $e)
6
require 'getSetData.php';
183
  {
-
 
184
    die($e);
-
 
185
  }
-
 
186
}
-
 
187
 
7
 
188
$response = array();
8
$response = array();
189
 
9
 
190
 
-
 
191
if (isset($_GET["strength"]))
10
if (isset($_GET["DimmerStrength"]))
192
{
11
{
193
  if (intval($_GET["strength"]) < 10)
12
  if (intval($_GET["strength"]) < 10)
194
  {
13
  {
195
    $_GET["strength"] = 0;
14
    $_GET["strength"] = 0;
196
  }
15
  }
197
 
16
 
198
  SetCurrentValue($_GET["alias"], $_GET["strength"]);
17
  SetDimmerValue($_GET["alias"], $_GET["strength"]);
199
 
18
 
200
  echo json_encode($response);
19
  echo json_encode($response);
201
  exit(0);
20
  exit(0);
202
}
21
}
203
else if (isset($_GET["getvalue"]))
22
else if (isset($_GET["getvalue"]))
204
{
23
{
205
  foreach ($aliases as $alias)
24
  foreach ($aliasesDimmer as $alias)
206
  {
25
  {
207
    $response[$alias] = GetCurrentValue($alias);
26
    $response[$alias] = GetLastValue($alias);
-
 
27
  }
-
 
28
 
-
 
29
  echo json_encode($response);
-
 
30
  exit(0);
-
 
31
}
-
 
32
else if (isset($_GET["getdata"]))
-
 
33
{
-
 
34
  foreach ($aliasesDimmer as $alias)
-
 
35
  {
-
 
36
    $response[$alias] = GetLastData($alias);
208
  }
37
  }
209
 
38
 
210
  echo json_encode($response);
39
  echo json_encode($response);
211
  exit(0);
40
  exit(0);
212
}
41
}
213
 
-
 
214
 
-
 
215
 
-
 
216
 
-
 
217
?>
42
?>
218
 
43
 
219
 
44
 
220
<!DOCTYPE html>
45
<!DOCTYPE html>
221
<html>
46
<html>
222
<head>
47
<head>
223
  <meta charset="utf-8">
48
  <meta charset="utf-8">
224
  <meta name="viewport" content="width=device-width, initial-scale=1">
49
  <meta name="viewport" content="width=device-width, initial-scale=1">
225
  <title>Light switch</title>
50
  <title>Light switch</title>
-
 
51
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
-
 
52
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
-
 
53
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
-
 
54
<?
226
  <link rel="stylesheet"  href="jquery.mobile-1.0b1.css" />
55
//  <link rel="stylesheet"  href="jquery.mobile-1.0b1.css" /> 
227
  <script src="jquery-1.6.1.min.js"></script>
56
//  <script src="jquery-1.6.1.min.js"></script> 
228
  <script src="jquery.mobile-1.0b1.min.js"></script>
57
//  <script src="jquery.mobile-1.0b1.min.js"></script> 
229
 
58
?>
230
  <script>
59
  <script>
231
 
60
 
232
    $(document).bind("pagecreate", function(event, ui)
61
    $(document).bind("pagecreate", function(event, ui)
233
    {
62
    {
234
      <?
63
      <?
235
      foreach ($aliases as $alias)
64
      foreach ($aliasesDimmer as $alias)
236
      {
65
      {
237
      ?>
66
      ?>
238
        $('#slider<?=$alias?>' ).bind('change', function(event, ui){ makeAjaxChange("<?=$alias?>"); });
67
        $('#slider<?=$alias?>' ).bind('change', function(event, ui){ makeAjaxChange("<?=$alias?>"); });
239
      <?
68
      <?
240
      }
69
      }
Line 244... Line 73...
244
 
73
 
245
    var current_value = {};
74
    var current_value = {};
246
    var next_value = {};
75
    var next_value = {};
247
   
76
   
248
    <?
77
    <?
249
    foreach ($aliases as $alias)
78
    foreach ($aliasesDimmer as $alias)
250
    {
79
    {
251
    ?>
80
    ?>
252
      current_value['<?=$alias?>'] = 0;
81
      current_value['<?=$alias?>'] = 0;
253
      next_value['<?=$alias?>'] = -1;
82
      next_value['<?=$alias?>'] = -1;
254
    <?
83
    <?
Line 270... Line 99...
270
          $('#slider' + alias).val(value).slider("refresh");
99
          $('#slider' + alias).val(value).slider("refresh");
271
        });
100
        });
272
       
101
       
273
        updating_current = false;
102
        updating_current = false;
274
      });
103
      });
-
 
104
      timer = setTimeout(checkCurrentValue, 1000);
275
    }
105
    }
276
 
106
 
277
    function makeAjaxChange(alias)
107
    function makeAjaxChange(alias)
278
    {
108
    {
279
      if (updating_current)
109
      if (updating_current)
280
      {
110
      {
281
        return;
111
        return;
282
      }
112
      }
283
   
113
   
Line 299... Line 129...
299
   
129
   
300
    function do_call(alias, strength)
130
    function do_call(alias, strength)
301
    {
131
    {
302
      next_value[alias] = strength;
132
      next_value[alias] = strength;
303
     
133
     
304
      //clearInterval(timer);
134
      clearInterval(timer);
305
      timer = null;
135
      timer = null;
306
     
136
     
307
      jQuery.get("?alias=" + alias + "&strength=" + strength, "", function()
137
      jQuery.get("?alias=" + alias + "&DimmerStrength=" + strength, "", function()
308
      {
138
      {
309
        current_value[alias] = strength;
139
        current_value[alias] = strength;
310
       
140
       
311
        if (next_value[alias] != current_value[alias])
141
        if (next_value[alias] != current_value[alias])
312
        {
142
        {
313
          do_call(alias, next_value[alias]);
143
          do_call(alias, next_value[alias]);
314
        }
144
        }
315
        else
145
        else
316
        {
146
        {
317
          //timer = setTimeout(delayed_start_of_poll, 3000);
147
          timer = setTimeout(delayed_start_of_poll, 3000);
318
          next_value[alias] = -1;
148
          next_value[alias] = -1;
319
        }
149
        }
320
      });
150
      });
321
    }
151
    }
322
   
152
   
Line 328... Line 158...
328
  </script>
158
  </script>
329
 
159
 
330
</head>
160
</head>
331
<body>
161
<body>
332
<div data-role="page" id="jqm-home" class="type-home" data-theme="a">
162
  <div data-role="page" id="jqm-home" class="type-home" data-theme="a">
-
 
163
    <div data-role="header" data-position="inline">
-
 
164
        <a href="index.html" data-icon="delete">Cancel</a>
-
 
165
        <h1>Testar</h1>
-
 
166
        <a href="index.html" data-icon="check">Save</a>
-
 
167
    </div>
333
  <div data-role="content">
168
    <div data-role="content">
-
 
169
      <div data-role="collapsible-set" data-theme="" data-content-theme="">
-
 
170
    <div data-role="collapsible" data-collapsed="true">
334
   
171
      <h3>
-
 
172
        Dimmers
-
 
173
      </h3>
335
     <?
174
      <?
336
    foreach ($aliases as $alias)
175
      foreach ($aliasesDimmer as $alias)
337
    {
176
      {
338
    ?>
177
      ?>
339
      <div data-role="fieldcontain" id="slider-container">
178
      <div data-role="fieldcontain" id="slider-container">
340
        <label for="slider<?=$alias?>"><?=ucfirst($alias)?>:</label>
179
        <label for="slider<?=$alias?>"><?=ucfirst($alias)?>:</label>
341
        <input type="range" name="slider<?=$alias?>" id="slider<?=$alias?>" value="<?=intval($level)?>" min="0" max="255"  />
180
        <input type="range" name="slider<?=$alias?>" id="slider<?=$alias?>" value="<?=intval($level)?>" min="0" max="255"  />
342
      </div>
181
      </div>
-
 
182
 
-
 
183
      <div style="">
-
 
184
 
-
 
185
      </div>
343
    <?
186
      <?
-
 
187
      }
-
 
188
      ?>
-
 
189
    </div>
-
 
190
      </div>
-
 
191
      <div data-role="collapsible-set" data-theme="" data-content-theme="">
-
 
192
    <div data-role="collapsible" data-collapsed="true">
-
 
193
      <h3>
-
 
194
        Power
-
 
195
      </h3>
-
 
196
      <?
-
 
197
      foreach ($aliasesPower as $alias)
-
 
198
      {
-
 
199
      ?>
-
 
200
      <div data-role="fieldcontain" id="slider-container">
-
 
201
        <label for="slider<?=$alias?>"><?=ucfirst($alias)?>:</label>
-
 
202
      </div>
-
 
203
 
-
 
204
      <div style="">
-
 
205
 
-
 
206
      </div>
-
 
207
      <?
344
    }
208
      }
345
    ?>
209
      ?>
346
   
-
 
347
 
-
 
348
  </div>
210
    </div>
-
 
211
      </div>
-
 
212
      <div data-role="collapsible-set" data-theme="" data-content-theme="">
-
 
213
    <div data-role="collapsible" data-collapsed="true">
-
 
214
      <h3>
-
 
215
        Ute temperatur
-
 
216
      </h3>
349
 
217
<?
-
 
218
//    <div style="width: 997px; height: 519px; position: relative; background-color: #fbfbfb; border: 1px solid #b8b8b8;">
-
 
219
//      <img style=" top: 50%; left: 50%; " src="http://linuxz-home.mine.nu/CANgraph/outside_day.png" />
-
 
220
//    </div>
-
 
221
?>
-
 
222
    </div>
-
 
223
      </div>
-
 
224
    </div>
350
</div>
225
  </div>
351
</body>
226
</body>
352
</html>
227
</html>