From 62beabfd1466fd3a6b6c0736acc8c5a3b3ec4b3b Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期日, 28 四月 2024 10:10:35 +0800 Subject: [PATCH] 完善1400转发流程 --- client/system.go | 84 ++++++++++++++++++++++++++++++++++++++---- 1 files changed, 76 insertions(+), 8 deletions(-) diff --git a/client/system.go b/client/system.go index 805d169..03b42ae 100644 --- a/client/system.go +++ b/client/system.go @@ -1,11 +1,14 @@ package client import ( + "encoding/json" "fmt" + "gat1400Exchange/util" "io/ioutil" "gat1400Exchange/config" "gat1400Exchange/pkg/logger" + "gat1400Exchange/vo" dac "github.com/xinsnake/go-http-digest-auth-client" ) @@ -17,27 +20,90 @@ 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, + }, + } + + b, _ := json.Marshal(body) + rsp, err := util.HttpPost(KeepaliveUrI, 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 +111,7 @@ } func syncTime() { - + if clientStatus != vo.StatusSuccess { + return + } } -- Gitblit v1.8.0