ashok 2 years ago
commit 5361e73055

@ -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')) {
result = await Tank.findOneAndUpdate( if (existingRecord.motor_status !== status) {
const result = await Tank.findOneAndUpdate(
{ motor_id: motor_id }, { motor_id: motor_id },
{ $set: { motor_status: status } } { $set: { motor_status: status } },
{ new: true } // To return the updated document
); );
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) { } 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" },

@ -576,8 +576,8 @@ module.exports = function (fastify, opts, next) {
fastify.post('/api/motor/write', { fastify.post('/api/motor/write', {
schema: { schema: {
tags: ["Tank"], tags: ["Tank"],
description: "This is to Write the motor status", description: "This is to Write the motor status from iot",
summary: "This is to Write the motor status", summary: "This is to Write the motor status from iot",
body: { body: {
type: 'object', type: 'object',
required: ['motor_id', 'status'], required: ['motor_id', 'status'],
@ -598,6 +598,7 @@ module.exports = function (fastify, opts, next) {
fastify.get('/api/motor/read', { fastify.get('/api/motor/read', {
schema: { schema: {
tags: ["Tank"], tags: ["Tank"],
@ -643,6 +644,8 @@ module.exports = function (fastify, opts, next) {
// fastify.post('/api/motor/write', { // fastify.post('/api/motor/write', {
// schema: { // schema: {
// tags: ["Tank"], // tags: ["Tank"],

Loading…
Cancel
Save