package hue import ( "bytes" "context" "encoding/json" "fmt" "git.aiterp.net/lucifer/new-server/models" "io" "log" "net" "net/http" "strconv" "strings" "sync" "time" ) type Bridge struct { mu sync.Mutex host string token string externalID int lightStates []*hueLightState sensorStates []*hueSensorState syncingPublish uint32 } func (b *Bridge) Refresh(ctx context.Context) error { lightMap, err := b.getLights(ctx) if err != nil { return err } b.mu.Lock() for index, light := range lightMap { var state *hueLightState for _, existingState := range b.lightStates { if existingState.index == index { state = existingState } } if state == nil { state = &hueLightState{ index: index, uniqueID: light.Uniqueid, externalID: -1, info: light, } b.lightStates = append(b.lightStates, state) } else { if light.Uniqueid != state.uniqueID { state.uniqueID = light.Uniqueid state.externalID = -1 } } state.CheckStaleness(light.State) } b.mu.Unlock() sensorMap, err := b.getSensors(ctx) if err != nil { return err } b.mu.Lock() for index, sensor := range sensorMap { var state *hueSensorState for _, existingState := range b.sensorStates { if existingState.index == index { state = existingState } } if state == nil { state = &hueSensorState{ index: index, uniqueID: sensor.UniqueID, externalID: -1, presenceCooldown: -2, } b.sensorStates = append(b.sensorStates, state) } else { if sensor.UniqueID != state.uniqueID { state.uniqueID = sensor.UniqueID state.externalID = -1 } } } b.mu.Unlock() return nil } func (b *Bridge) SyncStale(ctx context.Context) error { indices := make([]int, 0, 4) arrayIndices := make([]int, 0, 4) inputs := make([]LightStateInput, 0, 4) b.mu.Lock() for i, state := range b.lightStates { if !state.stale { continue } indices = append(indices, state.index) arrayIndices = append(arrayIndices, i) inputs = append(inputs, state.input) } b.mu.Unlock() if len(inputs) == 0 { return nil } groups := make([]*syncGroup, 0, 4) for i := range inputs { input := inputs[i] found := false for _, group := range groups { if group.State.Equal(input) { group.Indexes = append(group.Indexes, indices[i]) group.ArrayIndexes = append(group.ArrayIndexes, arrayIndices[i]) found = true } } if !found { groups = append(groups, &syncGroup{ GroupIndex: -1, State: input, Indexes: []int{indices[i]}, ArrayIndexes: []int{arrayIndices[i]}, }) } } groupMap, err := b.getGroups(ctx) if err != nil { return err } for id, data := range groupMap { for _, group := range groups { if group.Matches(&data) { group.GroupIndex = id break } } } for _, group := range groups { if group.GroupIndex == -1 { data := GroupData{ Name: "lucifer_auto_group", Lights: []string{}, } for _, idx := range group.Indexes { data.Lights = append(data.Lights, strconv.Itoa(idx)) } id, err := b.postGroup(ctx, data) if err != nil { return err } group.GroupIndex = id } } log.Println("Updating", len(inputs), "lights on Hue bridge", b.externalID, "in", len(groups), "groups") for _, group := range groups { err := b.putGroupLightState(ctx, group.GroupIndex, group.State) if err != nil { return err } b.mu.Lock() for _, arrayIndex := range group.ArrayIndexes { b.lightStates[arrayIndex].stale = false } b.mu.Unlock() if groupMap[group.GroupIndex].Name == "lucifer_auto_group" { err := b.deleteGroup(ctx, group.GroupIndex) if err != nil { log.Println("Could not delete temporary group", err) } } } return nil } func (b *Bridge) SyncSensors(ctx context.Context) ([]models.Event, error) { sensorMap, err := b.getSensors(ctx) if err != nil { return nil, err } var events []models.Event b.mu.Lock() for idx, sensorData := range sensorMap { for _, state := range b.sensorStates { if idx == state.index { event := state.Update(sensorData) if event != nil { events = append(events, *event) } break } } } b.mu.Unlock() return events, nil } func (b *Bridge) StartDiscovery(ctx context.Context, model string) error { return b.post(ctx, model, nil, nil) } func (b *Bridge) putLightState(ctx context.Context, index int, input LightStateInput) error { return b.put(ctx, fmt.Sprintf("lights/%d/state", index), input, nil) } func (b *Bridge) putGroupLightState(ctx context.Context, index int, input LightStateInput) error { return b.put(ctx, fmt.Sprintf("groups/%d/action", index), input, nil) } func (b *Bridge) getToken(ctx context.Context) (string, error) { result := make([]CreateUserResponse, 0, 1) err := b.post(ctx, "", CreateUserInput{DeviceType: "git.aiterp.net/lucifer"}, &result) if err != nil { return "", err } if len(result) == 0 || result[0].Error != nil { return "", errLinkButtonNotPressed } if result[0].Success == nil { return "", models.ErrUnexpectedResponse } return result[0].Success.Username, nil } func (b *Bridge) getLights(ctx context.Context) (map[int]LightData, error) { result := make(map[int]LightData, 16) err := b.get(ctx, "lights", &result) if err != nil { return nil, err } return result, nil } func (b *Bridge) getSensors(ctx context.Context) (map[int]SensorData, error) { result := make(map[int]SensorData, 16) err := b.get(ctx, "sensors", &result) if err != nil { return nil, err } return result, nil } func (b *Bridge) getGroups(ctx context.Context) (map[int]GroupData, error) { result := make(map[int]GroupData, 16) err := b.get(ctx, "groups", &result) if err != nil { return nil, err } return result, nil } func (b *Bridge) postGroup(ctx context.Context, input GroupData) (int, error) { var res []struct { Success struct { ID string `json:"id"` } `json:"success"` } err := b.post(ctx, "groups", input, &res) id, _ := strconv.Atoi(res[0].Success.ID) return id, err } func (b *Bridge) deleteGroup(ctx context.Context, index int) error { return b.delete(ctx, "groups/"+strconv.Itoa(index), nil) } func (b *Bridge) get(ctx context.Context, resource string, target interface{}) error { if b.token != "" { resource = b.token + "/" + resource } req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/%s", b.host, resource), nil) if err != nil { return err } res, err := httpClient.Do(req.WithContext(ctx)) if err != nil { return err } defer res.Body.Close() return json.NewDecoder(res.Body).Decode(target) } func (b *Bridge) delete(ctx context.Context, resource string, target interface{}) error { if b.token != "" { resource = b.token + "/" + resource } req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/api/%s", b.host, resource), nil) if err != nil { return err } res, err := httpClient.Do(req.WithContext(ctx)) if err != nil { return err } defer res.Body.Close() if target == nil { return nil } return json.NewDecoder(res.Body).Decode(target) } func (b *Bridge) post(ctx context.Context, resource string, body interface{}, target interface{}) error { rb, err := reqBody(body) if err != nil { return err } if b.token != "" { resource = b.token + "/" + resource } req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/%s", b.host, resource), rb) if err != nil { return err } res, err := httpClient.Do(req.WithContext(ctx)) if err != nil { return err } defer res.Body.Close() if target == nil { return nil } return json.NewDecoder(res.Body).Decode(target) } func (b *Bridge) put(ctx context.Context, resource string, body interface{}, target interface{}) error { rb, err := reqBody(body) if err != nil { return err } if b.token != "" { resource = b.token + "/" + resource } req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s/api/%s", b.host, resource), rb) if err != nil { return err } res, err := httpClient.Do(req.WithContext(ctx)) if err != nil { return err } defer res.Body.Close() if target == nil { return nil } return json.NewDecoder(res.Body).Decode(target) } func reqBody(body interface{}) (io.Reader, error) { if body == nil { return nil, nil } switch v := body.(type) { case []byte: return bytes.NewReader(v), nil case string: return strings.NewReader(v), nil case io.Reader: return v, nil default: jsonData, err := json.Marshal(v) if err != nil { return nil, err } return bytes.NewReader(jsonData), nil } } var httpClient = &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).DialContext, MaxIdleConns: 256, MaxIdleConnsPerHost: 16, IdleConnTimeout: 10 * time.Minute, }, Timeout: time.Minute, }