Created
January 5, 2022 19:41
-
-
Save peterhellberg/f0a66c6c8937674cfe09b37359eb7723 to your computer and use it in GitHub Desktop.
Tibber GraphQL Subscription from Go client
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
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"log" | |
"os" | |
"github.com/hasura/go-graphql-client" | |
) | |
func run(token, endpoint, homeID string) error { | |
params := map[string]interface{}{ | |
"token": token, | |
} | |
variables := map[string]interface{}{ | |
"homeId": homeID, | |
} | |
logger := log.New(os.Stdout, "", 0) | |
errLogger := log.New(os.Stderr, "", 0) | |
onErrorFunc := func(sc *graphql.SubscriptionClient, err error) error { | |
errLogger.Print("ERR", err) | |
return err | |
} | |
client := graphql.NewSubscriptionClient(endpoint). | |
WithConnectionParams(params). | |
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE). | |
WithLog(logger.Println). | |
OnError(onErrorFunc) | |
defer client.Close() | |
var sub struct { | |
LiveMeasurement struct { | |
Timestamp graphql.String | |
Power graphql.Float | |
LastMeterConsumption graphql.Float | |
AccumulatedConsumption graphql.Float | |
AccumulatedProduction graphql.Float | |
AccumulatedConsumptionLastHour graphql.Float | |
AccumulatedProductionLastHour graphql.Float | |
AccumulatedCost graphql.Float | |
AccumulatedReward graphql.Float | |
currency graphql.String | |
MinPower graphql.Float | |
AveragePower graphql.Float | |
MaxPower graphql.Float | |
PowerProduction graphql.Float | |
PowerReactive graphql.Float | |
PowerProductionReactive graphql.Float | |
MinPowerProduction graphql.Float | |
MaxPowerProduction graphql.Float | |
LastMeterProduction graphql.Float | |
PowerFactor graphql.Float | |
VoltagePhase1 graphql.Float | |
VoltagePhase2 graphql.Float | |
VoltagePhase3 graphql.Float | |
CurrentL1 graphql.Float | |
CurrentL2 graphql.Float | |
CurrentL3 graphql.Float | |
SignalStrength graphql.Int | |
} `graphql:"liveMeasurement(homeId: $homeId)"` | |
} | |
_, err := client.Subscribe(sub, variables, func(data *json.RawMessage, err error) error { | |
if err != nil { | |
return err | |
} | |
// Do something with the data, here I'm just logging it | |
logger.Println(string(*data)) | |
return nil | |
}) | |
if err != nil { | |
return err | |
} | |
return client.Run() | |
} | |
func main() { | |
var token, endpoint, homeID string | |
flag.StringVar(&token, "token", "476c477d8a039529478ebd690d35ddd80e3308ffc49b59c65b142321aee963a4", "API token") | |
flag.StringVar(&endpoint, "endpoint", "wss://api.tibber.com/v1-beta/gql/subscriptions", "GraphQL Subscriptions endpoint") | |
flag.StringVar(&homeID, "homeID", "cc83e83e-8cbf-4595-9bf7-c3cf192f7d9c", "Home ID") | |
flag.Parse() | |
if err := run(token, endpoint, homeID); err != nil { | |
log.Fatal(err) | |
} | |
} |
Author
peterhellberg
commented
Jan 5, 2022
Default flags are demo values copied from https://developer.tibber.com/explorer
You can find your own token at https://developer.tibber.com/settings/accesstoken
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment