|
|
@@ -1,24 +1,44 @@
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
import { EquipmentData } from "../database/models/EquipmentData";
|
|
|
+import { Status } from "../enums/EquipmentStatus";
|
|
|
+
|
|
|
+function getEquipmentDataFromRequest(req: Request) {
|
|
|
+ return {
|
|
|
+ battery: Number(req.body.Battery),
|
|
|
+ hook_battery_1: Number(req.body.BattHook1),
|
|
|
+ hook_battery_2: Number(req.body.BattHook2),
|
|
|
+ alarm: JSON.stringify(req.body.Alarm),
|
|
|
+ status: Status[req.body.Status],
|
|
|
+ latitude: Number(req.body.GPS_Lat).toFixed(5),
|
|
|
+ longitude: Number(req.body.GPS_Lon).toFixed(5),
|
|
|
+ };
|
|
|
+}
|
|
|
|
|
|
async function BlockDuplicatedEquipmentData(
|
|
|
req: Request,
|
|
|
res: Response,
|
|
|
next: NextFunction
|
|
|
) {
|
|
|
- const lastReceivedData = await EquipmentData.findOne({
|
|
|
+ const lastReceivedData: {
|
|
|
+ [index: string]: any;
|
|
|
+ } | null = await EquipmentData.findOne({
|
|
|
where: { aid_id: req.body.AidID },
|
|
|
order: [["id", "DESC"]],
|
|
|
});
|
|
|
+ const incomingData: { [index: string]: any } = getEquipmentDataFromRequest(
|
|
|
+ req
|
|
|
+ );
|
|
|
|
|
|
- if (
|
|
|
- !lastReceivedData ||
|
|
|
- lastReceivedData.battery != Number(req.body.Battery) ||
|
|
|
- lastReceivedData.alarm != JSON.stringify(req.body.Alarm)
|
|
|
- ) {
|
|
|
+ if (!lastReceivedData) {
|
|
|
return next();
|
|
|
}
|
|
|
|
|
|
+ for (const key in incomingData) {
|
|
|
+ if (lastReceivedData[key] != incomingData[key]) {
|
|
|
+ return next();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return res.json({ message: "data unchanged." });
|
|
|
}
|
|
|
|