-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
João Silva
committed
Oct 12, 2023
1 parent
2e58f3b
commit 78c4067
Showing
17 changed files
with
374 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var Sensor = require('../models/sensors'); | ||
var Joi = require('joi'); | ||
var httpStatus = require('http-status-codes'); | ||
var response = require('./response'); | ||
var config = require('../../config/env'); | ||
|
||
module.exports = { | ||
|
||
add : (req, res, next)=>{ | ||
|
||
const val = Joi.object({ | ||
ref: Joi.string().required(), | ||
name: Joi.string().required(), | ||
type: Joi.string().required() | ||
}).validate(req.body); | ||
|
||
if(val.error){ | ||
response.error(res,httpStatus.BAD_REQUEST,val.error.details[0].message) | ||
}else{ | ||
Sensor.add(req.params.model_id,req.body.ref,req.body.name,req.body.type,(err,rows)=>{ | ||
if(!err) response.send(res,rows); | ||
else response.error(res,httpStatus.INTERNAL_SERVER_ERROR,err); | ||
}); | ||
} | ||
}, | ||
|
||
update : (req, res, next)=>{ | ||
|
||
const val = Joi.object({ | ||
sensor_id: Joi.number().required(), | ||
property: Joi.string().required(), | ||
value: Joi.string().required() | ||
}).validate(req.body); | ||
|
||
if(val.error){ | ||
response.error(res,httpStatus.BAD_REQUEST,val.error.details[0].message) | ||
}else{ | ||
Sensor.update(req.body.sensor_id,req.body.property,req.body.value,(err,rows)=>{ | ||
if(!err) response.send(res,rows); | ||
else response.error(res,httpStatus.INTERNAL_SERVER_ERROR,err); | ||
}); | ||
} | ||
}, | ||
|
||
|
||
list : (req, res, next)=>{ | ||
Sensor.list((err,rows)=>{ | ||
if(!err) response.send(res,rows); | ||
else response.error(res,httpStatus.INTERNAL_SERVER_ERROR,err); | ||
}); | ||
}, | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var mysql = require('mysql2'); | ||
var db = require('../controllers/db'); | ||
const moment = require('moment'); | ||
|
||
var self = module.exports = { | ||
|
||
add : async(model_id,ref,name,type,cb)=>{ | ||
|
||
let obj = { | ||
model_id : model_id, | ||
ref : ref, | ||
name : name, | ||
type: type, | ||
createdAt : moment().utc().format('YYYY-MM-DD HH:mm:ss'), | ||
updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss') | ||
} | ||
|
||
db.insert("sensors",obj) | ||
.then (rows => { | ||
return cb(null,rows); | ||
}) | ||
.catch(error => { | ||
return cb(error,null); | ||
}); | ||
}, | ||
|
||
update : async (id,property,value,cb)=>{ | ||
|
||
let obj = { | ||
updatedAt : moment().utc().format('YYYY-MM-DD HH:mm:ss') | ||
}; | ||
obj[property] = value; | ||
|
||
let filter = { | ||
id : id | ||
}; | ||
|
||
db.update("sensors",obj,filter) | ||
.then (rows => { | ||
return cb(null,rows); | ||
}) | ||
.catch(error => { | ||
return cb(error,null); | ||
}); | ||
}, | ||
|
||
list : async (cb)=>{ | ||
|
||
var query = `select * from sensors`; | ||
var table = []; | ||
query = mysql.format(query,table); | ||
|
||
db.queryRow(query) | ||
.then(rows => { | ||
return cb(null,rows); | ||
}) | ||
.catch(error => { | ||
return cb(error,null); | ||
}) | ||
}, | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.