-
Notifications
You must be signed in to change notification settings - Fork 0
/
98_SMAUtils.pm
469 lines (380 loc) · 12.8 KB
/
98_SMAUtils.pm
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#################################################################
#
# A module to plot inverter data from SMA - Solar Technology
#
# Originally written 2013 by Sven Koethe <sven at koethe-privat dot de>
#
# The modul uses SBFSpot for communication with the Inverter - Linux Tool
#
#################################################################
# Definition: define <name> SMAUtils <address> <path-to-sbfspot>
# Parameters:
# address - Specify the BT/IP-Address of your Inverter
# path-sbfspot - Specify path to SBFspot utility, can be either
# relative to FHEM Home or absolute
# Custom attributes:
# interval - Specify the delay time for update the readings
#
#################################################################
# Versions History
#
# 2.2 16.09.2016 SMAUtils_Attr added
# 2.1 16.09.2016 sub Define changed
# 2.0 06.09.2016 attribute mode
package main;
use strict;
use warnings;
use MIME::Base64;
use Blocking;
###################################
# Initialize module and define
# available end points and
# attributes
###################################
sub SMAUtils_Initialize($) {
my ($hash) = @_;
$hash->{DefFn} = "SMAUtils_Define";
$hash->{GetFn} = "SMAUtils_Get";
$hash->{UndefFn} = "SMAUtils_Undef";
$hash->{AttrFn} = "SMAUtils_Attr";
$hash->{AttrList} = "interval ". "timeout ". "disable:0,1 ". "suppressSleep:0,1 ". $readingFnAttributes;
}
###################################
# Module Definition
###################################
sub SMAUtils_Define($$) {
my ( $hash, $def ) = @_;
my $name = $hash->{NAME};
my @a = split( "[ \t][ \t]*", $def );
if ( int(@a) < 3 ) {
return "You need to specify more parameters.\n". "Format: define <name> SMAUtils <address> <path-to-sbfspot>";
}
my $address = $a[2];
$hash->{TOOLPATH} = $a[3];
$hash->{STATE} = "initialized";
unless (
( $address =~ /^\s*([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\s*$/ )
|| ( $address =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
&& ( ( $1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ) ) )
){
return "given address is not a valid bluetooth or IP address";
} else {
$hash->{ADDRESS} = $address;
}
return;
}
####################################
# Get a value in a non-blocking way
####################################
sub SMAUtils_Get($$) {
my ( $hash, @a ) = @_;
return "\"get X\" needs at least an argument" if ( @a < 2 );
my $name = shift @a;
my $opt = shift @a;
my $getlist = "Unknown argument $opt, choose one of " . "data:noArg ";
return "module is deactivated" if ( IsDisabled($name) );
if ( $opt eq "data" ) {
# "1" is status bit for manual query.
Inverter_CallData( $hash, 1 );
} else {
return "$getlist";
}
}
#####################################
# Check for valid attributes
#####################################
sub SMAUtils_Attr {
my ( $cmd, $name, $aName, $aVal ) = @_;
# $cmd can be "del" or "set"
# $name is device name
# aName and aVal are Attribute name and value
my $hash = $defs{$name};
if ($cmd eq "set") {
if ( $aName eq "interval" ) {
if ( !AttrVal( $name, "disable", undef )) {
readingsSingleUpdate( $hash, "interval", $aVal, 1 );
SMAUtils_DeleteCurrentInternalTimer($hash);
# "0" is call mode
InternalTimer( gettimeofday() + $aVal,"Inverter_CallData", $hash, 0 );
}
} elsif ( $aName eq "disable") {
if ($aVal eq "1") {
SMAUtils_DeleteCurrentInternalTimer($hash);
} else {
readingsSingleUpdate( $hash, "interval", $aVal, 1 );
InternalTimer( gettimeofday() + $aVal,"Inverter_CallData", $hash, 0 );
}
}
} elsif ($cmd eq "del") {
if ( $aName eq "interval") {
SMAUtils_DeleteCurrentInternalTimer($hash);
} elsif ( $aName eq "disable") {
my $interVal = AttrVal( $name, "interval", undef );
if ($interVal > 0) {
readingsSingleUpdate( $hash, "interval", $aVal, 1 );
InternalTimer( gettimeofday() + $interVal, "Inverter_CallData", $hash, 0 );
}
}
}
return undef;
}
#####################################
# Delete any current active timer
# data
#####################################
sub SMAUtils_DeleteCurrentInternalTimer($) {
my ($hash) = @_;
delete( $hash->{helper}{RUNNING_PID} )
if ( defined( $hash->{helper}{RUNNING_PID} ) );
RemoveInternalTimer($hash);
return undef;
}
#####################################
# Remove the module
#####################################
sub SMAUtils_Undef($$) {
my ( $hash, $arg ) = @_;
RemoveInternalTimer($hash);
BlockingKill( $hash->{helper}{RUNNING_PID} )
if ( defined( $hash->{helper}{RUNNING_PID} ) );
return undef;
}
#####################################
## Main loop BlockingCall
#####################################
sub Inverter_CallData($$) {
my ( $hash, $mode ) = @_;
my $name = $hash->{NAME};
my $timeout = AttrVal( $name, "timeout", 30 );
if ( !AttrVal( $name, "disable", undef ) ) {
if ( exists( $hash->{helper}{RUNNING_PID} ) ) {
Log3 $name, 1,"$hash->{NAME} - Error: another BlockingCall is already running, can't start BlockingCall Inverter_CallData";
}else {
$hash->{helper}{RUNNING_PID} =BlockingCall( "Get_Inverterdata", $name, "Parse_Inverterdata",$timeout, "ParseInverterdata_Aborted", $hash );
}
}else {
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, "state", "disabled" );
readingsBulkUpdate( $hash, "summary", "disabled" );
readingsEndUpdate( $hash, 1 );
Log3 $name, 5, "$hash->{NAME} - is currently disabled";
}
if ( AttrVal( $name, "interval", undef ) > 0 ) {
RemoveInternalTimer( $hash, "Inverter_CallData" );
InternalTimer( gettimeofday() + AttrVal( $name, "interval", undef ),"Inverter_CallData", $hash, 0 );
}
return;
}
#####################################
## Retrieve Inverter Data using
## SBFSpot
#####################################
sub Get_Inverterdata {
my ($name) = @_;
my $hash = $defs{$name};
my $data = "";
Log3 $name, 4, "$hash->{NAME} start BlockingCall Get_Inverterdata";
my $toolpath = $hash->{TOOLPATH};
my $finq = "";
if ( AttrVal( $name, "suppressSleep", "0" ) eq "1" ) {
$finq = " -finq ";
}
$data .= qx( $toolpath $finq -nocsv -nosql -v );
Log3 $name, 5,"$hash->{NAME} BlockingCall Get_Inverterdata Data returned:" . "\n". "$data";
# Data has to be returned in one row.
my $dataenc = encode_base64( $data, "" );
Log3 $name, 5,"$hash->{NAME} BlockingCall Get_Inverterdata Data Base64: $dataenc";
Log3 $name, 4, "$hash->{NAME} BlockingCall Get_Inverterdata finished";
return "$name|$dataenc";
}
######################################
## Parse Data from SBFspot and set
## readings
######################################
sub Parse_Inverterdata($) {
my ($string) = @_;
my @a = split( "\\|", $string );
my $hash = $defs{ $a[0] };
my $name = $hash->{NAME};
my $response = decode_base64( $a[1] );
Log3 $hash->{NAME}, 4,"$hash->{NAME} start BlockingCall Parse_Inverterdata";
my $err_log = '';
my %pos;
# parse retrieve data from inverter
my @lines = split /\n/, $response;
my $readingsname = '';
my $readingsvalue = '';
my $value = '';
my $substr = '';
readingsBeginUpdate($hash);
my $end = 0;
my $i = 0;
foreach my $line (@lines) {
if ( $i > 16 && $end != 1 ) {
$pos{$line} = index( $response, $line );
my @reading = split( ":", $line, 2 );
$readingsname = ltrim( $reading[0] );
$readingsname = rtrim($readingsname);
$readingsname = lc($readingsname);
$readingsname =~ s/ /_/g;
$readingsvalue = ltrim( $reading[1] );
$substr = 'freq';
if ( index( $readingsname, $substr ) != -1 ) {
$value = ltrim( $reading[1] );
$value =~ /(\d+(?:\.\d+)?)/;
$readingsvalue = $1;
}
$substr = 'phase';
if ( index( $readingsname, $substr ) != -1 ) {
my $linesreading = substr $readingsname, 0, 7;
my $linesvalue = substr $line, 8;
my @line_readings = split( "-", $linesvalue );
foreach my $line_readings (@line_readings) {
@reading = split( ":", $line_readings );
$readingsname = ltrim( $reading[0] );
$readingsname = $linesreading . "_" . $readingsname;
$readingsname = rtrim($readingsname);
$readingsname = lc($readingsname);
$readingsname =~ s/ /_/g;
$value = ltrim( $reading[1] );
$value =~ /(\d+(?:\.\d+)?)/;
$readingsvalue = $1;
readingsBulkUpdate( $hash, $readingsname, $readingsvalue );
}
}
$substr = 'total';
if ( index( $readingsname, $substr ) != -1 ) {
$value = ltrim( $reading[1] );
$value =~ /(\d+(?:\.\d+)?)/;
$readingsvalue = $1;
}
$substr = 'today';
if ( index( $readingsname, $substr ) != -1 ) {
$value = ltrim( $reading[1] );
$value =~ /(\d+(?:\.\d+)?)/;
$readingsvalue = $1;
}
$substr = 'string';
if ( index( $readingsname, $substr ) != -1 ) {
my $linesreading = substr $readingsname, 0, 8;
my $linesvalue = substr $line, 9;
my @line_readings = split( "-", $linesvalue );
foreach my $line_readings (@line_readings) {
@reading = split( ":", $line_readings );
$readingsname = ltrim( $reading[0] );
$readingsname = $linesreading . "_" . $readingsname;
$readingsname = rtrim($readingsname);
$readingsname = lc($readingsname);
$readingsname =~ s/ /_/g;
$value = ltrim( $reading[1] );
$value =~ /(\d+(?:\.\d+)?)/;
$readingsvalue = $1;
readingsBulkUpdate( $hash, $readingsname, $readingsvalue );
}
}
$substr = 'starttime';
if ( index( $readingsname, $substr ) == -1 ) {
readingsBulkUpdate( $hash, $readingsname, $readingsvalue );
}
$substr = 'Sleep';
if ( index( $line, $substr ) != -1 ) {
$end = 1;
}
}
$i++;
}
readingsBulkUpdate( $hash, "state", "active" );
readingsBulkUpdate( $hash, "summary", "enabled" );
readingsEndUpdate( $hash, 1 );
Log3 $hash->{NAME}, 4,"$hash->{NAME} BlockingCall Parse_Inverterdata finished";
delete( $hash->{helper}{RUNNING_PID} );
return;
}
#######################################
## Timeout BlockingCall
#######################################
sub ParseInverterdata_Aborted($) {
my ($hash) = @_;
Log3 $hash, 1, "$hash->{NAME} -> BlockingCall Get_Inverterdata timed out";
delete( $hash->{helper}{RUNNING_PID} );
}
1;
=pod
=item device
=item summary Collects data from SMA Inverters using SBFspot
=item summary_DE Sammelt Daten von SMA Invertern mittels SBFspot
=begin html
<a name="SMAUtils"></a>
<h3>SMAUtils</h3>
<ul>
Module for SMA Inverters for collcting data with the helper utility SBFspot.
<br><br>
<b>Define</b>
<ul>
<code>define <name> SMAUtils address path-to-sbfspot </code><br>
<br>
<address> either defines the bluetooth or ip address
<path-to-sbfspot> defines the path to the SBFspot utility including the filename.
Example for bluetooth^: <br>
<code>define myInverter SMAUtils 00:80:25:A5:10:93 /opt/fhem/SBFspot/SBFspot</code>
<br>
<br>
</ul>
<b>Attributes</b>
<ul><li>
<code>interval</code><br>
Defines (in seconds) how often the data from the inverter is requested. If this is undefined
or set to zero, no automatic updates are done.
</li><li>
<code>disable</code><br>
If the value is <code>1</code> the device is deactivated and esp. no automatic data
requests are performed.
</li><li>
<code>timeout</code><br>
Defines how many seconds will be waited for a response using SBFspot (default: 30 seconds).
</li><li>
<code>suppressSleep</code><br>
Defines if data should also be requested at night. SBFspot uses longitude and latitude parameters
in SBFspot.cfg to decide when it's night.
</li>
<br>
</ul></ul>
=end html
=begin html_DE
<a name="SMAUtils"></a>
<h3>SMAUtils</h3>
<ul>
Modul für SMA Inverter, um deren Daten mit dem zusätzlichen Hilfsprogramm SBFspot abzufragen.
<br><br>
<b>Define</b>
<ul>
<code>define <name> SMAUtils address path-to-sbfspot </code><br>
<br>
<address> gibt die Bluetooth oder IP-Adresse an.
<path-to-sbfspot> gibt den Pfad zum externen SBFspot inklusive Dateiname an.
Beispiel: <br>
<code>define myInverter SMAUtils 00:80:25:A5:10:93 /opt/fhem/SBFspot/SBFspot</code>
<br>
<br>
</ul>
<b>Attribute</b>
<ul><li>
<code>interval</code><br>
Gibt an (in Sekunden), wie häufig die Werte vom Inverter abgefragt werden sollen. Ist der Wert 0 oder
undefiniert, werden die Werte nicht automatisch aktualisiert
</li><li>
<code>disable</code><br>
Beim Wert <code>1</code> ist das device abgeschaltet und es werden insbes. keine automatischen
Datenabrufe durchgeführt.
</li><li>
<code>timeout</code><br>
Gibt an, wieviel Sekunden auf Antwort mittels SBFspot gewartet wird (Standardwert: 30 Sekunden).
</li><li>
<code>suppressSleep</code><br>
Gibt an, ob auch im Dunkeln Daten von SBFspot geholt werden sollen. SBFspot entscheidet anhand der longitude und
latitude in der SBFspot.cfg wann Tag und wann Nacht ist.
</li>
<br>
</ul></ul>
=end html_DE
=cut