package devices import ( "strings" "git.zeroonesoft.cn/golib/xiaomihome/xiaomi" ) // Create creates the appropriate device control interface for the given device. // It uses the device's URN (SPEC type) to determine which control interface to return. // // Returns: // - Switch, Light, AirConditioner, Fan, Cover, Humidifier, Vacuum, // WaterHeater, or Thermostat depending on the device type // - nil, nil if the device type is not recognized (no error, just unsupported) // - an error if there was a problem creating the control // // Usage: // // info, _ := client.GetDevice(ctx, "some-did") // ctrl, err := devices.Create(client, info) // switch d := ctrl.(type) { // case devices.Switch: // d.TurnOn(ctx) // case devices.Light: // d.SetBrightness(ctx, 80) // } func Create(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) { return createImpl(client, info) } // NewDevice creates the appropriate device control interface, returning the // common Device interface. Use type switch to access type-specific methods: // // dev, err := devices.NewDevice(client, info) // switch d := dev.(type) { // case devices.Switch: // d.TurnOn(ctx) // case devices.Light: // d.SetBrightness(ctx, 80) // } func NewDevice(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Device, error) { ctrl, err := createImpl(client, info) if err != nil { return nil, err } if ctrl == nil { return nil, nil } return ctrl.(Device), nil } // createImpl is the internal implementation shared by Create and NewDevice. func createImpl(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) { if info == nil { return nil, xiaomi.ErrDeviceNotFound } urn := info.URN // Determine device type from URN and try constructors in order var ctrl interface{} var err error // URN-based routing // miot-spec-v2: air-conditioner → urn:miot-spec-v2:device:air-conditioner:... // miot-spec-v2: light → urn:miot-spec-v2:device:light:... // etc. switch { case matchURN(urn, "air-conditioner", "airer", "air-condition-outlet", "air-condition-eco", "ac-partner"): ctrl, err = NewAirConditioner(client, info) case matchURN(urn, "light", "lightbulb", "night-light", "ceiling-light"): ctrl, err = NewLight(client, info) case matchURN(urn, "switch", "powerstrip", "toggle-switch", "wireless-switch", "curtain-switch"): ctrl, err = NewSwitch(client, info) case matchURN(urn, "plug", "outlet"): ctrl, err = NewPlug(client, info) case matchURN(urn, "fan", "ceiling-fan", "ventilator", "air-fresh", "air-purifier"): ctrl, err = NewFan(client, info) case matchURN(urn, "curtain", "airer-rack", "airer"): ctrl, err = NewCover(client, info) case matchURN(urn, "humidifier", "dehumidifier", "humidifier-dehumidifier"): ctrl, err = NewHumidifier(client, info) case matchURN(urn, "vacuum", "sweeper", "robot-cleaner"): ctrl, err = NewVacuum(client, info) case matchURN(urn, "water-heater", "water-cooler", "kettle"): ctrl, err = NewWaterHeater(client, info) case matchURN(urn, "heater", "electric-blanket", "thermostat"): ctrl, err = NewAirConditioner(client, info) case matchURN(urn, "sensor-occupy", "sensor_occupy", "occupancy-sensor", "occupancy_sensor", "motion-sensor", "motion_sensor", "magnet"): ctrl, err = NewOccupancySensor(client, info) case matchURN(urn, "sensor-temp", "sensor_temp", "sensor-humidity", "sensor_humidity", "sensor-ht", "sensor_ht", "sensor-smoke", "sensor-gasm", "illumination"): ctrl, err = NewTempHumiditySensor(client, info) case matchURN(urn, "speaker", "bluetooth-speaker", "alarm-clock", "media-player", "playback", "music"): ctrl, err = NewSpeaker(client, info) case matchURN(urn, "gateway"): ctrl, err = NewGateway(client, info) default: // Not a supported device type — not an error, just returns nil return nil, nil } if err != nil { return nil, err } return ctrl, nil } // matchURN 匹配 URN 的 device 类型段。 // 标准格式 urn:miot-spec-v2:device:{TYPE}:... 做精确段匹配(避免 "tofan" 撞 "fan" 这类子串误判); // 非标准 URN 回退到边界子串匹配。 func matchURN(urn string, types ...string) bool { parts := strings.Split(urn, ":") if len(parts) >= 4 && parts[2] == "device" { devType := parts[3] for _, t := range types { if devType == t { return true } } return false } // 非标准 URN:按边界匹配,前后必须是分隔符,避免误撞 for _, t := range types { if boundaryContains(urn, t) { return true } } return false } // boundaryContains 检查 sub 是否在 s 中出现且前后是分隔符或边界。 func boundaryContains(s, sub string) bool { for i := 0; i+len(sub) <= len(s); i++ { if s[i:i+len(sub)] != sub { continue } beforeOK := i == 0 if !beforeOK { beforeOK = isBoundary(s[i-1]) } afterOK := i+len(sub) == len(s) if !afterOK { afterOK = isBoundary(s[i+len(sub)]) } if beforeOK && afterOK { return true } } return false } func isBoundary(c byte) bool { return c == ':' || c == '-' || c == '.' || c == '_' || c == '/' }