Iot integration waterlevel update and sum of waterlevel

master
Bhaskara Kishore 2 years ago
parent 79622d4f22
commit 0ab2787f4d

@ -186,13 +186,21 @@ exports.tankerBooking = async (req, reply) => {
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const bookingId = `ARM${year}${month}${day}${b_id}`
// const customer = await User.findOne({customerId:customerId})
// const customer_mobile = customer.phone
// const customer_name = customer.username
// const supplier = await Supplier.findOne({supplierId:req.body.supplierId})
// console.log(supplier, "11")
// const supplier_name = supplier.suppliername
// const supplier_mobile = supplier.phone
// console.log(supplier_mobile, "==========")
const customer = await User.findOne({customerId:customerId})
const customer_mobile = customer.phoneNumber
const customer_name = customer.username
const customerPhone = customer.phone
const customerName = customer.username
const supplier = await Supplier.findOne({supplierId:req.body.supplierId})
console.log(supplier)
const supplier_name = supplier.suppliername
const supplier_mobile = supplier.phoneNumber
const supplierName = supplier.suppliername
const supplierPhone = supplier.phone
var booking_id = await Tankerbooking.findOne({ bookingid: bookingId})
if(booking_id){
b_id= await generateBookingId()
@ -215,10 +223,10 @@ exports.tankerBooking = async (req, reply) => {
longitude: req.body.longitude,
//date: req.body.date,
// time: req.body.time,
customerPhone : customer_mobile,
supplierPhone : supplier_mobile,
customerName : customer_name,
supplierName : supplier_name,
customerPhone : customerPhone,
supplierPhone : supplierPhone,
customerName : customerName,
supplierName : supplierName,
};
@ -241,10 +249,10 @@ exports.tankerBooking = async (req, reply) => {
tankersBookingData.tankLocation = usertobeInserted.tankLocation;
//tankersBookingData.date = usertobeInserted.date;
//tankersBookingData.time = usertobeInserted.time;
tankersBookingData.customerPhone = customer_mobile;
tankersBookingData.supplierPhone = supplier_mobile;
tankersBookingData.customerName = customer_name;
tankersBookingData.supplierName = supplier_name;
tankersBookingData.customerPhone = customerPhone;
tankersBookingData.supplierPhone = supplierPhone;
tankersBookingData.customerName = customerName;
tankersBookingData.supplierName = supplierName;
tankersBookingData.latitude =usertobeInserted.latitude
tankersBookingData.longitude=usertobeInserted.longitude

@ -721,6 +721,58 @@ exports.checkStatusofIot = async (req, reply) => {
}
};
exports.totalwaterLevelSum = async (request, reply) => {
const { tankLocation, typeOfWater } = request.query;
const waterlevelSum = await Tank.aggregate([
{
$match: { tankLocation, typeOfWater }
},
{
$group: {
_id: null,
totalWaterlevel: { $sum: { $toInt: '$waterlevel' } }
}
}
]);
const result = waterlevelSum[0]?.totalWaterlevel ?? 0;
reply.send({ waterlevelSum: result });
}
exports.startUpdateLoop = async (request, reply) => {
const updateInterval = 5000;
setInterval(async () => {
try {
const iotTank = await IotData.findOne({ hardwareId: request.body.hardwareId });
if (!iotTank) {
console.log(`IOTtank not found for hardwareId ${request.body.hardwareId}`);
return;
}
const currentWaterlevel = Number(iotTank.tankHeight) * 200;
const tank = await Tank.findOne({ hardwareId: iotTank.hardwareId });
let combinedWaterlevel;
if (tank) {
combinedWaterlevel = currentWaterlevel + Number(tank.waterlevel);
} else {
combinedWaterlevel = currentWaterlevel;
}
await Tank.updateOne({ hardwareId: iotTank.hardwareId }, { $set: { waterlevel: combinedWaterlevel } });
console.log(`Waterlevel updated successfully for hardwareId ${iotTank.hardwareId}`);
console.log(`Previous waterlevel: ${tank ? tank.waterlevel : 0}`);
console.log(`Current waterlevel: ${currentWaterlevel}`);
console.log(`Combined waterlevel: ${combinedWaterlevel}`);
} catch (err) {
console.error(err);
}
}, updateInterval);
};

@ -91,8 +91,8 @@ const userSchema = new mongoose.Schema(
coordinates: { type: [Number], default: [0, 0] },
},
longitude: { type : Number},
latitude: {type: Number},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
isActive: Boolean,
tenantId: ObjectId,
fcmId: { type: String, default: null },

@ -63,8 +63,8 @@ const supplierSchema = new mongoose.Schema(
// type: { type: String },
// coordinates: [Number]
// },
longitude: { type : Number},
latitude: {type: Number},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
isActive: Boolean,
tenantId: ObjectId,
fcmId: { type: String, default: null },
@ -126,8 +126,8 @@ const supplierSchema = new mongoose.Schema(
zip: { type: String, default: null },
timestamp: { type: Date, default: Date.now },
status: { type: String, default: "Inactive" },
longitude: { type : Number},
latitude: {type: Number},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
fcmId: { type: String, default: null },
});

@ -63,8 +63,8 @@ const tankersbookingSchema = new mongoose.Schema({
customerName : { type: String, default: null },
supplierName : { type: String, default: null },
tankerRunningStatus: { type: String, default: "0" },
longitude: { type : Number},
latitude: {type: Number},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
});

@ -86,8 +86,8 @@ module.exports = function (fastify, opts, next) {
state: { type: "string", default: null },
zip: { type: "string", default: null },
status: { type: "string" },
latitude: { type: 'number' },
longitude: { type: 'number'},
latitude: { type: 'number', default: 0.0 },
longitude: { type: 'number', default: 0.0},
fcmId: { type: "string", default: null },
},

@ -278,8 +278,8 @@ module.exports = function (fastify, opts, next) {
state: { type: "string", default: null },
zip: { type: "string", default: null },
country: { type: "string", default: null },
latitude: { type: 'number' },
longitude: { type: 'number'},
latitude: { type: 'number', default: 0.0},
longitude: { type: 'number', default: 0.0},
fcmId: { type: "string", default: null },
},
},

@ -208,8 +208,8 @@ module.exports = function (fastify, opts, next) {
dateOfOrder: { type: "string"},
price: { type: "string"},
supplierId: { type: "string"},
latitude: { type: 'number' },
longitude: { type: 'number'}
latitude: { type: 'number' , default: 0.0},
longitude: { type: 'number', default: 0.0}
},

@ -1,5 +1,6 @@
const fastify = require("fastify");
const tanksController = require("../controllers/tanksController");
const { Tank } = require('../models/tanks')
module.exports = function (fastify, opts, next) {
@ -431,6 +432,78 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.checkStatusofIot,
});
fastify.route({
method: 'GET',
url: '/waterlevel-sum',
schema: {
tags: ["Tank"],
description: "This is for water level sum",
summary: "This is forwater level sum.",
querystring: {
type: 'object',
properties: {
tankLocation: { type: 'string' },
typeOfWater: { type: 'string' }
},
required: ['tankLocation', 'typeOfWater']
},
response: {
200: {
type: 'object',
properties: {
waterlevelSum: { type: 'integer' }
}
}
}
},
handler: tanksController.totalwaterLevelSum
});
fastify.route({
method: 'POST',
url: '/update-waterlevel',
schema: {
tags: ['Tank'],
description: 'This is for updating waterlevel of a tank based on its IOTtank document',
summary: 'This is for updating waterlevel of a tank',
body: {
type: 'object',
properties: {
hardwareId: { type: 'string' },
},
required: ['hardwareId'],
},
response: {
200: {
type: 'object',
properties: {
message: { type: 'string' },
},
},
404: {
type: 'object',
properties: {
message: { type: 'string' },
},
},
500: {
type: 'object',
properties: {
message: { type: 'string' },
},
},
},
security: [
{
basicAuth: [],
},
],
},
handler: tanksController.startUpdateLoop,
});
next();

@ -155,8 +155,8 @@ module.exports = function (fastify, opts, next) {
zip: { type: "string", default: null },
country: { type: "string", default: null },
notes: { type: "string", default: null },
latitude: { type: 'number' },
longitude: { type: 'number'},
latitude: { type: 'number', default: 0.0 },
longitude: { type: 'number', default: 0.0},
fcmId: { type: "string", default: null },
},
},

Loading…
Cancel
Save