forCaptureItsService.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package edge_service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "iot_manager_service/config"
  7. "net/http"
  8. url2 "net/url"
  9. "strings"
  10. "time"
  11. )
  12. // CaptureIts 获取设备能耗数据
  13. type ForCaptureIts struct{}
  14. // CaptureItsReq 请求参数
  15. type ForCaptureItsReq struct {
  16. Codes []string `json:"codes"` //抓拍单元 capture_sn列表
  17. Start string `json:"start"`
  18. End string `json:"end"`
  19. Flag int `json:"flag"` //0按日 1按月
  20. }
  21. // ForCaptureItsRes 返回数据
  22. type ForCaptureItsRes struct {
  23. Code int `json:"code"`
  24. Msg string `json:"msg"`
  25. Data []ForCaptureItsData `json:"data"`
  26. }
  27. type ForCaptureItsData struct {
  28. Code string `json:"code"` //抓拍单元 capture_sn
  29. Time string `json:"time"` //时间 2022-05-01 ||2022-05
  30. Flag int `json:"flag"` //0:日数据,1:月数据,2:小时数据
  31. Sflag int `json:"sflag"` //车速区间时:
  32. Total int `json:"total"` //数量
  33. Province string `json:"province"` //归属地时-用到的省
  34. City string `json:"city"` //归属地时-用到的城市
  35. Vtype int `json:"vtype"` //车型统计
  36. }
  37. // VehicleTotal 统计日 月 车流量 数据
  38. func (f ForCaptureIts) VehicleTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  39. api := "/data/v1/its/vehicletotal"
  40. return f.pubPost(reqPostData, api)
  41. }
  42. // Province 归属地省统计
  43. func (f ForCaptureIts) Province(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  44. api := "/data/v1/its/province"
  45. return f.pubPost(reqPostData, api)
  46. }
  47. // City 归属地城市统计
  48. func (f ForCaptureIts) City(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  49. api := "/data/v1/its/provincecity"
  50. return f.pubPost(reqPostData, api)
  51. }
  52. // Vehicletypeex 车型统计
  53. func (f ForCaptureIts) Vehicletypeex(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  54. api := "/data/v1/its/vehicletypeex"
  55. return f.pubPost(reqPostData, api)
  56. }
  57. // Vehiclespeedtotal 车速区间统计 日月
  58. func (f ForCaptureIts) VehicleSpeedTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  59. api := "/data/v1/its/vehiclespeedtotal"
  60. return f.pubPost(reqPostData, api)
  61. }
  62. // Vehicleoverspeedtotal 车速超速区间统计
  63. func (f ForCaptureIts) VehicleOverSpeedTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
  64. api := "/data/v1/its/vehicleoverspeedtotal"
  65. return f.pubPost(reqPostData, api)
  66. }
  67. //公用 post请求
  68. func (f ForCaptureIts) pubPost(reqPostData ForCaptureItsReq, api string) ([]ForCaptureItsData, error) {
  69. cfg := config.Instance()
  70. api = cfg.Foreign.IotEdgeUrl + api
  71. //fmt.Printf("api = %v \n", api)
  72. reqPostData.Start = reqPostData.Start + " 00:00:00"
  73. reqPostData.End = reqPostData.End + " 23:59:59"
  74. //fmt.Printf("reqPostData = %v \n", reqPostData)
  75. method := "POST"
  76. client := &http.Client{}
  77. marshal, _ := json.Marshal(reqPostData)
  78. req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
  79. if err != nil {
  80. return nil, err
  81. }
  82. res, err := client.Do(req)
  83. if err != nil {
  84. return nil, err
  85. }
  86. defer res.Body.Close()
  87. body, err := ioutil.ReadAll(res.Body)
  88. if err != nil {
  89. return nil, err
  90. }
  91. //fmt.Printf("body = %v", string(body))
  92. result := ForCaptureItsRes{}
  93. err = json.Unmarshal(body, &result)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if result.Code != 0 {
  98. panic(result.Msg)
  99. }
  100. //fmt.Printf("result.Data = %v", result.Data)
  101. return result.Data, nil
  102. }
  103. // SyncOverSpeedRecordData 同步超速车牌数据结构
  104. type SyncOverSpeedRecordData struct {
  105. ID int `json:"id"`
  106. Code string `json:"code"`
  107. Time string `json:"time"`
  108. Vtype int `json:"vtype"`
  109. Plate string `json:"plate"`
  110. Speed int `json:"speed"`
  111. Flag int `json:"flag"`
  112. }
  113. //同步超速车牌号返回数据
  114. type syncOverSpeedRecordDataRes struct {
  115. Code int `json:"code"`
  116. Msg string `json:"msg"`
  117. Data []SyncOverSpeedRecordData `json:"data"`
  118. }
  119. func (r *ForCaptureIts) SyncOverSpeedRecord(maxId int64, maxUpDateTime string, start, end string) ([]SyncOverSpeedRecordData, error) {
  120. maxId += 1
  121. if maxUpDateTime == "" {
  122. maxUpDateTime = time.Now().AddDate(-1, 0, 0).Format("2006-01-02 15:04:05")
  123. }
  124. cfg := config.Instance()
  125. api := cfg.Foreign.IotEdgeUrl + "/data/v1/its/overspeed/sync"
  126. url := fmt.Sprintf("%v?id=%d&start=%v&end=%v", api, maxId, url2.QueryEscape(maxUpDateTime), url2.QueryEscape(time.Now().Format("2006-01-02 15:04:05")))
  127. if start != "" {
  128. parse, err := time.Parse("2006-01-02", start)
  129. if err != nil {
  130. parse = time.Now().AddDate(-1, 0, 0)
  131. }
  132. startStr := parse.Format("2006-01-02 15:04:05")
  133. url = fmt.Sprintf("%v?id=%d&start=%v&end=%v", api, maxId, url2.QueryEscape(startStr), url2.QueryEscape(time.Now().Format("2006-01-02 15:04:05")))
  134. }
  135. //fmt.Printf("url = %v", url)
  136. method := "GET"
  137. client := &http.Client{}
  138. req, err := http.NewRequest(method, url, nil)
  139. if err != nil {
  140. return nil, err
  141. }
  142. res, err := client.Do(req)
  143. if err != nil {
  144. return nil, err
  145. }
  146. defer res.Body.Close()
  147. body, err := ioutil.ReadAll(res.Body)
  148. if err != nil {
  149. return nil, err
  150. }
  151. //fmt.Printf("body = %v", string(body))
  152. result := syncOverSpeedRecordDataRes{}
  153. err = json.Unmarshal(body, &result)
  154. if err != nil {
  155. return nil, err
  156. }
  157. if result.Code != 0 {
  158. panic(result.Msg)
  159. }
  160. //fmt.Printf("result.Data = %v", result.Data)
  161. return result.Data, nil
  162. }