package utils import ( "encoding/json" "fmt" "net" "os" "server/dao" "time" ) const connDataFilePath = "static/devicePath.json" const dataFilePath = "static/data.json" // LoadData 从文件读取数据 func LoadData() (regions []dao.Region, err error) { data, err := os.ReadFile(dataFilePath) if err != nil { if os.IsNotExist(err) { return nil, nil } return nil, err } err = json.Unmarshal(data, ®ions) if err != nil { return nil, err } return regions, nil } func GetOnlineDevices() (devices []dao.Device, err error) { regions, err := LoadData() if err != nil { return nil, err } for _, region := range regions { for _, device := range region.Devices { //if device.State == 0 { // continue //} devices = append(devices, device) } } return devices, err } func GetAllDevices() (devices []dao.Device, err error) { regions, err := LoadData() if err != nil { return nil, err } for _, region := range regions { for _, device := range region.Devices { devices = append(devices, device) } } return devices, err } // GetDataByDeviceId 按设备id获取信息 地区+设备 func GetDataByDeviceId(deviceId string) (reg dao.Region, dev dao.Device, err error) { regions, err := LoadData() if err != nil { return reg, dev, err } for _, region := range regions { for _, device := range region.Devices { if device.Sn == deviceId { return region, device, nil } } } return reg, dev, fmt.Errorf("设备%s未找到", deviceId) } func SaveRegionOnData(data dao.Region) ([]dao.Region, error) { regions, err := LoadData() if err != nil { return regions, err } for i, region := range regions { if region.Name == data.Name { regions[i] = data } } return regions, nil } // SaveData 保存数据到文件 func SaveData(path string, parameter interface{}) error { data, err := json.MarshalIndent(parameter, "", " ") if err != nil { return err } return os.WriteFile(path, data, 0644) } // CalculateCircuitSum 计算给定回路数量的总和 func CalculateCircuitSum(circuits int) (int, error) { if circuits <= 0 { return 0, fmt.Errorf("回路数量必须大于0") } // 计算结果 result := (1 << circuits) - 1 return result, nil } // WriteDevice 纯写 func WriteDevice(frame []byte, conn net.Conn) error { _, err := conn.Write(frame) if err != nil { fmt.Println("Error writing to connection:", err) return err } return nil } // ReadDevice 纯读 func ReadDevice(conn net.Conn) (data []byte) { buffer := make([]byte, 1024) n, err := conn.Read(buffer) if err != nil { fmt.Println("Error reading from connection:", err) return } fmt.Printf("Received %d bytes: %X\n", n, buffer[6:n]) return buffer } func WriteAndReadDevice(frame []byte, conn net.Conn, former, after int) (data []byte, err error) { // 发送 Modbus RTU 帧 n, err := conn.Write(frame) if err != nil { fmt.Println("Error writing to connection:", err) return } // 等待一段时间以接收响应 time.Sleep(1000 * time.Millisecond) // 读取响应 buffer := make([]byte, 1024) n, err = conn.Read(buffer) if err != nil { fmt.Println("Error reading from connection:", err) return } // 检查读取的字节数是否足够 if n < former+after { err = fmt.Errorf("not enough bytes read to satisfy the slice range") return } // 返回子切片 return buffer[former : n-after], err }