operate.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package modbus
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "net"
  6. "server/dao"
  7. "server/global"
  8. "server/model"
  9. "server/utils"
  10. "time"
  11. )
  12. // GetSunPowerInfo 获取太阳能信息
  13. func GetSunPowerInfo() error {
  14. model.ConnectionMap1.Range(func(key, value interface{}) bool {
  15. addr := key.(string) // 假设键是 string 类型
  16. conn := value.(net.Conn) // 假设值是 net.Conn 类型
  17. dev, err := dao.QueryDeviceByIp(addr)
  18. if err != nil {
  19. global.GVA_LOG.Error("<UNK> <UNK>: -- addr: " + addr + " err: " + err.Error())
  20. }
  21. if dev.IsSun && dev.State == 1 { //打开 并且在线
  22. err := utils.WriteDevice(SolarEnergyData(), conn)
  23. if err != nil {
  24. global.GVA_LOG.Error(fmt.Sprintf("获取太阳能信息 写命令错误: %s -- conn: %v", err, conn.RemoteAddr().String()))
  25. }
  26. }
  27. // 返回 true 继续遍历,返回 false 提前终止遍历
  28. return true
  29. })
  30. return nil
  31. }
  32. // GetDeviceInfo 获取设备信息
  33. func GetDeviceInfo() {
  34. model.ConnectionMap1.Range(func(key, value interface{}) bool {
  35. addr := key.(string) // 假设键是 string 类型
  36. conn := value.(net.Conn) // 假设值是 net.Conn 类型
  37. dev, err := dao.QueryDeviceByIp(addr)
  38. err = utils.WriteDevice(ReadDeviceInfo(dev.LoopNumber), conn)
  39. if err != nil {
  40. global.GVA_LOG.Error(fmt.Sprintf("获取设备信息 写命令错误: %s -- conn: %v", err, conn.RemoteAddr().String()))
  41. }
  42. time.Sleep(1 * time.Second)
  43. // 返回 true 继续遍历,返回 false 提前终止遍历
  44. return true
  45. })
  46. }
  47. // DealWithOffline 处理离线
  48. func DealWithOffline() {
  49. // 兜底:数据库未初始化直接退出
  50. if global.GVA_DB == nil {
  51. global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
  52. return
  53. }
  54. devices, err := dao.QueryAllDevices()
  55. if err != nil {
  56. global.GVA_LOG.Error("查询所有Device失败", zap.Error(err))
  57. return
  58. }
  59. now := time.Now()
  60. for _, device := range devices {
  61. // 关键判断:OnlineTime不是零时间 并且在线状态=1
  62. if device.OnlineTime.IsZero() {
  63. continue
  64. }
  65. if now.After(device.OnlineTime.Add(5*time.Minute)) && device.State == 1 {
  66. txErr := global.GVA_DB.Model(&dao.Device{}).
  67. Where("id = ?", device.ID).
  68. Update("state", 0).Error
  69. if txErr != nil {
  70. global.GVA_LOG.Error("更新Device离线失败",
  71. zap.Uint("id", device.ID),
  72. zap.Error(txErr))
  73. }
  74. }
  75. }
  76. }
  77. // DealWithOfflineGateway 网关离线
  78. func DealWithOfflineGateway() {
  79. if global.GVA_DB == nil {
  80. global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
  81. return
  82. }
  83. devices, err := dao.QueryAllGateways()
  84. if err != nil {
  85. global.GVA_LOG.Error("查询所有网关失败", zap.Error(err))
  86. return
  87. }
  88. now := time.Now()
  89. for _, device := range devices {
  90. if device.OnlineTime.IsZero() {
  91. continue
  92. }
  93. if now.After(device.OnlineTime.Add(11*time.Minute)) && device.Online == 1 {
  94. txErr := global.GVA_DB.Model(&dao.Gateway{}).
  95. Where("id = ?", device.ID).
  96. Update("online", 0).Error
  97. if txErr != nil {
  98. global.GVA_LOG.Error("更新网关离线失败",
  99. zap.Uint("id", device.ID),
  100. zap.Error(txErr))
  101. }
  102. }
  103. }
  104. }
  105. // DealWithOfflineBluetooth 灯具离线(原DealWithOfflineBluetooths)
  106. func DealWithOfflineBluetooths() {
  107. if global.GVA_DB == nil {
  108. global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
  109. return
  110. }
  111. devices, err := dao.QueryAllBluetooths()
  112. if err != nil {
  113. global.GVA_LOG.Error("查询所有蓝牙灯具失败", zap.Error(err))
  114. return
  115. }
  116. now := time.Now()
  117. for _, device := range devices {
  118. // 从未上报心跳,跳过不处理
  119. if device.OnlineTime.IsZero() {
  120. continue
  121. }
  122. if now.After(device.OnlineTime.Add(11*time.Minute)) && device.Online == 1 {
  123. txErr := global.GVA_DB.Model(&dao.Bluetooth{}).
  124. Where("id = ?", device.ID).
  125. Update("online", 0).Error
  126. if txErr != nil {
  127. global.GVA_LOG.Error("更新灯具离线失败",
  128. zap.Uint("id", device.ID),
  129. zap.Error(txErr))
  130. }
  131. }
  132. }
  133. }