-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantService.js
99 lines (91 loc) · 2.83 KB
/
plantService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const { exec } = require("child_process");
const { Plants, getPlantData } = require("./dbService");
const { writeToSerialPort } = require("./arduinoService");
async function listPlants() {
try {
const plants = await Plants.find({}, { _id: false, __v: false })
.sort({ rackId: "asc" })
.exec();
console.log("Plants retrieved successfully!");
return plants;
} catch (error) {
console.log("Error while retrieving plants", error);
throw error;
}
}
async function getPlantInfo(rackId) {
try {
let plant = await getPlantData(rackId);
if (!plant) throw new Error(`No plant exists in rack ${rackId}`);
const { stderr } = await exec("./checkPlant.sh");
const data = JSON.stringify({ action: "PLANT_INFO", rackId });
writeToSerialPort(data);
console.log("Retrieving plant info!");
return "Hang tight! I'm getting you the plant info :)";
} catch (error) {
console.log("Error while retrieving plant", error);
throw error;
}
}
async function setPlant(plantDetails) {
try {
let plant = new Plants(plantDetails);
plant = await plant.save();
console.log("Plant set successfully!");
const data = JSON.stringify({ action: "SET_PLANT", ...plantDetails });
writeToSerialPort(data);
return plant;
} catch (error) {
console.log("Error while setting plant", error);
if (error.message.includes("duplicate"))
throw new Error(`Rack ${plantDetails.rackId} is occupied!`);
throw error;
}
}
async function editPlant(plantDetails) {
try {
const result = await Plants.findOneAndUpdate(
{ rackId: plantDetails.rackId },
plantDetails.updateProperty
);
if (!result) throw new Error(`No plant exists in rack ${rackId}`);
const data = JSON.stringify({ action: "EDIT_PLANT", ...plantDetails });
writeToSerialPort(data);
console.log("Plant updated successfully!");
return "Plant updated successfully!";
} catch (error) {
console.log("Error while setting plant", error);
throw new Error(`No plant exists in rack ${plantDetails.rackId}`);
}
}
async function removePlant(rackId) {
try {
let plant = await Plants.findOne({ rackId }, "name");
if (!plant) throw new Error(`No plant exists in rack ${rackId}`);
await Plants.deleteOne({ rackId });
console.log("Plant removed successfully!");
return `${plant.name} has been removed from rack ${rackId}!`;
} catch (error) {
console.log("Error while removing plant", error);
throw error;
}
}
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;