aepCallbackRecordService.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package service
  2. import (
  3. "errors"
  4. device "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/operation/dao"
  6. "iot_manager_service/app/operation/model"
  7. "iot_manager_service/util/common"
  8. "time"
  9. )
  10. var AepCallbackRecordService = new(aepCallbackRecordService)
  11. type aepCallbackRecordService struct{}
  12. func (s *aepCallbackRecordService) Save(data model.AepCallbackPayload, timestamp int64) error {
  13. datatime := time.Unix(timestamp/1000, 0)
  14. imei := data.IMEI
  15. deviceId, err := device.ManholeCover{}.FindDeviceIdByImei(imei)
  16. if err != nil {
  17. return errors.New("没有添加这个imei的设备" + imei)
  18. }
  19. record := dao.ManholeCoverDao{
  20. CSQ: data.CSQ,
  21. IMEI: data.IMEI,
  22. ICCID: data.ICCID,
  23. Temperature: data.Temperature,
  24. LeanAngle: data.LeanAngle,
  25. BatteryLevel: data.BatteryLevel,
  26. }
  27. record.DeviceId = deviceId
  28. record.PostTime = common.Time(datatime)
  29. record.CreateDate = datatime.Format("2006-01-02")
  30. manholeCover := device.ManholeCover{
  31. ID: deviceId,
  32. Temperature: data.Temperature,
  33. LeanAngle: data.LeanAngle,
  34. BatteryLevel: data.BatteryLevel,
  35. Csq: data.CSQ,
  36. LastPostTime: common.Time(datatime),
  37. }
  38. err = manholeCover.Update()
  39. if err != nil {
  40. return errors.New(imei + "更新井盖的参数失败" + err.Error())
  41. }
  42. return record.Save()
  43. }
  44. func (s *aepCallbackRecordService) GetByDeviceId(deviceId int) (dao.ManholeCoverDao, error) {
  45. var record dao.ManholeCoverDao
  46. record.DeviceId = deviceId
  47. err := record.Get()
  48. if err != nil {
  49. return dao.ManholeCoverDao{}, err
  50. }
  51. return record, nil
  52. }
  53. func (s *aepCallbackRecordService) GetHistoryList(deviceId int, queryStartDate, queryEndDate, queryType string) ([]model.ManholeHistoryList, error) {
  54. var record dao.ManholeCoverDao
  55. record.DeviceId = deviceId
  56. records, err := record.GetList(queryStartDate, queryEndDate)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var list []model.ManholeHistoryList
  61. for _, coverDao := range records {
  62. var value interface{}
  63. if queryType == "temp" {
  64. value = coverDao.Temperature
  65. } else if queryType == "battery" {
  66. value = coverDao.BatteryLevel
  67. } else if queryType == "csq" {
  68. value = coverDao.CSQ
  69. } else if queryType == "lean_angle" {
  70. value = coverDao.LeanAngle
  71. }
  72. list = append(list, model.ManholeHistoryList{
  73. Name: coverDao.CreateDate,
  74. Value: value,
  75. })
  76. }
  77. return list, nil
  78. }