|
|
|
@ -679,23 +679,31 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
|
return reply.code(404).send({ message: "No data found for given hardwareId and tankhardwareId" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Check GSM Connection - if data is fresh within 1 minute
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const dataDate = new Date(latestData.date);
|
|
|
|
|
const diffInMs = now - dataDate;
|
|
|
|
|
const isGSMConnected = diffInMs <= 60000; // 60 seconds
|
|
|
|
|
|
|
|
|
|
// Step 2: Find the matching tank
|
|
|
|
|
const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
|
|
|
|
|
|
|
|
|
|
if (!matchedTank) {
|
|
|
|
|
return reply.code(404).send({ message: "Tank not found in latest record" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Determine LoRa connection status
|
|
|
|
|
const tankHeight = parseFloat(matchedTank.tankHeight || "0");
|
|
|
|
|
const isConnected = tankHeight > 0;
|
|
|
|
|
const isLoraConnected = isGSMConnected && tankHeight > 0;
|
|
|
|
|
|
|
|
|
|
const updateFields = {
|
|
|
|
|
connected_status: isConnected ? "connected" : "disconnected",
|
|
|
|
|
connected_status: isLoraConnected ? "connected" : "disconnected",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let connected_lora_date = null;
|
|
|
|
|
let connected_lora_date = null;
|
|
|
|
|
let connected_lora_time = null;
|
|
|
|
|
|
|
|
|
|
if (isConnected) {
|
|
|
|
|
if (isLoraConnected) {
|
|
|
|
|
connected_lora_date = new Date(matchedTank.date).toISOString().split("T")[0];
|
|
|
|
|
connected_lora_time = matchedTank.time || new Date(matchedTank.date).toTimeString().split(" ")[0];
|
|
|
|
|
|
|
|
|
@ -703,13 +711,15 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
|
updateFields.connected_lora_time = connected_lora_time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: Update Insensors collection
|
|
|
|
|
await Insensors.findOneAndUpdate(
|
|
|
|
|
{ connected_to: hardwareId, hardwareId: tankhardwareId },
|
|
|
|
|
{ $set: updateFields },
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const displayMessage = isConnected ? "LoRa connected" : "LoRa not connected";
|
|
|
|
|
// Step 5: Prepare response
|
|
|
|
|
const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected";
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|