mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-09 10:07:25 -06:00
fix bug when registering multiple listeners and change sunrise/sunset to non-ha impl
This commit is contained in:
@@ -15,8 +15,8 @@ type HttpClient struct {
|
||||
token string
|
||||
}
|
||||
|
||||
func NewHttpClient(url, token string) *HttpClient {
|
||||
url = fmt.Sprintf("http://%s/api", url)
|
||||
func NewHttpClient(ip, port, token string) *HttpClient {
|
||||
url := fmt.Sprintf("http://%s:%s/api", ip, port)
|
||||
return &HttpClient{url, token}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func GetId() int64 {
|
||||
func ParseTime(s string) carbon.Carbon {
|
||||
t, err := time.Parse("15:04", s)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.", s)
|
||||
log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.\n", s)
|
||||
}
|
||||
return carbon.Now().SetTimeMilli(t.Hour(), t.Minute(), 0, 0)
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func ParseTime(s string) carbon.Carbon {
|
||||
func ParseDuration(s string) time.Duration {
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Couldn't parse string duration: \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s))
|
||||
log.Fatalf(fmt.Sprintf("Couldn't parse string duration: \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units\n", s))
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ type AuthMessage struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
// TODO: use a mutex to prevent concurrent writes panic here
|
||||
// https://github.com/gorilla/websocket/issues/119
|
||||
func WriteMessage[T any](msg T, conn *websocket.Conn, ctx context.Context) error {
|
||||
msgJson, err := json.Marshal(msg)
|
||||
// fmt.Println(string(msgJson))
|
||||
@@ -44,36 +46,36 @@ func ReadMessage(conn *websocket.Conn, ctx context.Context) ([]byte, error) {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func SetupConnection(connString string, authToken string) (*websocket.Conn, context.Context, context.CancelFunc) {
|
||||
func SetupConnection(ip, port, authToken string) (*websocket.Conn, context.Context, context.CancelFunc) {
|
||||
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
|
||||
// Init websocket connection
|
||||
dialer := websocket.DefaultDialer
|
||||
conn, _, err := dialer.DialContext(ctx, fmt.Sprintf("ws://%s/api/websocket", connString), nil)
|
||||
conn, _, err := dialer.DialContext(ctx, fmt.Sprintf("ws://%s:%s/api/websocket", ip, port), nil)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
log.Fatalf("ERROR: Failed to connect to websocket at ws://%s/api/websocket. Check IP address and port\n", connString)
|
||||
log.Fatalf("ERROR: Failed to connect to websocket at ws://%s:%s/api/websocket. Check IP address and port\n", ip, port)
|
||||
}
|
||||
|
||||
// Read auth_required message
|
||||
_, err = ReadMessage(conn, ctx)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
panic("Unknown error creating websocket client")
|
||||
log.Fatalf("Unknown error creating websocket client\n")
|
||||
}
|
||||
|
||||
// Send auth message
|
||||
err = SendAuthMessage(conn, ctx, authToken)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
panic("Unknown error creating websocket client")
|
||||
log.Fatalf("Unknown error creating websocket client\n")
|
||||
}
|
||||
|
||||
// Verify auth message was successful
|
||||
err = VerifyAuthResponse(conn, ctx)
|
||||
if err != nil {
|
||||
ctxCancel()
|
||||
panic("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
||||
log.Fatalf("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile\n")
|
||||
}
|
||||
|
||||
return conn, ctx, ctxCancel
|
||||
@@ -132,7 +134,7 @@ func SubscribeToEventType(eventType string, conn *websocket.Conn, ctx context.Co
|
||||
}
|
||||
err := WriteMessage(e, conn, ctx)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error writing to websocket: %s", err))
|
||||
log.Fatalf("Error writing to websocket: %s\n", err)
|
||||
}
|
||||
// m, _ := ReadMessage(conn, ctx)
|
||||
// log.Default().Println(string(m))
|
||||
|
||||
Reference in New Issue
Block a user