123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package edge_service
- import (
- "encoding/json"
- "io/ioutil"
- "iot_manager_service/config"
- "net/http"
- "strings"
- )
- // CaptureIts 获取设备能耗数据
- type ForCaptureIts struct{}
- // CaptureItsReq 请求参数
- type ForCaptureItsReq struct {
- Codes []string `json:"codes"` //抓拍单元 capture_sn列表
- Start string `json:"start"`
- End string `json:"end"`
- Flag int `json:"flag"` //0按日 1按月
- }
- // ForCaptureItsRes 返回数据
- type ForCaptureItsRes struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data []ForCaptureItsData `json:"data"`
- }
- type ForCaptureItsData struct {
- Code string `json:"code"` //抓拍单元 capture_sn
- Time string `json:"time"` //时间 2022-05-01 ||2022-05
- Flag int `json:"flag"` //0:日数据,1:月数据,2:小时数据
- Sflag int `json:"sflag"` //车速区间时:
- Total int `json:"total"` //数量
- Province string `json:"province"` //归属地时-用到的省
- City string `json:"city"` //归属地时-用到的城市
- Vtype int `json:"vtype"` //车型统计
- }
- // VehicleTotal 统计日 月 车流量 数据
- func (f ForCaptureIts) VehicleTotal(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
- api := "/data/v1/its/vehicletotal"
- return f.pubPost(reqPostData, api)
- }
- // Province 归属地省统计
- func (f ForCaptureIts) Province(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
- api := "/data/v1/its/province"
- return f.pubPost(reqPostData, api)
- }
- // City 归属地城市统计
- func (f ForCaptureIts) City(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
- api := "/data/v1/its/provincecity"
- return f.pubPost(reqPostData, api)
- }
- // Vehicletypeex 车型统计
- func (f ForCaptureIts) Vehicletypeex(reqPostData ForCaptureItsReq) ([]ForCaptureItsData, error) {
- api := "/data/v1/its/vehicletypeex"
- return f.pubPost(reqPostData, api)
- }
- //公用 post请求
- func (f ForCaptureIts) pubPost(reqPostData ForCaptureItsReq, api string) ([]ForCaptureItsData, error) {
- cfg := config.Instance()
- api = cfg.Foreign.IotEdgeUrl + api
- //fmt.Printf("api = %v \n", api)
- reqPostData.Start = reqPostData.Start + " 00:00:00"
- reqPostData.End = reqPostData.End + " 23:59:59"
- //fmt.Printf("reqPostData = %v \n", reqPostData)
- method := "POST"
- client := &http.Client{}
- marshal, _ := json.Marshal(reqPostData)
- req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
- if err != nil {
- return nil, err
- }
- res, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- return nil, err
- }
- //fmt.Printf("body = %v", string(body))
- result := ForCaptureItsRes{}
- err = json.Unmarshal(body, &result)
- if err != nil {
- return nil, err
- }
- if result.Code != 0 {
- panic(result.Msg)
- }
- //fmt.Printf("result.Data = %v", result.Data)
- return result.Data, nil
- }
|