client.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package isapi
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/sirupsen/logrus"
  8. "io/ioutil"
  9. "lcfns/model/common/response"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. )
  14. const (
  15. contentTypeXML = `application/xml; charset="UTF-8"`
  16. contentTypeJSON = `application/json; charset="UTF-8"`
  17. contentTypeHTML = `text/html`
  18. )
  19. type Client struct {
  20. Client *http.Client
  21. BaseURL string
  22. }
  23. func NewClient(host, username, password string) (*Client, error) {
  24. u, err := url.Parse("http://" + host)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &Client{
  29. Client: &http.Client{
  30. Transport: NewAuthTransport(username, password),
  31. },
  32. BaseURL: u.String(),
  33. }, nil
  34. }
  35. func TouChuan(host string, o Operation, data []byte, c *gin.Context) (resp any, err error) {
  36. resp, err = Com(host, o, data)
  37. if err != nil {
  38. logrus.Error(err)
  39. if r, ok := resp.(ResponseStatus); ok {
  40. response.FailWithDetailed(r, err.Error(), c)
  41. } else {
  42. //可能是摄像头未初始化成功,返回err!=nil,且resp!=ResponseStatus
  43. response.FailWithDetailed("操作失败,请重试", err.Error(), c)
  44. }
  45. return
  46. }
  47. //断言成功,说明是配置响应,检查配置结果
  48. r, ok := resp.(ResponseStatus)
  49. if ok {
  50. if r.StatusCode == 0 || r.StatusCode == 1 {
  51. response.OkWithDetailed(resp, "操作成功", c)
  52. return
  53. } else {
  54. response.FailWithDetailed(resp, "操作失败,请联系管理员", c)
  55. return
  56. }
  57. }
  58. //断言失败,说明是查询响应,直接返回数据
  59. response.OkWithData(resp, c)
  60. return
  61. }
  62. // Com 透传
  63. func Com(host string, o Operation, data []byte) (any, error) {
  64. //TODO 应该使用公共的http client
  65. c, err := NewClient(host, "admin", "kk176@lc")
  66. if err != nil {
  67. return nil, err
  68. }
  69. switch o {
  70. case GetDeviceInfo:
  71. return c.GetDeviceInfo()
  72. case GetSystemCap:
  73. return c.GetSystemCap()
  74. case GetChanCap:
  75. return c.GetOneChanEventCap()
  76. //遮盖侦测
  77. //case GetTamperDetection:
  78. // return c.GetTamperDetection("1")
  79. //case PutTamperDetection:
  80. // return c.PutTamperDetection("1", data)
  81. //移动侦测
  82. //case GetMotionDetection:
  83. // return c.GetMotionDetection("1")
  84. //case PutMotionDetection:
  85. // return c.PutMotionDetection("1", data)
  86. //越界侦测
  87. case GetLineDetectionCap:
  88. return c.GetLineDetectionCap()
  89. case GetLineDetectionCal:
  90. return c.GetSizeLd()
  91. case PutLineDetectionCal:
  92. return c.PutSizeLd(data)
  93. case GetLineDetection:
  94. return c.GetLineDetection()
  95. case PutLineDetection:
  96. return c.PutLineDetection(data)
  97. //区域入侵
  98. case GetFieldDetectionCap:
  99. return c.GetFieldDetectionCap()
  100. case GetFieldDetectionCal:
  101. return c.GetSizeFd()
  102. case PutFieldDetectionCal:
  103. return c.PutSizeFd(data)
  104. case GetFieldDetection:
  105. return c.GetFieldDetection()
  106. case PutFieldDetection:
  107. return c.PutFieldDetection(data)
  108. //进入区域
  109. case GetRegionEntranceCap:
  110. return c.GetRegionEntranceCap()
  111. case GetRegionEntranceCal:
  112. return c.GetSizeRe()
  113. case PutRegionEntranceCal:
  114. return c.PutSizeRe(data)
  115. case GetRegionEntrance:
  116. return c.GetRegionEntrance()
  117. case PutRegionEntrance:
  118. return c.PutRegionEntrance(data)
  119. //离开区域
  120. //case GetRegionExiting:
  121. // return c.GetRegionExiting()
  122. //case PutRegionExiting:
  123. // return c.PutRegionExiting(data)
  124. case SetHosts:
  125. return c.PutHost(data)
  126. case SetSIP:
  127. return c.PutSip(data)
  128. case SetSIPInfo:
  129. return c.PutSipInfo(data)
  130. case PutEmail:
  131. return c.PutEmail(data)
  132. }
  133. return nil, errors.New("未定义操作")
  134. }
  135. func (c *Client) Do(r *http.Request) ([]byte, error) {
  136. resp, err := c.Client.Do(r)
  137. if err != nil {
  138. return nil, err
  139. }
  140. defer resp.Body.Close()
  141. body, err := ioutil.ReadAll(resp.Body)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return body, nil
  146. }
  147. func (c *Client) CommonGet(path string) (resp []byte, err error) {
  148. u, err := url.Parse(c.BaseURL + path)
  149. if err != nil {
  150. return nil, err
  151. }
  152. resp, err = c.Get(u)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return resp, nil
  157. }
  158. func (c *Client) CommonPut(data []byte, path string) (resp []byte, err error) {
  159. u, err := url.Parse(c.BaseURL + path)
  160. if err != nil {
  161. return nil, err
  162. }
  163. resp, err = c.PutXML(u, data)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return resp, nil
  168. }
  169. func (c *Client) CommonDel(data []byte, path string) (resp []byte, err error) {
  170. u, err := url.Parse(c.BaseURL + path)
  171. if err != nil {
  172. return nil, err
  173. }
  174. resp, err = c.DeleteXML(u, data)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return resp, nil
  179. }
  180. func (c *Client) Get(u *url.URL) ([]byte, error) {
  181. req, err := http.NewRequest("GET", u.String(), nil)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return c.Do(req)
  186. }
  187. func (c *Client) put(u *url.URL, contentType string, data []byte) ([]byte, error) {
  188. b := bytes.NewBuffer(data)
  189. req, err := http.NewRequest("PUT", u.String(), b)
  190. if err != nil {
  191. return nil, err
  192. }
  193. req.Header.Set("Content-Type", contentType)
  194. return c.Do(req)
  195. }
  196. func (c *Client) PutXML(u *url.URL, data []byte) ([]byte, error) {
  197. return c.put(u, contentTypeXML, data)
  198. }
  199. func (c *Client) PutJSON(u *url.URL, data []byte) ([]byte, error) {
  200. return c.put(u, contentTypeJSON, data)
  201. }
  202. func (c *Client) post(u *url.URL, contentType string, data []byte) ([]byte, error) {
  203. b := bytes.NewBuffer(data)
  204. req, err := http.NewRequest("POST", u.String(), b)
  205. if err != nil {
  206. return nil, err
  207. }
  208. req.Header.Set("Content-Type", contentType)
  209. return c.Do(req)
  210. }
  211. func (c *Client) PostXML(u *url.URL, data []byte) ([]byte, error) {
  212. return c.post(u, contentTypeXML, data)
  213. }
  214. func (c *Client) PostJSON(u *url.URL, data []byte) ([]byte, error) {
  215. return c.post(u, contentTypeJSON, data)
  216. }
  217. func (c *Client) delete(u *url.URL, contentType string, data []byte) ([]byte, error) {
  218. b := bytes.NewBuffer(data)
  219. req, err := http.NewRequest("DELETE", u.String(), b)
  220. if err != nil {
  221. return nil, err
  222. }
  223. req.Header.Set("Content-Type", contentType)
  224. return c.Do(req)
  225. }
  226. func (c *Client) DeleteXML(u *url.URL, data []byte) ([]byte, error) {
  227. return c.delete(u, contentTypeXML, data)
  228. }
  229. func (c *Client) DeleteJSON(u *url.URL, data []byte) ([]byte, error) {
  230. return c.delete(u, contentTypeJSON, data)
  231. }
  232. func DoModFacePicRecord(url, json, faceimage, boundary string) {
  233. var bodyParam = "--" + boundary + "\r\n" +
  234. "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" +
  235. "Content-Type: text/json\r\n" +
  236. "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" +
  237. json + "\r\n" +
  238. "--" + boundary + "\r\n" +
  239. "Content-Disposition: form-data; name=\"FaceImage\";\r\n" +
  240. "Content-Type: image/jpeg\r\n" +
  241. "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" +
  242. faceimage +
  243. "\r\n--" + boundary + "--\r\n"
  244. req, err := http.NewRequest("PUT", url, strings.NewReader(bodyParam))
  245. if err != nil {
  246. logrus.Error(err)
  247. }
  248. req.SetBasicAuth("", "")
  249. req.Header.Add("Accept", "text/html, application/xhtml+xml")
  250. req.Header.Add("Accept-Language", "zh-CN")
  251. req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary)
  252. req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")
  253. req.Header.Add("Accept-Encoding", "gzip, deflate")
  254. req.Header.Add("Connection", "Keep-Alive")
  255. req.Header.Add("Cache-Control", "no-cache")
  256. }
  257. func DoPostStorageCloud(url, json, faceimage, boundary string) {
  258. var bodyParam = "--" + boundary + "\r\n" +
  259. "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" +
  260. "Content-Type: text/json\r\n" +
  261. "Content-Length: " + fmt.Sprintf("%d", len(json)) + "\r\n\r\n" +
  262. json + "\r\n" +
  263. "--" + boundary + "\r\n" +
  264. "Content-Disposition: form-data; name=\"FaceImage\";\r\n" +
  265. "Content-Type: image/jpeg\r\n" +
  266. "Content-Length: " + fmt.Sprintf("%d", len(faceimage)) + "\r\n\r\n" +
  267. faceimage +
  268. "\r\n--" + boundary + "--\r\n"
  269. req, err := http.NewRequest("POST", url, strings.NewReader(bodyParam))
  270. if err != nil {
  271. logrus.Error(err)
  272. }
  273. req.SetBasicAuth("", "")
  274. req.Header.Add("Accept", "text/html, application/xhtml+xml")
  275. req.Header.Add("Accept-Language", "zh-CN")
  276. req.Header.Add("Content-Type", "multipart/form-data; boundary="+boundary)
  277. req.Header.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")
  278. req.Header.Add("Accept-Encoding", "gzip, deflate")
  279. req.Header.Add("Connection", "Keep-Alive")
  280. req.Header.Add("Cache-Control", "no-cache")
  281. }