Added Add new device.

Tue, 23 Apr 2024 16:52:13 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 23 Apr 2024 16:52:13 +0200
changeset 699
35382668a140
parent 698
92a080c1a5d5
child 700
942af738157c

Added Add new device.

thermferm/server.c file | annotate | diff | comparison | revisions
www/getdevices.php file | annotate | diff | comparison | revisions
www/js/set_devices.js file | annotate | diff | comparison | revisions
www/set_devices.php file | annotate | diff | comparison | revisions
--- a/thermferm/server.c	Tue Apr 23 13:06:33 2024 +0200
+++ b/thermferm/server.c	Tue Apr 23 16:52:13 2024 +0200
@@ -420,6 +420,7 @@
             device->gpiopin = -1;
             device->description = xstrcpy((char *)"Describe me here");
             device->comment = xstrcpy((char *)"Comment here");
+	    device->timestamp = time(NULL);
 
             pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
             if (Config.devices == NULL) {
--- a/www/getdevices.php	Tue Apr 23 13:06:33 2024 +0200
+++ b/www/getdevices.php	Tue Apr 23 16:52:13 2024 +0200
@@ -48,13 +48,37 @@
 }
 
 
-$answer = send_cmd("DEVICE JSON");
-header("Content-type: application/json");
+$response = array(
+   'error' => false,
+   'msg' => 'Ok',
+);
+
+
+if (isset($_POST['update'])) {
+
+} else if (isset($_POST['add'])) {
 
-$arr = explode("\r\n", $answer);
-if (startsWith($arr[0], "212")) {
-    echo $arr[1];
+    $answer = send_cmd("DEVICE ADD " . $_POST['type']);
+    $arr = explode("\r\n", $answer);
+    if (! startsWith($arr[0], "211")) {
+	$response['error'] = true;
+	$response['msg'] = $arr[0];
+    }
+    exit(json_encode($response));
+
+} else if (isset($_POST['del'])) {
+
 } else {
-    echo '{}';
+
+    $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/set_devices.js	Tue Apr 23 13:06:33 2024 +0200
+++ b/www/js/set_devices.js	Tue Apr 23 16:52:13 2024 +0200
@@ -42,8 +42,43 @@
 }
 
 
+function createAddElements() {
+ $('#addWindow').jqxWindow({
+  theme: theme,
+  position: { x: 400, y: 210 },
+  width: 480,
+  height: 180,
+  resizable: false,
+  isModal: true,
+  modalOpacity: 0.4,
+  okButton: $('#addOk'),
+  cancelButton: $('#addCancel'),
+  initContent: function() {
+   $('#addType').jqxDropDownList({
+    theme: theme,
+    source: DeviceTypeAdapter,
+    valueMember: 'mno',
+    displayMember: 'en',
+    width: 180,
+    height: 30,
+    autoDropDownHeight: true
+   });
+   $("#addType").jqxDropDownList('disableItem', "W1" );	/* These are auto created, so disable. */
+   $("#addType").jqxDropDownList('disableItem', "GPIO" );
+   $("#addType").jqxDropDownList('disableItem', "SIM" );
+   $('#addOk').jqxButton({ template: 'success', width: '65px', theme: theme });
+   $('#addCancel').jqxButton({ template: 'primary', width: '65px', theme: theme });
+   $('#addCancel').focus();
+  }
+ });
+ $('#addWindow').jqxWindow('hide');
+}
+
+
+
 $(document).ready(function() {
  var dataRecord = {},
+ url = 'getdevices.php',
  source = {
   datatype: 'json',
   cache: false,
@@ -63,14 +98,14 @@
    { name: 'timestamp', type: 'int' }
   ],
   id: 'uuid',
-  url: 'getdevices.php'
+  url: url
  },
  dataAdapter = new $.jqx.dataAdapter(source),
  editrow = -1,
  tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
 
  // initialize the input fields.
- $('#dev_uuid').jqxInput({ theme: theme, width: 480, height: 23 });
+ $('#dev_uuid').jqxInput({ theme: theme, width: 360, height: 23 });
  $('#dev_description').jqxInput({ theme: theme, width: 800, height: 23 });
  $('#dev_type').jqxDropDownList({
   theme: theme,
@@ -127,25 +162,35 @@
    addButton.jqxButton({ theme: theme, width: 90, height: 17 });
    // add new row.
    addButton.click(function(event) {
-    editrow = -1;
-    $('#popupWindow').jqxWindow({ position: { x: 110, y: 30 } });
-    $('#dev_uuid').val('');
-    $('#dev_description').val('');
-    $('#dev_type').val('NA');
-    $('#dev_direction').val('UNDEF');
-    $('#dev_value').val(0);
-    $('#dev_offset').val(0);
-    $('#dev_address').val('');
-    $('#dev_subdevice').val(0);
-    $('#dev_present').val('UNDEF');
-    $('#dev_gpiopin').val(-1);
-    $('#dev_inuse').val(0);
-    var now = new Date();
-    var date = new Date(now - tzoffset).toISOString().slice(0, 19).replace("T", " ");
-    $('#dev_timestamp').val(date);
-    $('#dev_comment').val('');
-
-    $('#popupWindow').jqxWindow('open');
+    $('#addType').val('NA');
+    $('#addWindow').jqxWindow('open');
+    $('#addOk').click(function() {
+     if ($('#addType').val() != 'NA') {
+      console.log('Add type ' + $('#addType').val());
+      var data,
+      data = 'add=true&type=' + $('#addType').val();
+      console.log(data);
+      $.ajax({
+       dataType: 'json',
+       url: url,
+       cache: false,
+       data: data,
+       type: 'POST',
+       success: function(data) {
+        if (data.error) {
+         console.log('add: ' + data.msg);
+         alert('Error: ' + data.msg);
+        } else {
+         console.log('add: success');
+        }
+       },
+       error: function(jqXHR, textStatus, errorThrown) {
+       }
+      });
+     } else {
+      console.log('Add wrong type');
+     }
+    });
    });
   },
   columns: [
@@ -233,6 +278,7 @@
   $('#popupWindow').jqxWindow('hide');
  });
  createDelElements();
+ createAddElements();
 
  websocket.onmessage = function(evt) {
   var msg = evt.data;
--- a/www/set_devices.php	Tue Apr 23 13:06:33 2024 +0200
+++ b/www/set_devices.php	Tue Apr 23 16:52:13 2024 +0200
@@ -74,6 +74,25 @@
     </div>
    </div>
 
+   <div id="addWindow">
+    <div>
+     Add new device
+    </div>
+    <div>
+     <div>
+      Select a new device type and press "OK" to create a new device record.<br>
+      Press "Cancel" to close without adding a new device.
+     </div>
+     <div>
+      <div style="float: right; margin-top: 25px; margin-bottom: 10px;">
+       <div style="float: left; margin-right: 15px" id="addType"></div>
+       <input type="button" id="addOk" value="OK" style="margin-right: 15px" />
+       <input type="button" id="addCancel" value="Cancel" style="margin-right: 65px" />
+      </div>
+     </div>
+    </div>
+   </div>
+
 <?php
 confirm_delete();
 page_footer();

mercurial