client.go 7.9 KB

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