package isapi import ( "bytes" "errors" "fmt" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "io/ioutil" "lcfns/model/common/response" "net/http" "net/url" "strings" ) const ( contentTypeXML = `application/xml; charset="UTF-8"` contentTypeJSON = `application/json; charset="UTF-8"` contentTypeHTML = `text/html` ) type Client struct { Client *http.Client BaseURL string } func NewClient(host, username, password string) (*Client, error) { u, err := url.Parse("http://" + host) if err != nil { return nil, err } return &Client{ Client: &http.Client{ Transport: NewAuthTransport(username, password), }, BaseURL: u.String(), }, nil } func TouChuan(host string, o Operation, data []byte, c *gin.Context) (resp any, err error) { resp, err = Com(host, o, data) if err != nil { logrus.Error(err) if r, ok := resp.(ResponseStatus); ok { response.FailWithDetailed(r, err.Error(), c) } else { //可能是摄像头未初始化成功,返回err!=nil,且resp!=ResponseStatus response.FailWithDetailed("操作失败,请重试", err.Error(), c) } return } //断言成功,说明是配置响应,检查配置结果 r, ok := resp.(ResponseStatus) if ok { if r.StatusCode == 0 || r.StatusCode == 1 { response.OkWithDetailed(resp, "操作成功", c) return } else { response.FailWithDetailed(resp, "操作失败,请联系管理员", c) return } } //断言失败,说明是查询响应,直接返回数据 response.OkWithData(resp, c) return } // Com 透传 func Com(host string, o Operation, data []byte) (any, error) { //TODO 应该使用公共的http client c, err := NewClient(host, "admin", "kk176@lc") if err != nil { return nil, err } switch o { case GetDeviceInfo: return c.GetDeviceInfo() case GetSystemCap: return c.GetSystemCap() case GetChanCap: return c.GetOneChanEventCap() //遮盖侦测 //case GetTamperDetection: // return c.GetTamperDetection("1") //case PutTamperDetection: // return c.PutTamperDetection("1", data) //移动侦测 //case GetMotionDetection: // return c.GetMotionDetection("1") //case PutMotionDetection: // return c.PutMotionDetection("1", data) //越界侦测 case GetLineDetectionCap: return c.GetLineDetectionCap() case GetLineDetectionCal: return c.GetSizeLd() case PutLineDetectionCal: return c.PutSizeLd(data) case GetLineDetection: return c.GetLineDetection() case PutLineDetection: return c.PutLineDetection(data) //区域入侵 case GetFieldDetectionCap: return c.GetFieldDetectionCap() case GetFieldDetectionCal: return c.GetSizeFd() case PutFieldDetectionCal: return c.PutSizeFd(data) case GetFieldDetection: return c.GetFieldDetection() case PutFieldDetection: return c.PutFieldDetection(data) //进入区域 case GetRegionEntranceCap: return c.GetRegionEntranceCap() case GetRegionEntranceCal: return c.GetSizeRe() case PutRegionEntranceCal: return c.PutSizeRe(data) case GetRegionEntrance: return c.GetRegionEntrance() case PutRegionEntrance: return c.PutRegionEntrance(data) //离开区域 //case GetRegionExiting: // return c.GetRegionExiting() //case PutRegionExiting: // return c.PutRegionExiting(data) case SetHosts: return c.PutHost(data) case SetSIP: return c.PutSip(data) case SetSIPInfo: return c.PutSipInfo(data) case PutEmail: return c.PutEmail(data) } return nil, errors.New("未定义操作") } func (c *Client) Do(r *http.Request) ([]byte, error) { resp, err := c.Client.Do(r) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } func (c *Client) CommonGet(path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.Get(u) if err != nil { return nil, err } return resp, nil } func (c *Client) CommonPut(data []byte, path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.PutXML(u, data) if err != nil { return nil, err } return resp, nil } func (c *Client) CommonDel(data []byte, path string) (resp []byte, err error) { u, err := url.Parse(c.BaseURL + path) if err != nil { return nil, err } resp, err = c.DeleteXML(u, data) if err != nil { return nil, err } return resp, nil } func (c *Client) Get(u *url.URL) ([]byte, error) { req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return nil, err } return c.Do(req) } func (c *Client) put(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("PUT", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) PutXML(u *url.URL, data []byte) ([]byte, error) { return c.put(u, contentTypeXML, data) } func (c *Client) PutJSON(u *url.URL, data []byte) ([]byte, error) { return c.put(u, contentTypeJSON, data) } func (c *Client) post(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("POST", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) PostXML(u *url.URL, data []byte) ([]byte, error) { return c.post(u, contentTypeXML, data) } func (c *Client) PostJSON(u *url.URL, data []byte) ([]byte, error) { return c.post(u, contentTypeJSON, data) } func (c *Client) delete(u *url.URL, contentType string, data []byte) ([]byte, error) { b := bytes.NewBuffer(data) req, err := http.NewRequest("DELETE", u.String(), b) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } func (c *Client) DeleteXML(u *url.URL, data []byte) ([]byte, error) { return c.delete(u, contentTypeXML, data) } func (c *Client) DeleteJSON(u *url.URL, data []byte) ([]byte, error) { return c.delete(u, contentTypeJSON, data) } func DoModFacePicRecord(url, json, faceimage, boundary string) { var bodyParam = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" + "Content-Type: text/json\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" + json + "\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceImage\";\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" + faceimage + "\r\n--" + boundary + "--\r\n" req, err := http.NewRequest("PUT", url, strings.NewReader(bodyParam)) if err != nil { logrus.Error(err) } req.SetBasicAuth("", "") req.Header.Add("Accept", "text/html, application/xhtml+xml") req.Header.Add("Accept-Language", "zh-CN") req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary) req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)") req.Header.Add("Accept-Encoding", "gzip, deflate") req.Header.Add("Connection", "Keep-Alive") req.Header.Add("Cache-Control", "no-cache") } func DoPostStorageCloud(url, json, faceimage, boundary string) { var bodyParam = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" + "Content-Type: text/json\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" + json + "\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"FaceImage\";\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" + faceimage + "\r\n--" + boundary + "--\r\n" req, err := http.NewRequest("POST", url, strings.NewReader(bodyParam)) if err != nil { logrus.Error(err) } req.SetBasicAuth("", "") req.Header.Add("Accept", "text/html, application/xhtml+xml") req.Header.Add("Accept-Language", "zh-CN") req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary) req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)") req.Header.Add("Accept-Encoding", "gzip, deflate") req.Header.Add("Connection", "Keep-Alive") req.Header.Add("Cache-Control", "no-cache") }