Wed, 01 May 2024 14:38:37 +0200
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.
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | <?php |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | function open_socket() |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | if (!($sock === false)) { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | if (socket_connect($sock, "localhost", 6554)) { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0)); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | } else { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | socket_close($sock); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | return $sock; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | /** |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * @param string $command to send to the server. |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | * @return string with the complete reply from the |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | * server. This can be a multiline reply. |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | */ |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | function send_cmd($command) |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | $sock = open_socket(); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | if ($sock == false) { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | return ""; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | socket_write($sock, $command . "\r\n", 4096); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | $answer = ""; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | while (1) { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | $line = socket_read($sock, 4096); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | if ($line === '') |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | break; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | $answer .= $line; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | socket_close($sock); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | return $answer; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | function startsWith($haystack, $needle) |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | return !strncmp($haystack, $needle, strlen($needle)); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | |
699 | 51 | $response = array( |
52 | 'error' => false, | |
53 | 'msg' => 'Ok', | |
54 | ); | |
55 | ||
56 | ||
57 | if (isset($_POST['update'])) { | |
58 | ||
701 | 59 | $cmd = "DEVICE PUT " . $_POST['uuid'] . "\r\n"; |
60 | $cmd .= "TYPE," . $_POST['type'] . "\r\n"; | |
61 | $cmd .= "DIRECTION," . $_POST['direction'] . "\r\n"; | |
62 | $cmd .= "VALUE," . $_POST['value'] . "\r\n"; // Only for outputs | |
63 | $cmd .= "OFFSET," . $_POST['offset'] . "\r\n"; // Only analog | |
64 | $cmd .= "PRESENT," . $_POST['present'] . "\r\n"; | |
65 | $cmd .= "ADDRESS," . $_POST['address'] . "\r\n"; // Not for auto detected | |
66 | $cmd .= "SUBDEVICE," . $_POST['subdevice'] . "\r\n"; // Not for auto detected | |
67 | $cmd .= "GPIOPIN," . $_POST['gpiopin'] . "\r\n"; | |
68 | $cmd .= "DESCRIPTION," . $_POST['description'] . "\r\n"; | |
69 | $cmd .= "COMMENT," . $_POST['comment'] . "\r\n"; | |
70 | $cmd .= "."; | |
71 | $answer = send_cmd($cmd); | |
72 | $arr = explode("\r\n", $answer); | |
73 | if (! startsWith($arr[0], "219")) { | |
74 | $response['error'] = true; | |
75 | $response['msg'] = $arr[0]; | |
76 | } | |
77 | exit(json_encode($response)); | |
78 | ||
699 | 79 | } else if (isset($_POST['add'])) { |
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | |
699 | 81 | $answer = send_cmd("DEVICE ADD " . $_POST['type']); |
82 | $arr = explode("\r\n", $answer); | |
83 | if (! startsWith($arr[0], "211")) { | |
84 | $response['error'] = true; | |
85 | $response['msg'] = $arr[0]; | |
86 | } | |
87 | exit(json_encode($response)); | |
88 | ||
89 | } else if (isset($_POST['del'])) { | |
90 | ||
700 | 91 | $answer = send_cmd("DEVICE DEL " . $_POST['uuid']); |
92 | $arr = explode("\r\n", $answer); | |
93 | if (! startsWith($arr[0], "211")) { | |
94 | $response['error'] = true; | |
95 | $response['msg'] = $arr[0]; | |
96 | } | |
97 | exit(json_encode($response)); | |
98 | ||
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | } else { |
699 | 100 | |
101 | $answer = send_cmd("DEVICE JSON"); | |
102 | header("Content-type: application/json"); | |
103 | ||
104 | $arr = explode("\r\n", $answer); | |
105 | if (startsWith($arr[0], "212")) { | |
106 | echo $arr[1]; | |
107 | } else { | |
108 | echo '{}'; | |
109 | } | |
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | |
699 | 112 | ?> |