|
|
|
@ -1023,6 +1023,114 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const motorActionAuto = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
|
const action = req.body.action;
|
|
|
|
|
const motorId = req.body.motor_id;
|
|
|
|
|
const motor_on_type = req.body.motor_on_type
|
|
|
|
|
|
|
|
|
|
if (!motorId) {
|
|
|
|
|
throw new Error("Motor ID is required.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let motorStopStatus;
|
|
|
|
|
|
|
|
|
|
if (action === "start") {
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
"connections.inputConnections.$.motor_stop_status": "2",
|
|
|
|
|
"connections.inputConnections.$.startTime": req.body.startTime,
|
|
|
|
|
"connections.inputConnections.$.motor)on_type": "auto",
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action === "stop") {
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
"connections.inputConnections.$.motor_stop_status": "1",
|
|
|
|
|
"connections.inputConnections.$.stopTime": req.body.stopTime,
|
|
|
|
|
"connections.inputConnections.$.motor_on_type": null,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error in motorActionAuto:", err);
|
|
|
|
|
reply.code(500).send({ error: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkAutoMode = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const tanks = await Tank.find();
|
|
|
|
|
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
if (tank.auto_mode === "active") {
|
|
|
|
|
const waterLevel = parseInt(tank.waterlevel, 10);
|
|
|
|
|
const capacity = parseInt(tank.capacity, 10);
|
|
|
|
|
const autoMinPercentage = parseInt(tank.auto_min_percentage, 10);
|
|
|
|
|
const autoMaxPercentage = parseInt(tank.auto_max_percentage, 10);
|
|
|
|
|
const currentPercentage = (waterLevel / capacity) * 100;
|
|
|
|
|
|
|
|
|
|
const now = moment().format('DD-MMM-YYYY-HH-mm');
|
|
|
|
|
|
|
|
|
|
if (capacity > 0) {
|
|
|
|
|
if (currentPercentage <= autoMinPercentage) {
|
|
|
|
|
await motorActionAuto({
|
|
|
|
|
params: { customerId: tank.customerId },
|
|
|
|
|
body: {
|
|
|
|
|
action: "start",
|
|
|
|
|
motor_id: tank.connections.inputConnections[0].motor_id,
|
|
|
|
|
motor_on_type: "auto",
|
|
|
|
|
startTime: now
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
code: (statusCode) => ({ send: (response) => console.log(response) })
|
|
|
|
|
});
|
|
|
|
|
} else if (currentPercentage >= autoMaxPercentage && tank.connections.inputConnections[0].motor_on_type === "auto") {
|
|
|
|
|
await motorActionAuto({
|
|
|
|
|
params: { customerId: tank.customerId },
|
|
|
|
|
body: {
|
|
|
|
|
action: "stop",
|
|
|
|
|
motor_id: tank.connections.inputConnections[0].motor_id,
|
|
|
|
|
motor_on_type: "auto",
|
|
|
|
|
stopTime: now
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
code: (statusCode) => ({ send: (response) => console.log(response) })
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error checking auto mode:", err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set the interval to check every 15 seconds (15000 milliseconds)
|
|
|
|
|
setInterval(checkAutoMode, 15000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.calculateCapacity = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|