Skip to content

Commit

Permalink
websocket initialization done in App
Browse files Browse the repository at this point in the history
  • Loading branch information
saml-dev committed Oct 10, 2022
1 parent 60b80ec commit 2597711
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 236 deletions.
147 changes: 36 additions & 111 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,48 @@ package gomeassistant

import (
"context"
"fmt"
"time"

"github.com/saml-dev/gome-assistant/internal/setup"
"nhooyr.io/websocket"
)

type app struct {
url string
ctx context.Context
schedules []Schedule
listeners []Listener
ctx context.Context
ctxCancel context.CancelFunc
conn *websocket.Conn
schedules []schedule
entityListeners []entityListener
}

/*
App establishes the websocket connection and returns an object
you can use to register schedules and listeners.
*/
func App(connString string) (app, error) {
conn, ctx, ctxCancel, err := setup.SetupConnection(connString)
if err != nil {
return app{}, err
}

return app{
conn: conn,
ctx: ctx,
ctxCancel: ctxCancel,
schedules: []schedule{},
entityListeners: []entityListener{},
}, nil
}

func App(url string) (app, error) {
// TODO: connect to websocket, return error if fails
return app{url: url}, nil
func (a *app) Cleanup() {
if a.ctxCancel != nil {
a.ctxCancel()
}
}

func (a app) RegisterSchedule(s Schedule) {
func (a *app) RegisterSchedule(s schedule) {
fmt.Println(a.schedules)
if s.err != nil {
panic(s.err) // something wasn't configured properly when the schedule was built
}
Expand All @@ -40,7 +66,8 @@ func (a app) RegisterSchedule(s Schedule) {
startTime = startTime.Add(s.frequency)
}

// TODO: save realStartTime or _startTime to s, add to list of Schedules
s.realStartTime = startTime
a.schedules = append(a.schedules, s)
}

const (
Expand All @@ -50,105 +77,3 @@ const (
Hourly time.Duration = time.Hour
Minutely time.Duration = time.Minute
)

type Listener struct {
}

const (
_0000 string = "0000"
_0015 string = "0015"
_0030 string = "0030"
_0045 string = "0045"
_0100 string = "0100"
_0115 string = "0115"
_0130 string = "0130"
_0145 string = "0145"
_0200 string = "0200"
_0215 string = "0215"
_0230 string = "0230"
_0245 string = "0245"
_0300 string = "0300"
_0315 string = "0315"
_0330 string = "0330"
_0345 string = "0345"
_0400 string = "0400"
_0415 string = "0415"
_0430 string = "0430"
_0445 string = "0445"
_0500 string = "0500"
_0515 string = "0515"
_0530 string = "0530"
_0545 string = "0545"
_0600 string = "0600"
_0615 string = "0615"
_0630 string = "0630"
_0645 string = "0645"
_0700 string = "0700"
_0715 string = "0715"
_0730 string = "0730"
_0745 string = "0745"
_0800 string = "0800"
_0815 string = "0815"
_0830 string = "0830"
_0845 string = "0845"
_0900 string = "0900"
_0915 string = "0915"
_0930 string = "0930"
_0945 string = "0945"
_1000 string = "1000"
_1015 string = "1015"
_1030 string = "1030"
_1045 string = "1045"
_1100 string = "1100"
_1115 string = "1115"
_1130 string = "1130"
_1145 string = "1145"
_1200 string = "1200"
_1215 string = "1215"
_1230 string = "1230"
_1245 string = "1245"
_1300 string = "1300"
_1315 string = "1315"
_1330 string = "1330"
_1345 string = "1345"
_1400 string = "1400"
_1415 string = "1415"
_1430 string = "1430"
_1445 string = "1445"
_1500 string = "1500"
_1515 string = "1515"
_1530 string = "1530"
_1545 string = "1545"
_1600 string = "1600"
_1615 string = "1615"
_1630 string = "1630"
_1645 string = "1645"
_1700 string = "1700"
_1715 string = "1715"
_1730 string = "1730"
_1745 string = "1745"
_1800 string = "1800"
_1815 string = "1815"
_1830 string = "1830"
_1845 string = "1845"
_1900 string = "1900"
_1915 string = "1915"
_1930 string = "1930"
_1945 string = "1945"
_2000 string = "2000"
_2015 string = "2015"
_2030 string = "2030"
_2045 string = "2045"
_2100 string = "2100"
_2115 string = "2115"
_2130 string = "2130"
_2145 string = "2145"
_2200 string = "2200"
_2215 string = "2215"
_2230 string = "2230"
_2245 string = "2245"
_2300 string = "2300"
_2315 string = "2315"
_2330 string = "2330"
_2345 string = "2345"
)
11 changes: 6 additions & 5 deletions cmd/main/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (

func main() {
app, err := ga.App("192.168.86.67:8123")
defer app.Cleanup()
if err != nil {
panic(err)
fmt.Println(err)
return
}

s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(ga.HourMinute(22, 00)).Build()
s2 := ga.ScheduleBuilder().Call(lightsOut).Every(time.Hour * 4).Offset(ga.HourMinute(1, 0)).Build()
s2 := ga.ScheduleBuilder().Call(lightsOut).Every(time.Hour*4 + time.Minute*30).Offset(ga.HourMinute(1, 0)).Build()
app.RegisterSchedule(s2)
// err = app.Start()

Expand All @@ -23,9 +26,7 @@ func main() {
OnlyBetween(ga.HourMinute(22, 00), ga.HourMinute(07, 00))
fmt.Println(simpleListener)

// p := ga.NewPersonBuilder().Lives().At("lskdjflskf").WithPostalCode("kdjf").Works().As("SWE")

fmt.Println(s, s2)
fmt.Println(s, "\n", s2)
}

func lightsOut(service ga.Service) {
Expand Down
53 changes: 0 additions & 53 deletions gomeassistant.go

This file was deleted.

51 changes: 0 additions & 51 deletions internal/network/setup.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/services/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package services
import (
"context"

"github.com/saml-dev/gome-assistant/internal/network"
"github.com/saml-dev/gome-assistant/internal/setup"
"nhooyr.io/websocket"
)

type Light struct {
conn websocket.Conn
conn *websocket.Conn
ctx context.Context
}

Expand Down Expand Up @@ -41,10 +41,10 @@ func LightOffRequest(entityId string) LightRequest {

func (l Light) TurnOn(entityId string) {
req := LightOnRequest(entityId)
network.WriteMessage(req, l.conn, l.ctx)
setup.WriteMessage(req, l.conn, l.ctx)
}

func (l Light) TurnOff(entityId string) {
req := LightOffRequest(entityId)
network.WriteMessage(req, l.conn, l.ctx)
setup.WriteMessage(req, l.conn, l.ctx)
}
Loading

0 comments on commit 2597711

Please sign in to comment.