diff --git a/arduinoService.js b/arduinoService.js index 7d86289..1a2cdc9 100644 --- a/arduinoService.js +++ b/arduinoService.js @@ -1,4 +1,6 @@ const { SerialPort } = require("serialport"); +const { ReadlineParser } = require("@serialport/parser-readline"); +const { sendNotification } = require("./notifier"); require("dotenv").config(); const arduinoSerialPort = new SerialPort({ @@ -10,12 +12,18 @@ arduinoSerialPort.on("open", () => { console.log("Serial Port is open"); }); +const parser = new ReadlineParser(); +arduinoSerialPort.pipe(parser); +parser.on("data", (message) => { + sendNotification(message); +}); + function writeToSerialPort(jsonData) { try { - arduinoSerialPort.write(jsonData); + arduinoSerialPort.write(jsonData + "\n"); } catch (error) { console.log(error); - throw new Error("Arduino error"); + throw new Error("Arduino write error"); } } diff --git a/notifier.js b/notifier.js new file mode 100644 index 0000000..810de60 --- /dev/null +++ b/notifier.js @@ -0,0 +1,13 @@ +const axios = require("axios"); +require("dotenv").config(); + +async function sendNotification(message) { + try { + await axios.post(process.env.WEB_HOOK_URL, { + content: message, + }); + } catch (error) { + console.log(error); + throw new Error("Error sending message"); + } +} diff --git a/package-lock.json b/package-lock.json index 1ba6ee1..f313044 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^0.26.1", "dotenv": "^16.0.0", "express": "^4.17.3", "mongoose": "^6.2.10", @@ -278,6 +279,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "node_modules/axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "dependencies": { + "follow-redirects": "^1.14.8" + } + }, "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -511,6 +520,25 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -1267,6 +1295,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "requires": { + "follow-redirects": "^1.14.8" + } + }, "base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -1433,6 +1469,11 @@ "unpipe": "~1.0.0" } }, + "follow-redirects": { + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" + }, "forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", diff --git a/package.json b/package.json index 1b8227e..20becb5 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^0.26.1", "dotenv": "^16.0.0", "express": "^4.17.3", "mongoose": "^6.2.10", diff --git a/plantService.js b/plantService.js index bb833b7..e540e9b 100644 --- a/plantService.js +++ b/plantService.js @@ -1,4 +1,5 @@ const mongoose = require("mongoose"); +const { writeToSerialPort } = require("./arduinoService"); require("dotenv").config(); mongoose @@ -105,8 +106,20 @@ async function removePlant(rackId) { } } +function waterPlant(rackId) { + const data = JSON.stringify({ action: "WATER", rackId }); + writeToSerialPort(data); +} + +function lightPlant(rackId, state) { + const data = JSON.stringify({ action: "LIGHT", state, rackId }); + writeToSerialPort(data); +} + exports.listPlants = listPlants; exports.getPlantInfo = getPlantInfo; exports.setPlant = setPlant; exports.editPlant = editPlant; exports.removePlant = removePlant; +exports.waterPlant = waterPlant; +exports.lightPlant = lightPlant; diff --git a/server.js b/server.js index 2a9c058..7d2916f 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,12 @@ const express = require("express"); -// const { writeToSerialPort } = require("./arduinoService"); const { listPlants, getPlantInfo, setPlant, editPlant, removePlant, + waterPlant, + lightPlant, } = require("./plantService"); const port = process.env.PORT || 3000; @@ -40,8 +41,6 @@ app.post("/setplant", async (req, res) => { try { const plantDetails = req.body; const response = await setPlant(plantDetails); - // JSON.stringify(plantDetails); - // writeToSerialPort(plantDetails); return res.send(response); } catch (error) { res.status(400).send(error.message); @@ -68,9 +67,17 @@ app.post("/removeplant", async (req, res) => { } }); -app.post("/water/:rackId", (req, res) => {}); +app.post("/water", (req, res) => { + const { rackId } = req.body; + waterPlant(rackId); + return res.send(`Plant in rack ${rackId} watered`); +}); -app.post("/light/:rackId", (req, res) => {}); +app.post("/light", (req, res) => { + const { rackId, state } = req.body; + lightPlant(rackId, state); + return res.send(`Lights turned ${state} in rack ${rackId}`); +}); app.post("/humidity/:rackId", (req, res) => {});