Skip to content

Commit

Permalink
actuation endpoints added and notifications sender created
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvinci committed Apr 14, 2022
1 parent c0c061f commit f4c7285
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
12 changes: 10 additions & 2 deletions arduinoService.js
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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");
}
}

Expand Down
13 changes: 13 additions & 0 deletions notifier.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.26.1",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"mongoose": "^6.2.10",
Expand Down
13 changes: 13 additions & 0 deletions plantService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const { writeToSerialPort } = require("./arduinoService");
require("dotenv").config();

mongoose
Expand Down Expand Up @@ -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;
17 changes: 12 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {});

Expand Down

0 comments on commit f4c7285

Please sign in to comment.