| | |
| | | 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" |
| | | ) |
| | |
| | | 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() { |
| | |
| | | } |
| | | |
| | | func syncTime() { |
| | | |
| | | if clientStatus != vo.StatusSuccess { |
| | | return |
| | | } |
| | | } |