-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tibber-live.ps1
77 lines (67 loc) · 2.5 KB
/
tibber-live.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Requires -Modules @{ ModuleName = 'PSTibber'; ModuleVersion = '0.6.0'; MaximumVersion = '0.6.99' }
[CmdletBinding()]
param (
[DateTime] $Until = [DateTime]::MinValue,
[int] $ReadTimeout = 30,
[switch] $Publish,
[string] $TimeZone = [TimeZoneInfo]::Local.Id
)
if ($Until -ne [DateTime]::MinValue -And $Until -le [DateTime]::Now) {
Write-Host "##[section]Time provided is in the past, returning..." -ForegroundColor Green
return
}
# Publish to Graphite
$global:fields = @(
'power'
'accumulatedConsumption'
'accumulatedConsumptionLastHour'
'accumulatedCost'
'powerProduction'
'voltagePhase1'
'voltagePhase2'
'voltagePhase3'
'currentL1'
'currentL2'
'currentL3'
)
# Import required modules
Import-Module -Name PSTibber -Force -PassThru
Import-Module -Name PSGraphite -Force -PassThru
Import-Module -Name $PSScriptRoot\tibber-pulse.psd1 -Force -PassThru
# Set log verbosity
$dbgpref = $global:DebugPreference
$vrbpref = $global:VerbosePreference
$global:DebugPreference = $DebugPreference
$global:VerbosePreference = $VerbosePreference
# Get the home Id
$homeId = Get-HomeId
# Connect WebSocket and register a subscription
$connection = Connect-TibberWebSocket -HomeId $homeId
$subscription = Register-TibberLiveMeasurementSubscription -Connection $connection -Fields ('timestamp', $global:fields, 'signalStrength')
Write-Host "New GraphQL subscription created: $($subscription.Id)"
# Read data stream
$splat = @{
Connection = $connection
Callback = ${function:Send-LiveMetricsToGraphite}
CallbackArgumentList = @(
$Publish.IsPresent
$TimeZone
)
TimeoutInSeconds = $ReadTimeout
}
if ($Until -ne [DateTime]::MinValue) {
$splat += @{
ReadUntil = $Until
}
$time = ([TimeZoneInfo]::ConvertTime([DateTime]::Parse($Until, [CultureInfo]::InvariantCulture), [TimeZoneInfo]::FindSystemTimeZoneById($TimeZone))).ToString('yyyy-MM-dd HH:mm:ss')
Write-Host "Reading metrics until $time ($TimeZone):"
}
$result = Read-TibberWebSocket @splat
Write-Host "Read $($result.NumberOfPackages) package(s) in $($result.ElapsedTimeInSeconds) seconds"
# Unregister subscription and close down the WebSocket connection
Unregister-TibberLiveMeasurementSubscription -Connection $connection -Subscription $subscription
Write-Host "GraphQL subscription stopped: $($subscription.Id)"
Disconnect-TibberWebSocket -Connection $connection
# Reset log verbosity
$global:DebugPreference = $dbgpref
$global:VerbosePreference = $vrbpref