

### parsing JSON

The data that is send by the nodes is send in json data. We chose this becouse we can easily use the data within javascript, this is called parsing. We use the parced data and send that in to the next function to make the data more acceseble to use in further instences. 

```js
      const jsonData = JSON.parse(event.data);
      // Use the parsed JSON data as needed
      handleIncomingData(jsonData);
```

Here is the function that receves the parced JSON data and set it into variables. So that it can be more easlily used in the next function with is the processNodeData, witch is setting the data in to the right array.

```js
function handleIncomingData(data) {
  nodeNumber = data.node;
  temperature = data.Temp;
  humidity = data.Humi;
  CO2 = data.eCO2;
  TVOC = data.TVOC;

  processNodeData(nodeNumber, temperature, humidity, CO2, TVOC);
}
```

In the function processNodeData we first check if there is a array for the node that is sending the data, this is done becouse if we want to seperate the data in to show witch node is sending what data. So if the nodeNumber plus sensorData (the name of the array) not already there the array is made.

```js
  // Initialize the array for this node if it doesn't exist yet
  if (!sensorData[nodeNumber]) {
    sensorData[nodeNumber] = [];
  }
```

For the actual data put in to array function is there a simple array.push for the data that is send allong from when the function is called.

```js
  // Push the new data onto the array for this node
  sensorData[nodeNumber].push({
    'node': nodeNumber,
    'temp': temperature,
    'humi': humidity,
    'CO2': CO2,
    'TVOC': TVOC,
  });
```

We want only use the last 10 readings from the nodes so there is a check for if the array is longer than 10 the first (or the oldest reading), if that is so there is a .shift executed. This is done to be later used in the graph function. Than there is a call for the next function, that is the updateNodeData and that will acctually find the right html id coresponding with the right reading to update that.

```js
  // If the array for this node has more than 10 elements, remove the oldest one
  if (sensorData[nodeNumber].length >= 10) {
    sensorData[nodeNumber].shift();
  }

  updateNodeData(nodeNumber, temperature, humidity, CO2, TVOC);
```

In the last function there are 2 updates the first is to actually update the text to the right value that we are getting from the node, and the connection checker. 

```js
function updateNodeData(node, temperature, humidity, eCO2, TVOC) {
  // Update the temperature, humidity and light intensity values
  document.getElementById("temperature" + node).textContent = temperature;
  document.getElementById("humidity" + node).textContent = humidity;
  document.getElementById("CO2" + node).textContent = eCO2;
  document.getElementById("TVOC" + node).textContent = TVOC;

  // Update the status text
  document.getElementById("tempStatus").textContent = "Connected";
  document.getElementById("humidStatus").textContent = "Connected";
  document.getElementById("CO2Status").textContent = "Connected";
  document.getElementById("TVOCStatus").textContent = "Connected";
}
```