This commit is contained in:
BIG2EYEZ 2023-12-17 20:46:42 +08:00
commit 4921411ed4
10 changed files with 860 additions and 568 deletions

View File

@ -0,0 +1,45 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../mySQL.js")
const adminUserModel = sequelize.define('adminusers', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
},
jobTitle: {
type: DataTypes.STRING,
allowNull: true,
length: 255
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { adminUserModel }

View File

@ -0,0 +1,40 @@
'use strict';
const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require("../mySQL.js")
const userModel = sequelize.define('users', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
username: {
type: DataTypes.STRING,
allowNull: true,
length: 50
},
email: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
password: {
type: DataTypes.STRING,
allowNull: true,
length: 255
},
lastLogin: {
type: DataTypes.timestamps,
allowNull: true,
}
},{
timestamps: false, // Disable automatic timestamps
});
module.exports = { userModel }

View File

@ -0,0 +1,21 @@
require("dotenv").config({ path: "../.env" });
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
"adminusers",
process.env.DB_USER,
process.env.DB_PASS,
{
host: "mpsqldatabasean.mysql.database.azure.com",
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 };

60
IoT-sensor/IoT-sensor.js Normal file
View File

@ -0,0 +1,60 @@
/*
1) PSI metric data
2) Humidity
3) Gases (O3,NO2,SO2)
4) temperature
5) Air pressure?
6) windspeed?
8) time when data was collected / generated
*/
/*
1) generate random data for each sensor
3) send the coap request to the server
*/
const { isNumber } = require("./functions/validateData");
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 temperatureData = getRandomValue(24, 40);
const windspeedData = getRandomValue(0, 35);
const currentTime = new Date(Date.now() + 28800000)
.toISOString()
.slice(0, 19)
.replace("T", " ");
var json = {
psi: psiData.toFixed(0),
humidity: humidityData.toFixed(0) + "%",
o3: o3Data.toFixed(0) + "ppm",
no2: no2Data.toFixed(0) + "ppm",
so2: so2Data.toFixed(0) + "ppm",
temperature: temperatureData.toFixed(0) + "°C",
windspeed: windspeedData.toFixed(0) + "km/h",
time: currentTime,
};
return json;
}
function getRandomValue(min, max) {
return Math.random() * (max - min) + min;
}
//5 minutes
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 300000);
/*
setInterval(() => {
var json = generateRandomData();
console.log(json);
}, 600);
*/

View File

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

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();

View File

View File

@ -7,7 +7,6 @@ const app = express();
const PORT = process.env.PORT || 3000;
require('dotenv').config();
// MySQL setup (replace with your MySQL connection details)
const mysqlConfig = {
host: process.env.host,
user: process.env.user,

1218
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,7 @@
"express-session": "^1.17.3",
"mysql": "^2.18.1",
"mysql2": "^3.6.5",
"sequelize": "^6.35.2"
"sequelize": "^6.35.2",
"validator": "^13.11.0"
}
}