diff --git a/.env_localDB b/.env_localDB new file mode 100644 index 0000000..abe95ea --- /dev/null +++ b/.env_localDB @@ -0,0 +1,4 @@ +DB_name="eco_saver" +DB_storage="database_test.sqlite" +DB_dialect="sqlite" +DB_logginf=false \ No newline at end of file diff --git a/consumerWebsite/app.js b/consumerWebsite/app.js index 82ca5a0..4159fa3 100644 --- a/consumerWebsite/app.js +++ b/consumerWebsite/app.js @@ -5,6 +5,7 @@ const app = express(); const port = 3000; const ejs = require("ejs"); +module.exports = app; app.use(express.json()); app.set("json spaces", 2); @@ -17,6 +18,9 @@ const limiter = rateLimit({ legacyHeaders: false, // Disable the `X-RateLimit-*` headers. }); +// Hold list of functions to run when the server is ready +app.onListen = [function(){console.log('Express is ready')}]; + // Apply the rate limiting middleware to all requests. app.use(limiter); @@ -84,4 +88,3 @@ app.use(function (err, req, res, next) { }); -module.exports = app ; diff --git a/consumerWebsite/bin/www b/consumerWebsite/bin/www index b1f606f..3857586 100644 --- a/consumerWebsite/bin/www +++ b/consumerWebsite/bin/www @@ -93,4 +93,8 @@ function onListening() { : 'port ' + addr.port; console.log('Listening on ' + bind); + // execute list of functions when app is ready + for(let listener of app.onListen){ + listener() + } } \ No newline at end of file diff --git a/consumerWebsite/database/mySql.js b/consumerWebsite/database/mySql.js index 4064f02..2ed3cc4 100644 --- a/consumerWebsite/database/mySql.js +++ b/consumerWebsite/database/mySql.js @@ -19,7 +19,6 @@ const sequelize = new Sequelize( ssl: { ca: fs.readFileSync(path.resolve(__dirname, '../cert/DigiCertGlobalRootCA.crt.pem')), }, - }, }, ); diff --git a/consumerWebsite/functions/sensorData.js b/consumerWebsite/functions/sensorData.js index 29bbe2b..9af0f44 100644 --- a/consumerWebsite/functions/sensorData.js +++ b/consumerWebsite/functions/sensorData.js @@ -1,6 +1,8 @@ +const { Op, Sequelize } = require("sequelize"); const { sequelize } = require("../database/mySql.js"); const { sensorDataModel } = require("../database/model/sensorDataModel.js"); -const { Op, Sequelize } = require("sequelize"); +const socket = require('../functions/socket'); +// const { io } = require('../app') //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 @@ -24,6 +26,10 @@ async function addSensorData(id_sensor, id_location, sensordata) { locationid: id_location, measurement: sensordata, }); + + socket.emit('sensordata:new', sensorData) + + return sensorData; } async function updateSensorData(id, id_sensor, id_location, sensordata) { diff --git a/consumerWebsite/functions/socket.js b/consumerWebsite/functions/socket.js new file mode 100644 index 0000000..ec35667 --- /dev/null +++ b/consumerWebsite/functions/socket.js @@ -0,0 +1,16 @@ +const app = require("../app"); +const socket = {}; + +app.onListen.push(function(){ + socket.emit = (topic, data) => app.io.emit(topic, data); + + app.io.on('connection', (socket) => { + console.log('User connected via WebsSocket') + }); + + app.io.on('disconnect', (socket) => { + console.log('User disconnect via WebsSocket') + }); +}); + +module.exports = socket; diff --git a/consumerWebsite/routes/sensorData.js b/consumerWebsite/routes/sensorData.js index 8a3aff0..8325361 100644 --- a/consumerWebsite/routes/sensorData.js +++ b/consumerWebsite/routes/sensorData.js @@ -24,8 +24,8 @@ router.get("/", async (req, res, next) => { router.post("/new", async (req, res, next) => { try { const { id_sensor, id_location, sensordata } = req.body; - await addSensorData(id_sensor, id_location, sensordata); - res.sendStatus(200).json({ message: "SensorData " + id + " added" }); + let data = await addSensorData(id_sensor, id_location, sensordata); + res.json({ message: "SensorData " + data.id + " added", ...data }); } catch (error) { console.error(error); next(error);