write motor status for sensor

master
varun 2 years ago
parent 11777a1aaa
commit d493ddbd3c

@ -1504,29 +1504,37 @@ exports.readMotorStatus = async (req, reply) => {
} }
}; };
exports.writeMotorStatus = async (req, reply) => { exports.writeMotorStatus = async (req, reply) => {
try { try {
const { motor_id, status, current, temp } = req.body; const motor_id = req.body.motor_id;
const status = req.body.status;
// Perform any necessary logic to handle motor status update from the device // 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 // For example, update a database with the new status, current, and temp values
let result; const existingRecord = await Tank.findOne({ motor_id: motor_id });
if (existingRecord && (existingRecord.motor_stop_status === '1' || existingRecord.motor_stop_status === '2')) {
if (existingRecord.motor_status !== status) {
const result = await Tank.findOneAndUpdate(
{ motor_id: motor_id },
{ $set: { motor_status: status } },
{ new: true } // To return the updated document
);
result = await Tank.findOneAndUpdate( reply.send({ status_code: 200, motor_status: result.motor_status });
{ motor_id: motor_id }, } else {
{ $set: { motor_status: status } } reply.send({ status_code: 200, motor_status: status });
); }
} else {
reply.send({ status_code: 200, message: `Motor ${motor_id} status updated to ${status}` }); reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' });
}
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.changeMotorStatus = async (req, reply) => { exports.changeMotorStatus = async (req, reply) => {
try { try {
const motor_id = req.body.motor_id; const motor_id = req.body.motor_id;
@ -1540,11 +1548,11 @@ exports.changeMotorStatus = async (req, reply) => {
const result = await Tank.findOneAndUpdate( const result = await Tank.findOneAndUpdate(
{ motor_id: motor_id }, { motor_id: motor_id },
{ $set: { motor_status: action } }); { $set: { motor_stop_status: action } });
reply.send({ status_code: 200,motor_status: action }); reply.send({ status_code: 200,motor_stop_status: action });
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }

@ -48,6 +48,7 @@ const tanksSchema = new mongoose.Schema({
waterlevel: { type: String, default: "0" }, waterlevel: { type: String, default: "0" },
tankLocation: { type: String, default: null }, tankLocation: { type: String, default: null },
motor_status: { type: String, default: "1" }, motor_status: { type: String, default: "1" },
motor_stop_status: { type: String, default: "1" },
motor_speed: { type: String, default: "100" }, motor_speed: { type: String, default: "100" },
motor_temperature: { type: String, default: "0" }, motor_temperature: { type: String, default: "0" },
waterlevel_at_midnight:{ type: String,default:"0" }, waterlevel_at_midnight:{ type: String,default:"0" },

Loading…
Cancel
Save