package business import ( "encoding/json" "fmt" "os" ) // SensorConfig 传感器配置 type SensorConfig struct { HostUUID string `json:"host_uuid"` DeviceUUID string `json:"device_uuid"` TaskID string `json:"task_id"` Address string `json:"address"` Type int `json:"type"` // 改为int类型 Facility string `json:"facility"` SlaveID int `json:"slave_id"` Name string `json:"name"` } // GetSensorType 获取传感器类型枚举 func (sc *SensorConfig) GetSensorType() SensorType { return SensorType(sc.Type) } // ParseServiceConfig 解析service.conf配置文件 func ParseServiceConfig(filePath string) ([]SensorConfig, error) { data, err := os.ReadFile(filePath) if err != nil { return nil, fmt.Errorf("Failed to read configuration file: %v", err) } var configs []SensorConfig if err := json.Unmarshal(data, &configs); err != nil { return nil, fmt.Errorf("Failed to parse JSON: %v", err) } return configs, nil } // ValidateConfig 验证配置有效性 func ValidateConfig(configs []SensorConfig) error { for i, config := range configs { if config.DeviceUUID == "" { return fmt.Errorf("Configuration %d: device_uuid is empty", i+1) } if config.TaskID == "" { return fmt.Errorf("Configuration %d: task_id is empty", i+1) } if config.Type < 1 || config.Type > 3 { return fmt.Errorf("Configuration %d: invalid type value: %d", i+1, config.Type) } } return nil } // GetSensorTypeName 获取传感器类型名称 func GetSensorTypeName(sensorType SensorType) string { switch sensorType { case SensorTypeTemperatureHumidity: return "温湿度传感器" case SensorTypeWaterLeak: return "水浸传感器" case SensorTypeSmoke: return "烟雾传感器" default: return "未知传感器" } }