blah
This commit is contained in:
parent
db1b513ad5
commit
1819956bd0
@ -1,9 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const { Sequelize, DataTypes } = require("sequelize");
|
const { Sequelize, DataTypes } = require("sequelize");
|
||||||
const { sequelize } = require("../mySQL");
|
const { sequelize } = require("../mySQL");
|
||||||
const {
|
|
||||||
isValidDateString,
|
|
||||||
} = require("../../Web-Server/functions/validateData");
|
|
||||||
|
|
||||||
//sequelize.sync();
|
//sequelize.sync();
|
||||||
const api_log_Model = sequelize.define(
|
const api_log_Model = sequelize.define(
|
||||||
@ -15,40 +12,22 @@ const api_log_Model = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
validate: {
|
|
||||||
isNumeric: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
ip: {
|
ip: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 45,
|
length: 45,
|
||||||
validate: {
|
|
||||||
isIP: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
time: {
|
time: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 20,
|
length: 20,
|
||||||
validate: {
|
|
||||||
//validate time: new Date().toUTCString(),
|
|
||||||
isValidDateString(value) {
|
|
||||||
if (!isValidDateString(value)) {
|
|
||||||
throw new Error("Time must be a valid date string");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
notEmpty: { msg: "Time cannot be empty" },
|
|
||||||
len: [1, 20],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
method: {
|
method: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validate: {
|
|
||||||
isIn: [["GET", "POST", "PUT", "DELETE"]],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
host: {
|
host: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
@ -59,47 +38,21 @@ const api_log_Model = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validate: {
|
|
||||||
isNumeric: true,
|
|
||||||
len: [1, 3],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Responsesize: {
|
Responsesize: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validate: {
|
|
||||||
isNumeric: true,
|
|
||||||
len: [1, 100],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
referrer: {
|
referrer: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 45,
|
length: 45,
|
||||||
validate: {
|
|
||||||
isString(value) {
|
|
||||||
if (typeof value !== "string") {
|
|
||||||
throw new Error("Referrer must be a string");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
notEmpty: { msg: "Referrer cannot be empty" },
|
|
||||||
len: [1, 45], // Length between 1 and 45 characters
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
userAgent: {
|
userAgent: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 100,
|
length: 100,
|
||||||
validate: {
|
|
||||||
isString(value) {
|
|
||||||
if (typeof value !== "string") {
|
|
||||||
throw new Error("UserAgent must be a string");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
notEmpty: { msg: "UserAgent cannot be empty" },
|
|
||||||
len: [1, 100], // Length between 1 and 100 characters
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
@ -110,7 +63,6 @@ const api_log_Model = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const { Sequelize, DataTypes } = require("sequelize");
|
const { Sequelize, DataTypes } = require("sequelize");
|
||||||
const { sequelize } = require("../mySQL");
|
const { sequelize } = require("../mySQL");
|
||||||
|
const { isAlphaNumericwithSpaces } = require('../../Web-Server/functions/validateData')
|
||||||
|
|
||||||
//sequelize.sync();
|
//sequelize.sync();
|
||||||
const locationModel = sequelize.define(
|
const locationModel = sequelize.define(
|
||||||
@ -19,18 +20,16 @@ const locationModel = sequelize.define(
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
|
unique: true,
|
||||||
validate: {
|
validate: {
|
||||||
notEmpty: { msg: "Name cannot be empty" },
|
notEmpty: true,
|
||||||
len: [1, 20],
|
len: [1, 20],
|
||||||
/*
|
//accept only alphanumeric and spaces
|
||||||
//will not validate this and fail it
|
isAlphaNumericwithSpaces(value){
|
||||||
"hello world" (contains a space)
|
if(!isAlphaNumericwithSpaces(value)){
|
||||||
"hello@123" (contains a symbol)
|
throw new Error('Invalid characters in name')
|
||||||
"" (empty string)
|
}
|
||||||
is: /^[a-z]+$/i, // matches this RegExp
|
}
|
||||||
is: ["^[a-z]+$",'i'],
|
|
||||||
*/
|
|
||||||
is: ["^[a-z0-9]+$", "i"]
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
added_by: {
|
added_by: {
|
||||||
@ -38,9 +37,10 @@ const locationModel = sequelize.define(
|
|||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
validate: {
|
validate: {
|
||||||
notEmpty: { msg: "Added by cannot be empty" },
|
notEmpty: true,
|
||||||
len: [1, 20],
|
len: [1, 20],
|
||||||
is: ["^[a-z0-9]+$", "i"]
|
is: ["^[a-z0-9]+$", "i"],
|
||||||
|
isIn: [['admin', 'system' , 'Admin', 'System']],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
description: {
|
description: {
|
||||||
@ -48,13 +48,12 @@ const locationModel = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
length: 100,
|
length: 100,
|
||||||
validate: {
|
validate: {
|
||||||
notEmpty: { msg: "Description cannot be empty" },
|
notEmpty: true,
|
||||||
len: [1, 100],
|
len: [1, 100],
|
||||||
/*
|
/*
|
||||||
//will not validate this and fail it
|
//will not validate this and fail it
|
||||||
"hello@123" (contains a symbol)
|
"hello@123" (contains a symbol)
|
||||||
"" (empty string)
|
"" (empty string)
|
||||||
|
|
||||||
*/
|
*/
|
||||||
is: ["^[a-zA-Z0-9 ]+$", "i"]
|
is: ["^[a-zA-Z0-9 ]+$", "i"]
|
||||||
},
|
},
|
||||||
|
@ -3,6 +3,7 @@ const { Sequelize, DataTypes } = require("sequelize");
|
|||||||
const { sequelize } = require("../mySQL");
|
const { sequelize } = require("../mySQL");
|
||||||
const { locationModel } = require("./locationModel");
|
const { locationModel } = require("./locationModel");
|
||||||
const { sensorModel } = require("./sensorModel");
|
const { sensorModel } = require("./sensorModel");
|
||||||
|
const { isJson } = require("../../Web-Server/functions/validateData");
|
||||||
|
|
||||||
//sequelize.sync();
|
//sequelize.sync();
|
||||||
const sensorDataModel = sequelize.define(
|
const sensorDataModel = sequelize.define(
|
||||||
@ -13,6 +14,11 @@ const sensorDataModel = sequelize.define(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
|
unique: true,
|
||||||
|
validate: {
|
||||||
|
isNumeric: true,
|
||||||
|
notEmpty: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
id_sensor: {
|
id_sensor: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
@ -23,6 +29,10 @@ const sensorDataModel = sequelize.define(
|
|||||||
model: sensorModel,
|
model: sensorModel,
|
||||||
key: "id",
|
key: "id",
|
||||||
},
|
},
|
||||||
|
validate: {
|
||||||
|
isNumeric: true,
|
||||||
|
notEmpty: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
id_location: {
|
id_location: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
@ -33,10 +43,22 @@ const sensorDataModel = sequelize.define(
|
|||||||
model: locationModel,
|
model: locationModel,
|
||||||
key: "id",
|
key: "id",
|
||||||
},
|
},
|
||||||
|
validate: {
|
||||||
|
isNumeric: true,
|
||||||
|
notEmpty: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
sensordata: {
|
sensordata: {
|
||||||
type: DataTypes.JSON,
|
type: DataTypes.JSON,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
isJson(value)
|
||||||
|
{
|
||||||
|
if (isJson(value) !== true)
|
||||||
|
throw new Error("sensordata must be a JSON");
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
|
@ -2,35 +2,82 @@
|
|||||||
const { Sequelize, DataTypes } = require("sequelize");
|
const { Sequelize, DataTypes } = require("sequelize");
|
||||||
const { sequelize } = require("../mySQL");
|
const { sequelize } = require("../mySQL");
|
||||||
const { locationModel } = require("./locationModel");
|
const { locationModel } = require("./locationModel");
|
||||||
|
const {
|
||||||
|
isAlphaNumericwithSpaces,
|
||||||
|
isAlphaNumericWithSpacesAndDash,
|
||||||
|
isMacAddress,
|
||||||
|
} = require("../../Web-Server/functions/validateData");
|
||||||
|
|
||||||
//sequelize.sync();
|
//sequelize.sync();
|
||||||
const sensorModel = sequelize.define("sensors",
|
const sensorModel = sequelize.define(
|
||||||
|
"sensors",
|
||||||
{
|
{
|
||||||
id: {
|
id: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
|
unique: true,
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
isNumeric: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
sensorname: {
|
sensorname: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
|
unique: true,
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
len: [1, 30],
|
||||||
|
//accept only alphanumeric and spaces
|
||||||
|
isAlphaNumericWithSpacesAndDash(value) {
|
||||||
|
if (!isAlphaNumericWithSpacesAndDash(value)) {
|
||||||
|
throw new Error("Invalid characters in name");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
added_by: {
|
added_by: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 10,
|
length: 10,
|
||||||
|
validate: {
|
||||||
|
notEmpty: { msg: "Added by cannot be empty" },
|
||||||
|
len: [1, 20],
|
||||||
|
is: ["^[a-z0-9]+$", "i"],
|
||||||
|
isIn: [["admin", "system", "Admin", "System"]],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
mac_address: {
|
mac_address: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
length: 12,
|
length: 12,
|
||||||
|
unique: true,
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
len: [12, 18],
|
||||||
|
isMacAddress(value) {
|
||||||
|
if (!isMacAddress(value)) {
|
||||||
|
throw new Error("Invalid Mac Address");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
description: {
|
description: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
length: 100,
|
length: 100,
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
len: [1, 100],
|
||||||
|
isAlphaNumericwithSpaces(value) {
|
||||||
|
if (!isAlphaNumericwithSpaces(value)) {
|
||||||
|
throw new Error("Invalid characters in name");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
location: {
|
location: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
@ -39,8 +86,12 @@ const sensorModel = sequelize.define("sensors",
|
|||||||
//one to many relationship
|
//one to many relationship
|
||||||
references: {
|
references: {
|
||||||
model: locationModel,
|
model: locationModel,
|
||||||
key: 'id'
|
key: "id",
|
||||||
}
|
},
|
||||||
|
validate: {
|
||||||
|
notEmpty: true,
|
||||||
|
isNumeric: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
|
@ -23,7 +23,7 @@ function insertData(data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertLogData(log){
|
async function insertLogData(log){
|
||||||
try{
|
try{
|
||||||
api_log_Model.create({
|
api_log_Model.create({
|
||||||
ip: log.ip,
|
ip: log.ip,
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
var validator = require("validator");
|
||||||
|
|
||||||
// Regular expressions for data validation
|
// Regular expressions for data validation
|
||||||
const psiPattern = /^\d+$/;
|
const psiPattern = /^\d+$/;
|
||||||
const humidityPattern = /^\d+%$/;
|
const humidityPattern = /^\d+%$/;
|
||||||
@ -22,11 +24,52 @@ function validateData(data) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateRegex = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
const dateRegex =
|
||||||
|
/^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
||||||
|
|
||||||
function isValidDateString(){
|
function isValidDateString(value) {
|
||||||
return dateRegex.test(value);
|
return dateRegex.test(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isAlphaNumericwithSpaces(value) {
|
||||||
|
return validator.isAlphanumeric(value, ["en-US"], { ignore: " " });
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = { validateData , isValidDateString };
|
//allow alphanumeric and spaces and -
|
||||||
|
function isAlphaNumericWithSpacesAndDash(value) {
|
||||||
|
const alphanumeric = /^[a-zA-Z0-9]+$/;
|
||||||
|
const valid = value
|
||||||
|
.split("")
|
||||||
|
.every((char) => alphanumeric.test(char) || char === " " || char === "-");
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isMacAddress(value) {
|
||||||
|
// Joi.string().regex(/^([0-9a-f]{2}-){5}([0-9a-f]{2})$/i).lowercase()
|
||||||
|
//return validator.isMACAddress(value, { no_separators: true, eui: 48 });
|
||||||
|
const macAddress = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
|
||||||
|
const valid = macAddress.test(value);
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isJson(value) {
|
||||||
|
//check if its object
|
||||||
|
if(typeof value === "object"){
|
||||||
|
console.log("its an object")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
validateData,
|
||||||
|
isValidDateString,
|
||||||
|
isAlphaNumericwithSpaces,
|
||||||
|
isAlphaNumericWithSpacesAndDash,
|
||||||
|
isMacAddress,
|
||||||
|
isJson,
|
||||||
|
};
|
||||||
|
/*
|
||||||
|
isMACAddress(str [, options])
|
||||||
|
|
||||||
|
*/
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
const { app } = require("./modules/express.js");
|
const { app } = require("./modules/express.js");
|
||||||
const client = require("./modules/mqtt");
|
const client = require("./modules/mqtt");
|
||||||
const { validateData } = require("./functions/validateData.js");
|
const { validateData } = require("./functions/validateData.js");
|
||||||
const { insertData } = require("./functions/Database.js");
|
const { insertData } = require("./functions/database.js");
|
||||||
/*
|
/*
|
||||||
1) validate data from IoT sensor
|
1) validate data from IoT sensor
|
||||||
2) upload data to database
|
2) upload data to database
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
const { getAPIKey } = require('../db/ApiKeys');
|
|
||||||
|
|
||||||
function apiKeyMiddleware(req, res, next) {
|
function apiKeyMiddleware(req, res, next) {
|
||||||
const apiKey = req.headers['x-api-key'];
|
const apiKey = req.headers['x-api-key'];
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const { insertLogData } = require("../functions/Database.js");
|
const { insertLogData } = require("../functions/database.js");
|
||||||
const APIlogger = (req, res, next) => {
|
const APIlogger = (req, res, next) => {
|
||||||
|
try {
|
||||||
const log = {
|
const log = {
|
||||||
ip: req.ip,
|
ip: req.ip,
|
||||||
time: new Date().toUTCString(),
|
time: new Date().toUTCString(),
|
||||||
@ -15,6 +15,10 @@ const APIlogger = (req, res, next) => {
|
|||||||
//upload to db logic here for api logs
|
//upload to db logic here for api logs
|
||||||
insertLogData(log);
|
insertLogData(log);
|
||||||
next();
|
next();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { APIlogger };
|
module.exports = { APIlogger };
|
||||||
|
@ -15,11 +15,11 @@ app.disable("x-powered-by");
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.set("json spaces", 2);
|
app.set("json spaces", 2);
|
||||||
|
|
||||||
//const { APIlogger } = require('../middleware/ApiLogger.js');
|
const { APIlogger } = require('../middleware/apiLogger.js');
|
||||||
|
|
||||||
//middleware logic ( called by next() )
|
//middleware logic ( called by next() )
|
||||||
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
//app.use('/api/v0', require('../middleware/ApiKey.js'));
|
||||||
//app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
app.use('/api/v0', APIlogger, require('../routes/api_route.js'));
|
||||||
|
|
||||||
//route logic
|
//route logic
|
||||||
app.use("/api/v0", require("../routes/api_route.js"));
|
app.use("/api/v0", require("../routes/api_route.js"));
|
||||||
|
@ -6,7 +6,7 @@ const {
|
|||||||
getLocationById,
|
getLocationById,
|
||||||
updateLocation,
|
updateLocation,
|
||||||
deleteLocation,
|
deleteLocation,
|
||||||
} = require("../functions/APIDatabase.js");
|
} = require("../functions/apiDatabase.js");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -15,7 +15,8 @@ const router = express.Router();
|
|||||||
router.get("/", async (req, res, next) => {
|
router.get("/", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const location = await getLocation();
|
const location = await getLocation();
|
||||||
res.json(location);
|
//res send json and status code
|
||||||
|
res.status(200).json(location);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -64,7 +65,7 @@ router.get("/:id", async (req, res, next) => {
|
|||||||
//get params
|
//get params
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const location = await getLocationById(id);
|
const location = await getLocationById(id);
|
||||||
res.json(location);
|
res.status(200).json(location);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
|
@ -22,14 +22,13 @@ router.post("/new", async (req, res, next) => {
|
|||||||
await sensorModel.create({
|
await sensorModel.create({
|
||||||
sensorname: `AQI-${Math.floor(Math.random()*898)+101}`,
|
sensorname: `AQI-${Math.floor(Math.random()*898)+101}`,
|
||||||
added_by: "system",
|
added_by: "system",
|
||||||
//random mac address
|
|
||||||
mac_address: `${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}`,
|
mac_address: `${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}-${Math.floor(Math.random()*256).toString(16).padStart(2, '0')}`,
|
||||||
description: "system generated sensor",
|
description: "system generated sensor",
|
||||||
location: location.id
|
location: location.id
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res.sendStatus(200).json({message: "seeded"})
|
res.sendStatus(200)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
const { sequelize } = require("../../Database/mySql.js");
|
||||||
|
const { locationModel } = require("../../Database/model/locationModel.js");
|
||||||
|
const { sensorModel } = require("../../Database/model/sensorModel.js");
|
||||||
|
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
let mockLocation = []
|
||||||
|
|
||||||
|
//add seed
|
||||||
|
router.post("/new", async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
||||||
|
/*
|
||||||
|
1) get id of sensor and location
|
||||||
|
2) mock data
|
||||||
|
3) take post req for start date and end date
|
||||||
|
4) post to db
|
||||||
|
|
||||||
|
that takes startDate and an option endDate (the defaults to day) and an option interval to declare the polling interval
|
||||||
|
i would have more field TBH, that i would start with that
|
||||||
|
|
||||||
|
*/
|
@ -6,7 +6,7 @@ const {
|
|||||||
updateSensor,
|
updateSensor,
|
||||||
deleteSensor,
|
deleteSensor,
|
||||||
getSensorById
|
getSensorById
|
||||||
} = require("../functions/APIDatabase.js");
|
} = require("../functions/apiDatabase.js");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -14,7 +14,7 @@ const router = express.Router();
|
|||||||
router.get("/", async (req, res, next) => {
|
router.get("/", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const sensor = await getSensor();
|
const sensor = await getSensor();
|
||||||
res.json(sensor);
|
res.status(200).json(sensor);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -25,6 +25,7 @@ router.post("/new", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { sensorname, added_by, mac_address , description, location } = req.body;
|
const { sensorname, added_by, mac_address , description, location } = req.body;
|
||||||
await addSensor(sensorname, added_by, mac_address ,description, location);
|
await addSensor(sensorname, added_by, mac_address ,description, location);
|
||||||
|
res.sendStatus(200)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -35,6 +36,7 @@ router.put("/update", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id, sensorname, added_by, mac_address ,description, location } = req.body;
|
const { id, sensorname, added_by, mac_address ,description, location } = req.body;
|
||||||
await updateSensor(id, sensorname, added_by, mac_address , description, location);
|
await updateSensor(id, sensorname, added_by, mac_address , description, location);
|
||||||
|
res.status(200).json({ message: "Sensor " + id + " updated" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -45,6 +47,7 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.body;
|
const { id } = req.body;
|
||||||
await deleteSensor(id);
|
await deleteSensor(id);
|
||||||
|
res.status(200).json({ message: "Sensor " + id + " deleted" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -54,7 +57,8 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
router.get("/:id", async (req, res, next) => {
|
router.get("/:id", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const sensor = await getSensorById(req.params.id);
|
const sensor = await getSensorById(req.params.id);
|
||||||
res.json(sensor);
|
res.status(200).json(sensor);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
|
@ -7,7 +7,7 @@ const {
|
|||||||
deleteSensorData,
|
deleteSensorData,
|
||||||
getSensorDataById,
|
getSensorDataById,
|
||||||
|
|
||||||
} = require("../functions/APIDatabase.js");
|
} = require("../functions/apiDatabase.js");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const { json } = require("body-parser");
|
const { json } = require("body-parser");
|
||||||
@ -16,7 +16,7 @@ const router = express.Router();
|
|||||||
router.get("/", async (req, res, next) => {
|
router.get("/", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const sensor = await getSensorData();
|
const sensor = await getSensorData();
|
||||||
res.json(sensor);
|
res.status(200).json(sensor);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -25,10 +25,9 @@ router.get("/", async (req, res, next) => {
|
|||||||
|
|
||||||
router.post("/new", async (req, res, next) => {
|
router.post("/new", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
//JSON.parse(d) /* d is the parameter of the method 'add()' */
|
|
||||||
|
|
||||||
const { id, id_sensor, id_location, sensordata } = req.body;
|
const { id, id_sensor, id_location, sensordata } = req.body;
|
||||||
await addSensorData(id , id_sensor , id_location , sensordata);
|
await addSensorData(id , id_sensor , id_location , sensordata);
|
||||||
|
res.sendStatus(200).json({message: "SensorData " + id + " added" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -39,6 +38,7 @@ router.put("/update", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id , id_sensor , id_location , sensordata } = req.body;
|
const { id , id_sensor , id_location , sensordata } = req.body;
|
||||||
await updateSensorData( id , id_sensor , id_location , sensordata );
|
await updateSensorData( id , id_sensor , id_location , sensordata );
|
||||||
|
res.status(200).json({ message: "SensorData " + id + " updated" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -49,6 +49,7 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.body;
|
const { id } = req.body;
|
||||||
await deleteSensorData(id);
|
await deleteSensorData(id);
|
||||||
|
res.status(200).json({ message: "SensorData " + id + " deleted" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
@ -58,7 +59,7 @@ router.delete("/delete", async (req, res, next) => {
|
|||||||
router.get("/:id", async (req, res, next) => {
|
router.get("/:id", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const sensor = await getSensorDataById(req.params.id);
|
const sensor = await getSensorDataById(req.params.id);
|
||||||
res.json(sensor);
|
res.status(200).json(sensor);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(error);
|
next(error);
|
||||||
|
@ -18,18 +18,14 @@ module.exports = router;
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
|
|
||||||
//location route
|
//location route
|
||||||
router.use('/location', require('./Location'));
|
router.use('/location', require('./location'));
|
||||||
|
|
||||||
//sensor route
|
//sensor route
|
||||||
router.use('/sensor', require('./Sensor'))
|
router.use('/sensor', require('./sensor'))
|
||||||
|
|
||||||
//sensor data route
|
//sensor data route
|
||||||
router.use('/sensor-data', require('./SensorData'));
|
router.use('/sensor-data', require('./sensorData'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
router.use('/test' , require('./test'));
|
|
||||||
router.use('/latest-data', require('./latest-data'));
|
router.use('/latest-data', require('./latest-data'));
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,28 +1,9 @@
|
|||||||
const { sequelize } = require("../../Database/mySql.js");
|
const { sequelize } = require("../../Database/mySql.js");
|
||||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
||||||
const { getLatestData } = require("../functions/APIDatabase.js");
|
const { getLatestData } = require("../functions/apiDatabase.js");
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Logic for model and API by 1
|
|
||||||
/*
|
|
||||||
async function getLatestData() {
|
|
||||||
try {
|
|
||||||
sequelize.sync();
|
|
||||||
const latestData = await IoTModel.findAll({
|
|
||||||
limit: 1,
|
|
||||||
order: [['createdAt', 'DESC']]
|
|
||||||
});
|
|
||||||
return latestData;
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const data = await getLatestData();
|
const data = await getLatestData();
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
|
|
||||||
//location route
|
//location route
|
||||||
router.use('/seed', require('./SeedLocationAndSensor'));
|
router.use('/seed', require('./seedLocationAndSensor'));
|
||||||
|
|
||||||
|
router.use('/seedSensorData ', require('./seedsensorData'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
const { sequelize } = require("../../Database/mySql.js");
|
|
||||||
const { IoTModel } = require("../../Database/model/IoTModel.js");
|
|
||||||
const { getallData } = require("../functions/APIDatabase.js");
|
|
||||||
|
|
||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
|
||||||
try {
|
|
||||||
const data = await getallData();
|
|
||||||
|
|
||||||
if (data === null) {
|
|
||||||
res.status(404).send("No data found");
|
|
||||||
} else {
|
|
||||||
res.send(data);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
res.status(500).send('Internal Server Error');
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Export the router
|
|
||||||
module.exports = router;
|
|
25
api.MD
25
api.MD
@ -6,9 +6,11 @@ curl localhost/api/v0/location
|
|||||||
curl http://localhost/api/v0/location/3
|
curl http://localhost/api/v0/location/3
|
||||||
|
|
||||||
//post
|
//post
|
||||||
curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "test", "added_by": "noot" , "description": "test"}'
|
curl localhost/api/v0/location/new -H "Content-Type: application/json" -X POST -d '{"name": "SAMPLE", "added_by": "system" , "description": "test"}'
|
||||||
|
|
||||||
status: 200
|
status: 200
|
||||||
|
added_name allowed: system , admin
|
||||||
|
name allowed: shld contain alphanumeric only
|
||||||
|
|
||||||
//put
|
//put
|
||||||
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
|
curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json" -d '{"id": "2" , "name": "test", "added_by": "noot", "description": "test12344444444"}'
|
||||||
@ -18,7 +20,6 @@ curl localhost/api/v0/location/update -X PUT -H "Content-Type: application/json"
|
|||||||
|
|
||||||
//delete
|
//delete
|
||||||
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
|
curl localhost/api/v0/location/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "6" }'
|
||||||
|
|
||||||
status: 200
|
status: 200
|
||||||
{
|
{
|
||||||
"message": "Location 13 deleted"
|
"message": "Location 13 deleted"
|
||||||
@ -32,13 +33,26 @@ curl localhost/api/v0/sensor
|
|||||||
curl http://localhost/api/v0/sensor/3
|
curl http://localhost/api/v0/sensor/3
|
||||||
|
|
||||||
//POST
|
//POST
|
||||||
curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensorname": "test", "added_by": "noot" , "mac_address": "99-6A-F8-7D-B4-90", "description": "test" , "location": "2"}'
|
curl localhost/api/v0/sensor/new -H "Content-Type: application/json" -X POST -d '{"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94", "description": "test" , "location": "11"}'
|
||||||
|
|
||||||
|
status: 200
|
||||||
|
added_name allowed: system , admin
|
||||||
|
sensor name : shld contain alphanumeric only and be unique
|
||||||
|
|
||||||
//put
|
//put
|
||||||
curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensorname": "test", "added_by": "noot" , "mac_address": "99-6A-F8-7D-B4-90" , "description": "test123333333" , "location": "3" }'
|
curl localhost/api/v0/sensor/update -H "Content-Type: application/json" -X PUT -d '{"id": "2" ,"sensorname": "test", "added_by": "system" , "mac_address": "99-6A-F8-7D-B4-94" , "description": "test123" , "location": "11" }'
|
||||||
|
|
||||||
|
status: 200
|
||||||
|
added_name allowed: system , admin
|
||||||
|
sensor name : shld contain alphanumeric only and be unique
|
||||||
|
"message": "Sensor 13 updated"
|
||||||
|
|
||||||
//delete
|
//delete
|
||||||
curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }'
|
curl localhost/api/v0/sensor/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "2" }'
|
||||||
|
{
|
||||||
|
"message": "sensor 13 deleted"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//sensor data
|
//sensor data
|
||||||
curl http://localhost/api/v0/sensor-data/
|
curl http://localhost/api/v0/sensor-data/
|
||||||
@ -48,7 +62,7 @@ curl http://localhost/api/v0/sensor-data/1
|
|||||||
|
|
||||||
|
|
||||||
//post
|
//post
|
||||||
curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POST -d '{"id": "2", "id_sensor": "1" , "id_location": "2" , "sensordata": {
|
curl localhost/api/v0/sensor-data/new -H "Content-Type: application/json" -X POST -d '{"id_sensor": "1" , "id_location": "11" , "sensordata": {
|
||||||
"psi": "34",
|
"psi": "34",
|
||||||
"humidity": "11%",
|
"humidity": "11%",
|
||||||
"o3": "326ppm",
|
"o3": "326ppm",
|
||||||
@ -79,5 +93,4 @@ curl localhost/api/v0/sensor-data/update -H "Content-Type: application/json" -X
|
|||||||
curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }'
|
curl localhost/api/v0/sensor-data/delete -X DELETE -H "Content-Type: application/json" -d '{"id": "3" }'
|
||||||
|
|
||||||
//seed
|
//seed
|
||||||
|
|
||||||
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["test", "test123"]}'
|
curl localhost/api/seed/v0/seed/new -H "Content-Type: application/json" -X POST -d '{"mockLocation": ["test", "test123"]}'
|
||||||
|
1611
package-lock.json
generated
1611
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user