Skip to content

Commit

Permalink
add user and consumption queries
Browse files Browse the repository at this point in the history
rename some files

cleanup

initial code
  • Loading branch information
SiddyP committed Jan 17, 2024
1 parent 0d233a6 commit 1719794
Show file tree
Hide file tree
Showing 21 changed files with 723 additions and 211 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
# You can test your matrix by printing the current Go version

- name: Display Go version
run: go version
- name: Go test
Expand Down
24 changes: 23 additions & 1 deletion .gitignore
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
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

116 changes: 116 additions & 0 deletions README.md
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
```
7 changes: 7 additions & 0 deletions examples/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func main() {
queryUserExample()
queryConsumptionExample()
setupWebsocket()
}
30 changes: 30 additions & 0 deletions examples/queryConsumption.go
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)
}
27 changes: 27 additions & 0 deletions examples/queryUser.go
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)
}
63 changes: 63 additions & 0 deletions examples/setupWebsocket.go
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!")
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ module github.com/SiddyP/gotibber
go 1.21.5

require (
github.com/google/uuid v1.5.0
github.com/jackc/pgx/v5 v5.5.1
github.com/machinebox/graphql v0.2.2
github.com/stretchr/testify v1.8.4
)

require golang.org/x/net v0.17.0 // indirect

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.5.1
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matryer/is v1.4.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
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/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down Expand Up @@ -29,12 +33,14 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
21 changes: 0 additions & 21 deletions main.go

This file was deleted.

Loading

0 comments on commit 1719794

Please sign in to comment.