database presetup

This commit is contained in:
newtbot 2023-12-17 04:05:37 +08:00
parent 662744558d
commit ca0d24248c
4 changed files with 61 additions and 13 deletions

View File

@ -1 +1,21 @@
require("dotenv").config({ path: "../../.env" });
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
"your_database_name",
process.env.
process.env.,
{
host: '',
dialect: 'mysql'
}
);
sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch((error) => {
console.error('Unable to connect to the database: ', error);
});
module.exports = { sequelize };

View File

@ -10,7 +10,7 @@
/*
1) generate random data for each sensor
2) validate the data
3) send the coap request to the server
*/
const { isNumber } = require("./functions/validateData");
@ -38,16 +38,6 @@ function generateRandomData() {
windspeed: windspeedData.toFixed(0) + "km/h",
time: currentTime,
};
/*
console.log(json.psi);
console.log(json.humidity);
console.log(json.o3);
console.log(json.no2);
console.log(json.so2);
console.log(json.temperature);
console.log(json.windspeed);
console.log(json.time);
*/
return json;
}
@ -55,9 +45,16 @@ function getRandomValue(min, max) {
return Math.random() * (max - min) + min;
}
//5 minutes
setInterval(() => {
var json = generateRandomData();
isNumber(json);
console.log(json);
}, 300000);
/*
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 600);
*/

View File

@ -1,7 +1,14 @@
var validator = require('validator');
function isNumber(data) {
console.log("isNumber");
if (validator.isNumeric(data))
{
console.log(data);
}
else
{
console.log("Invalid data");
}
}

View File

@ -0,0 +1,24 @@
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();