Stian Fredrik Aune
4 years ago
commit
5a56763526
21 changed files with 661 additions and 0 deletions
-
4.gitignore
-
20app/api/events.go
-
46app/api/util.go
-
7app/config/channels.go
-
13app/config/driver.go
-
31app/config/env.go
-
24app/config/repo.go
-
20app/server.go
-
59app/services/events.go
-
5go.mod
-
54go.sum
-
7main.go
-
15models/bridge.go
-
61models/colorvalue.go
-
122models/device.go
-
14models/driver.go
-
8models/errors.go
-
18models/event.go
-
121models/eventhandler.go
-
11models/shared.go
-
1webui
@ -0,0 +1,4 @@ |
|||||
|
./main |
||||
|
.env |
||||
|
.idea/ |
||||
|
*.iml |
@ -0,0 +1,20 @@ |
|||||
|
package api |
||||
|
|
||||
|
import ( |
||||
|
"git.aiterp.net/lucifer/server3/app/config" |
||||
|
"git.aiterp.net/lucifer/server3/models" |
||||
|
"github.com/gin-gonic/gin" |
||||
|
) |
||||
|
|
||||
|
func Events(r gin.IRoutes) { |
||||
|
r.POST("", handler(func(c *gin.Context) (interface{}, error) { |
||||
|
var event models.Event |
||||
|
err := parseBody(c, &event) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
config.EventChannel<-event |
||||
|
return event, nil |
||||
|
})) |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
package api |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"git.aiterp.net/lucifer/server3/models" |
||||
|
"github.com/gin-gonic/gin" |
||||
|
) |
||||
|
|
||||
|
var errorMap = map[error]int{ |
||||
|
models.ErrInvalidName: 400, |
||||
|
} |
||||
|
|
||||
|
type response struct { |
||||
|
Code int `json:"code"` |
||||
|
Message string `json:"message"` |
||||
|
Data interface{} `json:"data"` |
||||
|
} |
||||
|
|
||||
|
func handler(fun func(c *gin.Context) (interface{}, error)) gin.HandlerFunc { |
||||
|
return func(c *gin.Context) { |
||||
|
val, err := fun(c) |
||||
|
if err != nil { |
||||
|
errCode := errorMap[err] |
||||
|
if errCode == 0 { |
||||
|
errCode = 500 |
||||
|
} |
||||
|
|
||||
|
c.JSON(errCode, response{ |
||||
|
Code: errCode, |
||||
|
Message: err.Error(), |
||||
|
}) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
c.JSON(200, val) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func parseBody(c *gin.Context, target interface{}) error { |
||||
|
err := json.NewDecoder(c.Request.Body).Decode(target) |
||||
|
if err != nil { |
||||
|
return models.ErrBadInput |
||||
|
} |
||||
|
|
||||
|
return nil |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
package config |
||||
|
|
||||
|
import "git.aiterp.net/lucifer/server3/models" |
||||
|
|
||||
|
var EventChannel = make(chan models.Event, 8) |
||||
|
|
||||
|
var ChangeChannel = make(chan string, 16) |
@ -0,0 +1,13 @@ |
|||||
|
package config |
||||
|
|
||||
|
import "git.aiterp.net/lucifer/server3/models" |
||||
|
|
||||
|
var dr models.DriverResolver |
||||
|
|
||||
|
func DriverResolver() models.DriverResolver { |
||||
|
if dr == nil { |
||||
|
panic("not implemented yet") |
||||
|
} |
||||
|
|
||||
|
return dr |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package config |
||||
|
|
||||
|
import ( |
||||
|
"os" |
||||
|
"strconv" |
||||
|
) |
||||
|
|
||||
|
var MySqlHost = strEnv("LUCIFER_MYSQL_HOST") |
||||
|
var MySqlPort = intEnv("LUCIFER_MYSQL_PORT") |
||||
|
var MySqlUsername = strEnv("LUCIFER_MYSQL_USERNAME") |
||||
|
var MySQlPassword = strEnv("LUCIFER_MYSQL_PASSWORD") |
||||
|
|
||||
|
var ServerPort = intEnv("LUCIFER_SERVER_PORT") |
||||
|
|
||||
|
func strEnv(key string) string { |
||||
|
env, ok := os.LookupEnv(key) |
||||
|
if !ok { |
||||
|
panic("missing environment variable: " + key) |
||||
|
} |
||||
|
|
||||
|
return env |
||||
|
} |
||||
|
|
||||
|
func intEnv(key string) int { |
||||
|
val, err := strconv.Atoi(strEnv(key)) |
||||
|
if err != nil { |
||||
|
panic("invalid environment variable: " + key) |
||||
|
} |
||||
|
|
||||
|
return val |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package config |
||||
|
|
||||
|
import "git.aiterp.net/lucifer/server3/models" |
||||
|
|
||||
|
var ( |
||||
|
dRepo models.DeviceRepository |
||||
|
ehRepo models.EventHandlerRepository |
||||
|
) |
||||
|
|
||||
|
func DeviceRepository() models.DeviceRepository { |
||||
|
if dRepo == nil { |
||||
|
panic("panik") |
||||
|
} |
||||
|
|
||||
|
return dRepo |
||||
|
} |
||||
|
|
||||
|
func EventHandlerRepository() models.EventHandlerRepository { |
||||
|
if ehRepo == nil { |
||||
|
panic("panik") |
||||
|
} |
||||
|
|
||||
|
return ehRepo |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package app |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"git.aiterp.net/lucifer/server3/app/api" |
||||
|
"git.aiterp.net/lucifer/server3/app/config" |
||||
|
"git.aiterp.net/lucifer/server3/app/services" |
||||
|
"github.com/gin-gonic/gin" |
||||
|
"log" |
||||
|
) |
||||
|
|
||||
|
func StartServer() { |
||||
|
services.StartEventHandler() |
||||
|
|
||||
|
gin.SetMode(gin.ReleaseMode) |
||||
|
ginny := gin.New() |
||||
|
api.Events(ginny.Group("/api/events")) |
||||
|
|
||||
|
log.Fatal(ginny.Run(fmt.Sprintf("0.0.0.0:%d", config.ServerPort))) |
||||
|
} |
@ -0,0 +1,59 @@ |
|||||
|
package services |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"git.aiterp.net/lucifer/server3/app/config" |
||||
|
"git.aiterp.net/lucifer/server3/models" |
||||
|
"log" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
func StartEventHandler() { |
||||
|
config.EventChannel<-models.Event{Name: "LuciferStarted"} |
||||
|
|
||||
|
go func() { |
||||
|
for event := range config.EventChannel { |
||||
|
handleEvent(event) |
||||
|
} |
||||
|
}() |
||||
|
|
||||
|
// Dispatch an HourChanged event at every hour
|
||||
|
go func() { |
||||
|
for { |
||||
|
now := time.Now() |
||||
|
nextHour := now.Add(time.Hour).Truncate(time.Hour) |
||||
|
time.Sleep(nextHour.Sub(now)) |
||||
|
|
||||
|
config.EventChannel <- models.Event{Name: "HourChanged"} |
||||
|
} |
||||
|
}() |
||||
|
} |
||||
|
|
||||
|
var loc, _ = time.LoadLocation("Europe/Oslo") |
||||
|
var ctx = context.Background() |
||||
|
|
||||
|
func handleEvent(event models.Event) { |
||||
|
if !event.HasPayload("hour") { |
||||
|
event.AddPayload("hour", time.Now().In(loc).Format("15")) |
||||
|
} |
||||
|
|
||||
|
handlers, err := config.EventHandlerRepository().FetchAll(ctx) |
||||
|
if err != nil { |
||||
|
log.Printf("Error fetchin event halders: %d", err) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
for _, handler := range handlers { |
||||
|
devices, err := config.DeviceRepository().FetchByReference(ctx, handler.TargetKind, handler.TargetValue) |
||||
|
if err != nil { |
||||
|
log.Printf("Error fetchin event halders: %d", err) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
if !handler.MatchesEvent(event, devices) { |
||||
|
continue |
||||
|
} |
||||
|
|
||||
|
panic("panik") |
||||
|
} |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
module git.aiterp.net/lucifer/server3 |
||||
|
|
||||
|
go 1.16 |
||||
|
|
||||
|
require github.com/gin-gonic/gin v1.7.1 |
@ -0,0 +1,54 @@ |
|||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= |
||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= |
||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= |
||||
|
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8= |
||||
|
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= |
||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= |
||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= |
||||
|
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= |
||||
|
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= |
||||
|
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= |
||||
|
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= |
||||
|
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= |
||||
|
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= |
||||
|
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= |
||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= |
||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= |
||||
|
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= |
||||
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= |
||||
|
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= |
||||
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= |
||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= |
||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= |
||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= |
||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= |
||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= |
||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= |
||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
||||
|
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= |
||||
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= |
||||
|
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= |
||||
|
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= |
||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= |
||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= |
||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= |
||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= |
||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= |
||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= |
||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= |
||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
@ -0,0 +1,7 @@ |
|||||
|
package main |
||||
|
|
||||
|
import "git.aiterp.net/lucifer/server3/app" |
||||
|
|
||||
|
func main() { |
||||
|
app.StartServer() |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package models |
||||
|
|
||||
|
type Bridge struct { |
||||
|
ID int |
||||
|
Name string |
||||
|
Driver DriverType |
||||
|
ConnectionString string |
||||
|
} |
||||
|
|
||||
|
type DriverType string |
||||
|
|
||||
|
var ( |
||||
|
DTHue DriverType = "Hue" |
||||
|
DTNanoLeaf DriverType = "NanoLeaf" |
||||
|
) |
@ -0,0 +1,61 @@ |
|||||
|
package models |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"regexp" |
||||
|
"strconv" |
||||
|
) |
||||
|
|
||||
|
type ColorValue struct { |
||||
|
Hue int `json:"h,omitempty"` |
||||
|
Saturation int `json:"s,omitempty"` |
||||
|
Kelvin int `json:"kelvin,omitempty"` |
||||
|
} |
||||
|
|
||||
|
func (c *ColorValue) IsHueSat() bool { |
||||
|
return !c.IsKelvin() |
||||
|
} |
||||
|
|
||||
|
func (c *ColorValue) IsKelvin() bool { |
||||
|
return c.Kelvin > 0 |
||||
|
} |
||||
|
|
||||
|
func (c *ColorValue) String() string { |
||||
|
if c.Kelvin > 0 { |
||||
|
return fmt.Sprintf("kelvin:%d", c.Kelvin) |
||||
|
} |
||||
|
|
||||
|
return fmt.Sprintf("hsv:%d,%d", c.Hue, c.Saturation) |
||||
|
} |
||||
|
|
||||
|
var kelvinRegex = regexp.MustCompile("kelvin:([0-9]+)") |
||||
|
var hsRegex = regexp.MustCompile("hs:([0-9]+),([0-9]+)") |
||||
|
|
||||
|
func ParseColorValue(raw string) (ColorValue, error) { |
||||
|
if kelvinRegex.MatchString(raw) { |
||||
|
part := kelvinRegex.FindString(raw) |
||||
|
parsedPart, err := strconv.Atoi(part) |
||||
|
if err != nil { |
||||
|
return ColorValue{}, ErrBadInput |
||||
|
} |
||||
|
|
||||
|
return ColorValue{Kelvin: parsedPart}, nil |
||||
|
} |
||||
|
|
||||
|
if hsRegex.MatchString(raw) { |
||||
|
parts := kelvinRegex.FindAllString(raw, 2) |
||||
|
if len(parts) < 2 { |
||||
|
return ColorValue{}, ErrUnknownColorFormat |
||||
|
} |
||||
|
|
||||
|
part1, err1 := strconv.Atoi(parts[0]) |
||||
|
part2, err2 := strconv.Atoi(parts[1]) |
||||
|
if err1 != nil || err2 != nil { |
||||
|
return ColorValue{}, ErrBadInput |
||||
|
} |
||||
|
|
||||
|
return ColorValue{Hue: part1, Saturation: part2}, nil |
||||
|
} |
||||
|
|
||||
|
return ColorValue{}, ErrUnknownColorFormat |
||||
|
} |
@ -0,0 +1,122 @@ |
|||||
|
package models |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
type Device struct { |
||||
|
ID int `json:"id"` |
||||
|
BridgeID int `json:"bridgeID"` |
||||
|
Icon string `json:"icon"` |
||||
|
Name string `json:"name"` |
||||
|
Capabilities []DeviceCapability `json:"capabilities"` |
||||
|
Properties map[string]string `json:"properties"` |
||||
|
State DeviceState `json:"state"` |
||||
|
Tags []string `json:"tags"` |
||||
|
} |
||||
|
|
||||
|
// DeviceState contains optional state values that
|
||||
|
// - Power: Whether the device is powered on
|
||||
|
// - Color: Color value, if a color setting can be set on the device
|
||||
|
// - Intensity: e.g. brightness, from 0-255
|
||||
|
// - Temperature: e.g. for thermostats
|
||||
|
type DeviceState struct { |
||||
|
Power bool `json:"power"` |
||||
|
Color ColorValue `json:"color,omitempty"` |
||||
|
Intensity int `json:"intensity,omitempty"` |
||||
|
Temperature int `json:"temperature"` |
||||
|
} |
||||
|
|
||||
|
type NewDeviceState struct { |
||||
|
Power *bool `json:"power"` |
||||
|
Color *string `json:"color"` |
||||
|
Intensity int `json:"intensity"` |
||||
|
Temperature int `json:"temperature"` |
||||
|
} |
||||
|
|
||||
|
type DeviceCapability string |
||||
|
|
||||
|
type DeviceRepository interface { |
||||
|
FindByID(ctx context.Context, id int) (*Device, error) |
||||
|
FetchByReference(ctx context.Context, kind ReferenceKind, value string) ([]Device, error) |
||||
|
Save(ctx context.Context, device *Device) error |
||||
|
Delete(ctx context.Context, device *Device) error |
||||
|
} |
||||
|
|
||||
|
var ( |
||||
|
DCPower DeviceCapability = "Power" |
||||
|
DCColorHS DeviceCapability = "ColorHS" |
||||
|
DCColorKelvin DeviceCapability = "ColorKelvin" |
||||
|
DCButtonDefault DeviceCapability = "ButtonDefault" |
||||
|
DCButtonOn DeviceCapability = "ButtonOn" |
||||
|
DCButtonOff DeviceCapability = "ButtonOff" |
||||
|
DCButtonPlus DeviceCapability = "ButtonPlus" |
||||
|
DCButtonMinus DeviceCapability = "ButtonMinus" |
||||
|
DCButtonToggle DeviceCapability = "ButtonToggle" |
||||
|
DCIntensity DeviceCapability = "Intensity" |
||||
|
DCTemperature DeviceCapability = "Temperature" |
||||
|
) |
||||
|
|
||||
|
var Capabilities = []DeviceCapability{ |
||||
|
DCPower, |
||||
|
DCColorHS, |
||||
|
DCColorKelvin, |
||||
|
DCButtonDefault, |
||||
|
DCButtonOn, |
||||
|
DCButtonOff, |
||||
|
DCButtonPlus, |
||||
|
DCButtonMinus, |
||||
|
DCButtonToggle, |
||||
|
DCIntensity, |
||||
|
DCTemperature, |
||||
|
} |
||||
|
|
||||
|
func (d *Device) Validate() error { |
||||
|
d.Name = strings.Trim(d.Name, " \t\n ") |
||||
|
if d.Name == "" { |
||||
|
return ErrInvalidName |
||||
|
} |
||||
|
|
||||
|
newCaps := make([]DeviceCapability, 0, len(d.Capabilities)) |
||||
|
for _, currCap := range d.Capabilities { |
||||
|
for _, validCap := range Capabilities { |
||||
|
if currCap == validCap { |
||||
|
newCaps = append(newCaps, currCap) |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
d.Capabilities = newCaps |
||||
|
|
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func (d *Device) HasCapability(capacity DeviceCapability) bool { |
||||
|
for _, c := range d.Capabilities { |
||||
|
if c == capacity { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
func (d *Device) SetState(newState NewDeviceState) error { |
||||
|
if newState.Power != nil && d.HasCapability(DCPower) { |
||||
|
d.State.Power = *newState.Power |
||||
|
} |
||||
|
|
||||
|
if newState.Color != nil { |
||||
|
parsed, err := ParseColorValue(*newState.Color) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
if (parsed.IsKelvin() && d.HasCapability(DCColorKelvin)) || (parsed.IsHueSat() && d.HasCapability(DCColorHS)) { |
||||
|
d.State.Color = parsed |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return nil |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
package models |
||||
|
|
||||
|
import "context" |
||||
|
|
||||
|
type DriverResolver interface { |
||||
|
ResolveFor(bridge Bridge) (Driver, error) |
||||
|
} |
||||
|
|
||||
|
type Driver interface { |
||||
|
SearchBridge(ctx context.Context, address string) (Bridge, error) |
||||
|
SearchDevices(ctx context.Context, bridge Bridge) ([]Device, error) |
||||
|
Consume(ctx context.Context, bridge Bridge, devices []Device, ch chan Event) (chan <-struct{}, error) |
||||
|
Publish(ctx context.Context, bridge Bridge, device Device) error |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package models |
||||
|
|
||||
|
import "errors" |
||||
|
|
||||
|
var ErrInvalidName = errors.New("invalid name") |
||||
|
var ErrBadInput = errors.New("bad input") |
||||
|
var ErrBadColor = errors.New("bad color") |
||||
|
var ErrUnknownColorFormat = errors.New("unknown color format") |
@ -0,0 +1,18 @@ |
|||||
|
package models |
||||
|
|
||||
|
type Event struct { |
||||
|
Name string `json:"name"` |
||||
|
Payload map[string]string `json:"payload,omitempty"` |
||||
|
} |
||||
|
|
||||
|
func (e *Event) AddPayload(key, value string) { |
||||
|
if e.Payload == nil { |
||||
|
e.Payload = make(map[string]string, 8) |
||||
|
} |
||||
|
|
||||
|
e.Payload[key] = value |
||||
|
} |
||||
|
|
||||
|
func (e *Event) HasPayload(key string) bool { |
||||
|
return e.Payload != nil && e.Payload[key] != "" |
||||
|
} |
@ -0,0 +1,121 @@ |
|||||
|
package models |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"regexp" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
type EventHandler struct { |
||||
|
ID int `json:"id"` |
||||
|
EventName string `json:"eventName"` |
||||
|
Conditions map[string]EventCondition `json:"conditions"` |
||||
|
TargetKind ReferenceKind `json:"targetType"` |
||||
|
TargetValue string `json:"targetValue"` |
||||
|
} |
||||
|
|
||||
|
type EventHandlerRepository interface { |
||||
|
FindByID(ctx context.Context, id int) (EventHandler, error) |
||||
|
FetchAll(ctx context.Context) ([]EventHandler, error) |
||||
|
Save(ctx context.Context, handler *EventHandler) |
||||
|
Delete(ctx context.Context, handler *EventHandler) |
||||
|
} |
||||
|
|
||||
|
type EventCondition struct { |
||||
|
EQ string `json:"eq,omitempty"` |
||||
|
GT string `json:"gt,omitempty"` |
||||
|
GTE string `json:"gte,omitempty"` |
||||
|
LT string `json:"lt,omitempty"` |
||||
|
LTE string `json:"lte,omitempty"` |
||||
|
} |
||||
|
|
||||
|
func (h *EventHandler) MatchesEvent(event Event, targets []Device) bool { |
||||
|
if event.Name != h.EventName { |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
for key, condition := range h.Conditions { |
||||
|
if !event.HasPayload(key) { |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
if !condition.check(key, event.Payload[key], targets) { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true |
||||
|
} |
||||
|
|
||||
|
func (c *EventCondition) check(key, value string, targets []Device) bool { |
||||
|
any := strings.Index(key, "any.") == 0 |
||||
|
all := strings.Index(key, "all.") == 0 |
||||
|
if any || all && len(key) > 4 { |
||||
|
count := 0 |
||||
|
for _, target := range targets { |
||||
|
if c.checkDevice(key[4:], target) { |
||||
|
count++ |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return (any && count > 0) || (all && count == len(targets)) |
||||
|
} |
||||
|
|
||||
|
return c.matches(value) |
||||
|
} |
||||
|
|
||||
|
func (c *EventCondition) checkDevice(key string, device Device) bool { |
||||
|
switch key { |
||||
|
case "power": |
||||
|
return c.matches(strconv.FormatBool(device.State.Power)) |
||||
|
case "color": |
||||
|
return c.matches(device.State.Color.String()) |
||||
|
case "intensity": |
||||
|
return c.matches(strconv.Itoa(device.State.Intensity)) |
||||
|
case "temperature": |
||||
|
return c.matches(strconv.Itoa(device.State.Temperature)) |
||||
|
} |
||||
|
|
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
var numRegex = regexp.MustCompile("^{-[0-9].}+$") |
||||
|
|
||||
|
func (c *EventCondition) matches(value string) bool { |
||||
|
if numRegex.MatchString(value) { |
||||
|
numValue, _ := strconv.ParseFloat(c.LT, 64) |
||||
|
stillAlive := true |
||||
|
|
||||
|
if c.LT != "" { |
||||
|
lt, _ := strconv.ParseFloat(c.LT, 64) |
||||
|
stillAlive = numValue < lt |
||||
|
} |
||||
|
|
||||
|
if stillAlive && c.LTE != "" { |
||||
|
lte, _ := strconv.ParseFloat(c.LTE, 64) |
||||
|
stillAlive = numValue <= lte |
||||
|
} |
||||
|
|
||||
|
if stillAlive && c.EQ != "" { |
||||
|
eq, _ := strconv.ParseFloat(c.EQ, 64) |
||||
|
stillAlive = numValue == eq |
||||
|
} |
||||
|
|
||||
|
if stillAlive && c.GTE != "" { |
||||
|
gte, _ := strconv.ParseFloat(c.GTE, 64) |
||||
|
stillAlive = numValue == gte |
||||
|
} |
||||
|
|
||||
|
if stillAlive && c.GT != "" { |
||||
|
gt, _ := strconv.ParseFloat(c.GT, 64) |
||||
|
stillAlive = numValue > gt |
||||
|
} |
||||
|
|
||||
|
return stillAlive |
||||
|
} else if c.EQ != "" { |
||||
|
return strings.ToLower(c.EQ) == strings.ToLower(value) |
||||
|
} else { |
||||
|
return false |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package models |
||||
|
|
||||
|
type ReferenceKind string |
||||
|
|
||||
|
var ( |
||||
|
RTDeviceID ReferenceKind = "DeviceID" |
||||
|
RTBridgeID ReferenceKind = "BridgeID" |
||||
|
RTTag ReferenceKind = "Tag" |
||||
|
) |
||||
|
|
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue