Added web page to display the one-wire bus live status.

Mon, 22 Apr 2024 15:12:27 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 22 Apr 2024 15:12:27 +0200
changeset 696
fe042f9484ac
parent 695
ea20d8adbcaa
child 697
685f20ad87ed

Added web page to display the one-wire bus live status.

thermferm/server.c file | annotate | diff | comparison | revisions
www/getonewire.php file | annotate | diff | comparison | revisions
www/includes/global.inc.php file | annotate | diff | comparison | revisions
www/js/list_onewire.js file | annotate | diff | comparison | revisions
www/list_onewire.php file | annotate | diff | comparison | revisions
--- a/thermferm/server.c	Mon Apr 22 13:50:23 2024 +0200
+++ b/thermferm/server.c	Mon Apr 22 15:12:27 2024 +0200
@@ -23,6 +23,7 @@
 #include "rdconfig.h"
 #include "thermferm.h"
 #include "delay.h"
+#include "one-wire.h"
 #include "devices.h"
 #include "server.h"
 #include "lcd-buffer.h"
@@ -34,6 +35,7 @@
 extern int		run_pause;
 extern int		run_hold;
 extern sys_config       Config;
+extern w1_list		*w1_devices;
 extern const char	UNITMODE[5][8];
 extern const char	UNITSTAGE[4][12];
 extern const char	DEVTYPE[8][6];
@@ -620,6 +622,62 @@
 
 
 /*
+ * ONEWIRE JSON
+ */
+int cmd_onewire(int s, char *buf)
+{
+    char	*opt, *param;
+    w1_list	*dev_w1;
+
+    opt = strtok(buf, " \0");
+    opt = strtok(NULL, " \0");
+
+    if (opt == NULL) {
+	srv_send(s, (char *)"501 Subcommand missing");
+	return 0;
+    }
+    param = strtok(NULL, "\0");
+
+    if (strcmp(opt, (char *)"HELP") == 0) {
+	srv_send(s, (char *)"100 Help text follows:");
+        srv_send(s, (char *)"Recognized commands:");
+	srv_send(s, (char *)"ONEWIRE JSON               Json list all or a single one-wire device");
+	srv_send(s, (char *)".");
+	return 0;
+    }
+
+    if (strcmp(opt, (char *)"JSON") == 0) {
+	char    *payload = NULL, *payloadu = NULL;
+        bool    comma = false;
+
+	if (param == NULL) {
+            srv_send(s, (char *)"212 One-wire json list follows:");
+            payload = xstrcpy((char *)"[");
+	    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
+                if (comma)
+                    payload = xstrcat(payload, (char *)",");
+                payloadu = one_wire_json(dev_w1);
+                payload = xstrcat(payload, payloadu);
+                comma = true;
+                free(payloadu);
+                payloadu = NULL;
+            }
+            payload = xstrcat(payload, (char *)"]");
+            srv_send(s, payload);
+            srv_send(s, (char *)".");
+            free(payload);
+            payload = NULL;
+            return 0;
+        }
+    }
+
+    srv_send(s, (char *)"504 Subcommand error");
+    return 0;
+}
+
+
+
+/*
  * GLOBAL GET
  * GLOBAL PUT
  * GLOBAL JSON
@@ -2221,6 +2279,8 @@
 		srv_send(s, (char *)"GLOBAL HELP                   Global help screen");
 		srv_send(s, (char *)"LIST <CMD> [parameters]       List commands");
 		srv_send(s, (char *)"LIST HELP                     List help screen");
+		srv_send(s, (char *)"ONEWIRE <CMD> [parameters]    One-wire commands");
+		srv_send(s, (char *)"ONEWIRE HELP                  One-wire help screen");
 		srv_send(s, (char *)"PING                          Check if server is alive");
 #ifdef USE_SIMULATOR
 		srv_send(s, (char *)"SIMULATOR <CMD> [parameters]  Simulator commands");
@@ -2233,6 +2293,9 @@
 	    } else if (strncmp(buf, "LIST", 4) == 0) {
 		cmd_list(s, buf);
 
+	    } else if (strncmp(buf, "ONEWIRE", 7) == 0) {
+		cmd_onewire(s, buf);
+
 	    } else if (strncmp(buf, "PING", 4) == 0) {
 		srv_send(s, (char *)"101 PONG");
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/getonewire.php	Mon Apr 22 15:12:27 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("ONEWIRE JSON");
+header("Content-type: application/json");
+
+$arr = explode("\r\n", $answer);
+if (startsWith($arr[0], "212")) {
+    echo $arr[1];
+} else {
+    echo '{}';
+}
+
--- a/www/includes/global.inc.php	Mon Apr 22 13:50:23 2024 +0200
+++ b/www/includes/global.inc.php	Mon Apr 22 15:12:27 2024 +0200
@@ -174,6 +174,7 @@
        <li><img style='float: left; margin-right: 5px;' src='images/database.png' /><a href="set_devices.php">Devices</a></li>
        <li><img style='float: left; margin-right: 5px;' src='images/fermenter.png' /><a href="set_fermenters.php">Fermenters</a></li>
        <li><img style='float: left; margin-right: 5px;' src='images/computer.png' /><a href="set_simulators.php">Simulators</a></li>
+       <li><img style='float: left; margin-right: 5px;' src='images/database.png' /><a href="list_onewire.php">One-wire bus</a></li>
       </ul>
      </li>
      <li style='width: 80px;'><a href="gen_about.php">Over</a></li>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/js/list_onewire.js	Mon Apr 22 15:12:27 2024 +0200
@@ -0,0 +1,83 @@
+/*****************************************************************************
+ * Copyright (C) 2024
+ *
+ * Michiel Broek <mbroek at mbse dot eu>
+ *
+ * This file is part of mbsePi-apps
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * BrewCloud is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with ThermFerm; see the file COPYING.  If not, write to the Free
+ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *****************************************************************************/
+
+
+$(document).ready(function() {
+ var dataRecord = {},
+ source = {
+  datatype: 'json',
+  cache: false,
+  datafields: [
+   { name: 'address', type: 'string' },
+   { name: 'family', type: 'string' },
+   { name: 'value', type: 'int' },
+   { name: 'present', type: 'string' },
+   { name: 'timestamp', type: 'int' }
+  ],
+  id: 'address',
+  url: 'getonewire.php'
+ },
+ dataAdapter = new $.jqx.dataAdapter(source),
+ tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
+
+ // initialize jqxGrid
+ $('#jqxgrid').jqxGrid({
+  width: 1280,
+  height: 630,
+  source: dataAdapter,
+  theme: theme,
+  showstatusbar: true,
+  renderstatusbar: function(statusbar) {
+   var rowCount = $("#jqxgrid").jqxGrid('getrows').length;
+   statusbar.append('<div style="float: left; margin: 8px; color: orange !important;">Total items: ' + rowCount + '</div>');
+   var container;
+   container = $('<div style="overflow: hidden; position: relative; margin: 5px;"></div>');
+   statusbar.append(container);
+  },
+  columns: [
+   { text: 'Address', datafield: 'address' },
+   { text: 'Family', datafield: 'family', width: 100 },
+   { text: 'Present', datafield: 'present', width: 120 },
+   { text: 'Value', datafield: 'value', width: 100 },
+   { text: 'Last change', datafield: 'timestamp', width: 200,
+     cellsrenderer: function(index, datafield, value, defaultvalue, column, rowdata) {
+      var date = new Date((value * 1000) - tzoffset).toISOString().slice(0, 19).replace("T", " ");
+      return '<span style="margin: 3px; margin-top: 6px; float: left;">' +  date + '</span>';
+    }
+   }
+  ],
+ });
+
+ websocket.onmessage = function(evt) {
+  var msg = evt.data;
+  var obj = JSON.parse(msg);
+
+  if (obj.ping) {
+   websocket.send('{"pong":' + obj.ping + '}');
+  }
+
+  if (obj.type == 'onewire') {
+   // Use the message to trigger update.
+   $('#jqxgrid').jqxGrid('updatebounddata');
+  }
+ }
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/list_onewire.php	Mon Apr 22 15:12:27 2024 +0200
@@ -0,0 +1,14 @@
+<?php
+require_once($_SERVER['DOCUMENT_ROOT'].'/includes/global.inc.php');
+page_header('One-wire bus devices', 'list_onewire');
+?>
+
+   <div id="jqxgrid"></div>
+    <div style="margin-top: 30px;">
+    <div id="cellbegineditevent"></div>
+    <div style="margin-top: 10px;" id="cellendeditevent"></div>
+   </div>
+
+<?php
+page_footer();
+?>

mercurial