-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename some files cleanup initial code
- Loading branch information
Showing
21 changed files
with
723 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
.env | ||
.env | ||
.vscode/ | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
*.DS_Store |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,123 @@ | ||
# gotibber | ||
Query the tibber GraphQL API in a request/response fashion or setup a websocket connection to consume live measurements. | ||
|
||
Websocket/streaming data requires a meter like the Tibber Pulse or Watty connected to the serial port (P1-port) of your powermeter. | ||
|
||
## env | ||
Provide your tibber details trough e.g. environment variables | ||
```zsh | ||
export TIBBER_API_TOKEN=<REDACTED> | ||
export TIBBER_HOUSE_ID=<REDACTED> | ||
``` | ||
|
||
## example usage | ||
Verify functionality by setting aforementioned environment variables and then run `go run .` in the `examples/`-directory | ||
|
||
### query user | ||
```go | ||
func QueryUserExample() { | ||
|
||
ctx := context.Background() | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
} | ||
|
||
u := t.QueryUser(ctx, &tibber.User{}) | ||
|
||
fmt.Printf("User: %v\n", u.Viewer.Name) | ||
} | ||
``` | ||
|
||
### query consumption | ||
|
||
```go | ||
func QueryConsumptionExample() { | ||
ctx := context.Background() | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
} | ||
|
||
c := t.QueryConsumption(ctx, &tibber.Consumption{ | ||
Id: os.Getenv("TIBBER_HOUSE_ID"), | ||
Resolution: "HOURLY", | ||
Last: 5, | ||
}) | ||
|
||
fmt.Printf("Consumption: %v\n", c.Viewer.Homes) | ||
} | ||
``` | ||
|
||
### setup websocket | ||
|
||
```go | ||
func setupWebsocket() { | ||
// terminate listens for SIGINT and SIGTERM signals from the OS | ||
terminate := make(chan os.Signal, 1) | ||
signal.Notify(terminate, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
WebsocketClient: &tibber.WebsocketClient{ | ||
Config: tibber.NewWebsocketConfig(&tibber.WebsocketConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
Host: "websocket-api.tibber.com", | ||
Path: "/v1-beta/gql/subscriptions", | ||
Id: os.Getenv("TIBBER_HOUSE_ID"), | ||
}), | ||
Data: make(chan tibber.LiveMeasurement), | ||
}, | ||
Wg: &sync.WaitGroup{}, | ||
} | ||
|
||
t.Wg.Add(1) | ||
go t.Subscribe(ctx) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case liveMeasurement := <-t.WebsocketClient.Data: | ||
fmt.Printf( | ||
"New measurement: %v, %v W\n", | ||
*liveMeasurement.Timestamp, *liveMeasurement.Power, | ||
) | ||
case <-ctx.Done(): | ||
fmt.Println("Done!") | ||
return | ||
} | ||
} | ||
}() | ||
|
||
<-terminate //block until terminate is closed | ||
fmt.Println("*********************************\nShutdown signal received\n*********************************") | ||
cancelFunc() | ||
t.Wg.Wait() | ||
fmt.Println("All done!") | ||
} | ||
|
||
``` | ||
|
||
yields e.g. | ||
|
||
```shell | ||
New measurement: 2024-01-17 22:21:35 +0100 CET, 2834 W | ||
New measurement: 2024-01-17 22:21:40 +0100 CET, 2835 W | ||
New measurement: 2024-01-17 22:21:45 +0100 CET, 2839 W | ||
New measurement: 2024-01-17 22:21:50 +0100 CET, 2841 W | ||
New measurement: 2024-01-17 22:21:55 +0100 CET, 2842 W | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
func main() { | ||
queryUserExample() | ||
queryConsumptionExample() | ||
setupWebsocket() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/SiddyP/gotibber/tibber" | ||
) | ||
|
||
func queryConsumptionExample() { | ||
ctx := context.Background() | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
} | ||
|
||
c := t.QueryConsumption(ctx, &tibber.Consumption{ | ||
Id: os.Getenv("TIBBER_HOUSE_ID"), | ||
Resolution: "HOURLY", | ||
Last: 5, | ||
}) | ||
|
||
fmt.Printf("Consumption: %v\n", c.Viewer.Homes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/SiddyP/gotibber/tibber" | ||
) | ||
|
||
func queryUserExample() { | ||
|
||
ctx := context.Background() | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
} | ||
|
||
u := t.QueryUser(ctx, &tibber.User{}) | ||
|
||
fmt.Printf("User: %v\n", u.Viewer.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/SiddyP/gotibber/tibber" | ||
) | ||
|
||
func setupWebsocket() { | ||
// terminate listens for SIGINT and SIGTERM signals from the OS | ||
terminate := make(chan os.Signal, 1) | ||
signal.Notify(terminate, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
|
||
t := tibber.Client{ | ||
APIClient: tibber.NewAPIClient(&tibber.APIConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
URL: "https://api.tibber.com/v1-beta/gql", | ||
}), | ||
Logger: slog.Default(), | ||
WebsocketClient: &tibber.WebsocketClient{ | ||
Config: tibber.NewWebsocketConfig(&tibber.WebsocketConfig{ | ||
Token: os.Getenv("TIBBER_API_TOKEN"), | ||
Host: "websocket-api.tibber.com", | ||
Path: "/v1-beta/gql/subscriptions", | ||
Id: os.Getenv("TIBBER_HOUSE_ID"), | ||
}), | ||
Data: make(chan tibber.LiveMeasurement), | ||
}, | ||
Wg: &sync.WaitGroup{}, | ||
} | ||
|
||
t.Wg.Add(1) | ||
go t.Subscribe(ctx) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case liveMeasurement := <-t.WebsocketClient.Data: | ||
fmt.Printf( | ||
"New measurement: %v, %v W\n", | ||
*liveMeasurement.Timestamp, *liveMeasurement.Power, | ||
) | ||
case <-ctx.Done(): | ||
fmt.Println("Done!") | ||
return | ||
} | ||
} | ||
}() | ||
|
||
<-terminate //block until terminate is closed | ||
fmt.Println("*********************\nShutdown signal received\n*********************") | ||
cancelFunc() | ||
t.Wg.Wait() | ||
fmt.Println("All done!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.