Move alert code to its own module

This commit is contained in:
JayWll 2020-04-25 15:07:01 -06:00
parent 61bb0066b0
commit b2cafddf23
2 changed files with 44 additions and 65 deletions

View File

@ -4,41 +4,46 @@ require('dotenv').config({path: path.join(__dirname, '../../.env')})
const alert = (type) => { const alert = (type) => {
console.log('Alert triggered for type: ' + type) console.log('Alert triggered for type: ' + type)
const tweet = false
switch(type) { switch(type) {
// Water is needed // Water is needed
case 'wantwater': case 'wantwater':
const tweet = [ tweet = [
'I\'m getting pretty thirsty over here, @JayWll', 'I\'m getting pretty thirsty over here, @JayWll',
'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵',
'Hey @JayWll, water me. 🥛', '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! 🍞🚰💦', '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.' '@JayWll seriously bro. A little water. That\'s all I ask.'
] ]
break; break
// Water has been received // Water has been received
case 'gotwater': case 'gotwater':
const tweet = [ tweet = [
'Thanks to whoever watered me just now!! So moist.', 'Thanks to whoever watered me just now!! So moist.',
'So refreshing! Thank you, mysterious stranger, for the life-giving liquidy 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!' 'Whoever watered me (I\'m kind of guessing it was you, @Katheryn_mur), know that I appreciate it!'
] ]
break; 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)])
} }
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 module.exports = alert

View File

@ -1,8 +1,6 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize')
const Op = Sequelize.Op const Op = Sequelize.Op
const path = require('path') const alert = require('./alert')
const twit = require('twit')
require('dotenv').config({path: path.join(__dirname, '../../.env')})
// Setup database, using credentials set in .env // Setup database, using credentials set in .env
const sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { 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) => { afterCreate: (data, opts) => {
// Fetch all settings // Fetch all settings
Settings.findAll().then((result) => { 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 // 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)) { if (low !== '0' && data.reading >= high-trigger)) {
// Pick a random message from the array below // Send alert that water was received
const tweet = [ alert('gotwater')
'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 // Update low to false
db.Settings.update({ value: false }, { where: { key: 'low' } }) Settings.update({ value: false }, { where: { key: 'low' } })
// Update alert to false // 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 // 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 // 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 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') { if (low !== '0' && alert === '0') {
const lowtriggered = new Date().getTime() - new Date(result.find(o => o.dataValues.key === 'low').dataValues.value).getTime() const lowtriggered = new Date().getTime() - new Date(low).getTime()
const fourdays = 4 * 24 * 60 * 60 * 1000 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) { if (lowtriggered > fourdays) {
// Pick a random message from the array below // Send alert that water is needed
const tweet = [ alert('needwater')
'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 // Flag that an alert has been sent
db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } })
} else console.log('Low for less than 5 days') } else console.log('Low for less than 4 days')
} else console.log('No message needed') } else console.log('No message needed')
}) })
} }