forCaptureItsService.go 5.4 KB

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