# HG changeset patch # User Michiel Broek # Date 1713277234 -7200 # Node ID 1f81e52c5abffe853b816bc73bd49977bc457888 # Parent 8b3c86124a08915e95ef3bdd09328e89e19bb88c Minor details for server DEVICE commands. Added getdevice php scripts. diff -r 8b3c86124a08 -r 1f81e52c5abf thermferm/server.c --- a/thermferm/server.c Tue Apr 16 16:03:47 2024 +0200 +++ b/thermferm/server.c Tue Apr 16 16:20:34 2024 +0200 @@ -327,6 +327,7 @@ srv_send(s, (char *)"DEVICE LIST List all devices"); srv_send(s, (char *)"DEVICE GET uuid Read device by uuid parameters"); srv_send(s, (char *)"DEVICE PUT uuid Write device by uuid parameters"); + srv_send(s, (char *)"DEVICE JSON Json list all or a single device"); srv_send(s, (char *)"."); return 0; } diff -r 8b3c86124a08 -r 1f81e52c5abf www/getdevice.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/getdevice.php Tue Apr 16 16:20:34 2024 +0200 @@ -0,0 +1,65 @@ + 15, 'usec' => 0)); + } else { + socket_close($sock); + } + } + return $sock; +} + + +/** + * @param string $command to send to the server. + * @return string with the complete reply from the + * server. This can be a multiline reply. + */ +function send_cmd($command) +{ + $sock = open_socket(); + if ($sock == false) { + return ""; + } + socket_write($sock, $command . "\r\n", 4096); + + $answer = ""; + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + $answer .= $line; + } + socket_close($sock); + + return $answer; +} + + +function startsWith($haystack, $needle) +{ + return !strncmp($haystack, $needle, strlen($needle)); +} + + +if (isset($_GET["uuid"])) + $uuid = $_GET["uuid"]; +else + $uuid = "08eb9507-8b1a-42cc-ac66-9ffcf864dc0c"; + +$answer = send_cmd("DEVICE JSON ".$uuid); +header("Content-type: application/json"); + +$arr = explode("\r\n", $answer); +if (startsWith($arr[0], "213")) { + echo $arr[1]; +} else { + echo '{}'; +} + diff -r 8b3c86124a08 -r 1f81e52c5abf www/getdevices.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/getdevices.php Tue Apr 16 16:20:34 2024 +0200 @@ -0,0 +1,60 @@ + 15, 'usec' => 0)); + } else { + socket_close($sock); + } + } + return $sock; +} + + +/** + * @param string $command to send to the server. + * @return string with the complete reply from the + * server. This can be a multiline reply. + */ +function send_cmd($command) +{ + $sock = open_socket(); + if ($sock == false) { + return ""; + } + socket_write($sock, $command . "\r\n", 4096); + + $answer = ""; + while (1) { + $line = socket_read($sock, 4096); + if ($line === '') + break; + $answer .= $line; + } + socket_close($sock); + + return $answer; +} + + +function startsWith($haystack, $needle) +{ + return !strncmp($haystack, $needle, strlen($needle)); +} + + +$answer = send_cmd("DEVICE JSON"); +header("Content-type: application/json"); + +$arr = explode("\r\n", $answer); +if (startsWith($arr[0], "212")) { + echo $arr[1]; +} else { + echo '{}'; +} + diff -r 8b3c86124a08 -r 1f81e52c5abf www/js/fermenter.js --- a/www/js/fermenter.js Tue Apr 16 16:03:47 2024 +0200 +++ b/www/js/fermenter.js Tue Apr 16 16:20:34 2024 +0200 @@ -162,7 +162,6 @@ }); function updateScreen() { - console.log('mode ' + record.mode + ' profile ' + record.profile_state + ' name ' + record.profile_name); $('#room_thb').html(global.room_temp + '°C  ' + global.room_hum + '% humidity'); $('#info_system').html(record.unit); $('#info_beer').html(record.beercode + ' - ' + record.beername); @@ -459,7 +458,6 @@ } if (obj.type == 'fermenter' && obj.unit == record.unit) { -// console.log('ws got this device ' + msg); record.beeruuid = obj.metric.product.uuid; record.beercode = obj.metric.product.code; record.beername = obj.metric.product.name; @@ -496,7 +494,7 @@ record.profile_inittemp_high = obj.metric.profile.inittemp.high; record.profile_inittemp_low = obj.metric.profile.inittemp.low; } else { - record.profile_uuid = 'Haha'; + record.profile_uuid = ''; record.profile_name = ''; record.profile_state = ''; record.profile_percent = 0;