# HG changeset patch # User Michiel Broek # Date 1713883933 -7200 # Node ID 35382668a14086f24b358279978555613627ef0d # Parent 92a080c1a5d5b4a2d58fc68ec6cc5abf855b851f Added Add new device. diff -r 92a080c1a5d5 -r 35382668a140 thermferm/server.c --- 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) { diff -r 92a080c1a5d5 -r 35382668a140 www/getdevices.php --- 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 '{}'; + } } +?> diff -r 92a080c1a5d5 -r 35382668a140 www/js/set_devices.js --- 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; diff -r 92a080c1a5d5 -r 35382668a140 www/set_devices.php --- 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 @@ +
+
+ Add new device +
+
+
+ Select a new device type and press "OK" to create a new device record.
+ Press "Cancel" to close without adding a new device. +
+
+
+
+ + +
+
+
+
+