Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RESTserver/LEGGIMI.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ La latenza media (ritardo) tra un evento e la sua segnalazione in un client WEB

3. **Configurazione:**
* Aggiorna il tuo percorso di installazione nel file `run_server.bat`
* Per gli utenti Linux/Raspberry Pi, è fornito il file `iotwebui.service` per l'avvio automatico tramite systemd.

4. **Test e debug**
Ci sono tre file principali:
Expand Down
1 change: 1 addition & 0 deletions RESTserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The average latency (delay) between an event and its reporting in a WEB client u
* Click on `install_server.bat`: it will install the updated dependencies in the 'node_modules' dir.
3. **Configuration:**
* Update your installation path in the `run_server.bat` file
* For Linux/Raspberry Pi users, a `iotwebui.service` file is provided for autostart functionality via systemd.
4. **Testing and debugging**
There are three main files:
* `server.js`: the executable file with the IoTrest implementation, to be launched in a terminal or using `run_server.bat`.
Expand Down
15 changes: 15 additions & 0 deletions RESTserver/iotwebui.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=IoTwebUI REST Server
After=network.target

[Service]
Type=simple
# Update the path to your actual installation directory
WorkingDirectory=/opt/IoTwebUI/RESTserver
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
User=pi

[Install]
WantedBy=multi-user.target
34 changes: 28 additions & 6 deletions RESTserver/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,40 @@ let connectedClient = null;
});
});

// Improved getAnswer function using async/await
// Improved getAnswer function using queue to solve race conditions on multi-requests
let requestQueue = [];
let isFetching = false;

async function getAnswer(message) {
if (!connectedClient) {
throw new Error('No connected client');
}
connectedClient.send(message); // Send the request message

return new Promise((resolve, reject) => {
connectedClient.onmessage = ({ data }) => {
resolve(data);
};
});
requestQueue.push({ message, resolve, reject });
processQueue();
});
}

function processQueue() {
if (isFetching || requestQueue.length === 0 || !connectedClient) return;

isFetching = true;
const task = requestQueue.shift();

connectedClient.onmessage = ({ data }) => {
isFetching = false;
task.resolve(data);
processQueue();
};

try {
connectedClient.send(task.message);
} catch(e) {
isFetching = false;
task.reject(e);
processQueue();
}
}

// Define HAPI route for fetching all data
Expand Down