-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path73_AMADCommBridge.pm
executable file
·1499 lines (1201 loc) · 49.9 KB
/
73_AMADCommBridge.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# Developed with Kate
#
# (c) 2015-2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
# All rights reserved
#
# Special thanks goes to comitters:
# - Jens Wohlgemuth Thanks for Commandref
# - Schlimbo Tasker integration and Tasker Commandref
#
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# $Id$
#
###############################################################################
##
##
## Das JSON Modul immer in einem eval aufrufen
# $data = eval{decode_json($data)};
#
# if($@){
# Log3($SELF, 2, "$TYPE ($SELF) - error while request: $@");
#
# readingsSingleUpdate($hash, "state", "error", 1);
#
# return;
# }
#
#
###### Möglicher Aufbau eines JSON Strings für die AMADCommBridge
#
# first initial String
# {"amad": {"amad_id": "1495827100156","fhemcmd": "setreading"},"firstrun": {"fhemdevice": "TabletWohnzimmer","fhemserverip": "fhem02.tuxnet.local","amaddevice_ip": "10.6.9.35"}}
# {"amad": {"amad_id": "1495827100156","fhemcmd": "setreading"},"firstrun": {"fhemdevice": "TabletWohnzimmer","fhemserverip": "fhem02.tuxnet.local","amaddevice_ip": "10.6.9.35"}}
#
# default String
# {"amad": {"amad_id": "37836534","fhemcmd": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}}
# Aufruf zum testens
# curl --data '{"amad": {"amad_id": "37836534","fhemcmd": "setreading"},"payload": {"reading0": "value0","reading1": "value1","readingX": "valueX"}}' localhost:8090
#
#
##
##
package FHEM::AMADCommBridge;
use strict;
use warnings;
use POSIX;
use FHEM::Meta;
use GPUtils qw(GP_Import GP_Export);
use HttpUtils;
use TcpServerUtils;
my $missingModul = '';
eval "use Encode qw(encode encode_utf8);1" or $missingModul .= 'Encode ';
# try to use JSON::MaybeXS wrapper
# for chance of better performance + open code
eval {
require JSON::MaybeXS;
import JSON::MaybeXS qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# try to use JSON wrapper
# for chance of better performance
eval {
# JSON preference order
local $ENV{PERL_JSON_BACKEND} =
'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP'
unless ( defined( $ENV{PERL_JSON_BACKEND} ) );
require JSON;
import JSON qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# In rare cases, Cpanel::JSON::XS may
# be installed but JSON|JSON::MaybeXS not ...
eval {
require Cpanel::JSON::XS;
import Cpanel::JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# In rare cases, JSON::XS may
# be installed but JSON not ...
eval {
require JSON::XS;
import JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to built-in JSON which SHOULD
# be available since 5.014 ...
eval {
require JSON::PP;
import JSON::PP qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to JSON::backportPP in really rare cases
require JSON::backportPP;
import JSON::backportPP qw(decode_json encode_json);
1;
}
}
}
}
}
## Import der FHEM Funktionen
#-- Run before package compilation
BEGIN {
# Import from main context
GP_Import(
qw(readingsSingleUpdate
readingsBulkUpdate
readingsBeginUpdate
readingsEndUpdate
defs
modules
Log3
CommandAttr
CommandDelete
CommandSet
readingFnAttributes
attr
AttrVal
ReadingsVal
init_done
urlEncode
Dispatch
HttpUtils_NonblockingGet
TcpServer_Open
TcpServer_Close
TcpServer_Accept
AnalyzeCommandChain
AnalyzePerlCommand)
);
}
#-- Export to main context with different name
GP_Export(
qw(
Initialize
)
);
sub Initialize($) {
my ($hash) = @_;
# Provider
$hash->{ReadFn} = 'FHEM::AMADCommBridge::Read';
$hash->{WriteFn} = 'FHEM::AMADCommBridge::Write';
$hash->{Clients} = ':AMADDevice:';
$hash->{MatchList} = { "1:AMADDevice" => '{"amad": \{"amad_id":.+}}' };
# Consumer
$hash->{SetFn} = 'FHEM::AMADCommBridge::Set';
$hash->{DefFn} = 'FHEM::AMADCommBridge::Define';
$hash->{UndefFn} = 'FHEM::AMADCommBridge::Undef';
$hash->{AttrFn} = 'FHEM::AMADCommBridge::Attr';
$hash->{AttrList} =
'fhemControlMode:trigger,setControl,thirdPartControl '
. 'debugJSON:0,1 '
. 'enableSubCalls:0,1 '
. 'disable:1 '
. 'allowfrom '
. 'fhemServerIP '
. $readingFnAttributes;
return FHEM::Meta::InitMod( __FILE__, $hash );
}
sub Define($$) {
my ( $hash, $def ) = @_;
my @a = split( '[ \t][ \t]*', $def );
return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.44; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
return 'too few parameters: define <name> AMADCommBridge <tcp-port>'
if ( @a < 2 and @a > 3 );
return
'Cannot define a AMADCommBridge device. Perl modul '
. $missingModul
. ' is missing.'
if ($missingModul);
my $name = $a[0];
my $port;
$port = $a[2] if ( $a[2] );
$port = 8090 if ( not defined($port) and ( !$port ) );
$hash->{BRIDGE} = 1;
$hash->{PORT} = $port;
$hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{VERSIONFLOWSET} = FHEM::Meta::Get( $hash, 'x_flowsetversion' );
CommandAttr( undef, $name . ' room AMAD' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
Log3( $name, 3,
"AMADCommBridge ($name) - defined AMADCommBridge with Socketport $port"
);
Open($hash);
$modules{AMADCommBridge}{defptr}{BRIDGE} = $hash;
return undef;
}
sub Undef($$) {
my ( $hash, $arg ) = @_;
TcpServer_Close($hash);
delete $modules{AMADCommBridge}{defptr}{BRIDGE}
if ( defined( $modules{AMADCommBridge}{defptr}{BRIDGE} )
and $hash->{BRIDGE} );
return undef;
}
sub Attr(@) {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $orig = $attrVal;
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' ) {
if ( $attrVal == 0 ) {
readingsSingleUpdate( $hash, 'state', 'enabled', 1 );
Open($hash);
Log3( $name, 3, "AMADCommBridge ($name) - enabled" );
}
else {
Close($hash);
readingsSingleUpdate( $hash, 'state', 'disabled', 1 )
if ( not defined( $hash->{FD} ) );
Log3( $name, 3, "AMADCommBridge ($name) - disabled" );
}
}
else {
readingsSingleUpdate( $hash, 'state', 'enabled', 1 );
Open($hash);
Log3( $name, 3, "AMADCommBridge ($name) - enabled" );
}
}
elsif ( $attrName eq 'fhemControlMode' ) {
if ( $cmd eq 'set' ) {
CommandSet( undef,
'set TYPE=AMADDevice:FILTER=deviceState=online statusRequest' );
Log3( $name, 3,
"AMADCommBridge ($name) - set fhemControlMode global Variable at Device"
);
}
else {
CommandSet( undef,
'set TYPE=AMADDevice:FILTER=deviceState=online statusRequest' );
Log3( $name, 3,
"AMADCommBridge ($name) - set fhemControlMode global Variable NONE at Device"
);
}
}
return undef;
}
sub Set($@) {
my ( $hash, $name, $cmd, @args ) = @_;
my ( $arg, @params ) = @args;
if ( $cmd eq 'open' ) {
Open($hash);
}
elsif ( $cmd eq 'close' ) {
Close($hash);
}
else {
my $list = 'open:noArg close:noArg';
return "Unknown argument $cmd, choose one of $list";
}
}
sub Write($@) {
my ( $hash, $amad_id, $uri, $path, $header, $method ) = @_;
my $name = $hash->{NAME};
my $dhash = $modules{AMADDevice}{defptr}{$amad_id};
my $param;
my $remoteServer = AttrVal( $dhash->{NAME}, 'remoteServer', 'Automagic' );
Log3( $name, 4, "AMADCommBridge ($name) - Write Path: $path" );
if ( $remoteServer ne 'Automagic' and $path =~ /\?/ ) {
$path .= '&amad_id=' . $amad_id;
}
elsif ( $remoteServer ne 'Automagic' ) {
$path .= '?amad_id=' . $amad_id;
}
return readingsSingleUpdate( $dhash, 'lastSetCommand', $path, 1 )
if ( $remoteServer eq 'other' );
$param = {
url => 'http://' . $uri . $path,
timeout => 15,
hash => $hash,
amad_id => $amad_id,
method => $method,
header => $header . "\r\namadid: $amad_id",
doTrigger => 1,
callback => \&ErrorHandling
}
if ( $remoteServer eq 'Automagic' );
$param = {
url => 'http://' . $uri . '/',
data =>
"{\"message\":\"AMAD=:=$path\", \"sender\":\"AMAD\", \"ttl\":60, \"communication_base_params\":{\"type\":\"Message\", \"fallback\":false, \"via\":\"Wifi\"},\"version\":\"1.62\"}",
timeout => 15,
hash => $hash,
amad_id => $amad_id,
method => $method,
header =>
"agent: TeleHeater/2.2.3\r\nUser-Agent: TeleHeater/2.2.3\r\nAccept: application/json",
doTrigger => 1,
callback => \&ErrorHandling
}
if ( $remoteServer eq 'Autoremote' );
$param = {
url => 'http://' . $uri . '/',
data => 'device=AMAD&cmd=' . urlEncode($path),
timeout => 15,
hash => $hash,
amad_id => $amad_id,
method => $method,
header =>
"agent: TeleHeater/2.2.3\r\nUser-Agent: TeleHeater/2.2.3\r\nAccept: application/json",
doTrigger => 1,
callback => \&ErrorHandling
}
if ( $remoteServer eq 'TNES' );
my $logtext =
"AMADCommBridge ($name) - Send with remoteServer: $remoteServer URL: $param->{url}, HEADER: $param->{header}, METHOD: $method";
$logtext .= ", DATA: $param->{data}" if ( $remoteServer ne 'Automagic' );
Log3( $name, 5, "$logtext" );
HttpUtils_NonblockingGet($param) if ( defined($param) );
}
sub ErrorHandling($$$) {
my ( $param, $err, $data ) = @_;
my $hash = $param->{hash};
my $dhash = $modules{AMADDevice}{defptr}{ $param->{'amad_id'} };
my $dname = $dhash->{NAME};
if ( $param->{method} eq 'GET' ) {
### Begin Error Handling
if ( $dhash->{helper}{infoErrorCounter} > 0 ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'lastStatusRequestState',
'statusRequest_error', 1 );
if ( ReadingsVal( $dname, 'flow_Informations', 'active' ) eq
'inactive'
&& ReadingsVal( $dname, 'flow_SetCommands', 'active' ) eq
'inactive' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"
);
readingsBulkUpdate( $dhash, 'deviceState', 'offline', 1 );
readingsBulkUpdate( $dhash, 'state',
'AMAD Flows inactive, device set offline', 1 );
}
elsif ($dhash->{helper}{infoErrorCounter} > 7
&& $dhash->{helper}{setCmdErrorCounter} > 4 )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"
);
$attr{$dname}{disable} = 1;
readingsBulkUpdate( $dhash, 'state',
'Unknown Error, device disabled', 1 );
$dhash->{helper}{infoErrorCounter} = 0;
$dhash->{helper}{setCmdErrorCounter} = 0;
return;
}
elsif ( ReadingsVal( $dname, 'flow_Informations', 'active' ) eq
'inactive' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: Informations Flow on your Device is inactive, will try to reactivate"
);
}
elsif ( $dhash->{helper}{infoErrorCounter} > 7 ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"
);
readingsBulkUpdate( $dhash, 'deviceState', 'offline', 1 );
readingsBulkUpdate( $dhash, 'state',
'To many Errors, device set offline', 1 );
$dhash->{helper}{infoErrorCounter} = 0;
}
elsif ( $dhash->{helper}{infoErrorCounter} > 2
&& ReadingsVal( $dname, 'flow_Informations', 'active' ) eq
'active' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: Please check the AutomagicAPP on your Device"
);
}
readingsEndUpdate( $dhash, 1 );
}
if ( defined($err) ) {
if ( $err ne '' ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $err )
if ( ReadingsVal( $dname, 'state', 1 ) ne 'initialized' );
$dhash->{helper}{infoErrorCounter} =
( $dhash->{helper}{infoErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastStatusRequestState',
'statusRequest_error', 1 );
if ( $err =~ /timed out/ ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: connect to your device is timed out. check network"
);
}
elsif ( ( $err =~ /Keine Route zum Zielrechner/ )
&& $dhash->{helper}{infoErrorCounter} > 1 )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: no route to target. bad network configuration or network is down"
);
}
else {
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: $err" );
}
readingsEndUpdate( $dhash, 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: statusRequestErrorHandling: error while requesting AutomagicInfo: $err"
);
return;
}
}
if ( $data eq '' and exists( $param->{code} ) && $param->{code} ne 200 )
{
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $param->{code}, 1 )
if ( ReadingsVal( $dname, 'state', 1 ) ne 'initialized' );
$dhash->{helper}{infoErrorCounter} =
( $dhash->{helper}{infoErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastStatusRequestState',
'statusRequest_error', 1 );
if ( $param->{code} ne 200 ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: "
. $param->{code} );
}
readingsEndUpdate( $dhash, 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: received http code "
. $param->{code}
. " without any data after requesting AMAD AutomagicInfo" );
return;
}
if ( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $param->{code}, 1 )
if ( ReadingsVal( $dname, 'state', 0 ) ne 'initialized' );
$dhash->{helper}{infoErrorCounter} =
( $dhash->{helper}{infoErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastStatusRequestState',
'statusRequest_error', 1 );
if ( $param->{code} eq 404
&& ReadingsVal( $dname, 'flow_Informations', 'inactive' ) eq
'inactive' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: check the informations flow on your device"
);
}
elsif ( $param->{code} eq 404
&& ReadingsVal( $dname, 'flow_Informations', 'active' ) eq
'active' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: check the automagicApp on your device"
);
}
else {
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: http error "
. $param->{code} );
}
readingsEndUpdate( $dhash, 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - statusRequestERROR: received http code "
. $param->{code}
. " receive Error after requesting AMAD AutomagicInfo" );
return;
}
### End Error Handling
readingsSingleUpdate( $dhash, 'lastStatusRequestState',
'statusRequest_done', 1 );
$dhash->{helper}{infoErrorCounter} = 0;
}
elsif ( $param->{method} eq 'POST' ) {
### Begin Error Handling
if ( $dhash->{helper}{setCmdErrorCounter} > 2 ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'lastSetCommandState',
'statusRequest_error', 1 );
if ( ReadingsVal( $dname, 'flow_Informations', 'active' ) eq
'inactive'
&& ReadingsVal( $dname, 'flow_SetCommands', 'active' ) eq
'inactive' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: CHECK THE LAST ERROR READINGS FOR MORE INFO, DEVICE IS SET OFFLINE"
);
readingsBulkUpdate( $dhash, 'deviceState', 'offline', 1 );
readingsBulkUpdate( $dhash, 'state',
'AMAD Flows inactive, device set offline', 1 );
}
elsif ($dhash->{helper}{infoErrorCounter} > 7
&& $dhash->{helper}{setCmdErrorCounter} > 4 )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: UNKNOWN ERROR, PLEASE CONTACT THE DEVELOPER, DEVICE DISABLED"
);
$attr{$dname}{disable} = 1;
readingsBulkUpdate( $dhash, 'state',
'Unknown Error, device disabled', 1 );
$dhash->{helper}{infoErrorCounter} = 0;
$dhash->{helper}{setCmdErrorCounter} = 0;
return;
}
elsif ( ReadingsVal( $dname, 'flow_SetCommands', 'active' ) eq
'inactive' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: Flow SetCommands on your Device is inactive, will try to reactivate"
);
}
elsif ( $dhash->{helper}{setCmdErrorCounter} > 9 ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: To many Errors please check your Network or Device Configuration, DEVICE IS SET OFFLINE"
);
readingsBulkUpdate( $dhash, 'deviceState', 'offline', 1 );
readingsBulkUpdate( $dhash, 'state',
'To many Errors, device set offline', 1 );
$dhash->{helper}{setCmdErrorCounter} = 0;
}
elsif ( $dhash->{helper}{setCmdErrorCounter} > 4
&& ReadingsVal( $dname, 'flow_SetCommands', 'active' ) eq
'active' )
{
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: Please check the AutomagicAPP on your Device"
);
}
readingsEndUpdate( $dhash, 1 );
}
if ( defined($err) ) {
if ( $err ne '' ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $err, 1 )
if ( ReadingsVal( $dname, 'state', 0 ) ne 'initialized' );
$dhash->{helper}{setCmdErrorCounter} =
( $dhash->{helper}{setCmdErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastSetCommandState',
'setCmd_error', 1 );
if ( $err =~ /timed out/ ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: connect to your device is timed out. check network"
);
}
elsif ( $err =~ /Keine Route zum Zielrechner/ ) {
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: no route to target. bad network configuration or network is down"
);
}
else {
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: $err" );
}
readingsEndUpdate( $dhash, 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: error while POST Command: $err"
);
return;
}
}
if ( $data eq '' and exists( $param->{code} ) && $param->{code} ne 200 )
{
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $param->{code}, 1 )
if ( ReadingsVal( $dhash, 'state', 0 ) ne 'initialized' );
$dhash->{helper}{setCmdErrorCounter} =
( $dhash->{helper}{setCmdErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastSetCommandState', 'setCmd_error',
1 );
readingsEndUpdate( $dhash, 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: received http code "
. $param->{code} );
return;
}
if ( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, 'state', $param->{code}, 1 )
if ( ReadingsVal( $dname, 'state', 0 ) ne 'initialized' );
$dhash->{helper}{setCmdErrorCounter} =
( $dhash->{helper}{setCmdErrorCounter} + 1 );
readingsBulkUpdate( $dhash, 'lastSetCommandState', 'setCmd_error',
1 );
if ( $param->{code} eq 404 ) {
readingsBulkUpdate( $dhash, 'lastSetCommandError', '', 1 );
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: setCommands flow is inactive on your device!"
);
}
else {
Log3( $dname, 5,
"AMADCommBridge ($dname) - setCommandERROR: http error "
. $param->{code} );
}
return;
}
### End Error Handling
readingsSingleUpdate( $dhash, 'lastSetCommandState', 'setCmd_done', 1 );
$dhash->{helper}{setCmdErrorCounter} = 0;
return undef;
}
}
sub Open($) {
my $hash = shift;
my $name = $hash->{NAME};
my $port = $hash->{PORT};
if ( not defined( $hash->{FD} ) and ( !$hash->{FD} ) ) {
# Oeffnen des TCP Sockets
my $ret = TcpServer_Open( $hash, $port, 'global' );
if ( $ret && !$init_done ) {
Log3( $name, 3, "AMADCommBridge ($name) - $ret. Exiting." );
exit(1);
}
readingsSingleUpdate( $hash, 'state', 'opened', 1 )
if ( defined( $hash->{FD} ) );
Log3( $name, 3, "AMADCommBridge ($name) - Socket opened." );
return $ret;
}
else {
Log3( $name, 3, "AMADCommBridge ($name) - Socket already opened" );
}
return;
}
sub Close($) {
my $hash = shift;
my $name = $hash->{NAME};
TcpServer_Close($hash);
if ( not defined( $hash->{FD} ) ) {
readingsSingleUpdate( $hash, 'state', 'closed', 1 );
Log3( $name, 3, "AMADCommBridge ($name) - Socket closed." );
}
else {
Log3( $name, 3, "AMADCommBridge ($name) - can't close Socket." );
}
return;
}
sub Read($) {
my $hash = shift;
my $name = $hash->{NAME};
if ( $hash->{SERVERSOCKET} ) { # Accept and create a child
TcpServer_Accept( $hash, 'AMADCommBridge' );
return;
}
# Read 1024 byte of data
my $buf;
my $ret = sysread( $hash->{CD}, $buf, 2048 );
# When there is an error in connection return
if ( !defined($ret) or $ret <= 0 ) {
CommandDelete( undef, $name );
Log3( $name, 5,
"AMADCommBridge ($name) - Connection closed for $name" );
return;
}
ProcessRead( $hash, $buf );
}
sub ProcessRead($$) {
my ( $hash, $buf ) = @_;
my $name = $hash->{NAME};
my $flowsetversion =
$modules{AMADCommBridge}{defptr}{BRIDGE}->{VERSIONFLOWSET};
my @data = split( '\R\R', $buf );
my $data = $data[0];
my $json = $data[1];
my $buffer = '';
Log3( $name, 4, "AMADCommBridge ($name) - process read" );
my $response;
my $c;
my $fhempath = $attr{global}{modpath};
if ( $data =~ /currentFlowsetUpdate.xml/ ) {
$response =
qx(cat $fhempath/FHEM/lib/74_AMADautomagicFlowset_$flowsetversion.xml);
$c = $hash->{CD};
print $c "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain\r\n",
"Connection: close\r\n",
"Content-Length: " . length($response) . "\r\n\r\n",
$response;
return;
}
elsif ( $data =~ /currentTaskersetUpdate.prj.xml/ ) {
$response =
qx(cat $fhempath/FHEM/lib/74_AMADtaskerset_$flowsetversion.prj.xml);
$c = $hash->{CD};
print $c "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain\r\n",
"Connection: close\r\n",
"Content-Length: " . length($response) . "\r\n\r\n",
$response;
return;
}
elsif ( $data =~ /installFlow_([^.]*.xml)/ ) {
if ( defined($1) ) {
$response = qx(cat /tmp/$1);
$c = $hash->{CD};
print $c "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain\r\n",
"Connection: close\r\n",
"Content-Length: " . length($response) . "\r\n\r\n",
$response;
return;
}
}
if ( defined( $hash->{PARTIAL} ) and $hash->{PARTIAL} ) {
Log3( $name, 5,
"AMADCommBridge ($name) - PARTIAL: " . $hash->{PARTIAL} );
$buffer = $hash->{PARTIAL};
}
else {
Log3( $name, 4, "AMADCommBridge ($name) - No PARTIAL buffer" );
}
return
if ( !defined($json) );
Log3( $name, 5, "AMADCommBridge ($name) - Incoming data: " . $json );
$buffer = $buffer . $json;
Log3( $name, 4,
"AMADCommBridge ($name) - Current processing buffer (PARTIAL + incoming data): "
. $buffer );
my ( $correct_json, $tail ) = ParseMsg( $hash, $buffer );
while ($correct_json) {
$hash->{LAST_RECV} = time();
Log3( $name, 5,
"AMADCommBridge ($name) - Decoding JSON message. Length: "
. length($correct_json)
. " Content: "
. $correct_json );
Log3( $name, 5,
"AMADCommBridge ($name) - Vor Sub: Laenge JSON: "
. length($correct_json)
. " Content: "
. $correct_json
. " Tail: "
. $tail );
ResponseProcessing( $hash, $correct_json )
unless ( not defined($tail) and not($tail) );
( $correct_json, $tail ) = ParseMsg( $hash, $tail );
Log3( $name, 5,
"AMADCommBridge ($name) - Nach Sub: Laenge JSON: "
. length($correct_json)
. " Content: "
. $correct_json
. " Tail: "
. $tail );
}
$hash->{PARTIAL} = $tail;
Log3( $name, 4,
"AMADCommBridge ($name) - PARTIAL lenght: " . length($tail) );
Log3( $name, 5, "AMADCommBridge ($name) - Tail: " . $tail );
Log3( $name, 5, "AMADCommBridge ($name) - PARTIAL: " . $hash->{PARTIAL} );
}