changes in motor_id

master
varun 1 year ago
parent 783495fd02
commit 58ebd5ca6c

@ -569,7 +569,7 @@ exports.createConnections = async (req, body) => {
'connections.inputConnections': { 'connections.inputConnections': {
$each: [{ $each: [{
inputConnections: tankname, inputConnections: tankname,
input_type: 'sump', input_type: 'sump',
inputismotor: data.outputismotor || false, inputismotor: data.outputismotor || false,
motor_id: data.motor_id || null, motor_id: data.motor_id || null,
water_level: tankInfo.waterlevel || null, water_level: tankInfo.waterlevel || null,

@ -915,15 +915,16 @@ exports.motorAction = async (req, reply) => {
clearInterval(intervalId); // Stop monitoring water level clearInterval(intervalId); // Stop monitoring water level
} }
}, 60000); // Check water level every minute }, 60000); // Check water level every minute
} else if (req.body.threshold_type === "percentage") { } else if (req.body.threshold_type === "litres") {
// If threshold type is percentage, calculate percentage threshold // If threshold type is percentage, calculate percentage threshold
const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
if (!receiver_tank_info) { if (!receiver_tank_info) {
throw new Error("Receiver tank not found."); throw new Error("Receiver tank not found.");
} }
const capacity = parseInt(receiver_tank_info.capacity, 10); const capacity = parseInt(receiver_tank_info.capacity, 10);
const desired_percentage = parseInt(req.body.manual_threshold_percentage, 10); const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
const threshold_water_level = (capacity * desired_percentage) / 100; const desired_percentage = parseInt(req.body.manual_threshold_litres, 10);
const threshold_water_level = waterLevel+desired_percentage;
// Update water level threshold // Update water level threshold
await Tank.updateOne( await Tank.updateOne(
@ -1795,25 +1796,37 @@ exports.motorstatus = async (req, reply) => {
exports.readMotorStatus = async (req, reply) => { exports.readMotorStatus = async (req, reply) => {
try { try {
const motor_id = req.query.motor_id;
const motor_id = req.query.motor_id;
console.log(motor_id) console.log(motor_id)
// Perform any necessary logic based on action (1: Start, 2: Stop) // Perform any necessary logic based on action (1: Start, 2: Stop)
// For example, you can update a database or trigger an action // For example, you can update a database or trigger an action
const motorInfo = await Tank.findOne({motor_id : motor_id });
if (!motorInfo) { const tanks = await Tank.find({});
return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' });
let motor_stop_status = null;
for (let tank of tanks) {
const inputConnections = tank.connections.inputConnections;
const motorConnection = inputConnections.find(conn => conn.motor_id === motor_id);
if (motorConnection) {
motor_stop_status = motorConnection.motor_stop_status;
break;
}
} }
const motor_stop_status = motorInfo.motor_stop_status;
if (!motor_stop_status) {
return reply.status(404).send({
status_code: 404,
message: 'Motor not found for the specified motor_id'
});
}
reply.send({ status_code: 200, motor_stop_status:motor_stop_status }); reply.send({
status_code: 200,
motor_stop_status: motor_stop_status
});
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
@ -1822,26 +1835,32 @@ exports.readMotorStatus = async (req, reply) => {
exports.readMotorStatusFromIot = async (req, reply) => { exports.readMotorStatusFromIot = async (req, reply) => {
try { try {
const motor_id = req.query.motor_id;
const motor_id = req.query.motor_id;
console.log(motor_id) console.log(motor_id)
// Perform any necessary logic based on action (1: Start, 2: Stop) // Find the tank that contains the specified motor_id in its inputConnections
const tank = await Tank.findOne({ "connections.inputConnections.motor_id": motor_id });
// For example, you can update a database or trigger an action if (!tank) {
return reply.status(404).send({
const motorInfo = await Tank.findOne({motor_id : motor_id }); status_code: 404,
message: 'Motor not found for the specified motor_id'
if (!motorInfo) { });
return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' });
} }
const motor_status = motorInfo.motor_status;
const motor_stop_status = motorInfo.motor_stop_status;
// Find the inputConnection with the specified motor_id
const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === motor_id);
// Extract motor_status and motor_stop_status from the inputConnection
const motor_status = inputConnection.motor_status;
const motor_stop_status = inputConnection.motor_stop_status;
reply.send({ status_code: 200, motor_status:motor_status ,motor_stop_status:motor_stop_status}); // Send the response with motor_status and motor_stop_status
reply.send({
status_code: 200,
motor_status: motor_status,
motor_stop_status: motor_stop_status
});
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
@ -1849,6 +1868,7 @@ exports.readMotorStatusFromIot = async (req, reply) => {
// exports.writeMotorStatus = async (req, reply) => { // exports.writeMotorStatus = async (req, reply) => {
// try { // try {
// const motor_id = req.body.motor_id; // const motor_id = req.body.motor_id;
@ -1885,22 +1905,29 @@ exports.writeMotorStatus = async (req, reply) => {
const motor_id = req.body.motor_id; const motor_id = req.body.motor_id;
const status = req.body.status; const status = req.body.status;
// Perform any necessary logic to handle motor status update from the device // Find the tank that contains the specified motor_id in its inputConnections
const tank = await Tank.findOne({ "connections.inputConnections.motor_id": motor_id });
// For example, update a database with the new status, current, and temp values if (!tank) {
const result = await Tank.findOneAndUpdate( return reply.status(404).send({
{ motor_id: motor_id }, status_code: 404,
{ $set: { motor_status: status } message: 'Motor not found for the specified motor_id'
}); });
}
// Fetch the motor_status for the given motor_id // Find the inputConnection with the specified motor_id
const updatedMotor = await Tank.findOne({ motor_id: motor_id }); const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === motor_id);
// Send the response with motor_stop_status and motor_status // Update the motor_status of the inputConnection
inputConnection.motor_status = status;
// Save the updated tank
await tank.save();
// Send the response with the updated motor_status
reply.send({ reply.send({
status_code: 200, status_code: 200,
motor_status: status, motor_status: status
// Assuming motor_status is a field in your Tank model
}); });
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);

@ -54,9 +54,7 @@ const tanksSchema = new mongoose.Schema({
total_water_added_from_midnight:{ type: String,default:"0" }, total_water_added_from_midnight:{ type: String,default:"0" },
auto_min_percentage :{ type: String, default: "20" }, auto_min_percentage :{ type: String, default: "20" },
auto_max_percentage :{ type: String, default: "80" }, auto_max_percentage :{ type: String, default: "80" },
manual_threshold_percentage:{type: String, default: "90"},
manual_threshold_time:{type: String, default: null},
threshold_type:{type: String, default: "percentage"},
connections: { connections: {
@ -73,6 +71,9 @@ const tanksSchema = new mongoose.Schema({
motor_on_type :{ type: String, default: "manual" }, motor_on_type :{ type: String, default: "manual" },
capacity:{ type: String ,default: null}, capacity:{ type: String ,default: null},
water_level:{ type: String ,default: null}, water_level:{ type: String ,default: null},
manual_threshold_percentage:{type: String, default: "90"},
manual_threshold_time:{type: String, default: null},
threshold_type:{type: String, default: "percentage"},
} }
], ],
@ -86,6 +87,9 @@ const tanksSchema = new mongoose.Schema({
motor_stop_status: { type: String, default: "1" }, motor_stop_status: { type: String, default: "1" },
capacity:{ type: String ,default: null}, capacity:{ type: String ,default: null},
water_level:{ type: String ,default: null}, water_level:{ type: String ,default: null},
manual_threshold_percentage:{type: String, default: "90"},
manual_threshold_time:{type: String, default: null},
threshold_type:{type: String, default: "percentage"},
} }
] ]

@ -284,10 +284,10 @@ module.exports = function (fastify, opts, next) {
from_type: { type: "string" }, from_type: { type: "string" },
to_type: { type: "string" }, to_type: { type: "string" },
action: { type: "string" }, action: { type: "string" },
percentage: { type: "string",default: "100" },
startTime:{ type: "string" }, startTime:{ type: "string" },
threshold_type:{type:"string"}, threshold_type:{type:"string"},
manual_threshold_percentage:{type:"string"}, manual_threshold_litres:{type:"string"},
manual_threshold_time:{type:"string"}, manual_threshold_time:{type:"string"},
stop_at:{type:"number"}, stop_at:{type:"number"},
motor_id:{type:"string"}, motor_id:{type:"string"},

Loading…
Cancel
Save