change in iotdata if tank not found for tankhardwareId4

master
varun 1 year ago
parent dd0a632331
commit 228dfcc014

@ -871,7 +871,6 @@ exports.motorAction = async (req, reply) => {
// Determine the motor stop status based on the action
let motorStopStatus;
if (action === "start") {
motorStopStatus = "2"; // If action is start, set stop status to "2"
} else if (action === "stop") {
@ -901,27 +900,15 @@ exports.motorAction = async (req, reply) => {
} else {
// Update the motor stop status to "2" for start action
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2","connections.inputConnections.$.motor_on_type": req.body.on_type } }
);
}
if (action === "start"&&req.body.on_type==="auto") {
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2","connections.inputConnections.$.motor_on_type": req.body.on_type } }
{ $set: { "connections.inputConnections.$.motor_stop_status": "2" } }
);
}
// Check threshold settings if action is start
if (action === "start"&&req.body.on_type==="manual") {
if (action === "start") {
if (req.body.threshold_type === "time") {
// If threshold type is time, update threshold time
// await Tank.updateOne(
@ -935,7 +922,7 @@ exports.motorAction = async (req, reply) => {
if (index !== -1) {
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } }
{ $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
);
}
}
@ -952,8 +939,8 @@ exports.motorAction = async (req, reply) => {
{
$set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.motor_on_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null
}
@ -976,6 +963,9 @@ exports.motorAction = async (req, reply) => {
const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);
const capacity = parseInt(receiver_tank_info.capacity, 10);
const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
const desired_percentage = parseInt(req.body.manual_threshold_litres, 10);
@ -988,7 +978,7 @@ exports.motorAction = async (req, reply) => {
if (index !== -1) {
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } }
{ $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
);
}
}
@ -1010,7 +1000,7 @@ exports.motorAction = async (req, reply) => {
{
$set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.motor_on_type": null,
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null
@ -1031,49 +1021,6 @@ exports.motorAction = async (req, reply) => {
}
};
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);
if (capacity > 0) {
const currentPercentage = (waterLevel / capacity) * 100;
if (currentPercentage <= autoMinPercentage) {
// Call motorAction function
await motorAction({
params: { customerId: tank.customerId },
body: {
action: "start",
motor_id: tank.connections.inputConnections[0].motor_id,
to:tank.tankName,
from:tank.connections.inputConnections[0].inputConnections,
from_type:connections.inputConnections[0].input_type,
to_type:tank.tankLocation,
// startTime:,
on_type:"auto"
}
}, {
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);

@ -68,7 +68,7 @@ const tanksSchema = new mongoose.Schema({
motor_status: { type: String, default: "0" },
auto_mode:{type:String,default:"inactive"},
motor_stop_status: { type: String, default: "1" },
motor_on_type :{ type: String, default: null },
motor_on_type :{ type: String, default: "manual" },
capacity:{ type: String ,default: null},
water_level:{ type: String ,default: null},
manual_threshold_percentage:{type: String, default: "90"},
@ -93,7 +93,6 @@ const tanksSchema = new mongoose.Schema({
manual_threshold_time:{type: String, default: null},
threshold_type:{type: String, default: "percentage"},
}
]
}

@ -284,13 +284,13 @@ module.exports = function (fastify, opts, next) {
from_type: { type: "string" },
to_type: { type: "string" },
action: { type: "string" },
startTime:{ type: "string" },
threshold_type:{type:"string"},
manual_threshold_litres:{type:"string"},
manual_threshold_time:{type:"string"},
stopTime:{type:"string"},
motor_id:{type:"string"},
on_type:{type:"string"}
},
},
security: [

Loading…
Cancel
Save