express routing

This commit is contained in:
newtbot
2023-12-19 18:51:23 +08:00
parent e9ed0144aa
commit 7c53b362eb
11 changed files with 696 additions and 41 deletions

View File

@ -15,18 +15,22 @@
const { isNumber } = require("./functions/validateData");
let region = ["central", "north-east", "north", "east", "west"];
function generateRandomData() {
const psiData = getRandomValue(0, 500);
const humidityData = getRandomValue(0, 100);
const o3Data = getRandomValue(0, 600); //max 600
const no2Data = getRandomValue(0, 1000); //max 1000
const so2Data = getRandomValue(0, 1000); //max 1000
const coData = getRandomValue(0 , 100);
const temperatureData = getRandomValue(24, 40);
const windspeedData = getRandomValue(0, 35);
const currentTime = new Date(Date.now() + 28800000)
.toISOString()
.slice(0, 19)
.replace("T", " ");
const regionData = region[Math.floor(Math.random() * region.length)];
var json = {
psi: psiData.toFixed(0),
@ -34,27 +38,28 @@ function generateRandomData() {
o3: o3Data.toFixed(0) + "ppm",
no2: no2Data.toFixed(0) + "ppm",
so2: so2Data.toFixed(0) + "ppm",
co: coData.toFixed(0) + "ppm",
temperature: temperatureData.toFixed(0) + "°C",
windspeed: windspeedData.toFixed(0) + "km/h",
time: currentTime,
region: regionData,
};
return json;
return json;
}
function getRandomValue(min, max) {
return Math.random() * (max - min) + min;
}
function iot_sensor_data() {
//5 minutes
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 600); //every 1 second
}
//5 minutes
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 300000);
iot_sensor_data()
module.exports = { iot_sensor_data }
/*
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 600);
*/

View File

@ -1,24 +0,0 @@
const coap = require('coap');
const serverUri = 'coap://localhost:5683';
// Create a CoAP request
const req = coap.request({
hostname: 'localhost', // Replace with your server's hostname
port: 5683, // Replace with your server's port
method: 'PUT', // Use the CoAP method you need (e.g., PUT, POST)
pathname: '/resource', // Replace with your server's resource path
});
// Set the payload (data to be sent to the server)
const payload = 'Hello, CoAP Server!'; // Replace with your data
req.write(payload);
// Event handler for the response
req.on('response', (res) => {
console.log('CoAP server responded with:', res.payload.toString());
req.end();
});
// Send the CoAP request
req.end();