@ -55,6 +55,7 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
absences := make ( map [ int ] time . Time )
absences := make ( map [ int ] time . Time )
lastPress := make ( map [ string ] time . Time )
lastPress := make ( map [ string ] time . Time )
lastUpdate := time . Now ( )
needFull := false
needFull := false
for {
for {
@ -62,6 +63,10 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
case <- ctx . Done ( ) :
case <- ctx . Done ( ) :
return ctx . Err ( )
return ctx . Err ( )
case <- lightRefreshTimer . C :
case <- lightRefreshTimer . C :
if time . Since ( lastUpdate ) < time . Second * 10 {
continue
}
if needFull {
if needFull {
err := b . RefreshAll ( ctx )
err := b . RefreshAll ( ctx )
if err != nil {
if err != nil {
@ -79,9 +84,16 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
return err
return err
}
}
if updated > 0 {
if updated > 0 {
// Force this to cool for 30 seconds unless a manual update occurs.
if updated > 10 {
lastUpdate = time . Now ( ) . Add ( time . Second * 15 )
}
log . Println ( fmt . Sprintf ( "[Bridge %d]" , b . externalID ) , "Updated" , updated , "hue services (regular check)" )
log . Println ( fmt . Sprintf ( "[Bridge %d]" , b . externalID ) , "Updated" , updated , "hue services (regular check)" )
}
}
case <- b . needsUpdate :
case <- b . needsUpdate :
lastUpdate = time . Now ( )
updated , err := b . MakeCongruent ( ctx )
updated , err := b . MakeCongruent ( ctx )
if err != nil {
if err != nil {
return err
return err
@ -125,9 +137,11 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
continue
continue
}
}
b . mu . Lock ( )
if b . resources [ patch . Owner . ID ] == nil {
if b . resources [ patch . Owner . ID ] == nil {
needFull = true
needFull = true
}
}
b . mu . Unlock ( )
device , deviceOK := b . devices [ patch . Owner . ID ]
device , deviceOK := b . devices [ patch . Owner . ID ]
if ! deviceOK || device . ID == 0 {
if ! deviceOK || device . ID == 0 {
@ -141,7 +155,7 @@ func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
} else if patch . Button . LastEvent == "long_release" {
} else if patch . Button . LastEvent == "long_release" {
valid = false
valid = false
} else {
} else {
valid = lastPress [ patch . ID ] . Unix ( ) != data . CreationTime . Unix ( )
valid = data . CreationTime . Sub ( lastPress [ patch . ID ] ) >= time . Second * 2
}
}
if valid {
if valid {