Minor details for server DEVICE commands. Added getdevice<s> php scripts.

Tue, 16 Apr 2024 16:20:34 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 16 Apr 2024 16:20:34 +0200
changeset 681
1f81e52c5abf
parent 680
8b3c86124a08
child 682
f82be2bd472f

Minor details for server DEVICE commands. Added getdevice<s> php scripts.

thermferm/server.c file | annotate | diff | comparison | revisions
www/getdevice.php file | annotate | diff | comparison | revisions
www/getdevices.php file | annotate | diff | comparison | revisions
www/js/fermenter.js file | annotate | diff | comparison | revisions
--- 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 <uuid>            Json list all or a single device");
 	srv_send(s, (char *)".");
 	return 0;
     }
--- /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 @@
+<?php
+
+
+function open_socket()
+{
+    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+    if (!($sock === false)) {
+        if (socket_connect($sock, "localhost", 6554)) {
+            socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 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 '{}';
+}
+
--- /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 @@
+<?php
+
+
+function open_socket()
+{
+    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+    if (!($sock === false)) {
+        if (socket_connect($sock, "localhost", 6554)) {
+            socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 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 '{}';
+}
+
--- 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 + '&deg;C&nbsp;&nbsp;' + 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;

mercurial