write motor status

master
varun 2 years ago
parent 8934e557b3
commit 39404e6cfb

@ -1346,7 +1346,7 @@ exports.totalwaterLevelSum = async (request, reply) => {
} }
]); ]);
const result = waterlevelSum[0]?totalWaterlevel : 0 const result = waterlevelSum[0]?.totalWaterlevel ?? 0;
reply.send({ waterlevelSum: result }); reply.send({ waterlevelSum: result });
} }
@ -1568,3 +1568,50 @@ exports.motortemperature = async (req, reply) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.readMotorStatus = async (req, reply) => {
try {
const { motor_id, action } = req.body;
// 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 });
const motor_stp_status = motorInfo.motor_stop_status
if (motor_stp_status === "1"){
}
reply.send({ status_code: 200, message: `Motor ${motor_id} is set to ${action === '1' ? 'start' : 'stop'}` });
} catch (err) {
throw boom.boomify(err);
}
};
exports.writeMotorStatus = async (req, reply) => {
try {
const { motor_id, status, current, temp } = req.body;
// 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;
if (status === 'on') {
result = await Tank.findOneAndUpdate(
{ motor_id: motor_id },
{ $set: { motor_status: "2" } }
);
} else if (status === 'off') {
result = await Tank.findOneAndUpdate(
{ motor_id: motor_id },
{ $set: { motor_stop_status: "1" } }
);
}
reply.send({ status_code: 200, message: `Motor ${motor_id} status updated to ${status}` });
} catch (err) {
throw boom.boomify(err);
}
};

@ -686,6 +686,70 @@ module.exports = function (fastify, opts, next) {
}); });
fastify.post('/api/motor/write', {
schema: {
tags: ["Tank"],
description: "This is to Write the motor status",
summary: "This is to Write the motor status",
body: {
type: 'object',
required: ['motor_id', 'status'],
properties: {
motor_id: { type: 'string' },
status: { type: 'string', enum: ['on', 'off'] },
current: { type: 'string' },
temp: { type: 'string' },
},
},
// response: {
// 200: {
// type: 'object',
// properties: {
// // Define your response properties here
// motor_id: { type: 'string' },
// status: { type: 'string' },
// current: { type: 'string' },
// temp: { type: 'string' },
// // Add other properties as needed
// },
// },
// },
},
handler: tanksController.writeMotorStatus
});
// fastify.get('/api/motor/read', {
// schema: {
// tags: ["Tank"],
// description: "This is to Read the motor status",
// summary: "This is to Read the motor status",
// querystring: {
// type: 'object',
// properties: {
// motor_id: { type: 'string' },
// action: { type: 'string', enum: ['1', '2'] },
// },
// // required: ['motor_id'],
// },
// // response: {
// // 200: {
// // type: 'object',
// // properties: {
// // // Define your response properties here
// // motor_id: { type: 'string' },
// // motor_status: { type: 'string' },
// // motor_speed: { type: 'string' },
// // motor_temperature: { type: 'string' },
// // // Add other properties as needed
// // },
// // },
// // },
// },
// handler: tanksController.readMotorStatus
// });
next(); next();
} }

Loading…
Cancel
Save