client.go 7.2 KB

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