forCaptureItsService.go 2.8 KB

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