client.go 7.7 KB

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