Skip to content

Commit

Permalink
add live-feed-timeout argument, fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
terjesannum committed Dec 4, 2023
1 parent 8ac7b3b commit 806e187
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion charts/tibber-exporter/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v2
name: tibber-exporter
version: 3.4.0
version: 3.5.0
description: Tibber exporter
type: application
keywords:
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
var (
token string
homesQuery tibber.HomesQuery
liveFeedTimeout int
liveUrl string
liveMeasurements stringArgs
disableLiveMeasurements stringArgs
Expand Down Expand Up @@ -49,6 +50,7 @@ func (sa *stringArgs) Set(s string) error {

func init() {
flag.StringVar(&token, "token", os.Getenv("TIBBER_TOKEN"), "Tibber API token")
flag.IntVar(&liveFeedTimeout, "live-feed-timeout", 1, "Timeout in minutes for live feed")
flag.StringVar(&liveUrl, "live-url", "", "Override websocket url for live measurements")
flag.Var(&liveMeasurements, "live", "Ids of homes to always start live measurements")
flag.Var(&disableLiveMeasurements, "disable-live", "Ids of homes to disable live measurements")
Expand Down Expand Up @@ -132,9 +134,11 @@ func main() {
log.Printf("Realtime consumption enabled for %v: %v\n", s.Id, s.Features.RealTimeConsumptionEnabled)
if (s.Features.RealTimeConsumptionEnabled || slices.Contains(liveMeasurements, string(s.Id))) && !slices.Contains(disableLiveMeasurements, string(s.Id)) {
log.Printf("Starting live measurements monitoring of home %v\n", s.Id)
log.Printf("Live feed timeout: %v minute\n", liveFeedTimeout)
go h.SubscribeMeasurements(ctx, hc, wsUrl, token)
prometheus.MustRegister(metrics.NewMeasurementCollector(string(s.Id), &h.Measurements.LiveMeasurement, &h.TimestampedValues, &h.GaugeValues))
started = append(started, string(s.Id))
h.Measurements.LiveMeasurement.Timestamp = time.Now()
} else {
log.Printf("Live measurements not available for home %v\n", s.Id)
}
Expand All @@ -153,7 +157,7 @@ func main() {
h.UpdatePrevious(ctx, client, tibber.ResolutionDaily)
if slices.Contains(started, string(h.Id)) {
timeDiff := time.Now().Sub(h.Measurements.LiveMeasurement.Timestamp)
if timeDiff.Minutes() > 1 {
if timeDiff.Minutes() > float64(liveFeedTimeout) {
exit(fmt.Sprintf("No measurements received for home %s since %s. Exiting...\n", h.Id, h.Measurements.LiveMeasurement.Timestamp))
}
}
Expand Down

0 comments on commit 806e187

Please sign in to comment.