Bug fix for alert functionality

This commit is contained in:
JayWll 2020-04-25 15:31:49 -06:00
parent ecd09c9073
commit b2eeb3e270
2 changed files with 8 additions and 9 deletions

View File

@ -4,7 +4,7 @@ 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 var tweet = false
switch(type) { switch(type) {
// Water is needed // Water is needed
@ -42,8 +42,7 @@ const alert = (type) => {
}) })
// Post tweet // Post tweet
//msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) 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,6 +1,6 @@
const Sequelize = require('sequelize') const Sequelize = require('sequelize')
const Op = Sequelize.Op const Op = Sequelize.Op
const alert = require('./alert') const message = require('./alert')
// 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,14 +24,14 @@ const Readings = sequelize.define('readings', {
Settings.findAll().then((result) => { Settings.findAll().then((result) => {
const lowtrigger = parseInt(result.find(o => o.dataValues.key === 'low-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 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 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 (low !== '0' && data.reading >= hightrigger) { if (low !== '0' && data.reading >= hightrigger) {
// Send alert that water was received // Send alert that water was received
alert('gotwater') message('gotwater')
// Update low to false // Update low to false
Settings.update({ value: false }, { where: { key: 'low' } }) 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 the first low reading was more than 4 days ago, send an alert
if (lowtriggered > fourdays) { if (lowtriggered > fourdays) {
// Send alert that water is needed // Send alert that water is needed
alert('needwater') message('needwater')
// Flag that an alert has been sent // Flag that an alert has been sent
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 4 days') }
} else console.log('No message needed') }
}) })
} }
} }