123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package edge_service
- import (
- "encoding/json"
- "io/ioutil"
- "iot_manager_service/config"
- "net/http"
- "strings"
- )
- // ForLightEnergy 获取设备能耗数据
- type ForLightEnergy struct{}
- // ForLightEnergyReq 请求参数
- type ForLightEnergyReq struct {
- Codes []string `json:"codes"` //设备sn列表
- Start string `json:"start"`
- End string `json:"end"`
- Flag int `json:"flag"` //1按日 2按月
- }
- // ForLightEnergyRes 返回数据
- type ForLightEnergyRes struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data map[string][]ForLightEnergyData `json:"data"`
- }
- type ForLightEnergyData struct {
- Date string `json:"date"` //日期
- Beginning float64 `json:"beginning"` //开始
- Ending float64 `json:"ending"` //结束
- Difference float64 `json:"difference"` //能耗
- }
- func (r *ForLightEnergy) GetLightEnergy(reqPostData ForLightEnergyReq) (map[string][]ForLightEnergyData, error) {
- cfg := config.Instance()
- api := cfg.Foreign.IotEdgeUrl + "/data/v1/energy"
- 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 := ForLightEnergyRes{}
- 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
- }
|