operate.go 728 B

12345678910111213141516171819202122232425262728293031323334
  1. package modbus
  2. import (
  3. "server/model"
  4. "server/utils"
  5. "time"
  6. )
  7. // GetSunPowerInfo 获取太阳能信息
  8. func GetSunPowerInfo() error {
  9. for id, conn := range model.ConnectionMap {
  10. _, dev, err := utils.GetDataByDeviceId(id)
  11. if dev.IsSun && dev.State == 1 { //打开 并且在线
  12. err = utils.WriteDevice(SolarEnergyData(), conn)
  13. if err != nil {
  14. return err
  15. }
  16. }
  17. }
  18. return nil
  19. }
  20. // DealWithOffline 处理离线
  21. func DealWithOffline() {
  22. regions, _ := utils.LoadData()
  23. for i, region := range regions {
  24. for i2, device := range region.Devices {
  25. if time.Now().After(device.OnlineTime.Add(4 * time.Minute)) {
  26. regions[i].Devices[i2].State = 0
  27. }
  28. }
  29. }
  30. utils.SaveData("static/data.json", regions)
  31. }