From 2c77f012601b7788dc58b0c9fd99aad587983b0d Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期二, 22 十月 2024 21:20:19 +0800 Subject: [PATCH] 完善楼层相关操作 --- client/system.go | 85 ++++++++++++++++++++++++++++++++++++++---- 1 files changed, 77 insertions(+), 8 deletions(-) diff --git a/client/system.go b/client/system.go index 805d169..52b4d68 100644 --- a/client/system.go +++ b/client/system.go @@ -1,11 +1,14 @@ package client import ( + "encoding/json" "fmt" + "gat1400Exchange/pkg" "io/ioutil" "gat1400Exchange/config" "gat1400Exchange/pkg/logger" + "gat1400Exchange/vo" dac "github.com/xinsnake/go-http-digest-auth-client" ) @@ -17,27 +20,91 @@ TimeUrI = "/VIID/System/Time" ) -func register() { - url := fmt.Sprintf("http://%s:%s%s", config.ClientConf.ServerAddr, config.ClientConf.ServerPort, RegisterUrI) - dr := dac.NewRequest(config.ClientConf.Username, config.ClientConf.Password, "GET", url, "") +var clientStatus = vo.StatusOtherError + +var headers = map[string]string{ + "User-Agent": "AI camera", + "Accept": "*", + "User-Identify": config.ClientConf.ChannelNo, + "Content-Type": "application/VIID+JSON", +} + +func register() bool { + url := fmt.Sprintf("%s://%s:%s%s", config.ClientConf.Proto, config.ClientConf.ServerAddr, config.ClientConf.ServerPort, RegisterUrI) + req := vo.RequestRegister{RegisterObject: vo.RequestDeviceID{DeviceID: config.ClientConf.ChannelNo}} + reqByte, _ := json.Marshal(req) + + dr := dac.NewRequest(config.ClientConf.Username, config.ClientConf.Password, "POST", url, string(reqByte)) + if headers != nil { + for k, v := range headers { + dr.Header.Set(k, v) + } + dr.Header.Del("Accept-Encoding") + } + resp, err := dr.Execute() if err != nil { logger.Error(err.Error()) - return + return false } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.Error(err.Error()) - return + return false } - fmt.Printf(string(body)) + fmt.Println("rsp:", string(body)) + var registerResponse vo.ResponseStatus + err = json.Unmarshal(body, ®isterResponse) + if err != nil { + fmt.Println("register error,", err.Error()) + return false + } + + clientStatus = registerResponse.StatusCode + + if registerResponse.StatusCode == vo.StatusSuccess { + fmt.Println("register success") + // 娉ㄥ唽鎴愬姛鍚庢彁浜や繚娲诲拰鏍℃椂 + keepalive() + syncTime() + } else { + fmt.Println("register failure, ", registerResponse.StatusString) + } + + return clientStatus == vo.StatusSuccess } -func keepalive() { +func keepalive() int { + if clientStatus != vo.StatusSuccess { + return clientStatus + } + var body = vo.RequestKeepalive{ + KeepaliveObject: vo.RequestDeviceID{ + DeviceID: config.ClientConf.ChannelNo, + }, + } + + url := fmt.Sprintf("%s://%s:%s%s", config.ClientConf.Proto, config.ClientConf.ServerAddr, config.ClientConf.ServerPort, KeepaliveUrI) + b, _ := json.Marshal(body) + rsp, err := pkg.HttpPost(url, headers, b) + if err != nil { + logger.Warn("Keepalive request failed, %s", err.Error()) + return vo.StatusOtherError + } + + var stat vo.ResponseStatus + err = json.Unmarshal(rsp, &stat) + if err != nil { + logger.Warn("Keepalive response unmarshal failed, %s", err.Error()) + return vo.StatusOtherError + } + + return stat.StatusCode } func unRegister() { @@ -45,5 +112,7 @@ } func syncTime() { - + if clientStatus != vo.StatusSuccess { + return + } } -- Gitblit v1.8.0