overhaul
This commit is contained in:
47
consumerWebsite/functions/api.js
Normal file
47
consumerWebsite/functions/api.js
Normal file
@ -0,0 +1,47 @@
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { apikeyModel } = require("../database/model/apiKeyModel");
|
||||
const { generateUUID } = require("./generateUUID.js");
|
||||
|
||||
/*
|
||||
1) take userid
|
||||
2) generate random api key
|
||||
3) hash the api key
|
||||
4) append userid with - and api key
|
||||
5) you give the user rowid-uuidv4
|
||||
6) store in database
|
||||
*/
|
||||
//can be used for api key or token. Both are the same logic
|
||||
async function addAPIKey(userId, permission) {
|
||||
let hashtoken = await generateUUID();
|
||||
let apikey = await hashAPIKey(hashtoken);
|
||||
|
||||
let token = await apikeyModel.create({
|
||||
userid: userId,
|
||||
apikey: apikey,
|
||||
permission: permission,
|
||||
});
|
||||
|
||||
//user token with - tokenid is table id
|
||||
return token.id + "-" + hashtoken;
|
||||
}
|
||||
|
||||
async function checkAPikey(SuppliedKey, rowid) {
|
||||
try {
|
||||
const retrivedKey = await apikeyModel.findOne({
|
||||
raw: true,
|
||||
attributes: ["apikey", "permission"],
|
||||
where: {
|
||||
id: rowid,
|
||||
},
|
||||
});
|
||||
//console.log(retrivedKey.apikey);
|
||||
if (compareHash(SuppliedKey, retrivedKey.apikey)) {
|
||||
//return true;
|
||||
return retrivedKey.permission;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { addAPIKey , checkAPikey };
|
@ -23,23 +23,18 @@ bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
|
||||
*/
|
||||
|
||||
//hash for pass or token lol doesnt matter
|
||||
async function hashPassword(password) {
|
||||
async function hash(password) {
|
||||
return await bcrypt.hash(password, saltRounds);
|
||||
}
|
||||
|
||||
async function hashAPIKey(apikey) {
|
||||
return await bcrypt.hash(apikey, saltRounds);
|
||||
}
|
||||
|
||||
//can be used to compare password or token
|
||||
async function comparePassword(password, hash) {
|
||||
async function compareHash(password, hash) {
|
||||
return await bcrypt.compare(password, hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
hashAPIKey,
|
||||
comparePassword
|
||||
hash,
|
||||
compareHash
|
||||
};
|
55
consumerWebsite/functions/location.js
Normal file
55
consumerWebsite/functions/location.js
Normal file
@ -0,0 +1,55 @@
|
||||
const {locationModel} = require("../database/model/locationModel");
|
||||
|
||||
async function getLocation() {
|
||||
const location = await locationModel.findAll();
|
||||
return location;
|
||||
}
|
||||
|
||||
async function addLocation(name, added_by, description) {
|
||||
const location = await locationModel.create({
|
||||
name: name,
|
||||
added_by: added_by,
|
||||
description: description,
|
||||
});
|
||||
}
|
||||
|
||||
async function updateLocation(id, name, added_by, description) {
|
||||
const location = await locationModel.update(
|
||||
{
|
||||
name: name,
|
||||
added_by: added_by,
|
||||
description: description,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteLocation(id) {
|
||||
//delete by id
|
||||
const location = await locationModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function getLocationById(id) {
|
||||
const location = await locationModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return location;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLocation,
|
||||
addLocation,
|
||||
updateLocation,
|
||||
deleteLocation,
|
||||
getLocationById,
|
||||
};
|
23
consumerWebsite/functions/logger.js
Normal file
23
consumerWebsite/functions/logger.js
Normal file
@ -0,0 +1,23 @@
|
||||
const { api_log_Model } = require("../database/model/apiLogModel.js");
|
||||
|
||||
|
||||
async function insertLogData(log) {
|
||||
try {
|
||||
api_log_Model.create({
|
||||
ip: log.ip,
|
||||
time: log.time,
|
||||
method: log.method,
|
||||
host: log.host,
|
||||
statusCode: log.statusCode,
|
||||
Responsesize: log.Responsesize,
|
||||
referrer: log.referrer,
|
||||
userAgent: log.userAgent,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
insertLogData,
|
||||
};
|
75
consumerWebsite/functions/sensor.js
Normal file
75
consumerWebsite/functions/sensor.js
Normal file
@ -0,0 +1,75 @@
|
||||
const {sensorModel} = require("../database/model/sensorModel");
|
||||
|
||||
|
||||
async function getSensor() {
|
||||
const sensor = await sensorModel.findAll();
|
||||
return sensor;
|
||||
}
|
||||
|
||||
async function addSensor(
|
||||
sensorname,
|
||||
added_by,
|
||||
mac_address,
|
||||
description,
|
||||
location
|
||||
) {
|
||||
const sensor = await sensorModel.create({
|
||||
name: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
location: location,
|
||||
});
|
||||
}
|
||||
|
||||
async function updateSensor(
|
||||
id,
|
||||
sensorname,
|
||||
added_by,
|
||||
mac_address,
|
||||
description,
|
||||
location
|
||||
) {
|
||||
const sensor = await sensorModel.update(
|
||||
{
|
||||
name: sensorname,
|
||||
added_by: added_by,
|
||||
mac_address: mac_address,
|
||||
description: description,
|
||||
location: location,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteSensor(id) {
|
||||
//delete by id
|
||||
const sensor = await sensorModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
async function getSensorById(id) {
|
||||
const sensor = await sensorModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return sensor;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSensor,
|
||||
addSensor,
|
||||
updateSensor,
|
||||
deleteSensor,
|
||||
getSensorById,
|
||||
};
|
728
consumerWebsite/functions/sensorData.js
Normal file
728
consumerWebsite/functions/sensorData.js
Normal file
@ -0,0 +1,728 @@
|
||||
const { Op, Sequelize } = require("sequelize");
|
||||
const { sequelize } = require("../database/mySQL.js");
|
||||
const { sensorDataModel } = require("../database/model/sensorDataModel.js");
|
||||
const io = require('../functions/socket');
|
||||
|
||||
//helper function to convert month name to month number
|
||||
//https://stackoverflow.com/questions/13566552/easiest-way-to-convert-month-name-to-month-number-in-js-jan-01
|
||||
function getMonthFromString(mon) {
|
||||
var d = Date.parse(mon + "1, 2012");
|
||||
if (!isNaN(d)) {
|
||||
return new Date(d).getMonth() + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
async function getSensorData() {
|
||||
const sensorData = await sensorDataModel.findAll();
|
||||
return sensorData;
|
||||
}
|
||||
|
||||
async function addSensorData(id_sensor, id_location, sensordata) {
|
||||
const sensorData = await sensorDataModel.create({
|
||||
sensorid: id_sensor,
|
||||
locationid: id_location,
|
||||
measurement: sensordata,
|
||||
});
|
||||
io().emit('sensordata:new', sensorData)
|
||||
|
||||
return sensorData;
|
||||
}
|
||||
|
||||
async function updateSensorData(id, id_sensor, id_location, sensordata) {
|
||||
const sensorData = await sensorDataModel.update(
|
||||
{
|
||||
ensorid: id_sensor,
|
||||
locationid: id_location,
|
||||
measurement: sensordata,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteSensorData(id) {
|
||||
const sensorData = await sensorDataModel.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function getSensorDataById(id) {
|
||||
const sensorData = await sensorDataModel.findAll({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
return sensorData;
|
||||
}
|
||||
var ormQuery = {};
|
||||
var whereClause = {};
|
||||
var whereDate = {};
|
||||
const allowedQuery = [
|
||||
"limit",
|
||||
"order",
|
||||
"year",
|
||||
"month",
|
||||
"week",
|
||||
"day",
|
||||
"hour",
|
||||
"minute",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
];
|
||||
const validMonths = [
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8",
|
||||
"9",
|
||||
"10",
|
||||
"11",
|
||||
"12",
|
||||
];
|
||||
//handle buildfunc for query
|
||||
buildQuery = {
|
||||
limit: async function (queryString) {
|
||||
if (queryString.limit !== undefined) {
|
||||
ormQuery.limit = parseInt(queryString.limit);
|
||||
}
|
||||
},
|
||||
order: async function (queryString) {
|
||||
if (queryString.order !== undefined) {
|
||||
ormQuery = {
|
||||
...ormQuery,
|
||||
order: [["createdAt", queryString.order.toUpperCase()]],
|
||||
};
|
||||
}
|
||||
},
|
||||
year: async function (queryString) {
|
||||
if (queryString.year !== undefined) {
|
||||
//whereclause assign a value
|
||||
whereClause.year = sequelize.where(
|
||||
sequelize.fn("YEAR", sequelize.col("createdAt")),
|
||||
queryString.year
|
||||
);
|
||||
}
|
||||
},
|
||||
month: async function (queryString) {
|
||||
if (queryString.month !== undefined) {
|
||||
if (validMonths.includes(queryString.month)) {
|
||||
whereClause.month = sequelize.where(
|
||||
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||
queryString.month
|
||||
);
|
||||
} else {
|
||||
queryString.month = getMonthFromString(queryString.month)
|
||||
whereClause.month = sequelize.where(
|
||||
sequelize.fn("MONTH", sequelize.col("createdAt")),
|
||||
queryString.month
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
week: async function (queryString) {
|
||||
if (queryString.week !== undefined) {
|
||||
whereClause.week = sequelize.where(
|
||||
sequelize.fn("WEEK", sequelize.col("createdAt")),
|
||||
queryString.week
|
||||
);
|
||||
}
|
||||
},
|
||||
day: async function (queryString) {
|
||||
if (queryString.day !== undefined) {
|
||||
whereClause.day = sequelize.where(
|
||||
sequelize.fn("DAY", sequelize.col("createdAt")),
|
||||
queryString.day
|
||||
);
|
||||
}
|
||||
},
|
||||
hour: async function (queryString) {
|
||||
if (queryString.hour !== undefined) {
|
||||
whereClause.hour = sequelize.where(
|
||||
sequelize.fn("HOUR", sequelize.col("createdAt")),
|
||||
queryString.hour
|
||||
);
|
||||
}
|
||||
},
|
||||
minute: async function (queryString) {
|
||||
if (queryString.minute !== undefined) {
|
||||
whereClause.minute = sequelize.where(
|
||||
sequelize.fn("MINUTE", sequelize.col("createdAt")),
|
||||
queryString.minute
|
||||
);
|
||||
}
|
||||
},
|
||||
sensorid: async function (queryString) {
|
||||
if (queryString.sensorid !== undefined) {
|
||||
whereClause.sensorid = sequelize.where(
|
||||
sequelize.col("sensorid"),
|
||||
queryString.sensorid
|
||||
);
|
||||
}
|
||||
},
|
||||
locationid: async function (queryString) {
|
||||
if (queryString.locationid !== undefined) {
|
||||
whereClause.locationid = sequelize.where(
|
||||
sequelize.col("locationid"),
|
||||
queryString.locationid
|
||||
);
|
||||
}
|
||||
},
|
||||
psi: async function (queryString) {
|
||||
if (queryString.psi !== undefined && queryString.psi === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "psi"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("psi"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.psi !== undefined && queryString.psi === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.psi')"), "psi"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("psi"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
co: async function (queryString) {
|
||||
if (queryString.co !== undefined && queryString.co === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "co"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("co"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.co !== undefined && queryString.co === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.co')"), "co"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("co"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
o3: async function (queryString) {
|
||||
if (queryString.o3 !== undefined && queryString.o3 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "o3"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("o3"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.o3 !== undefined && queryString.o3 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.o3')"), "o3"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("o3"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
no2: async function (queryString) {
|
||||
if (queryString.no2 !== undefined && queryString.no2 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "no2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("no2"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.no2 !== undefined && queryString.no2 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.no2')"), "no2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("no2"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
so2: async function (queryString) {
|
||||
if (queryString.so2 !== undefined && queryString.so2 === "highest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "so2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("so2"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (queryString.so2 !== undefined && queryString.so2 === "lowest") {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[sequelize.literal("JSON_EXTRACT(measurement, '$.so2')"), "so2"],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("so2"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
humidity: async function (queryString) {
|
||||
if (
|
||||
queryString.humidity !== undefined &&
|
||||
queryString.humidity === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"),
|
||||
"humidity",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("humidity"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.humidity !== undefined &&
|
||||
queryString.humidity === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.humidity')"),
|
||||
"humidity",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("humidity"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
windspeed: async function (queryString) {
|
||||
if (
|
||||
queryString.windspeed !== undefined &&
|
||||
queryString.windspeed === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"),
|
||||
"windspeed",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("windspeed"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.windspeed !== undefined &&
|
||||
queryString.windspeed === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.windspeed')"),
|
||||
"windspeed",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("windspeed"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
temperature: async function (queryString) {
|
||||
if (
|
||||
queryString.temperature !== undefined &&
|
||||
queryString.temperature === "highest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"),
|
||||
"temperature",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("temperature"), "DESC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
if (
|
||||
queryString.temperature !== undefined &&
|
||||
queryString.temperature === "lowest"
|
||||
) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
"id",
|
||||
"sensorid",
|
||||
"locationid",
|
||||
[
|
||||
sequelize.literal("JSON_EXTRACT(measurement, '$.temperature')"),
|
||||
"temperature",
|
||||
],
|
||||
"createdAt",
|
||||
],
|
||||
group: ["id", "sensorid", "locationid"],
|
||||
order: [[sequelize.literal("temperature"), "ASC"]],
|
||||
limit: 10,
|
||||
};
|
||||
}
|
||||
},
|
||||
//average
|
||||
avg: async function (queryString) {
|
||||
if (queryString.avg !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
//round to 2 decimal places
|
||||
[
|
||||
sequelize.fn(
|
||||
"ROUND",
|
||||
sequelize.fn(
|
||||
"AVG",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.avg}')`
|
||||
)
|
||||
),
|
||||
2
|
||||
),
|
||||
"avg of " + queryString.avg,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
sum: async function (queryString) {
|
||||
if (queryString.sum !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[
|
||||
sequelize.fn(
|
||||
"SUM",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.sum}')`
|
||||
)
|
||||
),
|
||||
"sum of " + queryString.sum,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
//total number of records
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
buildFunc = {
|
||||
limit: async function (queryString) {
|
||||
if (queryString.limit !== undefined) {
|
||||
ormQuery.limit = parseInt(queryString.limit);
|
||||
}
|
||||
},
|
||||
startdate: async function (queryString) {
|
||||
if (queryString.startdate !== undefined) {
|
||||
whereDate.startdate = new Date(queryString.startdate);
|
||||
}
|
||||
},
|
||||
enddate: async function (queryString) {
|
||||
if (queryString.enddate !== undefined) {
|
||||
whereDate.enddate = new Date(queryString.enddate);
|
||||
}
|
||||
},
|
||||
//total by startdate and enddate
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
//average
|
||||
avg: async function (queryString) {
|
||||
if (queryString.avg !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
//round to 2 decimal places
|
||||
[
|
||||
sequelize.fn(
|
||||
"ROUND",
|
||||
sequelize.fn(
|
||||
"AVG",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.avg}')`
|
||||
)
|
||||
),
|
||||
2
|
||||
),
|
||||
"avg of " + queryString.avg,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
sum: async function (queryString) {
|
||||
if (queryString.sum !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[
|
||||
sequelize.fn(
|
||||
"SUM",
|
||||
Sequelize.literal(
|
||||
`JSON_EXTRACT(measurement, '$.${queryString.sum}')`
|
||||
)
|
||||
),
|
||||
"sum of " + queryString.sum,
|
||||
],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
//total number of records
|
||||
total: async function (queryString) {
|
||||
if (queryString.total !== undefined) {
|
||||
ormQuery = {
|
||||
attributes: [
|
||||
[sequelize.fn("COUNT", sequelize.col("id")), "total id / records"],
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
async function getData(queryString) {
|
||||
if (queryString.pagesize || queryString.page) {
|
||||
//https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041
|
||||
//pass pageSize taken from page=4 or default to 50
|
||||
queryString.pagesize = queryString.pagesize || 50;
|
||||
let offset = (queryString.page || 0) * queryString.pagesize;
|
||||
queryString.limit = queryString.pagesize;
|
||||
//reset keys in whereClause and ormQuery. else it will keep appending to the previous query
|
||||
ormQuery = {};
|
||||
whereClause = {};
|
||||
whereDate = {};
|
||||
|
||||
for (let query in queryString) {
|
||||
if (buildQuery[query]) {
|
||||
await buildQuery[query](queryString);
|
||||
}
|
||||
}
|
||||
if (!whereClause) {
|
||||
return await sensorDataModel.findAll(ormQuery);
|
||||
} else if (whereClause) {
|
||||
console.log(whereClause);
|
||||
console.log(ormQuery);
|
||||
console.log(whereDate);
|
||||
return await sensorDataModel.findAll({
|
||||
limit: queryString.limit || 1000000,
|
||||
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#limits-and-pagination
|
||||
offset: parseInt(offset),
|
||||
//The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons.
|
||||
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor
|
||||
where: {
|
||||
[Op.and]: [whereClause],
|
||||
},
|
||||
//only use where clause to lookup based on condition that i put into whereClause
|
||||
...ormQuery,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
//reset keys in whereClause and ormQuery. else it will keep appending to the previous query
|
||||
ormQuery = {};
|
||||
whereClause = {};
|
||||
whereDate = {};
|
||||
|
||||
for (let query in queryString) {
|
||||
if (buildQuery[query]) {
|
||||
await buildQuery[query](queryString);
|
||||
}
|
||||
}
|
||||
if (!whereClause) {
|
||||
return await sensorDataModel.findAll(ormQuery);
|
||||
} else if (whereClause) {
|
||||
console.log(whereClause);
|
||||
console.log(ormQuery);
|
||||
console.log(whereDate);
|
||||
return await sensorDataModel.findAll({
|
||||
limit: queryString.limit || 1000000,
|
||||
//The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons.
|
||||
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor
|
||||
where: {
|
||||
[Op.and]: [whereClause],
|
||||
},
|
||||
//only use where clause to lookup based on condition that i put into whereClause
|
||||
...ormQuery,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getDatabyRange(queryString) {
|
||||
if (queryString.pagesize || queryString.page) {
|
||||
//https://blog.bitsrc.io/pagination-with-sequelize-explained-83054df6e041
|
||||
//pass pageSize taken from page=4 or default to 50
|
||||
queryString.pagesize = queryString.pagesize || 50;
|
||||
let offset = (queryString.page || 0) * queryString.pagesize;
|
||||
queryString.limit = queryString.pagesize;
|
||||
|
||||
whereDate = {};
|
||||
for (let query in queryString) {
|
||||
if (buildFunc[query]) {
|
||||
await buildFunc[query](queryString);
|
||||
}
|
||||
}
|
||||
if (whereClause) {
|
||||
console.log(ormQuery);
|
||||
console.log(whereDate);
|
||||
return await sensorDataModel.findAll({
|
||||
limit: queryString.limit || 1000000,
|
||||
offset: offset,
|
||||
//The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons.
|
||||
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor
|
||||
where: {
|
||||
createdAt: {
|
||||
[Op.between]: [whereDate.startdate, whereDate.enddate],
|
||||
},
|
||||
},
|
||||
//only use where clause to lookup based on condition that i put into whereClause
|
||||
...ormQuery,
|
||||
});
|
||||
} else {
|
||||
return "Invalid query";
|
||||
}
|
||||
} else {
|
||||
whereDate = {};
|
||||
for (let query in queryString) {
|
||||
if (buildFunc[query]) {
|
||||
await buildFunc[query](queryString);
|
||||
}
|
||||
}
|
||||
if (whereClause) {
|
||||
console.log(ormQuery);
|
||||
console.log(whereDate);
|
||||
return await sensorDataModel.findAll({
|
||||
limit: queryString.limit || 1000000,
|
||||
//The operators Op.and, Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons.
|
||||
//https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#examples-with-opand-and-opor
|
||||
where: {
|
||||
createdAt: {
|
||||
[Op.between]: [whereDate.startdate, whereDate.enddate],
|
||||
},
|
||||
},
|
||||
//only use where clause to lookup based on condition that i put into whereClause
|
||||
...ormQuery,
|
||||
});
|
||||
} else {
|
||||
return "Invalid query";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSensorData,
|
||||
addSensorData,
|
||||
updateSensorData,
|
||||
deleteSensorData,
|
||||
getSensorDataById,
|
||||
getData,
|
||||
getDatabyRange,
|
||||
|
||||
};
|
17
consumerWebsite/functions/socket.js
Normal file
17
consumerWebsite/functions/socket.js
Normal file
@ -0,0 +1,17 @@
|
||||
const app = require("../app");
|
||||
const io = ()=> app.io;
|
||||
|
||||
// We have to wait for the express HTTP server to be finished starting before we
|
||||
// can use any of the socket.io stuff.
|
||||
app.onListen.push(function(){
|
||||
app.io.on('connection', (socket) => {
|
||||
console.log('User connected via WebsSocket')
|
||||
|
||||
socket.on('disconnect', (socket) => {
|
||||
console.log('User disconnect via WebsSocket')
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
module.exports = io;
|
@ -1,13 +1,44 @@
|
||||
const { sequelize } = require("../database/mySql.js");
|
||||
const { apikeyModel } = require("../database/model/apikeyModel.js");
|
||||
const { userModel } = require("../database/model/userModel.js");
|
||||
const { Op, Sequelize } = require("sequelize");
|
||||
const { generateUUID } = require("../functions/generateUUID.js");
|
||||
const {
|
||||
hashPassword,
|
||||
comparePassword,
|
||||
hashAPIKey,
|
||||
} = require("../functions/bcrypt.js");
|
||||
const { Op } = require('sequelize')
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { addAPIKey } = require("./api");
|
||||
const { userModel } = require("../database/model/userModel");
|
||||
|
||||
|
||||
|
||||
//getuser
|
||||
//api/v0/user/me
|
||||
async function getUserID(userid) {
|
||||
//console.log(userid);
|
||||
//console.log(userid.id);
|
||||
let userRes = await userModel.findByPk(userid.id, {
|
||||
attributes: {
|
||||
exclude: ["password"],
|
||||
},
|
||||
});
|
||||
|
||||
if (!userRes) return false;
|
||||
return userRes;
|
||||
}
|
||||
|
||||
async function addUser(user) {
|
||||
//hash password
|
||||
let hashed = await hash(user.password);
|
||||
|
||||
const addRes = await userModel.create({
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
username: user.username,
|
||||
password: hashed,
|
||||
email: user.email,
|
||||
address: user.address,
|
||||
phone: user.phone,
|
||||
});
|
||||
if (addRes) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//getuser
|
||||
//api/v0/user/me
|
||||
@ -32,13 +63,13 @@ async function getUserID(userid) {
|
||||
*/
|
||||
async function addUser(user) {
|
||||
//hash password
|
||||
let hash = await hashPassword(user.password);
|
||||
let hashed = await hash(user.password);
|
||||
|
||||
const addRes = await userModel.create({
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
username: user.username,
|
||||
password: hash,
|
||||
password: hashed,
|
||||
email: user.email,
|
||||
address: user.address,
|
||||
phone: user.phone,
|
||||
@ -70,7 +101,7 @@ async function loginUser(user) {
|
||||
if (!userRes) return false;
|
||||
|
||||
// Compare passwords
|
||||
let match = await comparePassword(user.password, userRes.password);
|
||||
let match = await compareHash(user.password, userRes.password);
|
||||
if (!match) return false;
|
||||
//console.log('loginUser', userRes.id, userRes.username);
|
||||
|
||||
@ -89,20 +120,6 @@ async function loginUser(user) {
|
||||
6) store in database
|
||||
*/
|
||||
|
||||
//can be used for api key or token. Both are the same logic
|
||||
async function addAPIKey(userId, permission) {
|
||||
let hashtoken = await generateUUID();
|
||||
let apikey = await hashAPIKey(hashtoken);
|
||||
|
||||
let token = await apikeyModel.create({
|
||||
userid: userId,
|
||||
apikey: apikey,
|
||||
permission: permission,
|
||||
});
|
||||
|
||||
//user token with - tokenid is table id
|
||||
return token.id + "-" + hashtoken;
|
||||
}
|
||||
|
||||
//api/v0/user/update
|
||||
async function updateProfile(user, body) {
|
||||
@ -125,7 +142,7 @@ async function updateProfile(user, body) {
|
||||
if (!updateUser) return false;
|
||||
return true;
|
||||
} else {
|
||||
let hash = await hashPassword(body.password);
|
||||
let hashed = await hash(body.password);
|
||||
let updateUser = await userModel.update(
|
||||
{
|
||||
firstname: body.first_name,
|
||||
@ -134,7 +151,7 @@ async function updateProfile(user, body) {
|
||||
email: body.email,
|
||||
address: body.address,
|
||||
phone: body.phone,
|
||||
password: hash,
|
||||
password: hashed,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
@ -152,5 +169,4 @@ module.exports = {
|
||||
addUser,
|
||||
loginUser,
|
||||
updateProfile,
|
||||
addAPIKey,
|
||||
};
|
||||
};
|
@ -1,5 +1,27 @@
|
||||
var validator = require("validator");
|
||||
|
||||
const dateRegex = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
||||
|
||||
function isValidDateString(value) {
|
||||
return dateRegex.test(value);
|
||||
}
|
||||
|
||||
|
||||
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 isNumber(value) {
|
||||
if (typeof value === "number") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function isAlphaNumericwithSpaces(value) {
|
||||
return validator.isAlphanumeric(value, ["en-US"], { ignore: " " });
|
||||
}
|
||||
@ -16,7 +38,6 @@ function isAlphaNumericWithSpacesAndDash(value) {
|
||||
function isJson(value) {
|
||||
//check if its object
|
||||
if(typeof value === "object"){
|
||||
console.log("its an object")
|
||||
return true
|
||||
}
|
||||
|
||||
@ -44,6 +65,8 @@ module.exports = {
|
||||
isAlphaNumericwithSpaces,
|
||||
isAlphaNumericWithSpacesAndDash,
|
||||
isJson,
|
||||
isAddress
|
||||
};
|
||||
|
||||
isAddress,
|
||||
isValidDateString,
|
||||
isMacAddress,
|
||||
isNumber,
|
||||
};
|
Reference in New Issue
Block a user