operate.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package modbus
  2. import (
  3. "server/logger"
  4. "server/model"
  5. "server/utils"
  6. "time"
  7. )
  8. // GetSunPowerInfo 获取太阳能信息
  9. func GetSunPowerInfo() error {
  10. for id, conn := range model.ConnectionMap {
  11. _, dev, err := utils.GetDataByDeviceId(id)
  12. if dev.IsSun && dev.State == 1 { //打开 并且在线
  13. err = utils.WriteDevice(SolarEnergyData(), conn)
  14. if err != nil {
  15. return err
  16. }
  17. }
  18. }
  19. return nil
  20. }
  21. // GetDeviceInfo 获取设备信息
  22. func GetDeviceInfo() {
  23. for id, conn := range model.ConnectionMap {
  24. _, dev, _ := utils.GetDataByDeviceId(id)
  25. utils.WriteDevice(ReadDeviceInfo(dev.LoopNumber), conn)
  26. }
  27. }
  28. // DealWithOffline 处理离线
  29. func DealWithOffline() {
  30. regions, err := utils.LoadData()
  31. if err != nil {
  32. logger.Get().Errorln("离线" + err.Error())
  33. return
  34. }
  35. for i, region := range regions {
  36. for i2, device := range region.Devices {
  37. if time.Now().After(device.OnlineTime.Add(8*time.Minute)) && regions[i].Devices[i2].State != 0 {
  38. regions[i].Devices[i2].State = 0
  39. }
  40. }
  41. }
  42. if len(regions) == 0 {
  43. return
  44. }
  45. err = utils.SaveData("static/data.json", regions)
  46. logger.Get().Errorln("离线保存" + err.Error())
  47. }