forCaptureItsService.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package edge_service
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "iot_manager_service/config"
  6. "net/http"
  7. "strings"
  8. )
  9. // CaptureIts 获取设备能耗数据
  10. type ForCaptureIts struct{}
  11. // CaptureItsReq 请求参数
  12. type ForCaptureItsReq struct {
  13. Codes []string `json:"codes"` //抓拍单元 capture_sn列表
  14. Start string `json:"start"`
  15. End string `json:"end"`
  16. Flag int `json:"flag"` //0按日 1按月
  17. }
  18. // ForCaptureItsRes 返回数据
  19. type ForCaptureItsRes struct {
  20. Code int `json:"code"`
  21. Msg string `json:"msg"`
  22. Data []ForCaptureItsData `json:"data"`
  23. }
  24. type ForCaptureItsData struct {
  25. Code string `json:"code"` //抓拍单元 capture_sn
  26. Time string `json:"time"` //时间 2022-05-01 ||2022-05
  27. Flag int `json:"flag"` //0:日数据,1:月数据,2:小时数据
  28. Sflag int `json:"sflag"` //车速区间时:
  29. Total int `json:"total"` //数量
  30. }
  31. // VehicleTotal 统计日 月 车流量 数据
  32. func (f ForCaptureIts) VehicleTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  33. api := "/data/v1/its/vehicletotal"
  34. return f.pubPost(reqPostData, api)
  35. }
  36. //公用 post请求
  37. func (f ForCaptureIts) pubPost(reqPostData ForCaptureItsReq, api string) ([]ForCaptureItsData, error) {
  38. cfg := config.Instance()
  39. api = cfg.Foreign.IotEdgeUrl + api
  40. //fmt.Printf("api = %v \n", api)
  41. reqPostData.Start = reqPostData.Start + " 00:00:00"
  42. reqPostData.End = reqPostData.End + " 23:59:59"
  43. //fmt.Printf("reqPostData = %v \n", reqPostData)
  44. method := "POST"
  45. client := &http.Client{}
  46. marshal, _ := json.Marshal(reqPostData)
  47. req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
  48. if err != nil {
  49. return nil, err
  50. }
  51. res, err := client.Do(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer res.Body.Close()
  56. body, err := ioutil.ReadAll(res.Body)
  57. if err != nil {
  58. return nil, err
  59. }
  60. //fmt.Printf("body = %v", string(body))
  61. result := ForCaptureItsRes{}
  62. err = json.Unmarshal(body, &result)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if result.Code != 0 {
  67. panic(result.Msg)
  68. }
  69. //fmt.Printf("result.Data = %v", result.Data)
  70. return result.Data, nil
  71. }