www/getonewire.php

Wed, 01 May 2024 14:38:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 01 May 2024 14:38:37 +0200
changeset 715
f5d85af156ab
parent 696
fe042f9484ac
permissions
-rw-r--r--

Added device_present() function to easy update device present from one-wire and simulator devices. When a simulator temperature sensor present is changed, the device table is changed too. Controlling simulator relays is now for each simulator. The simulator runs under the state machine. If something changed in the running simulator, all data is broadcasted over websocket. Completed the web editor.

696
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 <?php
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 function open_socket()
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 if (!($sock === false)) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 if (socket_connect($sock, "localhost", 6554)) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 } else {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 socket_close($sock);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 return $sock;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 /**
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * @param string $command to send to the server.
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 * @return string with the complete reply from the
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 * server. This can be a multiline reply.
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 */
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 function send_cmd($command)
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 $sock = open_socket();
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 if ($sock == false) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 return "";
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 socket_write($sock, $command . "\r\n", 4096);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 $answer = "";
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 while (1) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 $line = socket_read($sock, 4096);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 if ($line === '')
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 break;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 $answer .= $line;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 socket_close($sock);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 return $answer;
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 function startsWith($haystack, $needle)
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 return !strncmp($haystack, $needle, strlen($needle));
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 $answer = send_cmd("ONEWIRE JSON");
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 header("Content-type: application/json");
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 $arr = explode("\r\n", $answer);
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 if (startsWith($arr[0], "212")) {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 echo $arr[1];
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 } else {
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 echo '{}';
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 }
fe042f9484ac Added web page to display the one-wire bus live status.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60

mercurial