forCaptureItsService.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //fmt.Printf("api = %v \n", api)
  85. if !strings.Contains(reqPostData.Start, ":") {
  86. reqPostData.Start = reqPostData.Start + " 00:00:00"
  87. reqPostData.End = reqPostData.End + " 23:59:59"
  88. }
  89. //fmt.Printf("reqPostData = %v \n", reqPostData)
  90. method := "POST"
  91. client := &http.Client{}
  92. marshal, _ := json.Marshal(reqPostData)
  93. req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
  94. if err != nil {
  95. return nil, err
  96. }
  97. res, err := client.Do(req)
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer res.Body.Close()
  102. body, err := ioutil.ReadAll(res.Body)
  103. if err != nil {
  104. return nil, err
  105. }
  106. //fmt.Printf("body = %v", string(body))
  107. result := ForCaptureItsRes{}
  108. err = json.Unmarshal(body, &result)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if result.Code != 0 {
  113. panic(result.Msg)
  114. }
  115. //fmt.Printf("result.Data = %v", result.Data)
  116. return result.Data, nil
  117. }
  118. // SyncOverSpeedRecordData 同步超速车牌数据结构
  119. type SyncOverSpeedRecordData struct {
  120. ID int `json:"id"`
  121. Code string `json:"code"`
  122. Time string `json:"time"`
  123. Vtype int `json:"vtype"`
  124. Plate string `json:"plate"`
  125. Speed int `json:"speed"`
  126. Flag int `json:"flag"`
  127. }
  128. //同步超速车牌号返回数据
  129. type syncOverSpeedRecordDataRes struct {
  130. Code int `json:"code"`
  131. Msg string `json:"msg"`
  132. Data []SyncOverSpeedRecordData `json:"data"`
  133. }
  134. func (r *ForCaptureIts) SyncOverSpeedRecord(maxId int64, maxUpDateTime string, start, end string) ([]SyncOverSpeedRecordData, error) {
  135. maxId += 1
  136. if maxUpDateTime == "" {
  137. maxUpDateTime = time.Now().AddDate(-1, 0, 0).Format("2006-01-02 15:04:05")
  138. }
  139. cfg := config.Instance()
  140. api := cfg.Foreign.IotEdgeUrl + "/data/v1/its/overspeed/sync"
  141. 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")))
  142. if start != "" {
  143. parse, err := time.Parse("2006-01-02", start)
  144. if err != nil {
  145. parse = time.Now().AddDate(-1, 0, 0)
  146. }
  147. startStr := parse.Format("2006-01-02 15:04:05")
  148. 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")))
  149. }
  150. //fmt.Printf("url = %v", url)
  151. method := "GET"
  152. client := &http.Client{}
  153. req, err := http.NewRequest(method, url, nil)
  154. if err != nil {
  155. return nil, err
  156. }
  157. res, err := client.Do(req)
  158. if err != nil {
  159. return nil, err
  160. }
  161. defer res.Body.Close()
  162. body, err := ioutil.ReadAll(res.Body)
  163. if err != nil {
  164. return nil, err
  165. }
  166. //fmt.Printf("body = %v", string(body))
  167. result := syncOverSpeedRecordDataRes{}
  168. err = json.Unmarshal(body, &result)
  169. if err != nil {
  170. return nil, err
  171. }
  172. if result.Code != 0 {
  173. panic(result.Msg)
  174. }
  175. //fmt.Printf("result.Data = %v", result.Data)
  176. return result.Data, nil
  177. }