for null received from iot device

master
varun 1 year ago
parent cee726aa9a
commit 38d6d699a7

@ -344,10 +344,6 @@ exports.motorAction = async (req, reply) => {
const receiver_type = (req.body.to_type).toLowerCase() const receiver_type = (req.body.to_type).toLowerCase()
// console.log(supplier_tank) // console.log(supplier_tank)
// const suplr_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); // const suplr_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type});
const interval_variable = supplier_tank+receiver_tank const interval_variable = supplier_tank+receiver_tank
let currentTank = supplier_tanks.find(tank => tank.supplier_tank === supplier_tank); let currentTank = supplier_tanks.find(tank => tank.supplier_tank === supplier_tank);
let currentSump = sump_water_levels.find(tank => tank.supplier_tank === supplier_tank); let currentSump = sump_water_levels.find(tank => tank.supplier_tank === supplier_tank);
@ -1158,13 +1154,17 @@ exports.IotDevice = async (req, reply) => {
} }
// Update waterlevel in tanksSchema for each tank // Update waterlevel in tanksSchema for each tank
for (const tank of tanks) { for (const tank of tanks) {
const { tankhardwareId, tankHeight } = tank; const { tankhardwareId, tankHeight } = tank;
// Skip if tankHeight is null or undefined
if (tankHeight === null || tankHeight === undefined) {
continue; // Skip this iteration and move to the next tank
}
// Find the corresponding tank in tanksSchema // Find the corresponding tank in tanksSchema
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
if (existingTank) { if (existingTank) {
// Update the waterlevel using the tankHeight value // Update the waterlevel using the tankHeight value
@ -1185,30 +1185,29 @@ exports.IotDevice = async (req, reply) => {
console.log(tank_height); console.log(tank_height);
// console.log(tank_height,1) // console.log(tank_height,1)
const water_level_height = tank_height - tankHeight const water_level_height = tank_height - tankHeight
console.log(water_level_height,2) console.log(water_level_height, 2)
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10) const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
console.log(waterCapacityPerCm,3) console.log(waterCapacityPerCm, 3)
const water_level = water_level_height * waterCapacityPerCm; const water_level = water_level_height * waterCapacityPerCm;
console.log(water_level, 4); console.log(water_level, 4);
// Function to add commas to a number // Function to add commas to a number
function numberWithCommas(x) { function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} }
const formatted_water_level = numberWithCommas(water_level);
console.log(formatted_water_level, 4);
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
console.log(existingTank.waterlevel);
const formatted_water_level = numberWithCommas(water_level);
console.log(formatted_water_level, 4);
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
console.log(existingTank.waterlevel);
// Save the updated tank document // Save the updated tank document
await existingTank.save(); await existingTank.save();
} }
} }
// Send the latest three documents // Send the latest three documents
const latestOttanks = await IotData.find({ hardwareId }) const latestOttanks = await IotData.find({ hardwareId })

@ -53,6 +53,9 @@ const tanksSchema = new mongoose.Schema({
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" },
total_water_added_from_midnight:{ type: String,default:"0" }, total_water_added_from_midnight:{ type: String,default:"0" },
auto_min_percentage :{ type: String, default: "20" },
auto_max_percentage :{ type: String, default: "80" },
manual_threesold_percentage:{type: String, default: "90"},
connections: { connections: {
source: { type: String }, source: { type: String },
inputConnections: [ inputConnections: [
@ -63,6 +66,8 @@ const tanksSchema = new mongoose.Schema({
motor_id:{ type: String ,default: null}, motor_id:{ type: String ,default: null},
motor_status: { type: String, default: "0" }, motor_status: { type: String, default: "0" },
motor_stop_status: { type: String, default: "1" }, motor_stop_status: { type: String, default: "1" },
motor_on_type :{ type: String, default: "manual" },
} }
], ],
outputConnections: [ outputConnections: [
@ -73,6 +78,7 @@ const tanksSchema = new mongoose.Schema({
motor_id:{ type: String ,default: null}, motor_id:{ type: String ,default: null},
motor_status: { type: String, default: "0" }, motor_status: { type: String, default: "0" },
motor_stop_status: { type: String, default: "1" }, motor_stop_status: { type: String, default: "1" },
} }
] ]
} }

Loading…
Cancel
Save