fixed hue updating too often, and change logic for fetchbyreference to support multiple tags, ids or bridges.
This commit is contained in:
@@ -35,8 +35,8 @@ func StartEventHandler() {
|
|||||||
for range time.NewTicker(time.Minute).C {
|
for range time.NewTicker(time.Minute).C {
|
||||||
config.EventChannel <- models.Event{Name: "TimeChanged", Payload: map[string]string{
|
config.EventChannel <- models.Event{Name: "TimeChanged", Payload: map[string]string{
|
||||||
"weekdayName": time.Now().In(loc).Format("Monday"),
|
"weekdayName": time.Now().In(loc).Format("Monday"),
|
||||||
"month": time.Now().In(loc).Format("01"),
|
"month": time.Now().In(loc).Format("01"),
|
||||||
"year": time.Now().In(loc).Format("2006"),
|
"year": time.Now().In(loc).Format("2006"),
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -49,7 +49,7 @@ func handleEvent(event models.Event) (responses []models.Event) {
|
|||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
duration := time.Since(startTime)
|
duration := time.Since(startTime)
|
||||||
if duration > time.Millisecond * 250 {
|
if duration > time.Millisecond*250 {
|
||||||
log.Println(event.Name, "took long to handle:", duration)
|
log.Println(event.Name, "took long to handle:", duration)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -134,7 +134,7 @@ func handleEvent(event models.Event) (responses []models.Event) {
|
|||||||
if action.SetIntensity != nil {
|
if action.SetIntensity != nil {
|
||||||
newState.Intensity = action.SetIntensity
|
newState.Intensity = action.SetIntensity
|
||||||
} else if action.AddIntensity != nil {
|
} else if action.AddIntensity != nil {
|
||||||
newIntensity := math.Max(0, math.Min(1, device.State.Intensity + *action.AddIntensity))
|
newIntensity := math.Max(0, math.Min(1, device.State.Intensity+*action.AddIntensity))
|
||||||
newState.Intensity = &newIntensity
|
newState.Intensity = &newIntensity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,20 +8,21 @@ import (
|
|||||||
"git.aiterp.net/lucifer/new-server/models"
|
"git.aiterp.net/lucifer/new-server/models"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bridge struct {
|
type Bridge struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
host string
|
host string
|
||||||
token string
|
token string
|
||||||
externalID int
|
externalID int
|
||||||
lightStates []*hueLightState
|
lightStates []*hueLightState
|
||||||
sensorStates []*hueSensorState
|
sensorStates []*hueSensorState
|
||||||
|
syncingPublish uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bridge) Refresh(ctx context.Context) error {
|
func (b *Bridge) Refresh(ctx context.Context) error {
|
||||||
@@ -115,10 +116,20 @@ func (b *Bridge) SyncStale(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, input := range inputs {
|
for i, input := range inputs {
|
||||||
|
iCopy := i
|
||||||
index := indices[i]
|
index := indices[i]
|
||||||
inputCopy := input
|
inputCopy := input
|
||||||
|
|
||||||
eg.Go(func() error { return b.putLightState(ctx, index, inputCopy) })
|
eg.Go(func() error {
|
||||||
|
err := b.putLightState(ctx, index, inputCopy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b.lightStates[iCopy].stale = false
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -309,6 +310,9 @@ func (d *Driver) Publish(ctx context.Context, bridge models.Bridge, devices []mo
|
|||||||
}
|
}
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
atomic.StoreUint32(&b.syncingPublish, 1)
|
||||||
|
defer atomic.StoreUint32(&b.syncingPublish, 0)
|
||||||
|
|
||||||
return b.SyncStale(ctx)
|
return b.SyncStale(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +331,10 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-selectedTicker.C:
|
case <-selectedTicker.C:
|
||||||
|
if atomic.LoadUint32(&b.syncingPublish) == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if ticksUntilRefresh <= 0 {
|
if ticksUntilRefresh <= 0 {
|
||||||
err := b.Refresh(ctx)
|
err := b.Refresh(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -56,22 +56,28 @@ func (r *DeviceRepo) Find(ctx context.Context, id int) (*models.Device, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *DeviceRepo) FetchByReference(ctx context.Context, kind models.ReferenceKind, value string) ([]models.Device, error) {
|
func (r *DeviceRepo) FetchByReference(ctx context.Context, kind models.ReferenceKind, value string) ([]models.Device, error) {
|
||||||
var err error
|
q := sq.Select("device.*").From("device")
|
||||||
records := make([]deviceRecord, 0, 8)
|
|
||||||
|
|
||||||
switch kind {
|
switch kind {
|
||||||
case models.RKDeviceID:
|
case models.RKDeviceID:
|
||||||
err = r.DBX.SelectContext(ctx, &records, "SELECT * FROM device WHERE id=?", value)
|
q = q.Where(sq.Eq{"id": strings.Split(value, ",")})
|
||||||
case models.RKBridgeID:
|
case models.RKBridgeID:
|
||||||
err = r.DBX.SelectContext(ctx, &records, "SELECT * FROM device WHERE bridge_id=?", value)
|
q = q.Where(sq.Eq{"bridge_id": strings.Split(value, ",")})
|
||||||
case models.RKTag:
|
case models.RKTag:
|
||||||
err = r.DBX.SelectContext(ctx, &records, "SELECT device.* FROM device JOIN device_tag dt ON device.id = dt.device_id WHERE dt.tag_name=?", value)
|
q = q.Join("device_tag dt ON device.id=dt.device_id").Where(sq.Eq{"dt.tag_name": strings.Split(value, ",")})
|
||||||
case models.RKAll:
|
case models.RKAll:
|
||||||
err = r.DBX.SelectContext(ctx, &records, "SELECT device.* FROM device")
|
|
||||||
default:
|
default:
|
||||||
log.Println("Unknown reference kind used for device fetch:", kind)
|
log.Println("Unknown reference kind used for device fetch:", kind)
|
||||||
return []models.Device{}, nil
|
return []models.Device{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query, args, err := q.ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, dbErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
records := make([]deviceRecord, 0, 8)
|
||||||
|
err = r.DBX.SelectContext(ctx, &records, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, dbErr(err)
|
return nil, dbErr(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user