package service
|
|
import (
|
"bytes"
|
"context"
|
"encoding/binary"
|
"encoding/hex"
|
"encoding/json"
|
"fmt"
|
"io/ioutil"
|
"net"
|
"net/http"
|
"strings"
|
"time"
|
|
"gat1400Exchange/config"
|
"gat1400Exchange/models"
|
"gat1400Exchange/pkg/logger"
|
"gat1400Exchange/rfid"
|
"gat1400Exchange/util"
|
|
"github.com/gin-gonic/gin"
|
"github.com/google/uuid"
|
"golang.org/x/text/encoding/simplifiedchinese"
|
"golang.org/x/text/transform"
|
)
|
|
type A1SetFloorFrame struct {
|
Header [16]byte // Fixed 16-byte header
|
Command byte // 1 byte command
|
MAC [6]byte // 6 bytes MAC address
|
DataLength uint16 // 2 bytes data length
|
Data []byte // Data area, length defined by DataLength
|
Checksum uint16 // 2 bytes CRC16 checksum
|
}
|
|
// Convert frame to byte slice
|
func (f *A1SetFloorFrame) toBytes(includeChecksum bool) []byte {
|
buf := new(bytes.Buffer)
|
binary.Write(buf, binary.LittleEndian, f.Header)
|
binary.Write(buf, binary.LittleEndian, f.Command)
|
binary.Write(buf, binary.LittleEndian, f.MAC)
|
binary.Write(buf, binary.LittleEndian, f.DataLength)
|
buf.Write(f.Data)
|
if includeChecksum {
|
binary.Write(buf, binary.LittleEndian, f.Checksum)
|
}
|
return buf.Bytes()
|
}
|
|
type A1ElevatorStatus struct {
|
TotalFloors int `json:"TotalFloors"`
|
Floor int `json:"Floor"`
|
FloorName string `json:"FloorName"`
|
RunDir int `json:"RunDir"`
|
Speed string `json:"Speed"`
|
}
|
|
type A1Elevator struct {
|
Name string `json:"Name"`
|
IP string `json:"IP"`
|
Status A1ElevatorStatus `json:"Status"`
|
Alarm []interface{} `json:"Alarm"` // You might want to define a specific type for alarms
|
}
|
|
type A1ElevatorData struct {
|
Elevator []A1Elevator `json:"Elevator"`
|
}
|
|
const (
|
RunStop = iota
|
RunUp
|
RunDown
|
)
|
|
var ElevatorRunState int
|
var ElevatorRunFloor string
|
var RunningCorrectTaskId string
|
|
func A1CorrectFloor() {
|
if RunningCorrectTaskId != "" || config.RFIDConf.EPC == "" {
|
return
|
}
|
|
taskId := uuid.New().String()
|
RunningCorrectTaskId = taskId
|
|
rfidReader := rfid.NewReader(config.RFIDConf.DevName, 115200, 5)
|
defer rfidReader.CloseSerial()
|
|
err := rfidReader.OpenSerial()
|
if err != nil {
|
logger.Error(err.Error())
|
return
|
}
|
|
isFind, err := rfidReader.ScanSpecificEPC(config.RFIDConf.EPC, 5)
|
if isFind && RunningCorrectTaskId == taskId {
|
frame := NewA1SetFloorFrame(config.NVCSConf.Mac, config.RFIDConf.Position)
|
address := "192.168.10.253:50000"
|
err := A1SendFrame(frame, address)
|
if err != nil {
|
logger.Warn(err.Error())
|
} else {
|
logger.Debug("The floor has been calibrated.")
|
}
|
}
|
}
|
|
func calculateCRC16(data []byte) uint16 {
|
var crc16 uint16 = 0xFFFF
|
|
for i := 0; i < len(data); i++ {
|
crc16 ^= uint16(data[i])
|
for j := 0; j < 8; j++ {
|
if crc16&0x0001 != 0 {
|
crc16 >>= 1
|
crc16 ^= 0xA001
|
} else {
|
crc16 >>= 1
|
}
|
}
|
}
|
|
return crc16
|
}
|
|
// Create a new frame based on provided data
|
func NewA1SetFloorFrame(macAddr string, floor uint8) *A1SetFloorFrame {
|
b, err := hex.DecodeString(macAddr)
|
if err != nil {
|
return nil
|
}
|
|
if len(b) != 6 {
|
return nil
|
}
|
|
var mac [6]byte
|
copy(mac[:], b)
|
|
//b, err = hex.DecodeString(floor)
|
frame := &A1SetFloorFrame{
|
Header: [16]byte{0x45, 0x4c, 0x45, 0x56, 0x41, 0x54, 0x4f, 0x52, 0x2d, 0x53, 0x45, 0x4e, 0x53, 0x4f, 0x52, 0x00},
|
Command: 0x0c,
|
MAC: mac,
|
DataLength: 1,
|
Data: []byte{floor},
|
}
|
|
frame.Checksum = calculateCRC16(frame.toBytes(false)) // Calculate CRC without including the checksum itself
|
|
return frame
|
}
|
|
func A1SendFrame(frame *A1SetFloorFrame, address string) error {
|
conn, err := net.Dial("udp", address)
|
if err != nil {
|
return err
|
}
|
defer conn.Close()
|
|
// Send frame
|
_, err = conn.Write(frame.toBytes(true))
|
return err
|
}
|
|
// 对接网络视频字符叠加器,接收udp发送的楼层信息, 更新device地址
|
func NVCSA1UDPServer() {
|
// 指定监听的端口
|
port := config.ServeConf.Port
|
|
// 创建一个UDP地址
|
address, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%s", port))
|
if err != nil {
|
logger.Error("Error resolving address:", err)
|
return
|
}
|
|
// 创建一个UDP连接
|
conn, err := net.ListenUDP("udp", address)
|
if err != nil {
|
logger.Error("Error listening:", err)
|
return
|
}
|
defer conn.Close()
|
|
logger.Info("UDP server listening on port %s...", port)
|
|
rfidReader := rfid.NewReader(config.RFIDConf.DevName, 115200, 5)
|
if config.RFIDConf.ReadFloor {
|
defer rfidReader.CloseSerial()
|
|
err := rfidReader.OpenSerial()
|
if err != nil {
|
logger.Error("open rfid. %s", err.Error())
|
return
|
}
|
|
ctx, _ := context.WithCancel(context.Background())
|
|
go func() {
|
err := rfidReader.ReadEPCData(ctx)
|
if err != nil {
|
logger.Error("rfid read. %s", err.Error())
|
}
|
}()
|
}
|
|
var lastFloor int
|
var lastSaveTime int64
|
// 无限循环等待接收数据
|
for {
|
// 创建一个缓冲区来存储接收的数据
|
buffer := make([]byte, 256)
|
|
// 从连接中读取数据
|
numBytes, clientAddr, err := conn.ReadFromUDP(buffer)
|
if err != nil {
|
logger.Warn("Error reading from UDP connection:%s", err.Error())
|
continue
|
}
|
|
// 设备上传的中文数据为GBK编码, 转换为utf8
|
decoder := simplifiedchinese.GBK.NewDecoder()
|
reader := transform.NewReader(bytes.NewReader(buffer[:numBytes]), decoder)
|
decodedBytes, err := ioutil.ReadAll(reader)
|
|
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
|
}
|
|
elevator := data.Elevator[0]
|
|
var runStateStr string
|
if config.NVCSConf.RunState {
|
if elevator.Status.RunDir == RunUp {
|
runStateStr = "上"
|
} else if elevator.Status.RunDir == RunDown {
|
runStateStr = "下"
|
}
|
}
|
|
if !config.NVCSConf.RunState {
|
runStateStr = ""
|
}
|
|
// 设置osd 格式 "1F上 固 枪"
|
if config.NVCSConf.OSD != "" {
|
floorText := fmt.Sprintf("%s%s %s", data.Elevator[0].Status.FloorName, runStateStr, config.NVCSConf.OSD)
|
|
// 调用hik api 将文字添加到osd的左下角
|
AddFloorToOSD(floorText)
|
}
|
|
// correct floor when elevator stopped.
|
//if elevator.Status.RunDir == 0 {
|
// go A1CorrectFloor()
|
//} else {
|
// RunningCorrectTaskId = ""
|
//}
|
|
if lastFloor == elevator.Status.Floor && (time.Now().Unix()-lastSaveTime < 2) {
|
continue
|
}
|
lastFloor = elevator.Status.Floor
|
lastSaveTime = time.Now().Unix()
|
|
// 程序部署在设备端, 字符叠加器上报的名称允许为空. 在云端, 名称必须与摄像机相同
|
if elevator.Name == "" {
|
elevator.Name = "1"
|
}
|
|
ElevatorRunState = elevator.Status.RunDir
|
ElevatorRunFloor = elevator.Status.FloorName
|
if config.RFIDConf.ReadFloor && rfidReader.EPCData != "" {
|
ElevatorRunFloor, _ = rfid.Parse2Floor(rfidReader.EPCData)
|
|
if ElevatorRunFloor != elevator.Status.FloorName {
|
logger.Warn("rfid epc %s, nvcs floor %s", ElevatorRunFloor, elevator.Status.FloorName)
|
}
|
}
|
|
go func() {
|
var d = models.Positions{
|
DeviceId: elevator.Name,
|
Pos: ElevatorRunFloor,
|
RunDir: ElevatorRunState,
|
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())
|
}
|
}()
|
}
|
}
|
|
/*
|
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"`
|
Speed string `json:"Speed"`
|
} `json:"State"`
|
}
|
|
type A2ElevatorConfig struct {
|
FloorData []string `json:"floordata"`
|
}
|
|
var A2TopFloor string
|
var A2BottomFloor string
|
|
func GetA2ElevatorConfig() {
|
url := "http://192.168.10.253/cgi-bin/liftnum.cgi"
|
payload := []byte("{\"display\":1}")
|
header := map[string]string{
|
"Cookie": "eyJuYW1lIjoiYWRtaW4iLCAicGFzZCI6ImFkbWluMTIzIn0=",
|
}
|
|
rsp, err := util.HttpPost(url, header, payload)
|
if err != nil {
|
logger.Warn("Get A2 floor data failure,%s", err.Error())
|
return
|
}
|
|
var configData A2ElevatorConfig
|
err = json.Unmarshal(rsp, &configData)
|
if err != nil {
|
logger.Warn("Unmarshal A2 floor data failure,%s", err.Error())
|
return
|
}
|
|
if len(configData.FloorData) > 0 {
|
A2BottomFloor, A2TopFloor = configData.FloorData[0], configData.FloorData[len(configData.FloorData)-1]
|
}
|
|
logger.Info("A2 floor config total:%d, bottomFloor:%s, topFloor:%s", len(configData.FloorData), A2BottomFloor, A2TopFloor)
|
}
|
|
func NVCSA2WebServer() {
|
// 先获取总楼层数, 记录最高层
|
//GetA2ElevatorConfig()
|
|
r := gin.Default()
|
|
r.POST("/", func(c *gin.Context) {
|
var req A2ElevatorData
|
var runState string
|
var iRunState int
|
|
err := c.BindJSON(&req)
|
if err != nil {
|
c.JSON(http.StatusBadRequest, nil)
|
return
|
}
|
|
logger.Debug("Received A2 report data %+v", req)
|
|
// 记录电梯运行状态
|
if strings.Contains(req.State.Status, "上") {
|
runState = "上"
|
iRunState = RunUp
|
} else if strings.Contains(req.State.Status, "下") {
|
runState = "下"
|
iRunState = RunDown
|
}
|
|
ElevatorRunState = iRunState
|
ElevatorRunFloor = req.State.Floor
|
|
//// 已到最下层
|
//if req.State.Floor == A2BottomFloor {
|
// runState = "上"
|
// iRunState = RunUp
|
//}
|
//if req.State.Floor == A2TopFloor {
|
// runState = "下"
|
// iRunState = RunDown
|
//}
|
|
if !config.NVCSConf.RunState {
|
runState = ""
|
}
|
|
if config.NVCSConf.OSD != "" {
|
floorText := fmt.Sprintf("%s%s %s", req.State.Floor, runState, config.NVCSConf.OSD)
|
|
// 调用hik api 将文字添加到osd的左下角
|
AddFloorToOSD(floorText)
|
}
|
|
var d = models.Positions{
|
DeviceId: req.Id,
|
Pos: req.State.Floor,
|
RunDir: iRunState,
|
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())
|
}
|
}
|
|
type A3ElevatorData struct {
|
Id string `json:"id"`
|
Time string `json:"time"`
|
Total int `json:"total"`
|
Status int `json:"status"`
|
Floor int `json:"floor"`
|
}
|
|
func NVCSA3WebServer() {
|
r := gin.Default()
|
|
r.POST("/", func(c *gin.Context) {
|
var req A3ElevatorData
|
var runState string
|
var iRunState int
|
|
err := c.BindJSON(&req)
|
if err != nil {
|
c.JSON(http.StatusBadRequest, nil)
|
return
|
}
|
|
iRunState = req.Status
|
ElevatorRunState = req.Status
|
ElevatorRunFloor = fmt.Sprintf("%dF", req.Floor)
|
logger.Debug("Received A2 report data %+v", req)
|
|
// 记录电梯运行状态
|
if iRunState == RunUp {
|
runState = "上"
|
} else if iRunState == RunDown {
|
runState = "下"
|
}
|
|
if !config.NVCSConf.RunState {
|
runState = ""
|
}
|
|
if config.NVCSConf.OSD != "" {
|
floorText := fmt.Sprintf("%dF%s %s", req.Floor, runState, config.NVCSConf.OSD)
|
|
// 调用hik api 将文字添加到osd的左下角
|
AddFloorToOSD(floorText)
|
}
|
|
var d = models.Positions{
|
DeviceId: req.Id,
|
Pos: fmt.Sprintf("%dF", req.Floor),
|
RunDir: iRunState,
|
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()
|
}
|
|
if config.NVCSConf.Model == "A3" {
|
go NVCSA3WebServer()
|
}
|
}
|