syncAlarmService.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. )
  10. type RecordAlarmRecord struct{}
  11. type RecordAlarmRecordRes struct {
  12. Code int `json:"code"`
  13. Msg string `json:"msg"`
  14. Data []RecordAlarmRecordData `json:"data"`
  15. }
  16. type RecordAlarmRecordData struct {
  17. ID int `json:"id"`
  18. Code string `json:"code"`
  19. Tstart string `json:"tstart"`
  20. Tend string `json:"tend"`
  21. Content string `json:"content"`
  22. Alarmtype int `json:"alarmtype"`
  23. Level int `json:"level"`
  24. Threshold int `json:"threshold"`
  25. Value int `json:"value"`
  26. Sid int `json:"sid"`
  27. Cid int `json:"cid"`
  28. Cname string `json:"cname"`
  29. Updatedat string `json:"updatedat"`
  30. }
  31. // SyncAlartRecord 同步报警 数据
  32. func (r *RecordAlarmRecord) SyncAlartRecord(maxId int64, maxUpDateTime string) ([]RecordAlarmRecordData, error) {
  33. maxId += 1
  34. cfg := config.Instance()
  35. api := cfg.Foreign.IotEdgeUrl + "/data/v1/alarm/sync"
  36. url := fmt.Sprintf("%v?id=%d&&updatedat=%v", api, maxId, url2.QueryEscape(maxUpDateTime))
  37. method := "GET"
  38. client := &http.Client{}
  39. req, err := http.NewRequest(method, url, nil)
  40. if err != nil {
  41. return nil, err
  42. }
  43. res, err := client.Do(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. defer res.Body.Close()
  48. body, err := ioutil.ReadAll(res.Body)
  49. if err != nil {
  50. return nil, err
  51. }
  52. result := RecordAlarmRecordRes{}
  53. err = json.Unmarshal(body, &result)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if result.Code != 0 {
  58. panic(result.Msg)
  59. }
  60. return result.Data, nil
  61. }