| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package modbus
- import (
- "fmt"
- "go.uber.org/zap"
- "net"
- "server/dao"
- "server/global"
- "server/model"
- "server/utils"
- "time"
- )
- // GetSunPowerInfo 获取太阳能信息
- func GetSunPowerInfo() error {
- model.ConnectionMap1.Range(func(key, value interface{}) bool {
- addr := key.(string) // 假设键是 string 类型
- conn := value.(net.Conn) // 假设值是 net.Conn 类型
- dev, err := dao.QueryDeviceByIp(addr)
- if err != nil {
- global.GVA_LOG.Error("<UNK> <UNK>: -- addr: " + addr + " err: " + err.Error())
- }
- if dev.IsSun && dev.State == 1 { //打开 并且在线
- err := utils.WriteDevice(SolarEnergyData(), conn)
- if err != nil {
- global.GVA_LOG.Error(fmt.Sprintf("获取太阳能信息 写命令错误: %s -- conn: %v", err, conn.RemoteAddr().String()))
- }
- }
- // 返回 true 继续遍历,返回 false 提前终止遍历
- return true
- })
- return nil
- }
- // GetDeviceInfo 获取设备信息
- func GetDeviceInfo() {
- model.ConnectionMap1.Range(func(key, value interface{}) bool {
- addr := key.(string) // 假设键是 string 类型
- conn := value.(net.Conn) // 假设值是 net.Conn 类型
- dev, err := dao.QueryDeviceByIp(addr)
- err = utils.WriteDevice(ReadDeviceInfo(dev.LoopNumber), conn)
- if err != nil {
- global.GVA_LOG.Error(fmt.Sprintf("获取设备信息 写命令错误: %s -- conn: %v", err, conn.RemoteAddr().String()))
- }
- time.Sleep(1 * time.Second)
- // 返回 true 继续遍历,返回 false 提前终止遍历
- return true
- })
- }
- // DealWithOffline 处理离线
- func DealWithOffline() {
- // 兜底:数据库未初始化直接退出
- if global.GVA_DB == nil {
- global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
- return
- }
- devices, err := dao.QueryAllDevices()
- if err != nil {
- global.GVA_LOG.Error("查询所有Device失败", zap.Error(err))
- return
- }
- now := time.Now()
- for _, device := range devices {
- // 关键判断:OnlineTime不是零时间 并且在线状态=1
- if device.OnlineTime.IsZero() {
- continue
- }
- if now.After(device.OnlineTime.Add(5*time.Minute)) && device.State == 1 {
- txErr := global.GVA_DB.Model(&dao.Device{}).
- Where("id = ?", device.ID).
- Update("state", 0).Error
- if txErr != nil {
- global.GVA_LOG.Error("更新Device离线失败",
- zap.Uint("id", device.ID),
- zap.Error(txErr))
- }
- }
- }
- }
- // DealWithOfflineGateway 网关离线
- func DealWithOfflineGateway() {
- if global.GVA_DB == nil {
- global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
- return
- }
- devices, err := dao.QueryAllGateways()
- if err != nil {
- global.GVA_LOG.Error("查询所有网关失败", zap.Error(err))
- return
- }
- now := time.Now()
- for _, device := range devices {
- if device.OnlineTime.IsZero() {
- continue
- }
- if now.After(device.OnlineTime.Add(11*time.Minute)) && device.Online == 1 {
- txErr := global.GVA_DB.Model(&dao.Gateway{}).
- Where("id = ?", device.ID).
- Update("online", 0).Error
- if txErr != nil {
- global.GVA_LOG.Error("更新网关离线失败",
- zap.Uint("id", device.ID),
- zap.Error(txErr))
- }
- }
- }
- }
- // DealWithOfflineBluetooth 灯具离线(原DealWithOfflineBluetooths)
- func DealWithOfflineBluetooths() {
- if global.GVA_DB == nil {
- global.GVA_LOG.Error("定时离线任务:GVA_DB未初始化,跳过执行")
- return
- }
- devices, err := dao.QueryAllBluetooths()
- if err != nil {
- global.GVA_LOG.Error("查询所有蓝牙灯具失败", zap.Error(err))
- return
- }
- now := time.Now()
- for _, device := range devices {
- // 从未上报心跳,跳过不处理
- if device.OnlineTime.IsZero() {
- continue
- }
- if now.After(device.OnlineTime.Add(11*time.Minute)) && device.Online == 1 {
- txErr := global.GVA_DB.Model(&dao.Bluetooth{}).
- Where("id = ?", device.ID).
- Update("online", 0).Error
- if txErr != nil {
- global.GVA_LOG.Error("更新灯具离线失败",
- zap.Uint("id", device.ID),
- zap.Error(txErr))
- }
- }
- }
- }
|