Tue, 23 Apr 2024 16:52:13 +0200
Added Add new device.
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 | ||
59 | } 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
|
60 | |
699 | 61 | $answer = send_cmd("DEVICE ADD " . $_POST['type']); |
62 | $arr = explode("\r\n", $answer); | |
63 | if (! startsWith($arr[0], "211")) { | |
64 | $response['error'] = true; | |
65 | $response['msg'] = $arr[0]; | |
66 | } | |
67 | exit(json_encode($response)); | |
68 | ||
69 | } else if (isset($_POST['del'])) { | |
70 | ||
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | } else { |
699 | 72 | |
73 | $answer = send_cmd("DEVICE JSON"); | |
74 | header("Content-type: application/json"); | |
75 | ||
76 | $arr = explode("\r\n", $answer); | |
77 | if (startsWith($arr[0], "212")) { | |
78 | echo $arr[1]; | |
79 | } else { | |
80 | echo '{}'; | |
81 | } | |
681
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | |
699 | 84 | ?> |