diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6aa55ec3..f95db004 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1504,29 +1504,37 @@ exports.readMotorStatus = async (req, reply) => { } }; - exports.writeMotorStatus = async (req, reply) => { - 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 // 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( - { motor_id: motor_id }, - { $set: { motor_status: status } } - ); - - reply.send({ status_code: 200, message: `Motor ${motor_id} status updated to ${status}` }); + reply.send({ status_code: 200, motor_status: result.motor_status }); + } else { + reply.send({ status_code: 200, motor_status: status }); + } + } else { + reply.send({ status_code: 200, message: 'Motor stop status is not "on" or "off".' }); + } } catch (err) { throw boom.boomify(err); } }; + exports.changeMotorStatus = async (req, reply) => { try { const motor_id = req.body.motor_id; @@ -1540,11 +1548,11 @@ exports.changeMotorStatus = async (req, reply) => { const result = await Tank.findOneAndUpdate( { 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) { throw boom.boomify(err); } diff --git a/src/models/tanks.js b/src/models/tanks.js index a948e714..61621605 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -48,6 +48,7 @@ const tanksSchema = new mongoose.Schema({ waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, motor_status: { type: String, default: "1" }, + motor_stop_status: { type: String, default: "1" }, motor_speed: { type: String, default: "100" }, motor_temperature: { type: String, default: "0" }, waterlevel_at_midnight:{ type: String,default:"0" },