forCaptureItsService.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. Province string `json:"province"` //归属地时-用到的省
  31. City string `json:"city"` //归属地时-用到的城市
  32. }
  33. // VehicleTotal 统计日 月 车流量 数据
  34. func (f ForCaptureIts) VehicleTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  35. api := "/data/v1/its/vehicletotal"
  36. return f.pubPost(reqPostData, api)
  37. }
  38. // Province 归属地省统计
  39. func (f ForCaptureIts) Province(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  40. api := "/data/v1/its/province"
  41. return f.pubPost(reqPostData, api)
  42. }
  43. // City 归属地城市统计
  44. func (f ForCaptureIts) City(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  45. api := "/data/v1/its/provincecity"
  46. return f.pubPost(reqPostData, api)
  47. }
  48. //公用 post请求
  49. func (f ForCaptureIts) pubPost(reqPostData ForCaptureItsReq, api string) ([]ForCaptureItsData, error) {
  50. cfg := config.Instance()
  51. api = cfg.Foreign.IotEdgeUrl + api
  52. //fmt.Printf("api = %v \n", api)
  53. reqPostData.Start = reqPostData.Start + " 00:00:00"
  54. reqPostData.End = reqPostData.End + " 23:59:59"
  55. //fmt.Printf("reqPostData = %v \n", reqPostData)
  56. method := "POST"
  57. client := &http.Client{}
  58. marshal, _ := json.Marshal(reqPostData)
  59. req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
  60. if err != nil {
  61. return nil, err
  62. }
  63. res, err := client.Do(req)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer res.Body.Close()
  68. body, err := ioutil.ReadAll(res.Body)
  69. if err != nil {
  70. return nil, err
  71. }
  72. //fmt.Printf("body = %v", string(body))
  73. result := ForCaptureItsRes{}
  74. err = json.Unmarshal(body, &result)
  75. if err != nil {
  76. return nil, err
  77. }
  78. if result.Code != 0 {
  79. panic(result.Msg)
  80. }
  81. //fmt.Printf("result.Data = %v", result.Data)
  82. return result.Data, nil
  83. }