forked from tskaard/tibber-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
9 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
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") | ||
} | ||
``` |
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