Thu, 25 Apr 2024 16:44:54 +0200
Version 0.9.19b1. Less noisy logging.
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 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | if (isset($_GET["uuid"])) |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | $uuid = $_GET["uuid"]; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | else |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | $uuid = "08eb9507-8b1a-42cc-ac66-9ffcf864dc0c"; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | $answer = send_cmd("DEVICE JSON ".$uuid); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | header("Content-type: application/json"); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | $arr = explode("\r\n", $answer); |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | if (startsWith($arr[0], "213")) { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | echo $arr[1]; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | } else { |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | echo '{}'; |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | } |
1f81e52c5abf
Minor details for server DEVICE commands. Added getdevice<s> php scripts.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 |