From 0f5242991185b4e97d61ffaeb7bcf4ced51ec1da Mon Sep 17 00:00:00 2001 From: JayWll Date: Mon, 13 Apr 2020 09:49:56 -0600 Subject: [PATCH 1/4] Adding table definition for settings. --- nodeapp/db/db.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/nodeapp/db/db.js b/nodeapp/db/db.js index f83acdd..42053d0 100644 --- a/nodeapp/db/db.js +++ b/nodeapp/db/db.js @@ -1,4 +1,4 @@ -const Sequelize = require('sequelize'); +const Sequelize = require('sequelize') const Op = Sequelize.Op // Setup database, using credentials set in .env @@ -6,7 +6,7 @@ const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_ dialect: 'sqlite', logging: false, storage: '.data/database.sqlite' -}); +}) // Define 'readings' table structure const Readings = sequelize.define('readings', { @@ -16,6 +16,17 @@ const Readings = sequelize.define('readings', { reading: { type: Sequelize.INTEGER } -}); +}) -module.exports = {Op, Readings} +// Define 'settings' table structure +const Settings = sequelize.define('settings', { + key: { + type: Sequelize.STRING, + unique: true + }, + value: { + type: Sequelize.STRING + } +}) + +module.exports = {Op, Readings, Settings} From 8832c658bfa1959163fac31fe62fc8a104e87e8a Mon Sep 17 00:00:00 2001 From: JayWll Date: Mon, 13 Apr 2020 09:56:52 -0600 Subject: [PATCH 2/4] Script to initialize app settings table --- nodeapp/utils/init-settings.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nodeapp/utils/init-settings.js diff --git a/nodeapp/utils/init-settings.js b/nodeapp/utils/init-settings.js new file mode 100644 index 0000000..b49ce69 --- /dev/null +++ b/nodeapp/utils/init-settings.js @@ -0,0 +1,12 @@ +const db = require('../db/db') +const path = require('path') +require('dotenv').config({path: path.join(__dirname, '../../.env')}) + +// Remove all previous settings +db.Settings.destroy({truncate: true}) + +// Create settings +db.Settings.create({ key: 'low-trigger', value: 100 }) +db.Settings.create({ key: 'high-trigger', value: 900 }) +db.Settings.create({ key: 'low', value: false }) +db.Settings.create({ key: 'alert', value: false }) From 6d3e74aac9b7c5b79689b7ca8908b6a8d7fc931b Mon Sep 17 00:00:00 2001 From: JayWll Date: Mon, 13 Apr 2020 10:01:40 -0600 Subject: [PATCH 3/4] Adding sync() commands to database scripts --- nodeapp/utils/import.js | 3 +++ nodeapp/utils/init-settings.js | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/nodeapp/utils/import.js b/nodeapp/utils/import.js index 975a158..0413359 100644 --- a/nodeapp/utils/import.js +++ b/nodeapp/utils/import.js @@ -19,6 +19,9 @@ request({ console.log(response.body.length + ' items retrieved') + // Sync the database table + await db.Settings.sync() + // Remove existing database items await db.Readings.destroy({truncate: true}) diff --git a/nodeapp/utils/init-settings.js b/nodeapp/utils/init-settings.js index b49ce69..5c13409 100644 --- a/nodeapp/utils/init-settings.js +++ b/nodeapp/utils/init-settings.js @@ -2,11 +2,13 @@ const db = require('../db/db') const path = require('path') require('dotenv').config({path: path.join(__dirname, '../../.env')}) -// Remove all previous settings -db.Settings.destroy({truncate: true}) +db.Settings.sync().then(() => { + // Remove all previous settings + db.Settings.destroy({truncate: true}) -// Create settings -db.Settings.create({ key: 'low-trigger', value: 100 }) -db.Settings.create({ key: 'high-trigger', value: 900 }) -db.Settings.create({ key: 'low', value: false }) -db.Settings.create({ key: 'alert', value: false }) + // Create settings + db.Settings.create({ key: 'low-trigger', value: 100 }) + db.Settings.create({ key: 'high-trigger', value: 900 }) + db.Settings.create({ key: 'low', value: false }) + db.Settings.create({ key: 'alert', value: false }) +}) From bbad48ee90d55edf9a2bc37b5a858bf39d38aa37 Mon Sep 17 00:00:00 2001 From: JayWll Date: Mon, 13 Apr 2020 11:04:27 -0600 Subject: [PATCH 4/4] Added handlers to set flags when a new reading is received - Reset the 'low' and 'alert' flags if the reading is greater than high-trigger - Set the 'low' flag if it isn't set, and the reading is less than low-trigger - Send an alert and set the 'alert' flag if the 'low' flag is more than 5 days old --- nodeapp/app.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/nodeapp/app.js b/nodeapp/app.js index a8c273f..b031ce5 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -17,6 +17,35 @@ app.get('/newreading', (req, res) => { db.Readings.create({ timestamp: new Date(), reading: req.query.v }); res.status(200).send('Reading received: ' + req.query.v).end(); + + // Fetch all settings + db.Settings.findAll().then((result) => { + // Reset low moisture flag if it's set, and the reading is greater than high-trigger + if (result.find(o => o.dataValues.key === 'low').dataValues.value !== '0' && req.query.v >= parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value)) { + // Update low to false + db.Settings.update({ value: false }, { where: { key: 'low' } }) + // Update alert to false + db.Settings.update({ value: false }, { where: { key: 'alert' } }) + } + + // Set the low moisture flag if it's not set, and the value is less than low-trigger + if (result.find(o => o.dataValues.key === 'low').dataValues.value === '0' && req.query.v <= parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value)) { + // Update low to true + db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'low' } }) + } + + // If the low moisture flag is set but an alert has not yet been sent, determine if an alert is necessarry + if (result.find(o => o.dataValues.key === 'low').dataValues.value !== '0' && result.find(o => o.dataValues.key === 'alert').dataValues.value === '0') { + const lowtriggered = new Date().getTime() - new Date(result.find(o => o.dataValues.key === 'low').dataValues.value).getTime() + const fivedays = 5 * 24 * 60 * 60 * 1000 + + // If the first low reading was more than 5 days ago, send an alert + if (lowtriggered > fivedays) { + console.log('More than 5 days!') // NEED TO REPLACE THIS WITH ALERT BEING SENT + db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) + } + } + }) }); // Handle requests for /getdata by returning a JSON object of data for the relevant time period