changed motor status read and write

master
varun 2 years ago
parent c85c87ca72
commit 860a4e47a7

@ -1474,42 +1474,98 @@ exports.readMotorStatus = async (req, reply) => {
return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' });
}
const motor_stop_status = motorInfo.motor_stop_status;
const motor_status = motorInfo.motor_status;
reply.send({ status_code: 200, motor_stop_status:motor_stop_status });
reply.send({ status_code: 200, motor_status:motor_status });
} catch (err) {
throw boom.boomify(err);
}
};
exports.readMotorStatusFromIot = async (req, reply) => {
try {
const motor_id = req.query.motor_id;
console.log(motor_id)
// Perform any necessary logic based on action (1: Start, 2: Stop)
// For example, you can update a database or trigger an action
const motorInfo = await Tank.findOne({motor_id : 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;
reply.send({ status_code: 200, motor_status:motor_status });
} catch (err) {
throw boom.boomify(err);
}
};
// exports.writeMotorStatus = async (req, reply) => {
// try {
// const motor_id = req.body.motor_id;
// // Perform any necessary logic to handle motor status update from the device
// // For example, update a database with the new status, current, and temp values
// const existingRecord = await Tank.findOne({ motor_id: motor_id });
// if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) {
// const newMotorStatus = existingRecord.motor_stop_status;
// if (existingRecord.motor_status !== newMotorStatus) {
// const result = await Tank.findOneAndUpdate(
// { motor_id: motor_id },
// { $set: { motor_status: newMotorStatus } },
// { new: true } // To return the updated document
// );
// reply.send({ status_code: 200, motor_status: result.motor_status });
// } else {
// reply.send({ status_code: 200, motor_status: newMotorStatus });
// }
// } else {
// reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' });
// }
// } catch (err) {
// throw boom.boomify(err);
// }
// };
exports.writeMotorStatus = async (req, reply) => {
try {
const motor_id = req.body.motor_id;
const status = req.body.status;
// Perform any necessary logic to handle motor status update from the device
// For example, update a database with the new status, current, and temp values
const existingRecord = await Tank.findOne({ motor_id: motor_id });
if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) {
const newMotorStatus = existingRecord.motor_stop_status;
if (existingRecord.motor_status !== newMotorStatus) {
const result = await Tank.findOneAndUpdate(
{ motor_id: motor_id },
{ $set: { motor_status: newMotorStatus } },
{ new: true } // To return the updated document
);
{ $set: { motor_status: status }
});
reply.send({ status_code: 200, motor_status: result.motor_status });
} else {
reply.send({ status_code: 200, motor_status: newMotorStatus });
}
} else {
reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' });
}
// Fetch the motor_status for the given motor_id
const updatedMotor = await Tank.findOne({ motor_id: motor_id });
// Send the response with motor_stop_status and motor_status
reply.send({
status_code: 200,
motor_status: status,
// Assuming motor_status is a field in your Tank model
});
} catch (err) {
throw boom.boomify(err);
}

@ -620,6 +620,26 @@ module.exports = function (fastify, opts, next) {
});
fastify.get('/api/motor/readMotorIotStatus', {
schema: {
tags: ["Tank"],
description: "This is to Read the motor status from Iot",
summary: "This is to Read the motor status from Iot",
querystring: {
type: 'object',
properties: {
motor_id: { type: 'string' },
},
// required: ['motor_id'],
},
},
handler: tanksController.readMotorStatusFromIot
});
fastify.post('/api/motor/changeMotorStatus', {
schema: {
tags: ["Tank"],

Loading…
Cancel
Save