Browse Source

Bridge events

pull/1/head
Stian Fredrik Aune 4 years ago
parent
commit
a2e719def9
  1. 50
      app/services/bridges.go
  2. 32
      app/services/events.go
  3. 4
      internal/drivers/nanoleaf/bridge.go
  4. 16
      models/event.go
  5. 6
      models/shared.go
  6. 4
      webui/src/primitives/Forms.tsx
  7. 8
      webui/src/setupProxy.js

50
app/services/bridges.go

@ -0,0 +1,50 @@
package services
import (
"context"
"git.aiterp.net/lucifer/new-server/app/config"
"log"
"time"
)
var cancelMap = make(map[int]context.CancelFunc, 8)
func ConnectToBridges() {
go func() {
for {
err := runConnectToBridges()
if err != nil {
log.Println("Bridge connection error: ", err.Error())
}
time.Sleep(15 * time.Second)
}
}()
}
func runConnectToBridges() error {
bridges, err := config.BridgeRepository().FetchAll(context.Background())
if err != nil {
return err
}
for _, bridge := range bridges {
if cancelMap[bridge.ID] != nil {
continue
}
driver, err := config.DriverProvider().Provide(bridge.Driver)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
err = driver.Run(ctx, bridge, config.EventChannel)
cancelMap[bridge.ID] = cancel
log.Printf("Connected to bridge \"%s\" (%d)", bridge.Name, bridge.ID)
}
return nil
}

32
app/services/events.go

@ -5,6 +5,7 @@ import (
"git.aiterp.net/lucifer/new-server/app/config" "git.aiterp.net/lucifer/new-server/app/config"
"git.aiterp.net/lucifer/new-server/models" "git.aiterp.net/lucifer/new-server/models"
"log" "log"
"strconv"
"time" "time"
) )
@ -40,6 +41,12 @@ func handleEvent(event models.Event) {
event.AddPayload("minute", time.Now().In(loc).Format("04")) event.AddPayload("minute", time.Now().In(loc).Format("04"))
} }
err := handleSpecial(event)
if err != nil {
log.Printf("Special event handler error (%s): %v", event.Name, err)
return
}
if !X { if !X {
log.Println("Unhandled event: " + event.Name) log.Println("Unhandled event: " + event.Name)
return return
@ -64,3 +71,28 @@ func handleEvent(event models.Event) {
} }
} }
func handleSpecial(event models.Event) error {
switch event.Name {
case models.ENBridgeConnected:
bridgeId, _ := strconv.Atoi(event.Payload["bridgeId"])
bridge, err := config.BridgeRepository().Find(ctx, bridgeId)
if err != nil {
return err
}
devices, err := config.DeviceRepository().FetchByReference(ctx, models.RKBridgeID, event.Payload["bridgeId"])
if err != nil {
return err
}
driver, err := config.DriverProvider().Provide(bridge.Driver)
if err != nil {
return err
}
return driver.Publish(ctx, bridge, devices)
default:
return nil
}
}

4
internal/drivers/nanoleaf/bridge.go

@ -195,9 +195,9 @@ func (b *bridge) Run(ctx context.Context, info models.Bridge, ch chan<- models.E
defer conn.Close() defer conn.Close()
// Notify connections and disconnections // Notify connections and disconnections
ch <- models.Event{Name: "BridgeConnected", Payload: map[string]string{"bridgeId": strconv.Itoa(info.ID)}}
ch <- models.BridgeConnectedEvent(info)
defer func() { defer func() {
ch <- models.Event{Name: "BridgeDisconnected", Payload: map[string]string{"bridgeId": strconv.Itoa(info.ID)}}
ch <- models.BridgeDisconnectedEvent(info)
}() }()
// Start touch listener. This one should go down together with this one, though, so it needs a new context. // Start touch listener. This one should go down together with this one, though, so it needs a new context.

16
models/event.go

@ -19,9 +19,21 @@ func (e *Event) HasPayload(key string) bool {
return e.Payload != nil && e.Payload[key] != "" return e.Payload != nil && e.Payload[key] != ""
} }
var (
ENBridgeConnected = "BridgeConnected"
ENBridgeDisconnected = "BridgeDisconnected"
)
func BridgeConnectedEvent(bridge Bridge) Event { func BridgeConnectedEvent(bridge Bridge) Event {
e := Event{Name: "BridgeConnected"}
e.AddPayload("id", strconv.Itoa(bridge.ID))
e := Event{Name: ENBridgeConnected}
e.AddPayload("bridgeId", strconv.Itoa(bridge.ID))
return e
}
func BridgeDisconnectedEvent(bridge Bridge) Event {
e := Event{Name: ENBridgeDisconnected}
e.AddPayload("bridgeId", strconv.Itoa(bridge.ID))
return e return e
} }

6
models/shared.go

@ -3,9 +3,9 @@ package models
type ReferenceKind string type ReferenceKind string
var ( var (
RTDeviceID ReferenceKind = "DeviceID"
RTBridgeID ReferenceKind = "BridgeID"
RTTag ReferenceKind = "Tag"
RKDeviceID ReferenceKind = "DeviceID"
RKBridgeID ReferenceKind = "BridgeID"
RKTag ReferenceKind = "Tag"
) )

4
webui/src/primitives/Forms.tsx

@ -1,4 +1,4 @@
import React, {useLayoutEffect, useState} from 'react';
import React, {useLayoutEffect, useMemo} from 'react';
// @ts-ignore // @ts-ignore
import iro from "@jaames/iro"; import iro from "@jaames/iro";
@ -11,7 +11,7 @@ interface ColorPickerProps {
const randomId = () => Math.floor(Math.random() * 100000); const randomId = () => Math.floor(Math.random() * 100000);
export const HSColorPicker: React.FC<ColorPickerProps> = ({h, s, onChange}) => { export const HSColorPicker: React.FC<ColorPickerProps> = ({h, s, onChange}) => {
const [random] = useState(() => `color-picker-${randomId()}`);
const random = useMemo(() => `color-picker-${randomId()}`, []);
useLayoutEffect(() => { useLayoutEffect(() => {
// @ts-ignore // @ts-ignore

8
webui/src/setupProxy.js

@ -0,0 +1,8 @@
const proxy = require("http-proxy-middleware");
let config = {url: "http://127.0.0.1:9000/"};
module.exports = function(app) {
app.use(proxy('/api/ws', { target: config.url, ws: true }));
app.use(proxy('/api', { target: config.url, ws: false }));
};
Loading…
Cancel
Save