From 5ddd4f4ba6aaf1fe52f93c0966315d0424bd2a5f Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期日, 20 十月 2024 20:30:57 +0800
Subject: [PATCH] 调整楼层获取方式

---
 nvcs/a3.go                |    2 
 nvcs/a2.go                |    2 
 nvcs/a1.go                |    2 
 nvcs/osd.go               |   22 ++---
 nvcs/rfid.go              |   13 +++
 repository/captureRepo.go |   45 +++-------
 nvcs/queue.go             |    8 +-
 nvcs/nvcs.go              |   16 +++
 nvcs/cache.go             |   84 +++++++++++++++++++-
 9 files changed, 136 insertions(+), 58 deletions(-)

diff --git a/nvcs/a1.go b/nvcs/a1.go
index 9dd2d3f..4e72d45 100644
--- a/nvcs/a1.go
+++ b/nvcs/a1.go
@@ -209,7 +209,7 @@
 			elevator.Name = "1"
 		}
 
-		var runState = elevatorRunData{
+		var runState = ElevatorRunData{
 			Device:    elevator.Name,
 			Timestamp: time.Now().Unix(),
 			Floor:     elevator.Status.FloorName,
diff --git a/nvcs/a2.go b/nvcs/a2.go
index 1fe6954..2ffd4fd 100644
--- a/nvcs/a2.go
+++ b/nvcs/a2.go
@@ -103,7 +103,7 @@
 			iRunState = RunDown
 		}
 
-		var runState = elevatorRunData{
+		var runState = ElevatorRunData{
 			Device:    req.Id,
 			Timestamp: time.Now().Unix(),
 			Floor:     req.State.Floor,
diff --git a/nvcs/a3.go b/nvcs/a3.go
index e69067d..8131cbc 100644
--- a/nvcs/a3.go
+++ b/nvcs/a3.go
@@ -38,7 +38,7 @@
 
 		floor := fmt.Sprintf("%dF", req.Floor)
 
-		var runState = elevatorRunData{
+		var runState = ElevatorRunData{
 			Device:    req.Id,
 			Timestamp: time.Now().Unix(),
 			Floor:     floor,
diff --git a/nvcs/cache.go b/nvcs/cache.go
index 011ebf6..18c2b0b 100644
--- a/nvcs/cache.go
+++ b/nvcs/cache.go
@@ -23,7 +23,7 @@
 }
 
 // 瀛樺偍鏁版嵁鍒扮紦瀛樹腑
-func (c *simpleCache) store(data elevatorRunData) {
+func (c *simpleCache) store(data ElevatorRunData) {
 	var floorChanged = true
 
 	// 鍙朢FID妤煎眰
@@ -35,13 +35,17 @@
 		data.Floor = gRFIDFloor
 	}
 
+	if data.Floor == "" {
+		return
+	}
+
 	lastData := c.data.Back()
 
 	// 濡傛灉妤煎眰鐩稿悓锛屽苟涓旀暟鎹湪1绉掑唴锛屽垯蹇界暐
 	if lastData != nil {
-		if lastData.Value.(elevatorRunData).Floor == data.Floor {
+		if lastData.Value.(ElevatorRunData).Floor == data.Floor {
 			floorChanged = false
-			if lastData.Value.(elevatorRunData).Timestamp == data.Timestamp {
+			if lastData.Value.(ElevatorRunData).Timestamp == data.Timestamp {
 				return
 			}
 		}
@@ -82,9 +86,11 @@
 		floorText := fmt.Sprintf("%s%s %s", data.Floor, runStateStr, config.NVCSConf.OSD)
 
 		// 璋冪敤hik api 灏嗘枃瀛楁坊鍔犲埌osd鐨勫乏涓嬭
-		addFloorToOSD(floorText)
+		err = addFloorToOSD(floorText)
+		if err != nil {
+			logger.Warn("%s", err.Error())
+		}
 	}
-
 }
 
 // 鍒犻櫎杩囨湡鏁版嵁
@@ -94,7 +100,7 @@
 	// 浠庨槦鍒楀ご閮ㄥ紑濮嬫鏌ユ槸鍚︽湁杩囨湡鏁版嵁
 	for c.data.Len() > 0 {
 		elem := c.data.Front() // 鑾峰彇闃熷垪澶撮儴鐨勫厓绱�
-		item := elem.Value.(elevatorRunData)
+		item := elem.Value.(ElevatorRunData)
 		if now.Sub(time.Unix(item.Timestamp, 0)) > c.expiration {
 			// 濡傛灉鏁版嵁宸茬粡杩囨湡锛屽垯浠庨槦鍒椾腑鍒犻櫎
 			c.data.Remove(elem)
@@ -104,3 +110,69 @@
 		}
 	}
 }
+
+func (c *simpleCache) getPositionByTime(timestamp int64) (runData ElevatorRunData) {
+	node := c.data.Back()
+	if node == nil {
+		return
+	}
+
+	for {
+		if node.Prev() == nil {
+			break
+		}
+
+		if node.Prev().Value.(ElevatorRunData).Timestamp >= timestamp {
+			node = node.Prev()
+		} else {
+			break
+		}
+	}
+
+	if node.Value.(ElevatorRunData).Timestamp >= timestamp {
+		runData = node.Value.(ElevatorRunData)
+	}
+
+	return
+}
+
+func (c *simpleCache) getMovePosition(timestamp int64, floor string) (runData ElevatorRunData) {
+	node := c.data.Back()
+	if node == nil {
+		return
+	}
+
+	// 鍏堟壘鍒版渶杩戣妭鐐�
+	for {
+		if node.Prev() == nil {
+			break
+		}
+
+		if node.Prev().Value.(ElevatorRunData).Timestamp >= timestamp {
+			node = node.Prev()
+		} else {
+			break
+		}
+	}
+
+	if node.Value.(ElevatorRunData).Timestamp >= timestamp {
+		for {
+			if node.Next() == nil {
+				break
+			}
+
+			if node.Next().Value.(ElevatorRunData).Floor == floor {
+				node = node.Next()
+			} else {
+				break
+			}
+		}
+	}
+
+	if node.Value.(ElevatorRunData).Timestamp >= timestamp &&
+		node.Next().Value.(ElevatorRunData).Floor != floor {
+		runData = node.Value.(ElevatorRunData)
+	}
+
+	return
+}
diff --git a/nvcs/nvcs.go b/nvcs/nvcs.go
index 2f7ccf6..e8cd45f 100644
--- a/nvcs/nvcs.go
+++ b/nvcs/nvcs.go
@@ -13,7 +13,7 @@
 )
 
 // 鏁版嵁缁撴瀯
-type elevatorRunData struct {
+type ElevatorRunData struct {
 	Device    string
 	Timestamp int64
 	Floor     string
@@ -50,14 +50,26 @@
 		return "", 0
 	}
 
-	return runState.(elevatorRunData).Floor, runState.(elevatorRunData).RunState
+	return runState.(ElevatorRunData).Floor, runState.(ElevatorRunData).RunState
+}
+
+func FindPositionByTime(timestamp int64) ElevatorRunData {
+	return cache.getPositionByTime(timestamp)
+}
+
+func FindMovePosition(timestamp int64, floor string) ElevatorRunData {
+	return cache.getMovePosition(timestamp, floor)
 }
 
 func listenQueue() {
 	for {
 		data := queue.get()
+
+		//t := time.Now()
 		cache.store(data)
 
+		//logger.Debug("process queue data %+v, use time %v", data, time.Since(t))
+
 		// 娓呯悊杩囨湡鏁版嵁
 		cache.cleanExpired()
 	}
diff --git a/nvcs/osd.go b/nvcs/osd.go
index ee7ce0e..1046498 100644
--- a/nvcs/osd.go
+++ b/nvcs/osd.go
@@ -2,9 +2,8 @@
 
 import (
 	"encoding/xml"
+	"fmt"
 	"io/ioutil"
-
-	"gat1400Exchange/pkg/logger"
 
 	dac "github.com/xinsnake/go-http-digest-auth-client"
 )
@@ -18,9 +17,9 @@
 	cameraWebPassword      = "a1234567"
 )
 
-var overlayText textOverlay
+var overlayText TextOverlay
 
-type textOverlay struct {
+type TextOverlay struct {
 	Id               int64  `xml:"id"`
 	Enabled          bool   `xml:"enabled"`
 	PositionX        int64  `xml:"positionX"`
@@ -42,33 +41,32 @@
 	return ioutil.ReadAll(resp.Body)
 }
 
-func addFloorToOSD(osdText string) {
+func addFloorToOSD(osdText string) error {
 	overlay7Url := cameraWebAddr + hikISAPIOverlay7SetUrl
 
 	// 鑾峰彇宸︿笅瑙掔涓�涓瓧绗︾殑浣嶇疆
 	if overlayText.DisplayText == "" {
 		rsp, err := hikISAPIRequest(cameraWebUser, cameraWebPassword, "GET", overlay7Url, "")
 		if err != nil {
-			logger.Warn("Get osd info failure")
-			return
+			return fmt.Errorf("get osd info. %s", err.Error())
 		}
 
 		err = xml.Unmarshal(rsp, &overlayText)
 		if err != nil {
-			logger.Warn("%s", err.Error())
-			return
+			return fmt.Errorf("xml unmarshal. %s", err.Error())
 		}
 	}
 
 	if overlayText.DisplayText == osdText {
-		return
+		return nil
 	}
 
 	overlayText.DisplayText = osdText
 	body, _ := xml.Marshal(overlayText)
 	_, err := hikISAPIRequest(cameraWebUser, cameraWebPassword, "PUT", overlay7Url, string(body))
 	if err != nil {
-		logger.Warn("Camera osd set failure!!")
-		return
+		return fmt.Errorf("set osd. %s", err.Error())
 	}
+
+	return nil
 }
diff --git a/nvcs/queue.go b/nvcs/queue.go
index f1857e5..71a9ef3 100644
--- a/nvcs/queue.go
+++ b/nvcs/queue.go
@@ -1,20 +1,20 @@
 package nvcs
 
 type chQueue struct {
-	data chan elevatorRunData
+	data chan ElevatorRunData
 }
 
 func newChQueue(size int) *chQueue {
 	return &chQueue{
-		data: make(chan elevatorRunData, size),
+		data: make(chan ElevatorRunData, size),
 	}
 }
 
-func (q *chQueue) put(data elevatorRunData) {
+func (q *chQueue) put(data ElevatorRunData) {
 	q.data <- data
 }
 
-func (q *chQueue) get() elevatorRunData {
+func (q *chQueue) get() ElevatorRunData {
 	data := <-q.data
 	return data
 }
diff --git a/nvcs/rfid.go b/nvcs/rfid.go
index 0a39412..0e1fd9e 100644
--- a/nvcs/rfid.go
+++ b/nvcs/rfid.go
@@ -39,8 +39,19 @@
 				if floor != gRFIDFloor {
 					gRFIDFloor = floor
 					logger.Debug("rfid read epc floor %s", gRFIDFloor)
-				}
 
+					cacheData := cache.data.Back()
+					if cacheData != nil {
+						runState := cacheData.Value.(ElevatorRunData)
+						runState.Floor = floor
+						runState.Timestamp = time.Now().Unix()
+						runState.Device = "rfid-reader"
+
+						queue.put(runState)
+					}
+				}
+			} else {
+				gRFIDFloor = ""
 			}
 
 			time.Sleep(200 * time.Millisecond)
diff --git a/repository/captureRepo.go b/repository/captureRepo.go
index 9941a4a..73f0347 100644
--- a/repository/captureRepo.go
+++ b/repository/captureRepo.go
@@ -3,12 +3,12 @@
 import (
 	"encoding/base64"
 	"encoding/json"
-	"gat1400Exchange/nvcs"
 	"time"
 
 	"gat1400Exchange/client"
 	"gat1400Exchange/config"
 	"gat1400Exchange/models"
+	"gat1400Exchange/nvcs"
 	"gat1400Exchange/pkg"
 	"gat1400Exchange/pkg/logger"
 	"gat1400Exchange/util"
@@ -214,16 +214,12 @@
 
 	// 鍒ゆ柇鏄惁闇�瑕佸尮閰嶆ゼ灞�
 	if config.NVCSConf.Model != "" {
-		// 鍖归厤妤煎眰
-		var devPos models.Positions
-		_ = devPos.FindDevicePosition(deviceId, faceAppearTime.Unix()+5) // 鍔�5绉掔數姊叧闂ㄧ殑鏃堕棿
-
-		floor = devPos.Pos
+		runState := nvcs.FindPositionByTime(faceAppearTime.Unix() + 3) // 鍔�3绉掔數姊叧闂ㄧ殑鏃堕棿
+		floor = runState.Floor
 
 		for i := 0; i < config.NVCSConf.WaitRunTime; i++ {
-			var dbPos models.Positions
-			if err := dbPos.FindMovePosition(faceAppearTime.Unix()+5, floor); err == nil {
-				switch dbPos.RunDir {
+			if runState = nvcs.FindMovePosition(faceAppearTime.Unix()+3, floor); runState.Floor != "" {
+				switch runState.RunState {
 				case nvcs.RunUp:
 					runDir = "in"
 				case nvcs.RunDown:
@@ -268,6 +264,7 @@
 
 func (c CaptureRepository) VIIDFaceMsgForward(msg *vo.RequestFaceList) {
 	faceInfo := msg.FaceListObject.FaceObject[0]
+	var floor, runDir string
 
 	// 鍒ゆ柇鏄惁寮�鍚簡姊帶
 	if config.NVCSConf.Model != "" {
@@ -278,25 +275,18 @@
 			faceAppearTime = time.Now()
 		}
 
-		var floor, runDir string
-		var devPos models.Positions
-		_ = devPos.FindPositionByTime(faceAppearTime.Unix() + 5) // 鍔�5绉掔數姊叧闂ㄧ殑鏃堕棿
-		if devPos.Pos == "" {
-			devPos.Pos = "1F"
-		}
-
-		floor = devPos.Pos
+		runState := nvcs.FindPositionByTime(faceAppearTime.Unix() + 3) // 鍔�3绉掔數姊叧闂ㄧ殑鏃堕棿
+		floor = runState.Floor
 
 		for i := 0; i < config.NVCSConf.WaitRunTime; i++ {
-			var dbPos models.Positions
-			if err := dbPos.FindMovePosition(faceAppearTime.Unix()+5, floor); err == nil {
-				switch dbPos.RunDir {
+			if runState = nvcs.FindMovePosition(faceAppearTime.Unix()+3, floor); runState.Floor != "" {
+				switch runState.RunState {
 				case nvcs.RunUp:
-					runDir = "1"
+					runDir = "in"
 				case nvcs.RunDown:
-					runDir = "2"
+					runDir = "out"
 				case nvcs.RunStop:
-					runDir = "0"
+					runDir = ""
 				}
 
 				break
@@ -311,7 +301,6 @@
 			//	msg.FaceListObject.FaceObject[idx].FaceID = pkg.GenerateFaceIdContainFloor(face.FaceID, devPos.Pos)
 			//}
 		}
-
 	}
 
 	b, _ := json.Marshal(msg)
@@ -333,14 +322,10 @@
 		appearTime = time.Now()
 	}
 
-	var devPos models.Positions
-	_ = devPos.FindPositionByTime(appearTime.Unix() + 5) // 鍔�5绉掔數姊叧闂ㄧ殑鏃堕棿
-	if devPos.Pos == "" {
-		devPos.Pos = "1F"
-	}
+	runState := nvcs.FindPositionByTime(appearTime.Unix() + 3) // 鍔�3绉掔數姊叧闂ㄧ殑鏃堕棿
 
 	for idx, _ := range msg.PersonListObject.PersonObject {
-		msg.PersonListObject.PersonObject[idx].BehaviorDescription = devPos.Pos
+		msg.PersonListObject.PersonObject[idx].BehaviorDescription = runState.Floor
 		//if config.ClientConf.AddFloorToFaceId {
 		//	msg.PersonListObject.PersonObject[idx].PersonID = pkg.GenerateFaceIdContainFloor(v.PersonID, devPos.Pos)
 		//}

--
Gitblit v1.8.0