From b2cafddf233f6cbdb3c2bc9bdad7cb6d8dc9983f Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 25 Apr 2020 15:07:01 -0600 Subject: [PATCH] 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') }) }