|
@@ -212,7 +212,9 @@ func parseReportData(deviceCode string, buf []byte) (*ReportData, error) {
|
|
|
|
|
|
|
|
rssi := int(buf[6])
|
|
rssi := int(buf[6])
|
|
|
antenna := int(buf[22])
|
|
antenna := int(buf[22])
|
|
|
- epc := hex.EncodeToString(buf[11:21])
|
|
|
|
|
|
|
+ epc := hex.EncodeToString(buf[11:23])
|
|
|
|
|
+
|
|
|
|
|
+ fmt.Printf(hex.EncodeToString(buf))
|
|
|
|
|
|
|
|
return &ReportData{
|
|
return &ReportData{
|
|
|
DeviceCode: deviceCode,
|
|
DeviceCode: deviceCode,
|
|
@@ -239,20 +241,22 @@ func handleReportData(report *ReportData) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
epc := report.Epcs[0]
|
|
epc := report.Epcs[0]
|
|
|
-
|
|
|
|
|
fmt.Println("<UNK>", epc)
|
|
fmt.Println("<UNK>", epc)
|
|
|
now := time.Now().Unix()
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
+ // ===================== 【修复】锁只包防抖,绝不包业务!=====================
|
|
|
epcMutex.Lock()
|
|
epcMutex.Lock()
|
|
|
- defer epcMutex.Unlock()
|
|
|
|
|
-
|
|
|
|
|
- // ===================== 修复:3秒防抖 =====================
|
|
|
|
|
|
|
+ // 3秒防抖
|
|
|
if lastTime, ok := epcLastTime[epc]; ok && now-lastTime < debounceSecond {
|
|
if lastTime, ok := epcLastTime[epc]; ok && now-lastTime < debounceSecond {
|
|
|
|
|
+ epcMutex.Unlock()
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+ epcLastTime[epc] = now
|
|
|
|
|
+ epcMutex.Unlock()
|
|
|
|
|
+ // ===================== 锁已释放,后面逻辑不会阻塞 =====================
|
|
|
|
|
|
|
|
// 获取设备处理器
|
|
// 获取设备处理器
|
|
|
- handler, exists := DeviceManager.Get(report.DeviceCode)
|
|
|
|
|
|
|
+ _, exists := DeviceManager.Get(report.DeviceCode)
|
|
|
if !exists {
|
|
if !exists {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -276,7 +280,6 @@ func handleReportData(report *ReportData) {
|
|
|
if err != nil || vehicle.ID == 0 {
|
|
if err != nil || vehicle.ID == 0 {
|
|
|
isTempVehicle = true
|
|
isTempVehicle = true
|
|
|
} else {
|
|
} else {
|
|
|
- // ===================== 修复:防止空指针panic =====================
|
|
|
|
|
if vehicle.VehicleType != nil {
|
|
if vehicle.VehicleType != nil {
|
|
|
isTempVehicle = (vehicle.VehicleTypeID == 1)
|
|
isTempVehicle = (vehicle.VehicleTypeID == 1)
|
|
|
} else {
|
|
} else {
|
|
@@ -290,7 +293,6 @@ func handleReportData(report *ReportData) {
|
|
|
// ===================== 3. 黑名单优先判断 =====================
|
|
// ===================== 3. 黑名单优先判断 =====================
|
|
|
if isBlack {
|
|
if isBlack {
|
|
|
fmt.Printf("🚫 禁止通行 | 车辆在黑名单中 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
fmt.Printf("🚫 禁止通行 | 车辆在黑名单中 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
|
- epcLastTime[epc] = now
|
|
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -298,62 +300,52 @@ func handleReportData(report *ReportData) {
|
|
|
if isTempVehicle {
|
|
if isTempVehicle {
|
|
|
if !allowTemporary && !isWhite {
|
|
if !allowTemporary && !isWhite {
|
|
|
fmt.Printf("🚫 禁止通行 | 临时车不允许此通道且不在白名单 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
fmt.Printf("🚫 禁止通行 | 临时车不允许此通道且不在白名单 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
|
- epcLastTime[epc] = now
|
|
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
if !isWhite {
|
|
if !isWhite {
|
|
|
fmt.Printf("🚫 禁止通行 | 临时车不在白名单 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
fmt.Printf("🚫 禁止通行 | 临时车不在白名单 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
|
- epcLastTime[epc] = now
|
|
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ===================== 5. 入口 =====================
|
|
// ===================== 5. 入口 =====================
|
|
|
if direction == "in" {
|
|
if direction == "in" {
|
|
|
- // 关键:只要状态是 false 就允许进入
|
|
|
|
|
- if !epcStatus[epc] {
|
|
|
|
|
- fmt.Printf("🟢 入口开闸 | 设备:%s EPC:%s 临时车:%v 白名单:%v\n", report.DeviceCode, epc, isTempVehicle, isWhite)
|
|
|
|
|
- handler.reader.CloseRelay1(2)
|
|
|
|
|
|
|
+ epcMutex.Lock()
|
|
|
|
|
+ canIn := !epcStatus[epc]
|
|
|
|
|
+ epcMutex.Unlock()
|
|
|
|
|
+
|
|
|
|
|
+ if canIn {
|
|
|
|
|
+ fmt.Printf("🟢 入口开闸 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
|
|
|
+ //handler.reader.CloseRelay1(2)
|
|
|
|
|
|
|
|
- // 记录入场
|
|
|
|
|
var vehicleEntry request.VehicleEntry
|
|
var vehicleEntry request.VehicleEntry
|
|
|
vehicleEntry.RFIDTag = epc
|
|
vehicleEntry.RFIDTag = epc
|
|
|
vehicleEntry.ParkingLotID = channel.ParkingLotID
|
|
vehicleEntry.ParkingLotID = channel.ParkingLotID
|
|
|
_, err := vehicleService.VehicleEntry(vehicleEntry)
|
|
_, err := vehicleService.VehicleEntry(vehicleEntry)
|
|
|
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Printf("❌ 入场记录失败:%v | EPC:%s\n", err, epc)
|
|
|
|
|
- // 关键:不设置 epcStatus[epc] = true
|
|
|
|
|
- // 让下一次刷卡可以继续进入
|
|
|
|
|
- } else {
|
|
|
|
|
- // ✅ 只有成功才修改状态
|
|
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ epcMutex.Lock()
|
|
|
epcStatus[epc] = true
|
|
epcStatus[epc] = true
|
|
|
|
|
+ epcMutex.Unlock()
|
|
|
}
|
|
}
|
|
|
- } else {
|
|
|
|
|
- fmt.Printf("⚠️ 已在场内,禁止重复入场 | EPC:%s\n", epc)
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ===================== 6. 出口 =====================
|
|
// ===================== 6. 出口 =====================
|
|
|
if direction == "out" {
|
|
if direction == "out" {
|
|
|
- fmt.Printf("🔴 出口开闸 | 设备:%s EPC:%s 临时车:%v 白名单:%v\n", report.DeviceCode, epc, isTempVehicle, isWhite)
|
|
|
|
|
- handler.reader.CloseRelay1(2)
|
|
|
|
|
|
|
+ fmt.Printf("🔴 出口开闸 | 设备:%s EPC:%s\n", report.DeviceCode, epc)
|
|
|
|
|
+ //handler.reader.CloseRelay1(2)
|
|
|
|
|
|
|
|
- // 记录出场
|
|
|
|
|
var vehicleExit request.VehicleExit
|
|
var vehicleExit request.VehicleExit
|
|
|
vehicleExit.RFIDTag = epc
|
|
vehicleExit.RFIDTag = epc
|
|
|
_, err := vehicleService.VehicleExit(vehicleExit)
|
|
_, err := vehicleService.VehicleExit(vehicleExit)
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Printf("❌ 出场记录失败:%v | EPC:%s\n", err, epc)
|
|
|
|
|
- // 关键:不设置 epcStatus[epc] = false
|
|
|
|
|
- } else {
|
|
|
|
|
- // ✅ 只有成功才重置状态
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ epcMutex.Lock()
|
|
|
epcStatus[epc] = false
|
|
epcStatus[epc] = false
|
|
|
|
|
+ epcMutex.Unlock()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 更新最后读卡时间
|
|
|
|
|
- epcLastTime[epc] = now
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ===================== 手动重置离场 =====================
|
|
// ===================== 手动重置离场 =====================
|