package service import ( "fmt" "net" "server/dao" "server/logger" "server/modbus" "server/model" "server/utils" ) func SaveData(data []dao.Region) error { if len(data) == 0 { return fmt.Errorf("数据不能为空") } utils.SaveRegions(data) return nil } func DeviceSave(device dao.Device) error { regions, _ := utils.LoadData() for i, region := range regions { for j, d := range region.Devices { if device.Sn == d.Sn { regions[i].Devices[j] = device } } } err := SaveData(regions) if err != nil { return err } go utils.SaveData("./static/data.json", regions) return nil } func DevicesSave(devices []dao.Device) error { regions, _ := utils.LoadData() for i, rd := range regions[0].Devices { for _, d := range devices { if rd.Sn == d.Sn { regions[0].Devices[i] = d } } } err := SaveData(regions) if err != nil { return err } go utils.SaveData("./static/data.json", regions) // Remove the redundant return err statement. return nil // Return nil to indicate success } func DeviceLoopSwitch(deviceLoop dao.DeviceLoop) error { data := modbus.DeviceLoopSwitch(deviceLoop.ID, deviceLoop.State) _, dev1, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId) if conn1, ok := model.ConnectionMap1.Load(dev1.Ip); ok { // 成功找到连接 netConn := conn1.(net.Conn) _, dev, _ := utils.GetDataByDeviceIP(dev1.Ip) if dev.State == 0 { return fmt.Errorf("设备离线无法操作") } err := utils.WriteDevice(data, netConn) if err != nil { logger.Get().Errorf("设备回路开关 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String()) return err } } else { // 没有找到对应的连接 logger.Get().Printf("设备回路开关Connection for key %s not found", dev1.Ip) } return nil } func DeviceSwitch(req model.DeviceRequest) error { data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State) if conn1, ok := model.ConnectionMap1.Load(req.Device.Ip); ok { // 成功找到连接 netConn := conn1.(net.Conn) modbus.OperationCommand = data err := utils.WriteDevice(data, netConn) if err != nil { logger.Get().Errorf("设备开关 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String()) return err } } else { // 没有找到对应的连接 logger.Get().Printf("设备开关Connection for key %s not found", req.Device.Ip) } return nil } func DeviceBatchSwitch(req model.DevicesRequest) error { for _, device := range req.Devices { if device.State == 0 { continue } data := modbus.DeviceSwitch(device.LoopNumber, req.State) modbus.OperationCommand = data if conn1, ok := model.ConnectionMap1.Load(device.Ip); ok { // 成功找到连接 netConn := conn1.(net.Conn) err := utils.WriteDevice(data, netConn) if err != nil { logger.Get().Errorf("设备批量控制 写命令错误: %s -- conn: %v", err, netConn.RemoteAddr().String()) } } else { // 没有找到对应的连接 logger.Get().Printf("设备批量控制Connection for key %s not found", device.Ip) } } return nil } // GetOnlineDevice 获取在线设备 总共设备 在线率 func GetOnlineDevice() (total, online int) { devices, err := utils.GetOnlineDevices() if err != nil { logger.Get().Errorf("GetOnlineDevices err = %s\n", err.Error()) return } allDevices, err := utils.GetAllDevices() if err != nil { logger.Get().Errorf("GetAllDevices err = %s\n", err.Error()) return } return len(allDevices), len(devices) } func GetSunDevices() (devices []dao.Device, err error) { allDevices, err := utils.GetAllDevices() if err != nil { return nil, err } for _, device := range allDevices { if device.IsSun { devices = append(devices, device) } } return }