Finished global setup including Save.

Tue, 23 Apr 2024 13:06:33 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 23 Apr 2024 13:06:33 +0200
changeset 698
92a080c1a5d5
parent 697
685f20ad87ed
child 699
35382668a140

Finished global setup including Save.

thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
www/getglobal.php file | annotate | diff | comparison | revisions
www/includes/global.inc.php file | annotate | diff | comparison | revisions
www/js/set_global.js file | annotate | diff | comparison | revisions
www/set_global.php file | annotate | diff | comparison | revisions
--- a/thermferm/mqtt.c	Mon Apr 22 20:58:55 2024 +0200
+++ b/thermferm/mqtt.c	Tue Apr 23 13:06:33 2024 +0200
@@ -1416,6 +1416,8 @@
 
     payload = xstrcpy((char *)"{\"type\":\"global\",\"name\":\"");
     payload = xstrcat(payload, Config.name);
+    payload = xstrcat(payload, (char *)"\",\"uuid\":\"");
+    payload = xstrcat(payload, Config.uuid);
     payload = xstrcat(payload, (char *)"\",\"node\":\"");
     if (uname(&ubuf) == 0) {
 	payload = xstrcat(payload, ubuf.nodename);
@@ -1434,6 +1436,9 @@
     payload = xstrcat(payload, (char *)",\"websocket_port\":");
     sprintf(buf, "%d", Config.websocket_port);
     payload = xstrcat(payload, buf);
+    payload = xstrcat(payload, (char *)",\"nextunit\":");
+    sprintf(buf, "%d", Config.next_unit);
+    payload = xstrcat(payload, buf);
 
     if (Config.temp_uuid || Config.hum_uuid) {
         payload = xstrcat(payload, (char *)",\"THB\":{");
--- a/thermferm/server.c	Mon Apr 22 20:58:55 2024 +0200
+++ b/thermferm/server.c	Tue Apr 23 13:06:33 2024 +0200
@@ -709,6 +709,7 @@
 	srv_send(s, (char *)"213 Global Settings record follows:");
 	srv_send(s, (char *)"RELEASE,%s", VERSION);
 	srv_send(s, (char *)"NAME,%s", Config.name);
+	srv_send(s, (char *)"UUID,%s", Config.uuid);
 	srv_send(s, (char *)"PORT,%d", Config.server_port);
 	srv_send(s, (char *)"TEMP_UUID,%s", Config.temp_uuid);
 	srv_send(s, (char *)"TEMP_STATE,%s", DEVPRESENT[Config.temp_state]);
@@ -808,6 +809,13 @@
 			    Config.temp_hum_idx = ival;
 			}
 
+		    } else if (val && (strcmp(kwd, (char *)"LCD_ADDRESS") == 0)) {
+                        if (sscanf(val, "%d", &ival) == 1) {
+                            if (Config.lcd_address != ival)
+                                syslog(LOG_NOTICE, "Global LCD address %d to %d", Config.lcd_address, ival);
+                            Config.lcd_address = ival;
+                        }
+
 		    } else if (val && (strcmp(kwd, (char *)"LCD_COLS") == 0)) {
 			if (sscanf(val, "%d", &ival) == 1) {
 			    if (Config.lcd_cols != ival)
--- a/www/getglobal.php	Mon Apr 22 20:58:55 2024 +0200
+++ b/www/getglobal.php	Tue Apr 23 13:06:33 2024 +0200
@@ -47,14 +47,49 @@
     return !strncmp($haystack, $needle, strlen($needle));
 }
 
+$response = array(
+   'error' => false,
+   'msg' => 'Ok',
+);
 
-$answer = send_cmd("GLOBAL JSON");
-header("Content-type: application/json");
+
+if (isset($_POST['update'])) {
 
-$arr = explode("\r\n", $answer);
-if (startsWith($arr[0], "213")) {
-    echo $arr[1];
+    /* Changing ports does not yet work in the web scripts, disabled. */
+    $cmd  = "GLOBAL PUT\r\n";
+    $cmd .= "NAME," . $_POST['name'] . "\r\n";
+//  $cmd .= "PORT," . $_POST['port'] . "\r\n";
+    $cmd .= "TEMP_UUID," . $_POST['temp_uuid'] . "\r\n";
+    $cmd .= "HUM_UUID," . $_POST['hum_uuid'] . "\r\n";
+    $cmd .= "TEMP_HUM_IDX," . $_POST['temp_hum_idx'] . "\r\n";
+    $cmd .= "LCD_ADDRESS," . $_POST['lcd_address'] . "\r\n";
+    $cmd .= "LCD_COLS," . $_POST['lcd_cols'] . "\r\n";
+    $cmd .= "LCD_ROWS," . $_POST['lcd_rows'] . "\r\n";
+    $cmd .= "MQTT_HOST," . $_POST['mqtt_host'] . "\r\n";
+    $cmd .= "MQTT_PORT," . $_POST['mqtt_port'] . "\r\n";
+    $cmd .= "MQTT_USER," . $_POST['mqtt_user'] . "\r\n";
+    $cmd .= "MQTT_PASS," . $_POST['mqtt_pass'] . "\r\n";
+//  $cmd .= "WEBSOCKET_PORT," . $_POST['websocket_port'] . "\r\n";
+    $cmd .= ".";
+    $answer = send_cmd($cmd);
+    $arr = explode("\r\n", $answer);
+    if (! startsWith($arr[0], "219")) {
+	$response['error'] = true;
+    }
+    exit(json_encode($response));
+
 } else {
-    echo '{}';
+
+    $answer = send_cmd("GLOBAL JSON");
+    header("Content-type: application/json");
+
+    $arr = explode("\r\n", $answer);
+    if (startsWith($arr[0], "213")) {
+    	echo $arr[1];
+    } else {
+    	echo '{}';
+    }
 }
 
+
+?>
--- a/www/includes/global.inc.php	Mon Apr 22 20:58:55 2024 +0200
+++ b/www/includes/global.inc.php	Tue Apr 23 13:06:33 2024 +0200
@@ -129,6 +129,7 @@
   <script src="jqwidgets/jqxcheckbox.js"></script>
   <script src="jqwidgets/jqxgauge.js"></script>
   <script src="jqwidgets/jqxdraw.js"></script>
+  <script src="jqwidgets/jqxpasswordinput.js"></script>
   <script src="jqwidgets/globalization/globalize.js"></script>
   <script src="js/global.js"></script>
 <?php
--- a/www/js/set_global.js	Mon Apr 22 20:58:55 2024 +0200
+++ b/www/js/set_global.js	Tue Apr 23 13:06:33 2024 +0200
@@ -23,6 +23,7 @@
 
 $(document).ready(function() {
  var global = {},
+ url = 'getglobal.php',
  globalSource = {
   datatype: 'json',
   cache: false,
@@ -36,6 +37,7 @@
    { name: 'FW' },
    { name: 'server_port', type: 'int' },
    { name: 'websocket_port', type: 'int' },
+   { name: 'nextunit', type: 'int' },
    { name: 'thb_temp_uuid', map: 'THB>temperature>uuid' },
    { name: 'temp_uuid', map: 'THB>temperature>uuid' },
    { name: 'temp_state', map: 'THB>temperature>state' },
@@ -49,52 +51,49 @@
    { name: 'lcd_rows', map: 'LCD>rows', type: 'int' },
    { name: 'mqtt_host', map: 'MQTT>host' },
    { name: 'mqtt_port', map: 'MQTT>port', type: 'int' },
-   { name: 'mqtt_username', map: 'MQTT_username' },
-   { name: 'mqtt_password', map: 'MQTT_password' }
+   { name: 'mqtt_username', map: 'MQTT>username' },
+   { name: 'mqtt_password', map: 'MQTT>password' }
   ],
   id: 'name',
-  url: 'getglobal.php'
+  url: url
  },
  globalData = new $.jqx.dataAdapter(globalSource, {
   loadComplete: function(records) {
    global = globalData.records[0];
-   updateScreen();
+   $('#name').val(global.name);
+   $('#uuid').val(global.uuid);
+   $('#node').val(global.node);
+   $('#os').val(global.os + ' ' + global.os_version);
+   $('#FW').val(global.FW);
+   $('#server_port').val(global.server_port);
+   $('#websocket_port').val(global.websocket_port);
+   $('#nextunit').val(global.nextunit);
+   $('#temp_uuid').val(global.temp_uuid);
+   $('#temp_state').val(global.temp_state);
+   $('#temp_value').val(global.temp_value / 1000.0);
+   $('#hum_uuid').val(global.hum_uuid);
+   $('#hum_state').val(global.hum_state);
+   $('#hum_value').val(global.hum_value / 1000.0);
+   $('#temp_hum_idx').val(global.temp_hum_idx);
+   $('#lcd_address').val(global.lcd_address);
+   $('#lcd_cols').val(global.lcd_cols);
+   $('#lcd_rows').val(global.lcd_rows)
+   $('#mqtt_host').val(global.mqtt_host);
+   $('#mqtt_port').val(global.mqtt_port);
+   $('#mqtt_username').val(global.mqtt_username);
+   $('#mqtt_password').val(global.mqtt_password);
   }
- }),
- tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
-
- function updateScreen() {
-  $('#name').val(global.name);
-  $('#uuid').val(global.uuid);
-  $('#node').val(global.node);
-  $('#os').val(global.os + ' ' + global.os_version);
-  $('#FW').val(global.FW);
-  $('#server_port').val(global.server_port);
-  $('#websocket_port').val(global.websocket_port);
-  $('#temp_uuid').val(global.temp_uuid);
-  $('#temp_state').val(global.temp_state);
-  $('#temp_value').val(global.temp_value / 1000.0);
-  $('#hum_uuid').val(global.hum_uuid);
-  $('#hum_state').val(global.hum_state);
-  $('#hum_value').val(global.hum_value / 1000.0);
-  $('#temp_hum_idx').val(global.temp_hum_idx);
-  $('#lcd_address').val(global.lcd_address);
-  $('#lcd_cols').val(global.lcd_cols);
-  $('#lcd_rows').val(global.lcd_rows)
-  $('#mqtt_host').val(global.mqtt_host);
-  $('#mqtt_port').val(global.mqtt_port);
-  $('#mqtt_username').val(global.mqtt_username);
-  $('#mqtt_password').val(global.mqtt_password);
- }
+ });
 
  // initialize the input fields.
  $('#name').jqxInput({ theme: theme, width: 240, height: 23 });
- $('#uuid').jqxInput({ theme: theme, width: 480, height: 23 });
+ $('#uuid').jqxInput({ theme: theme, width: 360, height: 23 });
  $('#node').jqxInput({ theme: theme, width: 240, height: 23 });
  $('#os').jqxInput({ theme: theme, width: 240, height: 23 });
  $('#FW').jqxInput({ theme: theme, width: 120, height: 23 });
- $('#server_port').jqxNumberInput(Spin0dec);
- $('#websocket_port').jqxNumberInput(Spin0dec);
+ $('#server_port').jqxNumberInput(Show0dec);
+ $('#websocket_port').jqxNumberInput(Show0dec);
+ $('#nextunit').jqxNumberInput(Show0dec);
  $('#temp_uuid').jqxDropDownList({
   theme: theme,
   source: tempsensorlist,
@@ -118,30 +117,53 @@
  $('#hum_state').jqxInput({ theme: theme, width: 120, height: 23 });
  $('#hum_value').jqxNumberInput(Show1dec);
  $('#temp_hum_idx').jqxNumberInput(Spin0dec);
-
  $('#lcd_address').jqxNumberInput(Spin0dec);
  $('#lcd_cols').jqxNumberInput(GPIOInt);
  $('#lcd_rows').jqxNumberInput(GPIOInt);
-
  $('#mqtt_host').jqxInput({ theme: theme, width: 240, height: 23 });
  $('#mqtt_port').jqxNumberInput(Spin0dec);
  $('#mqtt_username').jqxInput({ theme: theme, width: 240, height: 23 });
- $('#mqtt_password').jqxInput({ theme: theme, width: 240, height: 23 });
+ $('#mqtt_password').jqxPasswordInput({ placeHolder: "Enter Password", showPasswordIcon: true, theme: theme, width: 240, height: 23 });
 
  globalData.dataBind();
 
 
  $('#Save').jqxButton({ template: 'success', width: '90px', theme: theme });
  $('#Save').click(function() {
-  var row, rowID = -1;
-  if (editrow >= 0) {
-   rowID = $('#jqxgrid').jqxGrid('getrowid', editrow);
-  }
+  var data,
   row = {
-   uuid: dataRecord.uuid,
-   type: $('#type').val()
+   name: $('#name').val(),
+   port: parseInt($('#server_port').jqxNumberInput('decimal')),
+   temp_uuid: $('#temp_uuid').val(),
+   hum_uuid: $('#hum_uuid').val(),
+   temp_hum_idx: $('#temp_hum_idx').val(),
+   lcd_address: parseInt($('#lcd_address').jqxNumberInput('decimal')),
+   lcd_cols: parseInt($('#lcd_cols').jqxNumberInput('decimal')),
+   lcd_rows: parseInt($('#lcd_rows').jqxNumberInput('decimal')),
+   mqtt_host: $('#mqtt_host').val(),
+   mqtt_port: parseInt($('#mqtt_port').jqxNumberInput('decimal')),
+   mqtt_user: $('#mqtt_username').val(),
+   mqtt_pass: $('#mqtt_password').val(),
+   websocket_port: parseInt($('#websocket_port').jqxNumberInput('decimal'))
   };
-//   $('#jqxgrid').jqxGrid('updaterow', rowID, row);
+  data = 'update=true&' + $.param(row);
+  $.ajax({
+   dataType: 'json',
+   url: url,
+   cache: false,
+   data: data,
+   type: 'POST',
+   success: function(data) {
+    if (data.error) {
+     console.log('update: ' + data.msg);
+     alert('Fout: ' + data.msg);
+    } else {
+     console.log('update: success');
+    }
+   },
+    error: function(jqXHR, textStatus, errorThrown) {
+   }
+  });
  });
 
  websocket.onmessage = function(evt) {
--- a/www/set_global.php	Mon Apr 22 20:58:55 2024 +0200
+++ b/www/set_global.php	Tue Apr 23 13:06:33 2024 +0200
@@ -11,7 +11,7 @@
       </tr>
       <tr>
        <td style="vertical-align: top; float: right; padding: 3px;">System name:</td>
-       <td align="left" style="vertical-align: top; padding: 3px;"><input id="name" readonly /></td>
+       <td align="left" style="vertical-align: top; padding: 3px;"><input id="name" /></td>
        <td style="vertical-align: top; float: right; padding: 3px;">Firmware:</td>
        <td align="left" colspan="3" style="vertical-align: top; padding: 3px;"><input id="FW" readonly /></td>
       </tr>
@@ -28,10 +28,12 @@
        <td colspan="6">&nbsp;</td>
       </tr>
       <tr>
-       <td style="vertical-align: top; float: right; padding: 3px;">Server client port:</td>
+       <td style="vertical-align: top; float: right; padding: 3px;">Server port:</td>
        <td style="padding: 3px;"><div id="server_port"></div></td>
        <td style="vertical-align: top; float: right; padding: 3px;">Websocket port:</td>
-       <td style="padding: 3px;" colspan="3"><div style="float: left;" id="websocket_port"></div></td>
+       <td style="padding: 3px;"><div style="float: left;" id="websocket_port"></div></td>
+       <td style="vertical-align: top; float: right; padding: 3px;">Next unit:</td>
+       <td style="padding: 3px;"><div style="float: left;" id="nextunit"></div></td>
       </tr>
       <tr>
        <td colspan="6">&nbsp;</td>
@@ -80,10 +82,10 @@
        <td style="vertical-align: top; float: right; padding: 3px;">MQTT port:</td>
        <td style="padding: 3px;"><div id="mqtt_port"></div></td>
        <td style="vertical-align: top; float: right; padding: 3px;">MQTT password:</td>
-       <td align="left" colspan="3" style="vertical-align: top; padding: 3px;"><input id="mqtt_password" /></td>
+       <td align="left" colspan="3" style="vertical-align: top; padding: 3px;"><input type="password" id="mqtt_password" /></td>
       </tr>
      </table>
-     <div style="float: right; margin-top: 120px; margin-bottom: 10px;">
+     <div style="float: right; margin-top: 80px; margin-bottom: 10px;">
       <input style="margin-right: 595px;" type="button" id="Save"   value="Save" />
      </div>
     </div>

mercurial