changes in motor action,till this the code worked fine

master^2
Varun 6 months ago
parent ca968ac483
commit a03e4557d3

@ -2857,7 +2857,19 @@ exports.motorAction = async (req, reply) => {
const blockName = req.body.from || "Unknown Block";
const tankName = req.body.to || "Unknown Tank";
const stopTime = req.body.stopTime
if (action === "start") {
const inputConnection_1 = receiverTank.connections?.inputConnections?.find(
conn => conn.motor_id === motorId
);
// Step 2: Check motor_stop_status
if (action === "start" && inputConnection_1.motor_stop_status === "2") {
// ✅ Proceed with motor start logic
return reply.status(400).send({ error: "Motor is already running or blocked from starting." });
// ... your logic to handle starting the motor
}
if (action === "start" && inputConnection_1.motor_stop_status !== "2") {
if (motorIntervals[motorId]) {
console.log(`🛑 Clearing old interval for motorId: ${motorId}`);
@ -2879,7 +2891,7 @@ exports.motorAction = async (req, reply) => {
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: {
"connections.inputConnections.$.motor_stop_status": "2",
"connections.inputConnections.$.manual_threshold_time": manual_threshold_time,
"connections.inputConnections.$.threshold_type": threshold_type,
"connections.inputConnections.$.motor_on_type": "manual"
}}
@ -2908,6 +2920,7 @@ exports.motorAction = async (req, reply) => {
console.log("entered time",motorId,motorStopStatus)
// Update the tank connections with start time and threshold time
this.publishMotorStopStatus(motorId, motorStopStatus);
const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
@ -2918,7 +2931,7 @@ exports.motorAction = async (req, reply) => {
$set: {
[`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time,
[`connections.inputConnections.${index}.threshold_type`]: "time",
[`connections.inputConnections.${index}.startTime`]: req.body.startTime,
[`connections.inputConnections.${index}.startTime`]: startTime1,
[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id
}
}
@ -2979,6 +2992,7 @@ exports.motorAction = async (req, reply) => {
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
const currentTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
@ -2988,7 +3002,7 @@ exports.motorAction = async (req, reply) => {
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null,
"connections.inputConnections.$.stopTime": currentTime,
"connections.inputConnections.$.stopTime": currentTime1,
}
}
);
@ -3223,11 +3237,12 @@ exports.motorAction = async (req, reply) => {
async function stopMotor(motorId, customerId, start_instance_id,user_name) {
console.log(user_name,"user_name in stop2")
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
const currentTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.stopTime": currentTime,
"connections.inputConnections.$.stopTime": currentTime1,
"connections.inputConnections.$.start_instance_id": null,
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
@ -6131,14 +6146,19 @@ async function processIotData(hw_Id, data) {
if (inputConnection) {
inputConnection.motor_status = status;
if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
const now1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
const nowMoment = moment(now1, 'DD-MMM-YYYY - HH:mm:ss');
const startMoment = moment(inputConnection.startTime, 'DD-MMM-YYYY - HH:mm:ss');
const stopMoment = moment(inputConnection.stopTime, 'DD-MMM-YYYY - HH:mm:ss');
if (inputConnection.motor_stop_status === "1" && status === 2 && nowMoment.diff(stopMoment, 'seconds') >= 15 && inputConnection.motor_on_type !== "forced_manual") {
const currentTime = moment().tz('Asia/Kolkata');
const formattedTime = currentTime.format('DD-MMM-YYYY - HH:mm');
const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
const startInstanceId = `${hw_Id}${formattedTime}`;
inputConnection.motor_stop_status = "2";
inputConnection.motor_on_type = "forced_manual";
inputConnection.startTime = formattedTime;
inputConnection.startTime = startTime1;
inputConnection.start_instance_id = startInstanceId;
const newMotorData = new MotorData({
customerId:motorTank.customerId,
@ -6155,14 +6175,15 @@ async function processIotData(hw_Id, data) {
await newMotorData.save();
}
if (inputConnection.motor_stop_status === "2" && status === 1) {
if (inputConnection.motor_stop_status === "2" && status === 1 && nowMoment.diff(startMoment, 'seconds') >= 15) {
const motorData = await MotorData.findOne({ customerId:motorTank.customerId, motor_id: hw_Id, start_instance_id: inputConnection.start_instance_id });
const startinstance = inputConnection.start_instance_id;
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
const stopTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
inputConnection.motor_stop_status = "1";
inputConnection.motor_on_type = "manual";
inputConnection.stopTime = currentTime;
inputConnection.stopTime = stopTime1;
inputConnection.start_instance_id = null;
const motorId = hw_Id;

Loading…
Cancel
Save