Feat: event stream parsing and dumping

This commit is contained in:
kayos@tcp.direct 2023-04-07 01:15:58 -07:00
parent 4b311bcd5d
commit 6f2a84d665
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
3 changed files with 112 additions and 5 deletions

77
internal/haptic/event.go Normal file
View File

@ -0,0 +1,77 @@
package haptic
import "time"
type Color struct {
Xy struct {
X float64 `json:"x"`
Y float64 `json:"y"`
} `json:"xy"`
}
type Dynamics struct {
Status string `json:"status,omitempty"`
Speed float64 `json:"speed,omitempty"`
SpeedValid bool `json:"speed_valid,omitempty"`
}
type Dimming struct {
Brightness float64 `json:"brightness"`
}
type Owner struct {
Rid string `json:"rid"`
Rtype string `json:"rtype"`
}
type Button struct {
ButtonReport struct {
Event string `json:"event"`
Updated time.Time `json:"updated"`
} `json:"button_report"`
LastEvent string `json:"last_event"`
}
type ColorTemperature struct {
Mirek interface{} `json:"mirek"`
MirekValid bool `json:"mirek_valid"`
}
type Status struct {
Active string `json:"active"`
}
type PowerState struct {
BatteryLevel int `json:"battery_level"`
BatteryState string `json:"battery_state"`
}
type Temperature struct {
Temperature float64 `json:"temperature"`
TemperatureReport struct {
Changed time.Time `json:"changed"`
Temperature float64 `json:"temperature"`
} `json:"temperature_report"`
TemperatureValid bool `json:"temperature_valid"`
}
type WrappedEvent struct {
Timestamp time.Time `json:"creationtime"`
Id string `json:"id"`
Type string `json:"type"`
Event Event `json:"data"`
}
type Event struct {
IdV1 string `json:"id_v1"`
Button Button `json:"button,omitempty"`
Owner Owner `json:"owner,omitempty"`
Dimming Dimming `json:"dimming,omitempty"`
Dynamics Dynamics `json:"dynamics,omitempty"`
Color Color `json:"color,omitempty"`
ColorTemperature ColorTemperature `json:"color_temperature,omitempty"`
Temperature Temperature `json:"temperature,omitempty"`
PowerState PowerState `json:"power_state,omitempty"`
Status Status `json:"status,omitempty"`
}

View File

@ -32,6 +32,10 @@ func (c *EventClient) Subscribe(event string, ch chan string) {
}
func (c *EventClient) Start(hueHost, hueKey string) error {
if strings.HasPrefix(hueHost, "http") {
hueHost = strings.Split(hueHost, "://")[1]
hueHost = strings.TrimSuffix(hueHost, "/")
}
req, err := http.NewRequest("GET", "https://"+hueHost+"/eventstream/clip/v2", nil)
if err != nil {
return err

36
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"errors"
"fmt"
"io"
"os"
@ -17,6 +18,7 @@ import (
"git.tcp.direct/kayos/ziggs/internal/common"
"git.tcp.direct/kayos/ziggs/internal/config"
"git.tcp.direct/kayos/ziggs/internal/data"
"git.tcp.direct/kayos/ziggs/internal/haptic"
"git.tcp.direct/kayos/ziggs/internal/ziggy"
)
@ -26,7 +28,7 @@ var (
const banner = "H4sIAAAAAAACA5OONja2NjHIfTSl59GUBjCaIB1tkAvCCtLIkmtwyjRQLmOQyyUdbYnukhmoeg2NwSyYsiagoDmIqYCkDFkSQ8caShROwe5oqGaYPHZXg2W34JZqoIYU0DkAuowN3c4BAAA="
func init() {
func aesthetic() {
bnr, _ := squish.UnpackStr(banner)
_, _ = io.Copy(os.Stdout, strings.NewReader(bnr))
compileTime, Version := common.Version()
@ -36,18 +38,25 @@ func init() {
if compileTime == "" {
compileTime = time.Now().Format(time.RFC3339)
}
config.Init()
log = config.StartLogger()
log.Trace().Msg("Logger started")
index := len(Version)
if len(Version) > 18 {
index = 18
}
log.Info().Str("version", Version[:index]).Send()
log.Info().Str("built", compileTime).Send()
if len(os.Args) < 1 {
}
func init() {
config.Init()
log = config.StartLogger()
log.Trace().Msg("Logger started")
if len(os.Args) > 1 {
return
}
aesthetic()
}
func TurnAll(Known []*ziggy.Bridge, mode ziggy.ToggleMode) {
@ -151,6 +160,23 @@ func main() {
for _, arg := range os.Args {
switch arg {
case "events":
evch := make(chan string, 10)
go func() {
log.Info().Msg("starting event client")
defer log.Warn().Msg("event client stopped")
for e := range evch {
println(e)
}
}()
evc := haptic.NewEventClient()
evc.Subscribe("*", evch)
if err = evc.Start(config.KnownBridges[0].Hostname, config.KnownBridges[0].Username); err != nil &&
!errors.Is(err, context.Canceled) && !errors.Is(err, io.EOF) {
log.Fatal().Err(err).Msg("failed to start event client")
}
close(evch)
return
case "discover":
case "on":