/
70_KEBA.pm
443 lines (295 loc) · 8.86 KB
/
70_KEBA.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
##############################################################################
#
# 70_KEBA.pm
#
# This fhem module allows sending/receiving messages to/from a
# KEBA KeContact P20 Wallbox (c-Series with Ethernet option only).
#
# This wallbox is intended to charge Electric Vehicles
#
# written 2015 by Marcus Schlappa <mschlappa at gmx dot de>
#
# Version = 1.3 26.02.2020
#
##############################################################################
package main;
use strict;
use warnings;
use JSON;
use Scalar::Util qw(looks_like_number);
my %KEBA_gets = (
"info" => "i",
"update" => "report"
);
my %KEBA_sets = (
"enableState" => "ena",
"current" => "curr",
"outputX2" => "output"
);
my %KEBA_state = (
0 => "starting",
1 => "not ready for charging",
2 => "ready for charging",
3 => "charging",
4 => "error",
5 => "authorization rejected"
);
my %KEBA_plug = (
0 => "unplugged",
1 => "plugged on wallbox",
3 => "plugged on wallbox, locked",
5 => "plugged on wallbox and ev",
7 => "plugged on wallbox and ev, locked"
);
my %KEBA_enablestate = (
0 => "disabled",
1 => "enabled"
);
my %KEBA_output = (
0 => "open",
1 => "closed"
);
sub KEBA_Initialize($) {
my ($hash) = @_;
$hash->{DefFn} = 'KEBA_Define';
$hash->{UndefFn} = 'KEBA_Undef';
$hash->{SetFn} = 'KEBA_Set';
$hash->{GetFn} = 'KEBA_Get';
$hash->{ReadFn} = 'KEBA_Read';
$hash->{AttrList} = $readingFnAttributes;
}
sub KEBA_Define($$) {
my ($hash, $def) = @_;
my @param = split('[ \t]+', $def);
if(int(@param) != 5) {
return "KEBA_Define: number of arguments incorrect. Usage:\n" .
"define <name> KEBA <host> <port> <interval>";
}
$hash->{Host} = $param[2];
$hash->{Port} = $param[3];
$hash->{Interval} = $param[4];
if (!looks_like_number($hash->{Interval})){
return "interval must be a number";
}
if ($hash->{Interval} < 10 || $hash->{Interval} > 900){
return "The value for interval must be between 10 and 900";
}
Log3 $hash, 3, "$hash->{NAME} will read from KEBA at $hash->{Host}:$hash->{Port} with Interval of $hash->{Interval} seconds" ;
KEBA_connect($hash);
InternalTimer(gettimeofday()+$hash->{Interval}, "KEBA_GetUpdate", $hash, 0);
}
sub KEBA_Undef($$)
{
my ( $hash, $arg ) = @_;
my $socket = $hash->{CD};
if ($socket) {close($socket);}
RemoveInternalTimer($hash);
return undef;
}
sub KEBA_connect($){
my ($hash) = @_;
my $name = $hash->{NAME};
my $ip = $hash->{Host};
my $port = $hash->{Port};
my $socket = IO::Socket::INET->new(Proto => 'udp', LocalPort => $port);
if($socket) {
$hash->{STATE} = "Listening";
$hash->{LAST_CONNECT} = FmtDateTime( gettimeofday() );
$hash->{FD} = $socket->fileno();
$hash->{CD} = $socket; # sysread / close won't work on fileno
$hash->{CONNECTS}++;
$selectlist{$name} = $hash;
Log3 $name, 3, "listening for $name on $hash->{Port}";
}else{
Log3 $name, 3, "socket could not be created!";
}
}
sub KEBA_sendCommand($)
{
my ($hash) = @_;
my $name = $hash->{NAME};
my $ip = $hash->{Host};
my $port = $hash->{Port};
my $socket = $hash->{CD};
my $commandStack;
my $command;
if (defined ($hash->{Command})){
$commandStack = $hash->{Command};
my @a = split(/#/,$commandStack,2);
$command = $a[0];
$commandStack = $a[1];
$hash->{Command} = $commandStack;
} else{
Log 3, "KEBA sendCommand: No Command to send";
return;
}
my $response;
if (!(defined $command)){
return;
}
Log 3, "$name Sending command: ".$command;
my $ipaddr = inet_aton($ip);
my $destaddr = sockaddr_in($port, $ipaddr);
send($socket, $command, 0, $destaddr);
Log 3, "$name Command was sent";
}
sub KEBA_Read($){
my ($hash) = @_;
my $socket = $hash->{CD};
my $response;
$socket->recv($response,512);
Log 3, "Message received";
Log 3, "Data: $response";
$hash->{LAST_CONNECT} = FmtDateTime( gettimeofday() );
my $first = substr($response,0,1);
if ($first ne "{"){
$hash->{LAST_MESSAGE} = $response;
Log 3, "received message: $response";
return;
}
my $decoded = decode_json($response);
readingsBeginUpdate($hash);
foreach my $key (keys %{$decoded}){
my $value = $decoded->{$key};
$key =~ tr/ //d;
if ($key eq "State"){
$value = $KEBA_state{$value};
}elsif ($key eq "Plug"){
$value = $KEBA_plug{$value};
}elsif ($key eq "Enablesys" || $key eq "Enableuser"){
$value = $KEBA_enablestate{$value};
}elsif ($key eq "Output"){
$value = $KEBA_output{$value};
}else {
Log 4, "KEBA: No Mapping found: $key";
}
readingsBulkUpdate($hash, $key, $value);
}
readingsEndUpdate($hash, 1);
#send futher command if exists in command Queue
KEBA_sendCommand($hash);
}
sub KEBA_Get($@) {
my ($hash, @param) = @_;
return '"get KEBA" needs at least one argument' if (int(@param) < 2);
return '"get KEBA" has no parameter' if (int(@param) > 2);
my $name = shift @param;
my $opt = shift @param;
return '"get KEBA $opt" has no parameter' if (int(@param) > 2);
if(!$KEBA_gets{$opt}) {
my @cList = keys %KEBA_gets;
return "Unknown argument $opt, choose one of " . join(" ", @cList);
}
my $cmd = $KEBA_gets{$opt};
# get Commands in Queue
my $commandStack;
if (defined ($hash->{Command})){
$commandStack = $hash->{Command};
}
if ($opt eq "update"){
# prepare new commands
my $commands = "$cmd 1#$cmd 2#$cmd 3#";
# add new commands
$commandStack = $commandStack.$commands;
$hash->{Command} = $commandStack;
KEBA_sendCommand($hash);
}elsif($opt eq "info"){
# prepare new command
my $commands = "$cmd";
# add new commands
$commandStack = join("#",$commands);
# write Command-Queue back to hash
$hash->{Command} = $commandStack;
KEBA_sendCommand($hash);
}else{
Log 3, "Unknown command: $cmd";
}
}
sub KEBA_Set($@) {
my ($hash, @param) = @_;
#return 'set KEBA needs at least one argument and option' if (int(@param) < 3);
my $name = shift @param;
my $opt = shift @param;
my $value = join("", @param);
if(!defined($KEBA_sets{$opt})) {
my @cList = keys %KEBA_sets;
return "Unknown argument $opt, choose one of " . join(" ", @cList);
}
if (!looks_like_number($value)){
return "Parameter must be a number";
}
if ($opt eq "current" && ($value < 6000 || $value > 63000)){
return "The value for current must be between 6000 and 63000";
}elsif ($opt eq "enableState" && ($value < 0 || $value > 1)){
return "The value for enableState must be 0 (disable) or 1 (enable)";
}elsif ($opt eq "outputX2" && ($value < 0 || $value > 150)){
return "The value for outputX2 must be\n 0 (open)\n 1 (closed)\n between 10 and 150 (Pulse output with the specified number of pulses (pulses / kWh))";
}
my $cmd = $KEBA_sets{$opt}." ".$value;
# get Commands in Queue
my $commandStack;
if (defined ($hash->{Command})){
$commandStack = $hash->{Command};
}
# prepare new command
my $commands = $cmd;
# add new commands
$commandStack = join("#",$commands);
# write Command-Queue back to hash
$hash->{Command} = $commandStack;
$hash->{STATE} = $cmd;
Log 3 , "setCmd: $cmd";
KEBA_sendCommand($hash);
}
sub KEBA_GetUpdate($$) {
my ($hash) = @_;
# start internal timer; do it at the beginning to achieve equal intervals no matter how long it takes to gather data
InternalTimer(gettimeofday()+$hash->{Interval}, "KEBA_GetUpdate", $hash, 1);
# gather data
Log 3, "Start Update";
# get Commands in Queue
my $commandStack;
# prepare new commands
my $commands = "report 1#report 2#report 3#";
# add new commands
$commandStack = $commandStack.$commands;
$hash->{Command} = $commandStack;
KEBA_sendCommand($hash);
}
1;
=pod
=item device
=item summary Controls Keba P20 Wallbox for Charging EVs
=begin html
<a name="KEBA"></a>
<h3>KEBA</h3>
<ul>
This module sends and receives messages/commands to P20 wallbox from KEBA with ethernet option.
<br><br>
<a name="KEBAdefine"></a>
<b>Define</b>
<ul>
<code>define <name> KEBA <ip> <port> <refreshIntervalSeconds> </code>
<br><br>
Example: <code>define myKeba KEBA 192.168.1.10 7090 60</code><br>
</ul>
<br>
<a name="KEBAset"></a>
<b>Set</b><br>
<ul>
<code>set <name> <option></code>
<br><br>
You can enable or disable the wallbox and set the current with <i>set</i> command.
</ul>
<br>
<a name="KEBAget"></a>
<b>Get</b><br>
<ul>
<code>get <name> <option></code>
<br><br>
You can <i>get</i> the status of the wallbox via 'info' and 'update' (no other parameter required)
</ul>
<br>
</ul>
=end html
=cut