forLightRateService.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package edge_service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/mitchellh/mapstructure"
  6. "io/ioutil"
  7. "iot_manager_service/config"
  8. "net/http"
  9. "strings"
  10. )
  11. // ForLightRate 获取设备能耗数据
  12. type ForLightRate struct{}
  13. // ForLightRateReq 请求参数
  14. type ForLightRateReq struct {
  15. Tenant string `json:"tenant"` //租户号
  16. Start string `json:"start"`
  17. End string `json:"end"`
  18. Flag int `json:"flag"` //0日 1月 2季 3年
  19. }
  20. // ForLightRateRes 返回数据
  21. type ForLightRateRes struct {
  22. Code int `json:"code"`
  23. Msg string `json:"msg"`
  24. Data map[string]interface{} `json:"data"`
  25. }
  26. type ForLightRateData struct {
  27. Date string `json:"date"`
  28. Month string `json:"month"`
  29. Total int `json:"total"`
  30. Number int `json:"number"`
  31. Rate float64 `json:"rate"`
  32. }
  33. func (r *ForLightRate) GetLightRate(reqPostData ForLightRateReq) ([]ForLightRateData, error) {
  34. if reqPostData.Tenant == "100000" {
  35. reqPostData.Tenant = "000000"
  36. }
  37. cfg := config.Instance()
  38. api := cfg.Foreign.IotEdgeUrl + "/data/v1/lightingrate"
  39. method := "POST"
  40. client := &http.Client{}
  41. marshal, _ := json.Marshal(reqPostData)
  42. req, err := http.NewRequest(method, api, strings.NewReader(string(marshal)))
  43. if err != nil {
  44. return nil, err
  45. }
  46. res, err := client.Do(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. defer res.Body.Close()
  51. body, err := ioutil.ReadAll(res.Body)
  52. if err != nil {
  53. return nil, err
  54. }
  55. //fmt.Printf("body = %v", string(body))
  56. result := ForLightRateRes{}
  57. err = json.Unmarshal(body, &result)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if result.Code != 0 {
  62. panic(result.Msg)
  63. }
  64. data := result.Data
  65. var list []ForLightRateData
  66. err = mapstructure.Decode(data["data"], &list)
  67. if err != nil {
  68. return nil, err
  69. }
  70. println(data["data"])
  71. fmt.Printf("list = %v \n", list)
  72. return list, nil
  73. }