forCaptureItsService.go 5.6 KB

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