From 9c8b2d45c17976de43aa4b4d08df0fbe284ad1f2 Mon Sep 17 00:00:00 2001 From: JayWll Date: Thu, 23 Apr 2020 09:49:49 -0600 Subject: [PATCH 01/12] Minor change to wording of thank you tweet --- nodeapp/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodeapp/app.js b/nodeapp/app.js index c91963d..38a5187 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -26,7 +26,7 @@ app.get('/newreading', (req, res) => { // Pick a random message from the array below const tweet = [ 'Thanks to whoever watered me just now!! So moist.', - 'So refreshing! Thank you, mysterious stranger, for the life-giving sustenance.', + 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' ] From 7612cb4feef094530e1a76fa0c9960d9aab41b3b Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 14:06:48 -0600 Subject: [PATCH 02/12] Refactoring code for new reading Removed code that runs after a new reading has been received from the express model, and added it as a sequelize hook instead. --- nodeapp/app.js | 67 -------------------------------------------- nodeapp/db/db.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 67 deletions(-) diff --git a/nodeapp/app.js b/nodeapp/app.js index 38a5187..44e42ca 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -1,7 +1,6 @@ // Initialize project const db = require('./db/db') const path = require('path') -const twit = require('twit') require('dotenv').config({path: path.join(__dirname, '../.env')}) const express = require('express'); const app = express(); @@ -18,72 +17,6 @@ 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)) { - // Pick a random message from the array below - const tweet = [ - 'Thanks to whoever watered me just now!! So moist.', - 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', - 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' - ] - - // Define options for the twitter API - const msg = new twit({ - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET - }) - - msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) - - // 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 fourdays = 4 * 24 * 60 * 60 * 1000 - - // If the first low reading was more than 5 days ago, send an alert - if (lowtriggered > fourdays) { - // Pick a random message from the array below - const tweet = [ - 'I\'m getting pretty thirsty over here, @JayWll', - 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', - 'Hey @JayWll, water me. 🥛', - 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', - '@JayWll seriously bro. A little water. That\'s all I ask.' - ] - - // Define options for the twitter API - const msg = new twit({ - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET - }) - - // Post a tweet - msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) - - // Flag that an alert has been sent - db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) - } else console.log('Low for less than 5 days') - } else console.log('No message needed') - }) }); // Handle requests for /getdata by returning a JSON object of data for the relevant time period diff --git a/nodeapp/db/db.js b/nodeapp/db/db.js index 42053d0..4d61dfe 100644 --- a/nodeapp/db/db.js +++ b/nodeapp/db/db.js @@ -1,5 +1,8 @@ const Sequelize = require('sequelize') const Op = Sequelize.Op +const path = require('path') +const twit = require('twit') +require('dotenv').config({path: path.join(__dirname, '../../.env')}) // Setup database, using credentials set in .env const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { @@ -16,6 +19,76 @@ const Readings = sequelize.define('readings', { reading: { type: Sequelize.INTEGER } +}, { + hooks: { + afterCreate: (data, opts) => { + // Fetch all settings + 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' && data.reading >= parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value)) { + // Pick a random message from the array below + const tweet = [ + 'Thanks to whoever watered me just now!! So moist.', + 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', + 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' + ] + + // Define options for the twitter API + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + + // 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' && data.reading <= 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 fourdays = 4 * 24 * 60 * 60 * 1000 + + // If the first low reading was more than 5 days ago, send an alert + if (lowtriggered > fourdays) { + // Pick a random message from the array below + const tweet = [ + 'I\'m getting pretty thirsty over here, @JayWll', + 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', + 'Hey @JayWll, water me. 🥛', + 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', + '@JayWll seriously bro. A little water. That\'s all I ask.' + ] + + // Define options for the twitter API + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + // Post a tweet + msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + + // Flag that an alert has been sent + db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) + } else console.log('Low for less than 5 days') + } else console.log('No message needed') + }) + } + } }) // Define 'settings' table structure From c462f805bef07c1c399bc0cb9d939dce098155c1 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 14:07:32 -0600 Subject: [PATCH 03/12] Renamed db folder to src --- nodeapp/app.js | 2 +- nodeapp/{db => src}/db.js | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename nodeapp/{db => src}/db.js (100%) diff --git a/nodeapp/app.js b/nodeapp/app.js index 44e42ca..b61ba4d 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -1,5 +1,5 @@ // Initialize project -const db = require('./db/db') +const db = require('./src/db') const path = require('path') require('dotenv').config({path: path.join(__dirname, '../.env')}) const express = require('express'); diff --git a/nodeapp/db/db.js b/nodeapp/src/db.js similarity index 100% rename from nodeapp/db/db.js rename to nodeapp/src/db.js From 169c242e351334e7713c672b3f0e8aea259604cb Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 14:12:55 -0600 Subject: [PATCH 04/12] Created new module for alerts --- nodeapp/src/alert.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 nodeapp/src/alert.js diff --git a/nodeapp/src/alert.js b/nodeapp/src/alert.js new file mode 100644 index 0000000..00e0e4c --- /dev/null +++ b/nodeapp/src/alert.js @@ -0,0 +1,9 @@ +const path = require('path') +const twit = require('twit') +require('dotenv').config({path: path.join(__dirname, '../../.env')}) + +const alert = (type) => { + console.log('Alert triggered for type: ' + type) +} + +module.exports = alert From 61bb0066b04db93ab4e3b5ce1540ac39166fdafe Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 14:53:12 -0600 Subject: [PATCH 05/12] Moved tweet functionality out to alert.js --- nodeapp/src/alert.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/nodeapp/src/alert.js b/nodeapp/src/alert.js index 00e0e4c..40a69a2 100644 --- a/nodeapp/src/alert.js +++ b/nodeapp/src/alert.js @@ -4,6 +4,41 @@ require('dotenv').config({path: path.join(__dirname, '../../.env')}) const alert = (type) => { console.log('Alert triggered for type: ' + type) + + switch(type) { + // Water is needed + case 'wantwater': + const tweet = [ + 'I\'m getting pretty thirsty over here, @JayWll', + 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', + 'Hey @JayWll, water me. 🥛', + 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', + '@JayWll seriously bro. A little water. That\'s all I ask.' + ] + break; + + // Water has been received + case 'gotwater': + const tweet = [ + 'Thanks to whoever watered me just now!! So moist.', + 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', + 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' + ] + break; + } + + // Define options for the twitter API + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + // Post a tweet + //msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + console.log(tweet[Math.floor(Math.random() * tweet.length)]) + } } module.exports = alert From b2cafddf233f6cbdb3c2bc9bdad7cb6d8dc9983f Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:07:01 -0600 Subject: [PATCH 06/12] Move alert code to its own module --- nodeapp/src/alert.js | 39 +++++++++++++----------- nodeapp/src/db.js | 70 ++++++++++++++------------------------------ 2 files changed, 44 insertions(+), 65 deletions(-) diff --git a/nodeapp/src/alert.js b/nodeapp/src/alert.js index 40a69a2..ce63f51 100644 --- a/nodeapp/src/alert.js +++ b/nodeapp/src/alert.js @@ -4,41 +4,46 @@ require('dotenv').config({path: path.join(__dirname, '../../.env')}) const alert = (type) => { console.log('Alert triggered for type: ' + type) + const tweet = false switch(type) { // Water is needed case 'wantwater': - const tweet = [ + tweet = [ 'I\'m getting pretty thirsty over here, @JayWll', 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', 'Hey @JayWll, water me. 🥛', 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', '@JayWll seriously bro. A little water. That\'s all I ask.' ] - break; + break // Water has been received case 'gotwater': - const tweet = [ + tweet = [ 'Thanks to whoever watered me just now!! So moist.', 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' ] - break; - } - - // Define options for the twitter API - const msg = new twit({ - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET - }) - - // Post a tweet - //msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) - console.log(tweet[Math.floor(Math.random() * tweet.length)]) + break } + + if (!tweet) { + console.log('nothing to send') + return + } + + // Define options for the twitter API + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + // Post tweet + //msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + console.log(tweet[Math.floor(Math.random() * tweet.length)]) } module.exports = alert diff --git a/nodeapp/src/db.js b/nodeapp/src/db.js index 4d61dfe..dea4e4d 100644 --- a/nodeapp/src/db.js +++ b/nodeapp/src/db.js @@ -1,8 +1,6 @@ const Sequelize = require('sequelize') const Op = Sequelize.Op -const path = require('path') -const twit = require('twit') -require('dotenv').config({path: path.join(__dirname, '../../.env')}) +const alert = require('./alert') // Setup database, using credentials set in .env const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { @@ -24,67 +22,43 @@ const Readings = sequelize.define('readings', { afterCreate: (data, opts) => { // Fetch all settings Settings.findAll().then((result) => { + const low-trigger = parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value) + const high-trigger = parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value) + const low = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value + const alert = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value + + // 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' && data.reading >= parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value)) { - // Pick a random message from the array below - const tweet = [ - 'Thanks to whoever watered me just now!! So moist.', - 'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy sustenance.', - 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!' - ] - - // Define options for the twitter API - const msg = new twit({ - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET - }) - - msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + if (low !== '0' && data.reading >= high-trigger)) { + // Send alert that water was received + alert('gotwater') // Update low to false - db.Settings.update({ value: false }, { where: { key: 'low' } }) + Settings.update({ value: false }, { where: { key: 'low' } }) + // Update alert to false - db.Settings.update({ value: false }, { where: { key: 'alert' } }) + 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' && data.reading <= parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value)) { + if (low === '0' && data.reading <= low-trigger)) { // Update low to true - db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'low' } }) + 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() + if (low !== '0' && alert === '0') { + const lowtriggered = new Date().getTime() - new Date(low).getTime() const fourdays = 4 * 24 * 60 * 60 * 1000 - // If the first low reading was more than 5 days ago, send an alert + // If the first low reading was more than 4 days ago, send an alert if (lowtriggered > fourdays) { - // Pick a random message from the array below - const tweet = [ - 'I\'m getting pretty thirsty over here, @JayWll', - 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', - 'Hey @JayWll, water me. 🥛', - 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', - '@JayWll seriously bro. A little water. That\'s all I ask.' - ] - - // Define options for the twitter API - const msg = new twit({ - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET - }) - - // Post a tweet - msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + // Send alert that water is needed + alert('needwater') // Flag that an alert has been sent - db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) - } else console.log('Low for less than 5 days') + Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) + } else console.log('Low for less than 4 days') } else console.log('No message needed') }) } From 302eadc7c3e9be79794cd17b45c7c18b5260cbf5 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:09:59 -0600 Subject: [PATCH 07/12] Fixing syntax errors --- nodeapp/src/db.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nodeapp/src/db.js b/nodeapp/src/db.js index dea4e4d..3d17f42 100644 --- a/nodeapp/src/db.js +++ b/nodeapp/src/db.js @@ -22,14 +22,14 @@ const Readings = sequelize.define('readings', { afterCreate: (data, opts) => { // Fetch all settings Settings.findAll().then((result) => { - const low-trigger = parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value) - const high-trigger = parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value) + const lowtrigger = parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value) + const hightrigger = parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value) const low = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value const alert = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value // Reset low moisture flag if it's set, and the reading is greater than high-trigger - if (low !== '0' && data.reading >= high-trigger)) { + if (low !== '0' && data.reading >= hightrigger) { // Send alert that water was received alert('gotwater') @@ -41,7 +41,7 @@ const Readings = sequelize.define('readings', { } // Set the low moisture flag if it's not set, and the value is less than low-trigger - if (low === '0' && data.reading <= low-trigger)) { + if (low === '0' && data.reading <= lowtrigger) { // Update low to true Settings.update({ value: new Date().toISOString() }, { where: { key: 'low' } }) } From a9e25632046cb7dd40f21f102fc3e2809cc53518 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:29:37 -0600 Subject: [PATCH 08/12] Changing UI moisture reading to a percentage --- nodeapp/views/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nodeapp/views/index.html b/nodeapp/views/index.html index a874895..3ce3d31 100644 --- a/nodeapp/views/index.html +++ b/nodeapp/views/index.html @@ -78,7 +78,7 @@ legend: {display: false}, scales: { xAxes: [{type: 'time', time: {unit: 'day', min: fromdate, max: todate}, scaleLabel: {display: true, labelString: 'Date', fontColor: '#999999'}, gridLines: {color: '#CCCCCC'}, ticks: {fontColor: '#666666'}}], - yAxes: [{gridLines: {color: '#CCCCCC'}, ticks: {beginAtZero: true, max: 1200, fontColor: '#666666'}}] + yAxes: [{gridLines: {color: '#CCCCCC'}, ticks: {beginAtZero: true, max: 100, fontColor: '#666666'}}] }, maintainAspectRatio: false } @@ -113,7 +113,7 @@ $.getJSON('/getdata?from=' + from.toISOString() + '&to=' + to.toISOString(), function(j) { $(j).each(function() { this.x = new Date(this.timestamp); - this.y = this.reading; + this.y = this.reading / 1024 * 100; delete this.timestamp; delete this.reading; }); From 2643bf44105584bb3cca6cfb41f2ff1d8500ab59 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:30:06 -0600 Subject: [PATCH 09/12] Adding functionality to import settings from production --- nodeapp/utils/import.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/nodeapp/utils/import.js b/nodeapp/utils/import.js index 0413359..9ecf511 100644 --- a/nodeapp/utils/import.js +++ b/nodeapp/utils/import.js @@ -1,14 +1,15 @@ -const db = require('../db/db') +const db = require('../src/db') const request = require('request') const path = require('path') require('dotenv').config({path: path.join(__dirname, '../../.env')}) // The URL from which to export production data -const url = 'https://jasonsplant.glitch.me/exportall' +const readingsurl = 'https://jasonsplant.glitch.me/exportall' +const settingsurl = 'https://jasonsplant.glitch.me/showsettings' -// Get data from the production server +// Get readings from the production server request({ - url, + url: readingsurl, headers: { 'export-key': process.env.SECRET }, @@ -17,10 +18,7 @@ request({ if (error) return console.log(error) if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status') - console.log(response.body.length + ' items retrieved') - - // Sync the database table - await db.Settings.sync() + console.log(response.body.length + ' readings retrieved') // Remove existing database items await db.Readings.destroy({truncate: true}) @@ -28,3 +26,23 @@ request({ // Add new data to the database await db.Readings.bulkCreate(response.body) }); + +// Get settings from the production server +request({ + url: settingsurl, + headers: { + 'export-key': process.env.SECRET + }, + json: true +}, async(error, response) => { + if (error) return console.log(error) + if (response.statusCode != 200) return console.log('Unsuccessful, as indicated by HTTP status') + + console.log(response.body.length + ' settings retrieved') + + // Remove existing database items + await db.Settings.destroy({truncate: true}) + + // Add new data to the database + await db.Settings.bulkCreate(response.body) +}); From ecd09c90735bddf3f64fe78e47681b6a1b608014 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:30:47 -0600 Subject: [PATCH 10/12] Locking down settings --- nodeapp/app.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nodeapp/app.js b/nodeapp/app.js index b61ba4d..0180877 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -57,6 +57,11 @@ app.get('/exportall', (req, res) => { // Handle requests for /showsettings by retrieving all settings from the database and returning a JSON object app.get('/showsettings', (req, res) => { + // Check that the expected key has been included with the web request + if (!req.headers['export-key'] || req.headers['export-key'] != process.env.SECRET) { + return res.status(401).send('Authorization header not found').end(); + } + db.Settings.findAll().then((result) => { res.status(200).send(result).end(); }) From b2eeb3e2703589b5eb5c3063a7874d196913237c Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:31:49 -0600 Subject: [PATCH 11/12] Bug fix for alert functionality --- nodeapp/src/alert.js | 5 ++--- nodeapp/src/db.js | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/nodeapp/src/alert.js b/nodeapp/src/alert.js index ce63f51..e55424e 100644 --- a/nodeapp/src/alert.js +++ b/nodeapp/src/alert.js @@ -4,7 +4,7 @@ require('dotenv').config({path: path.join(__dirname, '../../.env')}) const alert = (type) => { console.log('Alert triggered for type: ' + type) - const tweet = false + var tweet = false switch(type) { // Water is needed @@ -42,8 +42,7 @@ const alert = (type) => { }) // Post tweet - //msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) - console.log(tweet[Math.floor(Math.random() * tweet.length)]) + msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) } module.exports = alert diff --git a/nodeapp/src/db.js b/nodeapp/src/db.js index 3d17f42..7b54517 100644 --- a/nodeapp/src/db.js +++ b/nodeapp/src/db.js @@ -1,6 +1,6 @@ const Sequelize = require('sequelize') const Op = Sequelize.Op -const alert = require('./alert') +const message = require('./alert') // Setup database, using credentials set in .env const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { @@ -24,14 +24,14 @@ const Readings = sequelize.define('readings', { Settings.findAll().then((result) => { const lowtrigger = parseInt(result.find(o => o.dataValues.key === 'low-trigger').dataValues.value) const hightrigger = parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value) - const low = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value + const low = result.find(o => o.dataValues.key === 'low').dataValues.value const alert = result.find(o => o.dataValues.key === 'low-trigger').dataValues.value // Reset low moisture flag if it's set, and the reading is greater than high-trigger if (low !== '0' && data.reading >= hightrigger) { // Send alert that water was received - alert('gotwater') + message('gotwater') // Update low to false Settings.update({ value: false }, { where: { key: 'low' } }) @@ -54,12 +54,12 @@ const Readings = sequelize.define('readings', { // If the first low reading was more than 4 days ago, send an alert if (lowtriggered > fourdays) { // Send alert that water is needed - alert('needwater') + message('needwater') // Flag that an alert has been sent Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) - } else console.log('Low for less than 4 days') - } else console.log('No message needed') + } + } }) } } From 0839e3583c80e7c97768edd02533b1141f0d2fae Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:32:10 -0600 Subject: [PATCH 12/12] Fixing db module path --- nodeapp/utils/init-settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodeapp/utils/init-settings.js b/nodeapp/utils/init-settings.js index 5c13409..ac74363 100644 --- a/nodeapp/utils/init-settings.js +++ b/nodeapp/utils/init-settings.js @@ -1,4 +1,4 @@ -const db = require('../db/db') +const db = require('../src/db') const path = require('path') require('dotenv').config({path: path.join(__dirname, '../../.env')})