package mill import ( "fmt" lucifer3 "git.aiterp.net/lucifer3/server" "git.aiterp.net/lucifer3/server/device" "git.aiterp.net/lucifer3/server/events" "strings" ) type Bridge interface { // SetBus to apply a bus that changes will be sent to SetBus(bus *lucifer3.EventBus) // SetState updates device state SetState(id string, state device.State) bool // Start triggers the event emitting part, including device discovery Start() // IsStarted returns true when it's running IsStarted() bool } func MakeBridge(id, apiKey string) (Bridge, bool) { parts := strings.Split(id, ":") if len(parts) < 3 { return nil, false } driver := parts[0] version := parts[1] target := parts[2] if driver != "mill" { return nil, false } id = fmt.Sprintf("%s:%s:%s", driver, version, target) switch version { case "2": return &OnlineBridge{ ID: id, Email: target, Password: apiKey, Gen2: true, Gen3: false, }, true case "23": return &OnlineBridge{ ID: id, Email: target, Password: apiKey, Gen2: true, Gen3: true, }, true case "3": if strings.Contains(target, "@") { return &OnlineBridge{ ID: id, Email: target, Password: apiKey, Gen2: false, Gen3: true, }, true } else { return &WifiBridge{ ID: id, IP: target, }, true } } return nil, false } func deviceFailed(id string, err any) events.DeviceFailed { msg := "(no message)" switch typed := err.(type) { case string: msg = typed case error: msg = typed.Error() } return events.DeviceFailed{ ID: id, Error: msg, } }