From dc765761b9bb6866635f06f754d28895507e711f Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期四, 06 六月 2024 16:26:40 +0800
Subject: [PATCH] 完善打印

---
 service/nvcs.go |  132 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 117 insertions(+), 15 deletions(-)

diff --git a/service/nvcs.go b/service/nvcs.go
index 455b7c8..3cbd488 100644
--- a/service/nvcs.go
+++ b/service/nvcs.go
@@ -4,9 +4,11 @@
 	"bytes"
 	"encoding/json"
 	"fmt"
+	"github.com/gin-gonic/gin"
 	"io/ioutil"
 	"net"
-	"strings"
+	"net/http"
+	"time"
 
 	"gat1400Exchange/config"
 	"gat1400Exchange/models"
@@ -31,12 +33,18 @@
 	Alarm  []interface{}  `json:"Alarm"` // You might want to define a specific type for alarms
 }
 
-type ElevatorData struct {
+type A1ElevatorData struct {
 	Elevator []Elevator `json:"Elevator"`
 }
 
+const (
+	RunStop = iota
+	RunUp
+	RunDown
+)
+
 // 瀵规帴缃戠粶瑙嗛瀛楃鍙犲姞鍣�,鎺ユ敹udp鍙戦�佺殑妤煎眰淇℃伅, 鏇存柊device鍦板潃
-func NVCSServer() {
+func NVCSA1UDPServer() {
 	// 鎸囧畾鐩戝惉鐨勭鍙�
 	port := config.ServeConf.Port
 
@@ -74,16 +82,33 @@
 		reader := transform.NewReader(bytes.NewReader(buffer[:numBytes]), decoder)
 		decodedBytes, err := ioutil.ReadAll(reader)
 
-		var data ElevatorData
-		// Unmarshal JSON into the struct
+		var data A1ElevatorData
 		err = json.Unmarshal(decodedBytes, &data)
 		if err != nil {
 			logger.Warn("ElevatorData unmarshal error:%s", err.Error())
 			continue
 		}
+		logger.Debug("Received %d bytes from %s, %+v", numBytes, clientAddr, data)
 
 		if len(data.Elevator) == 0 {
 			continue
+		}
+
+		// 璁剧疆osd  鏍煎紡 "1F 鍥� 鏋�"
+		if config.NVCSConf.OSD != "" {
+			floorText := data.Elevator[0].Status.FloorName
+			//if data.Elevator[0].Status.RunDir == RunStop {
+			//	floorText = floorText + "鍋�"
+			//} else if data.Elevator[0].Status.RunDir == RunUp {
+			//	floorText = floorText + "涓�"
+			//} else {
+			//	floorText = floorText + "涓�"
+			//}
+
+			floorText = floorText + " " + config.NVCSConf.OSD
+
+			// 璋冪敤hik api 灏嗘枃瀛楁坊鍔犲埌osd鐨勫乏涓嬭
+			AddFloorToOSD(floorText)
 		}
 
 		if data.Elevator[0].Status.RunDir > 0 {
@@ -91,24 +116,101 @@
 		}
 
 		elevator := data.Elevator[0]
-		elevator.Name = strings.Trim(elevator.Name, " ")
+
+		// 绋嬪簭閮ㄧ讲鍦ㄨ澶囩, 瀛楃鍙犲姞鍣ㄤ笂鎶ョ殑鍚嶇О鍏佽涓虹┖. 鍦ㄤ簯绔�, 鍚嶇О蹇呴』涓庢憚鍍忔満鐩稿悓
 		if elevator.Name == "" {
-			continue
+			elevator.Name = "1"
 		}
 
-		var d = models.Device{
-			Id:  elevator.Name,
-			Pos: elevator.Status.FloorName,
-			Ip:  elevator.IP,
+		var d = models.Positions{
+			DeviceId:   elevator.Name,
+			Pos:        elevator.Status.FloorName,
+			CreateTime: time.Now().Unix(),
+			TimeString: time.Now().Format("2006-01-02 15:04:05"),
 		}
 
-		err = d.Upsert()
+		err = d.Save()
 		if err != nil {
-			logger.Warn("Device db update error:%s", err.Error())
+			logger.Warn("Device position update error:%s", err.Error())
+		}
+	}
+}
+
+/*
+A2 娆� 鏁版嵁涓婃姤鏍煎紡
+
+	{
+		"id": "10c8a1b0051607361c",
+		"State": {
+		"Floor": "-1",
+		"Floor_flag": "宸叉牎鍑�",
+		"JZ_flag": "宸叉牎鍑�,",
+		"JZ_i": 7,
+		"Pressure": "99766",
+		"Speed": "0.000",
+		"Status": "鍋滄顒�1",
+		"TFloor": 7,
+		"T_acc": "0.062",
+		"X_acc": "1.175",
+		"Y_acc": "-1.129",
+		"Z_acc": "8.344"
+	}
+*/
+type A2ElevatorData struct {
+	Id    string `json:"id"`
+	State struct {
+		Floor  string `json:"Floor"`
+		Status string `json:"Status"`
+		TFloor int64  `json:"TFloor"`
+	} `json:"State"`
+}
+
+func NVCSA2WebServer() {
+	r := gin.Default()
+	r.POST("/", func(c *gin.Context) {
+		var req A2ElevatorData
+		err := c.BindJSON(&req)
+		if err != nil {
+			c.JSON(http.StatusBadRequest, nil)
+			return
 		}
 
-		deviceAliveCache.Add(elevator.Name, true)
+		logger.Debug("Received A2 report data %+v", req)
 
-		logger.Debug("Received %d bytes from %s, %+v", numBytes, clientAddr, data)
+		if config.NVCSConf.OSD != "" {
+			floorText := req.State.Floor
+			floorText = floorText + " " + config.NVCSConf.OSD
+
+			// 璋冪敤hik api 灏嗘枃瀛楁坊鍔犲埌osd鐨勫乏涓嬭
+			AddFloorToOSD(floorText)
+		}
+
+		var d = models.Positions{
+			DeviceId:   req.Id,
+			Pos:        req.State.Floor,
+			CreateTime: time.Now().Unix(),
+			TimeString: time.Now().Format("2006-01-02 15:04:05"),
+		}
+
+		err = d.Save()
+		if err != nil {
+			logger.Warn("Device position update error:%s", err.Error())
+		}
+
+		c.JSON(http.StatusOK, "ok")
+	})
+
+	err := r.Run(fmt.Sprintf(":%s", config.NVCSConf.Port))
+	if err != nil {
+		logger.Warn("Start NVCS WebServer error, %s", err.Error())
+	}
+}
+
+func StartNVCSServer() {
+	if config.NVCSConf.Model == "A1" {
+		go NVCSA1UDPServer()
+	}
+	if config.NVCSConf.Model == "A2" {
+		go NVCSA2WebServer()
 	}
 }

--
Gitblit v1.8.0