-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouLessDataLogger.psm1
More file actions
325 lines (257 loc) · 7.82 KB
/
YouLessDataLogger.psm1
File metadata and controls
325 lines (257 loc) · 7.82 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<#
Author: Harald Reisinger
Version: 0.1
Version History: initial release
Purpose: Querying a YouLess-EnergyMonitor DataLogger from Powershell
#>
function Get-YouLessLS110StatusInfo
{
<#
.SYNOPSIS
Gets you a basic status info about your LS110 Energy-Monitor
.DESCRIPTION
This command calls the YouLess LS110 Energy-Monitor an gets some basis status
info. It calls the YouLess Energy-Monitor via an http-call to get the basic
status info and transform it into a better readable Object
.PARAMETER DeviceAddress
Specifies the address to query in IP4-Format. Wildcards are not permitted.
This parameter is required.
.EXAMPLE
Get-YouLessStatusInfo -DeviceAddress "192.168.0.1"
This command sends a status request to the YouLess LS110 EnergyMonitor at IP-Address
192.168.0.1 and returns a basic status-info if it succeeds
.INPUTS
System.String
You can pipe a string that contains the address of the device to query.
.OUTPUTS
System.Management.Automation.PSCustomObject
Return the an Object, which contains the Status of your Energy-Monitor in its Properties
.LINK
Get-YouLessLS110HistoricalData
Get-YouLessLS110Measurements
#>
param
(
[Parameter(ValueFromPipeline, Mandatory, HelpMessage = 'The address to query in IP4-Format')]
[string]
$DeviceAddress
)
Invoke-RestMethod -Uri ('http://{0}/a?f=j' -f $DeviceAddress) -Method Get `
| Select-Object -Property `
@{
Name = 'Counter (kWh)'
Expression = {
$_.cnt
}
},
@{
Name = 'Reflection Level'
Expression = {
$_.lvl
}
},
@{
Name = 'Deviation of Reflection'
Expression = {
$_.dev.replace('±', '±')
}
},
@{
Name = 'Raw 10bit Reflection Level'
Expression = {
$_.raw
}
},
@{
Name = 'Power (W)'
Expression = {
$_.pwr
}
},
@{
Name = 'Upload'
Expression = {
$_.con
}
},
@{
Name = 'Next Status Update'
Expression = {
$_.sts
}
},
'det'
}
function Get-YouLessLS110HistoricalData
{
<#
.SYNOPSIS
Retrieves Historical Data from the Memory of your YouLess-energy-monitor
.DESCRIPTION
Add a more complete description of what the function does.
.PARAMETER DeviceAddress
Specifies the address to query in IP4-Format. Wildcards are not permitted.
This parameter is required.
.PARAMETER Range
With this parameter you decide, for what time period you want to fetch the
measurements from the device.
.PARAMETER Offset
TODO
.EXAMPLE
Get-YouLessHistoricalData -DeviceAddress Value -Range Value
Describe what this call does
.NOTES
Place additional notes here.
.INPUTS
System.String
You can pipe a string that contains the address of the device to query.
.OUTPUTS
List of output types produced by this function.
#>
param
(
[Parameter(Position = 0,HelpMessage = 'The address to query in IP4-Format',Mandatory,ValueFromPipeline)]
[String]$DeviceAddress,
[parameter(Position = 1, Mandatory,HelpMessage = 'Add help message for user')]
[ValidateSet('Hour','Day','Week','Year')]
[String]$Range
)
dynamicparam {
$attributes = New-Object -TypeName System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = '__AllParameterSets'
$attributes.Mandatory = $true
$attributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$values = $null
switch ($Range) {
'Hour' { $values = @(1..2) }
'Day' { $values = @(1..3) }
'Week' { $values = @(0..6) }
'Year' { $values = @(1..12) }
}
$ValidateSet = New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList ($values)
$attributeCollection.Add($ValidateSet)
$dynParam1 = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList ('Offset', [int], $attributeCollection)
$paramDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('Offset', $dynParam1)
return $paramDictionary
}
begin {
$_offset = $PSBoundParameters.Offset
$_lookuptable = @{
'Year' = 'm'
'Week' = 'd'
'Day' = 'w'
'Hour' = 'h'
}
}
process {
$uri = ('http://{0}/V?{1}={2}&f=j' -f $DeviceAddress, $_lookuptable[$Range], $_offset)
Write-Verbose -Message ('Querying URL: {0}' -f $uri)
Invoke-RestMethod -Uri $uri -Method Get
}
}
function Get-YouLessLS110Measurements
{
<#
.SYNOPSIS
Describe purpose of "Get-YouLessMeasurements" in 1-2 sentences.
.DESCRIPTION
Add a more complete description of what the function does.
.PARAMETER DeviceAddress
Specifies the address to query in IP4-Format. Wildcards are not permitted.
This parameter is required.
.PARAMETER Range
Describe parameter -Range.
.EXAMPLE
Get-YouLessMeasurements -DeviceAddress Value -Range Value
Describe what this call does
.NOTES
Place additional notes here.
.LINK
URLs to related sites
The first link is opened by Get-Help -Online Get-YouLessMeasurements
.INPUTS
List of input types that are accepted by this function.
.OUTPUTS
List of output types produced by this function.
#>
param(
[Parameter(Position = 0,HelpMessage = 'Add help message for user',Mandatory,ValueFromPipeline)]
[String]$DeviceAddress,
[parameter(Position = 1,HelpMessage = 'Add help message for user', Mandatory)]
[ValidateSet('Hour','Day','Week','Year')]
[String]$Range
)
$_range = $null
switch ($Range) {
'Hour'
{
$_range = @(2..1)
}
'Day'
{
$_range = @(3..1)
}
'Week'
{
$_range = @(6..0)
}
'Year'
{
$_range = @(12..1)
}
}
$_range | ForEach-Object -Process {
Get-YouLessLS110HistoricalData -DeviceAddress $DeviceAddress -Range $Range -Offset $_ `
| Select-YouLessMeasurements
}
}
function Select-YouLessMeasurements
{
<#
.SYNOPSIS
Converts the JSON-Object-Structure returned from the YouLess-Energy-Monitor into Powershell-Objects
for easier handling
.DESCRIPTION
Add a more complete description of what the function does.
.PARAMETER RawResponse
Describe parameter -InputObject.
.EXAMPLE
Select-YouLessMeasurements -InputObject Value -StartTime Value -Unit Value -StepSize Value
Describe what this call does
.NOTES
Place additional notes here.
.LINK
URLs to related sites
The first link is opened by Get-Help -Online Select-YouLessMeasurements
.INPUTS
List of input types that are accepted by this function.
.OUTPUTS
List of output types produced by this function.
#>
param
(
[PSObject]
[Parameter(Mandatory, ValueFromPipeline, HelpMessage = 'Data to process')]
$RawResponse
)
begin {
$_formatProvider = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList 'de-AT'
}
process
{
$StartTime = Get-Date -Date ($RawResponse.tm)
$i = 0
$RawResponse.val | Select-Object -SkipLast 1 | ForEach-Object {
$converted = [convert]::ToDouble($_, $_formatProvider)
$props = @{
DateTime = $StartTime.AddSeconds($i)
Value = $converted
Unit = $RawResponse.un
}
$i = $i + $RawResponse.dt
[PSCustomObject]$props
}
}
}