Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
tskaard committed Apr 2, 2019
1 parent 1c9d184 commit 6b5fdd5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
75 changes: 73 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
# tibber-golang
Tibber GraphQl API
https://developer.tibber.com
Limited implementation of the Tibber API in golang
[developer.tibber.com](https://developer.tibber.com)

**Possibilities:**
* Get a list of homes with id, name, meterID and features
* Send push notification from the Tibber App
* Subscribe to data from Tibber Pulse


## Usage

```go
package main

import (
"fmt"
"strconv"

tibber "github.com/tskaard/tibber-golang"
)
const token = "<Tibber token>"

type Handler struct {
tibber *tibber.Client
streams map[string]*tibber.Stream
msgChannal tibber.MsgChan
}

func NewHandler() *Handler {
h := &Handler{}
h.tibber = tibber.NewClient("")
h.streams = make(map[string]*tibber.Stream)
h.msgChannal = make(tibber.MsgChan)
return h
}

func main() {
h := NewHandler()
h.tibber.Token = token
homes, err := h.tibber.GetHomes()
if err != nil {
panic("Can not get homes from Tibber")
}
for _, home := range homes {
fmt.Println(home.ID)
if home.Features.RealTimeConsumptionEnabled {
stream := tibber.NewStream(home.ID, h.tibber.Token)
stream.StartSubscription(h.msgChannal)
h.streams[home.ID] = stream
}
}
_, err = h.tibber.SendPushNotification("Tibber-Golang", "Message from GO")
if err != nil {
panic("Push failed")
}
go func(msgChan tibber.MsgChan) {
for {
select {
case msg := <-msgChan:
h.handleStreams(msg)
}
}
}(h.msgChannal)

for {
}

}

func (fh *Handler) handleStreams(newMsg *tibber.StreamMsg) {
fmt.Println(newMsg.Payload.Data.LiveMeasurement.Timestamp + " :: " + strconv.Itoa(newMsg.Payload.Data.LiveMeasurement.Power) + " Watt")
}
```
5 changes: 0 additions & 5 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func NewStream(id, token string) *Stream {
func (ts *Stream) StartSubscription(outputChan MsgChan) error {
// Connect
var err error
//err := errors.New("<TibberStream> Not connected")
for {
err := ts.connect()
if err != nil {
Expand Down Expand Up @@ -121,9 +120,6 @@ func (ts *Stream) StartSubscription(outputChan MsgChan) error {
case "subscription_data":
tm.HomeID = ts.ID
outputChan <- &tm
//tc.InboundMsgCh <- &tm
//log.Info("subscription_data")
//log.Info(tm.Payload.Data.LiveMeasurement.Power)
}
}
if !ts.isRunning {
Expand Down Expand Up @@ -175,7 +171,6 @@ func (ts *Stream) sendInitMsg() {
func (ts *Stream) sendSubMsg() {
homeID := ts.ID
//sub := `{"query":"subscription{\n liveMeasurement(homeId:\"` + homeID + `\"){\n timestamp\n power\n accumulatedConsumption\n accumulatedCost\n currency\n minPower\n averagePower\n maxPower\n }\n}\n","variables":null,"type":"subscription_start","id":0}`

sub := `{"query":"subscription{\n liveMeasurement(homeId:\"` + homeID + `\"){\n timestamp\n power\n }\n}\n","variables":null,"type":"subscription_start","id":0}`
ts.client.WriteMessage(websocket.TextMessage, []byte(sub))
}
2 changes: 0 additions & 2 deletions tibber.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const graphQlEndpoint = "https://api.tibber.com/v1-beta/gql"
type Client struct {
Token string
gqlClient *graphql.Client
streams map[string]*Stream
}

// NewClient init tibber client
Expand All @@ -25,7 +24,6 @@ func NewClient(token string) *Client {
tc := Client{
Token: token,
gqlClient: gql,
streams: make(map[string]*Stream),
}
return &tc
}

0 comments on commit 6b5fdd5

Please sign in to comment.