# HG changeset patch # User Michiel Broek # Date 1713791547 -7200 # Node ID fe042f9484ac45bf307c14cf65fb5fa732e4bc21 # Parent ea20d8adbcaa2a0bac71286992d8aa67206425bc Added web page to display the one-wire bus live status. diff -r ea20d8adbcaa -r fe042f9484ac thermferm/server.c --- 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 [parameters] List commands"); srv_send(s, (char *)"LIST HELP List help screen"); + srv_send(s, (char *)"ONEWIRE [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 [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"); diff -r ea20d8adbcaa -r fe042f9484ac www/getonewire.php --- /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 @@ + 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 '{}'; +} + diff -r ea20d8adbcaa -r fe042f9484ac www/includes/global.inc.php --- 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 @@
  • Devices
  • Fermenters
  • Simulators
  • +
  • One-wire bus
  • Over
  • diff -r ea20d8adbcaa -r fe042f9484ac www/js/list_onewire.js --- /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 + * + * 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('
    Total items: ' + rowCount + '
    '); + var container; + container = $('
    '); + 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 '' + date + ''; + } + } + ], + }); + + 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'); + } + } +}); diff -r ea20d8adbcaa -r fe042f9484ac www/list_onewire.php --- /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 @@ + + +
    +
    +
    +
    +
    + +