|
|
|
@ -651,7 +651,6 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { hardwareId, tankhardwareId } = req.params;
|
|
|
|
@ -662,49 +661,51 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
console.log("Fetching tank data for:", { hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
// Get the latest record for the given hardwareId
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 }) // Get the latest one
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
if (!latestData || !Array.isArray(latestData.tanks)) {
|
|
|
|
|
return reply.code(404).send({ message: "No data found for given hardwareId and tankhardwareId" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the matching tank from the array
|
|
|
|
|
const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
|
|
|
|
|
|
|
|
|
|
if (!matchedTank) {
|
|
|
|
|
return reply.code(404).send({ message: "Tank not found in latest record" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connected_lora_date = new Date(matchedTank.date).toISOString().split("T")[0];
|
|
|
|
|
const connected_lora_time = matchedTank.time || new Date(matchedTank.date).toTimeString().split(" ")[0];
|
|
|
|
|
const tankHeight = parseFloat(matchedTank.tankHeight || "0");
|
|
|
|
|
const isConnected = tankHeight > 0;
|
|
|
|
|
|
|
|
|
|
// Update the Insensors collection where:
|
|
|
|
|
// - connected_to = master hardwareId
|
|
|
|
|
// - hardwareId = tankhardwareId
|
|
|
|
|
const updateResult = await Insensors.findOneAndUpdate(
|
|
|
|
|
const updateFields = {
|
|
|
|
|
connected_status: isConnected ? "connected" : "disconnected",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let connected_lora_date = null;
|
|
|
|
|
let connected_lora_time = null;
|
|
|
|
|
|
|
|
|
|
if (isConnected) {
|
|
|
|
|
connected_lora_date = new Date(matchedTank.date).toISOString().split("T")[0];
|
|
|
|
|
connected_lora_time = matchedTank.time || new Date(matchedTank.date).toTimeString().split(" ")[0];
|
|
|
|
|
|
|
|
|
|
updateFields.connected_lora_date = connected_lora_date;
|
|
|
|
|
updateFields.connected_lora_time = connected_lora_time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Insensors.findOneAndUpdate(
|
|
|
|
|
{ connected_to: hardwareId, hardwareId: tankhardwareId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
connected_lora_date,
|
|
|
|
|
connected_lora_time
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ $set: updateFields },
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!updateResult) {
|
|
|
|
|
console.log("No Insensor found for update.");
|
|
|
|
|
} else {
|
|
|
|
|
console.log("Updated Insensor:", updateResult._id);
|
|
|
|
|
}
|
|
|
|
|
const displayMessage = isConnected ? "LoRa connected" : "LoRa not connected";
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Success",
|
|
|
|
|
message: displayMessage,
|
|
|
|
|
data: matchedTank,
|
|
|
|
|
connected_status: updateFields.connected_status,
|
|
|
|
|
connected_lora_date,
|
|
|
|
|
connected_lora_time
|
|
|
|
|
});
|
|
|
|
@ -717,7 +718,6 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getAllocatedSensorsByTank = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
let { customerId, tankName } = req.params;
|
|
|
|
|