Devices list grid added.

Thu, 18 Apr 2024 14:20:19 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Apr 2024 14:20:19 +0200
changeset 685
819553a2b97e
parent 684
b2265c7e5707
child 686
372b2442a30f

Devices list grid added.

thermferm/devices.c file | annotate | diff | comparison | revisions
www/js/list_devices.js file | annotate | diff | comparison | revisions
www/list_devices.php file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Wed Apr 17 19:54:56 2024 +0200
+++ b/thermferm/devices.c	Thu Apr 18 14:20:19 2024 +0200
@@ -51,6 +51,9 @@
 #endif
 
 
+void devices_ws(void);
+
+
 /*
  * Since kernel version 4 there is a module, and a dtoverlay so that the
  * temperature and humidity can simply read from the /sys filesystem.
@@ -287,6 +290,7 @@
 		    syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
                     device->value = value;
 		    device->timestamp = time(NULL);
+		    devices_ws();
 //		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    return rc;
 	        }
@@ -299,8 +303,11 @@
 		    if (strncmp(device->address, (char *)"3a", 2) == 0) {
 		    	syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
 //			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-			device->value = (value == 0) ? 0 : 1;
-			device->timestamp = time(NULL);
+			if (device->value != (value == 0) ? 0 : 1) {
+			    device->value = (value == 0) ? 0 : 1;
+			    device->timestamp = time(NULL);
+			    devices_ws();
+			}
 //			pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    }
 		}
@@ -311,17 +318,18 @@
 			(strcmp((char *)"SimFan"   , device->address) == 0) || (strcmp((char *)"SimLight" , device->address) == 0)) {
 			if (value != device->value) {
 			    syslog(LOG_NOTICE, "SIM %s value=%d", device->address, value);
+			    device->value = value;
+			    device->timestamp = time(NULL);
+			    if (strcmp((char *)"SimCooler", device->address) == 0) 
+			    	SIMcooling = value;
+			    if (strcmp((char *)"SimHeater", device->address) == 0)
+			    	SIMheating = value;
+			    if (strcmp((char *)"SimFan", device->address) == 0)
+			    	SIMfan = value;
+			    if (strcmp((char *)"SimLight", device->address) == 0)
+			    	SIMlight = value;
+			    devices_ws();
 			}
-			device->value = value;
-			device->timestamp = time(NULL);
-			if (strcmp((char *)"SimCooler", device->address) == 0)
-			    SIMcooling = value;
-			if (strcmp((char *)"SimHeater", device->address) == 0)
-			    SIMheating = value;
-			if (strcmp((char *)"SimFan", device->address) == 0)
-			    SIMfan = value;
-			if (strcmp((char *)"SimLight", device->address) == 0)
-			    SIMlight = value;
 		    }
 		}
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/js/list_devices.js	Thu Apr 18 14:20:19 2024 +0200
@@ -0,0 +1,86 @@
+/*****************************************************************************
+ * 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 source = {
+  datatype: 'json',
+  cache: false,
+  datafields: [
+   { name: 'uuid', type: 'string' },
+   { name: 'type', type: 'string' },
+   { name: 'address', type: 'string' },
+   { name: 'subdevice', type: 'int' },
+   { name: 'inuse', type: 'int' },
+   { name: 'description', type: 'string' },
+   { name: 'direction', type: 'string' },
+   { name: 'value', type: 'int' },
+   { name: 'timestamp', type: 'int' }
+  ],
+  id: 'uuid',
+  url: 'getdevices.php'
+ },
+ dataAdapter = new $.jqx.dataAdapter(source);
+
+ // initialize jqxGrid
+ $('#jqxgrid').jqxGrid({
+  width: 1280,
+  height: 630,
+  source: dataAdapter,
+  theme: theme,
+  columns: [
+   { text: 'Address', datafield: 'address', width: 200 },
+   { text: 'Subdevice', datafield: 'subdevice', width: 100 },
+   { text: 'Direction', datafield: 'direction', width: 120 },
+   { text: 'Value', datafield: 'value', width: 80 },
+   { text: 'Description', datafield: 'description' },
+   { text: 'Last change', datafield: 'timestamp', width: 200,
+     cellsrenderer: function(index, datafield, value, defaultvalue, column, rowdata) {
+      var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
+      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>';
+    }
+   },
+   { text: '', datafield: 'Edit', width: 100, align: 'center', columntype: 'button', cellsrenderer: function() {
+    return 'Bekijk';
+    }, buttonclick: function(row) {
+     var datarecord = dataAdapter.records[row];
+     window.location.href = 'edit_device.php?uuid=' + datarecord.uuid;
+    }
+   }
+  ],
+ });
+
+ websocket.onmessage = function(evt) {
+  var msg = evt.data;
+  var obj = JSON.parse(msg);
+
+  if (obj.ping) {
+   websocket.send('{"pong":' + obj.ping + '}');
+  }
+
+  if (obj.type == 'device') {
+   // Use the message to trigger update.
+   $('#jqxgrid').jqxGrid('updatebounddata');
+  }
+ }
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/list_devices.php	Thu Apr 18 14:20:19 2024 +0200
@@ -0,0 +1,14 @@
+<?php
+require_once($_SERVER['DOCUMENT_ROOT'].'/includes/global.inc.php');
+page_header('Sensor devices', 'list_devices');
+?>
+
+   <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