From b2f6f216f6d8c2013edae749919de0eb9b387114 Mon Sep 17 00:00:00 2001 From: Mikail Killi Date: Mon, 14 Oct 2024 20:20:49 +0200 Subject: [PATCH] Added axios requests to show the nodes from the database, added calculator for battery level and input field for la and lo --- ApfelBruno/Node/Create node.bru | 2 +- web/src/components/ListViewElement.vue | 24 ++------- web/src/components/TableCategory.vue | 74 ++++++++++++-------------- web/src/types.ts | 10 ++-- 4 files changed, 47 insertions(+), 63 deletions(-) diff --git a/ApfelBruno/Node/Create node.bru b/ApfelBruno/Node/Create node.bru index 531735c..7df692b 100644 --- a/ApfelBruno/Node/Create node.bru +++ b/ApfelBruno/Node/Create node.bru @@ -17,6 +17,6 @@ body:json { "coord_lo":2, "battery_minimum":3, "battery_maximum":4, - "group":"20abe318-7238-4f63-908f-a484955ee3bc" + "group":"54eccfb5-1d5a-4cad-a1a2-468eca68ffd6" } } diff --git a/web/src/components/ListViewElement.vue b/web/src/components/ListViewElement.vue index 91d163d..04aa173 100644 --- a/web/src/components/ListViewElement.vue +++ b/web/src/components/ListViewElement.vue @@ -18,11 +18,11 @@
Coordinates: - La: {{ node.coordla }}, Long: {{ node.coordlong }} + La: {{ node.coord_la }}, Long: {{ node.coord_lo }}
Temperature: - {{ sensorData?.temperature }}°C + {{ sensorData?.temperature !== undefined ? sensorData.temperature : 'NaN' }}°C
Battery Voltage: @@ -30,9 +30,9 @@
Runtime: - {{ sensorData?.uptime }} hours -
+ {{ sensorData?.uptime ? (sensorData.uptime / 3600).toFixed(2) + ' hours' : 'N/A' }}
+ @@ -62,24 +62,10 @@ watch([searching, visibleIds], ([searching, visibleIds]) => { onMounted(async () => { try { const { data } = await axios.get( - `http://localhost:8080/api/v1/data?nodeId=${props.node.id}` + `http://localhost:8080/api/v1/data?id=${props.node.id}` ); sensorData.value = data; } catch (error) { console.error("Error fetching sensor data:", error); } }); - - - - diff --git a/web/src/components/TableCategory.vue b/web/src/components/TableCategory.vue index d596d6c..800284f 100644 --- a/web/src/components/TableCategory.vue +++ b/web/src/components/TableCategory.vue @@ -14,25 +14,25 @@ - {{ node.name }} + {{ node.id }} - - {{ node.sensorData.voltage ? 'ONLINE' : 'OFFLINE' }} + + {{ node.sensorData.voltage !== 'N/A' ? 'ONLINE' : 'OFFLINE' }} {{ node.coordla }} + @blur="validateAndUpdateLatLng(node, 'coord_la', $event)" + >{{ node.coord_la }} {{ node.coordlong }} - {{ calculateBatteryPercentage(node.sensorData.voltage, node.batteryMinimum, node.batteryMaximum) }}% + @blur="validateAndUpdateLatLng(node, 'coord_lo', $event)" + >{{ node.coord_lo }} + {{ calculateBatteryPercentage(node.sensorData.voltage, node.battery_minimum, node.battery_maximum) }}% {{ node.sensorData.temperature }}°C {{ formatRuntime(node.sensorData.uptime) }} - + @@ -50,29 +50,32 @@ export default { this.fetchNodesAndData(); }, methods: { - async fetchNodesAndData() { - try { - const nodesResponse = await axios.get('http://localhost:8080/api/v1/nodes'); - const nodes = nodesResponse.data; + async fetchNodesAndData() { + try { + const nodeGroupsResponse = await axios.get('http://localhost:8080/api/v1/nodes'); + const nodeGroups = nodeGroupsResponse.data; - const sensorDataResponse = await axios.get('http://localhost:8080/api/v1/data'); - const sensorData = sensorDataResponse.data; + const nodes = nodeGroups.flatMap(group => group.node); - this.tableData = nodes.map((node) => { - const nodeSensorData = sensorData.find((data) => data.id === node.id); - return { - ...node, - sensorData: nodeSensorData || { - temperature: 'N/A', - voltage: 'N/A', - uptime: 'N/A', - }, - }; - }); - } catch (error) { - console.error('Error fetching node or sensor data:', error); - } - }, + const sensorDataResponse = await axios.get('http://localhost:8080/api/v1/data'); + const sensorDataArray = sensorDataResponse.data; + + this.tableData = nodes.map(node => { + const nodeSensorData = sensorDataArray.find(data => data.node.id === node.id); + + return { + ...node, + sensorData: nodeSensorData ? nodeSensorData.sensor_data : { + temperature: 'N/A', + voltage: 'N/A', + uptime: 'N/A', + }, + }; + }); + } catch (error) { + console.error('Error fetching node or sensor data:', error); + } + }, calculateBatteryPercentage(voltage, batteryMinimum, batteryMaximum) { if (voltage <= batteryMinimum) { return 0; @@ -92,22 +95,16 @@ export default { const originalValue = node[field]; let newValue = event.target.innerText; - // Normalize separated values newValue = newValue.replace(',', '.'); - - // Check if it's a valid float value const validNumberRegex = /^-?\d+(\.\d+)?$/; if (validNumberRegex.test(newValue)) { const parsedValue = parseFloat(newValue); - - // Update if valid node[field] = parsedValue; - console.log(`Updated ${field} of ${node.name}: ${parsedValue}`); + console.log(`Updated ${field} of ${node.id}: ${parsedValue}`); } else { - // Reset to original value if invalid event.target.innerText = originalValue; - console.log(`Failed to set ${field} of ${node.name}: Invalid input "${newValue}"`); + console.log(`Failed to set ${field} of ${node.id}: Invalid input "${newValue}"`); } }, }, @@ -115,7 +112,6 @@ export default {