-
Notifications
You must be signed in to change notification settings - Fork 5
/
74_CometBlueBTLE.pm
1478 lines (1203 loc) · 47.7 KB
/
74_CometBlueBTLE.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) 2018 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
# All rights reserved
#
# Special thanks goes to:
# - mokeo for many bugfix code
#
#
# 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$
#
###############################################################################
## Info https://www.torsten-traenkner.de/wissen/smarthome/heizung.php
## Die folgenden Modelle sind identisch. Man kann sich das günstigste Modell auswählen.
##
## Name Preis Vertrieb über
## -----------------------------------------------
## Xavax Hama 40 Euro Media Markt
## Sygonix HT100 BT 20 Euro Conrad
## Comet Blue 20 Euro Real / Bauhaus
## SilverCrest 15 Euro Lidl
## THERMy blue Aldi
package main;
use strict;
use warnings;
my $version = "0.2.2";
sub CometBlueBTLE_Initialize($) {
my ($hash) = @_;
$hash->{SetFn} = "CometBlueBTLE::Set";
$hash->{GetFn} = "CometBlueBTLE::Get";
$hash->{DefFn} = "CometBlueBTLE::Define";
$hash->{NotifyFn} = "CometBlueBTLE::Notify";
$hash->{UndefFn} = "CometBlueBTLE::Undef";
$hash->{AttrFn} = "CometBlueBTLE::Attr";
$hash->{AttrList} =
"interval "
. "disable:1 "
. "disabledForIntervals "
. "hciDevice:hci0,hci1,hci2 "
. "batteryFirmwareAge:8h,16h,24h,32h,40h,48h "
. "sshHost "
. "blockingCallLoglevel:2,3,4,5 " . "pin "
. "model:CometBlue,SilverCrest,Sygonix,THERMyBlue "
. $readingFnAttributes;
foreach my $d ( sort keys %{ $modules{CometBlueBTLE}{defptr} } ) {
my $hash = $modules{CometBlueBTLE}{defptr}{$d};
$hash->{VERSION} = $version;
}
}
package CometBlueBTLE;
my $missingModul = "";
use strict;
use warnings;
use POSIX;
use GPUtils qw(GP_Import)
; # wird für den Import der FHEM Funktionen aus der fhem.pl benötigt
eval "use JSON;1" or $missingModul .= "JSON ";
eval "use Blocking;1" or $missingModul .= "Blocking ";
## Import der FHEM Funktionen
BEGIN {
GP_Import(
qw(readingsSingleUpdate
readingsBulkUpdate
readingsBulkUpdateIfChanged
readingsBeginUpdate
readingsEndUpdate
defs
modules
Log3
CommandAttr
AttrVal
ReadingsVal
ReadingsAge
IsDisabled
deviceEvents
init_done
gettimeofday
InternalTimer
RemoveInternalTimer
BlockingKill
BlockingCall
FmtDateTime)
);
}
my %gatttChar = (
CometBlue => {
'devicename' => '0x3',
'battery' => '0x41',
'payload' => '0x3f',
# 'payload2' => '0x3d',
'firmware' => '0x18',
'pin' => '0x47',
'date' => '0x1d',
'tempLists' => '0x29,0x2b,0x1f,0x21,0x23,0x25,0x27,end'
},
Sygonix => {
'devicename' => '0x3',
'battery' => '0x41',
'payload' => '0x3f',
'firmware' => '0x18',
'pin' => '0x47',
'date' => '0x1d',
'tempLists' => '0x29,0x2b,0x1f,0x21,0x23,0x25,0x27,end'
},
SilverCrest => {
'devicename' => '0x3',
'battery' => '0x3f',
'payload' => '0x3d',
'firmware' => '0x18',
'pin' => '0x48'
},
THERMyBlue => {
'devicename' => '0x3',
'battery' => '0x3f',
'payload' => '0x3d',
'firmware' => '0x18',
'pin' => '0x48',
'date' => '0x1b',
'tempLists' => '0x27,0x29,0x1d,0x1f,0x21,0x23,0x25,end'
}
);
my %winOpnSensitivity = (
Sensitivity => {
'12' => 'low',
'8' => 'medium',
'4' => 'high',
'low' => '12',
'medium' => '8',
'high' => '4'
}
);
my %CallBatteryAge = (
'8h' => 28800,
'16h' => 57600,
'24h' => 86400,
'32h' => 115200,
'40h' => 144000,
'48h' => 172800
);
# declare prototype
#sub ExecGatttool_Run($);
sub Define($$) {
my ( $hash, $def ) = @_;
my @a = split( "[ \t][ \t]*", $def );
return "too few parameters: define <name> CometBlueBTLE <BTMAC>"
if ( @a != 3 );
return
"Cannot define CometBlueBTLE device. Perl modul ${missingModul}is missing."
if ($missingModul);
my $name = $a[0];
my $mac = $a[2];
$hash->{BTMAC} = $mac;
$hash->{VERSION} = $version;
$hash->{INTERVAL} = 300;
$hash->{helper}{writePin} = 0;
$hash->{helper}{CallBattery} = 0;
$hash->{NOTIFYDEV} = "global,$name";
$hash->{loglevel} = 4;
$hash->{tempListsHandleQueue} = [];
readingsSingleUpdate( $hash, "state", "initialized", 0 );
CommandAttr( undef, $name . ' room CometBlueBTLE' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
Log3 $name, 3, "CometBlueBTLE ($name) - defined with BTMAC $hash->{BTMAC}";
$modules{CometBlueBTLE}{defptr}{ $hash->{BTMAC} } = $hash;
return undef;
}
sub Undef($$) {
my ( $hash, $arg ) = @_;
my $mac = $hash->{BTMAC};
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
BlockingKill( $hash->{helper}{RUNNING_PID} )
if ( defined( $hash->{helper}{RUNNING_PID} ) );
delete( $modules{CometBlueBTLE}{defptr}{$mac} );
Log3 $name, 3, "Sub CometBlueBTLE_Undef ($name) - delete device $name";
return undef;
}
sub Attr(@) {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
if ( $attrName eq "disable" ) {
if ( $cmd eq "set" and $attrVal eq "1" ) {
RemoveInternalTimer($hash);
readingsSingleUpdate( $hash, "state", "disabled", 1 );
Log3 $name, 3, "CometBlueBTLE ($name) - disabled";
}
elsif ( $cmd eq "del" ) {
Log3 $name, 3, "CometBlueBTLE ($name) - enabled";
}
}
elsif ( $attrName eq "disabledForIntervals" ) {
if ( $cmd eq "set" ) {
return
"check disabledForIntervals Syntax HH:MM-HH:MM or 'HH:MM-HH:MM HH:MM-HH:MM ...'"
unless ( $attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/ );
Log3 $name, 3, "CometBlueBTLE ($name) - disabledForIntervals";
readingsSingleUpdate( $hash, "state", "disabled", 1 );
}
elsif ( $cmd eq "del" ) {
Log3 $name, 3, "CometBlueBTLE ($name) - enabled";
readingsSingleUpdate( $hash, "state", "active", 1 );
}
}
elsif ( $attrName eq "pin" ) {
if ( $cmd eq "set" ) {
return "the pin string may not begin with 0"
unless ( $attrVal =~ /^[1-9]/ );
}
}
elsif ( $attrName eq "interval" ) {
RemoveInternalTimer($hash);
if ( $cmd eq "set" ) {
if ( $attrVal < 30 ) {
Log3 $name, 3,
"CometBlueBTLE ($name) - interval too small, please use something >= 30 (sec), default is 150 (sec)";
return
"interval too small, please use something >= 30 (sec), default is 150 (sec)";
}
else {
$hash->{INTERVAL} = $attrVal;
Log3 $name, 3,
"CometBlueBTLE ($name) - set interval to $attrVal";
}
}
elsif ( $cmd eq "del" ) {
$hash->{INTERVAL} = 300;
Log3 $name, 3, "CometBlueBTLE ($name) - set interval to default";
}
}
elsif ( $attrName eq "blockingCallLoglevel" ) {
if ( $cmd eq "set" ) {
$hash->{loglevel} = $attrVal;
Log3 $name, 3,
"CometBlueBTLE ($name) - set blockingCallLoglevel to $attrVal";
}
elsif ( $cmd eq "del" ) {
$hash->{loglevel} = 4;
Log3 $name, 3,
"CometBlueBTLE ($name) - set blockingCallLoglevel to default";
}
}
return undef;
}
sub Notify($$) {
my ( $hash, $dev ) = @_;
my $name = $hash->{NAME};
return if ( IsDisabled($name) );
my $devname = $dev->{NAME};
my $devtype = $dev->{TYPE};
my $events = deviceEvents( $dev, 1 );
return if ( !$events );
StateRequestTimer($hash)
if (
(
(
(
grep /^DEFINED.$name$/,
@{$events}
or grep /^INITIALIZED$/,
@{$events}
or grep /^MODIFIED.$name$/,
@{$events}
or grep /^DELETEATTR.$name.disable$/,
@{$events}
or grep /^ATTR.$name.disable.0$/,
@{$events}
or grep /^DELETEATTR.$name.interval$/,
@{$events}
or grep /^DELETEATTR.$name.model$/,
@{$events}
or grep /^ATTR.$name.model.+/,
@{$events}
or grep /^ATTR.$name.interval.[0-9]+/,
@{$events}
)
and $devname eq 'global'
)
or grep /^resetBatteryTimestamp$/,
@{$events}
)
and $init_done
);
return;
}
sub StateRequest($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my %readings;
if ( AttrVal( $name, 'model', 'none' ) eq 'none' ) {
readingsSingleUpdate( $hash, "state", "set attribute model first", 1 );
}
elsif ( !IsDisabled($name) ) {
if ( ReadingsVal( $name, 'firmware', 'none' ) ne 'none' ) {
return CreateParamGatttool( $hash, 'read',
$gatttChar{ AttrVal( $name, 'model', '' ) }{'battery'} )
if (
CallBattery_IsUpdateTimeAgeTooOld(
$hash,
$CallBatteryAge{ AttrVal( $name, 'BatteryFirmwareAge',
'24h' ) }
)
);
CreateParamGatttool( $hash, 'read',
$gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'} )
if ( $hash->{helper}{writePin} == 0 );
}
else {
CreateParamGatttool( $hash, 'read',
$gatttChar{ AttrVal( $name, 'model', '' ) }{'firmware'} );
}
}
else {
readingsSingleUpdate( $hash, "state", "disabled", 1 );
}
}
sub StateRequestTimer($) {
my ($hash) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
StateRequest($hash) if ($init_done);
InternalTimer( gettimeofday() + $hash->{INTERVAL} + int( rand(60) ),
"CometBlueBTLE::StateRequestTimer", $hash );
Log3 $name, 4,
"CometBlueBTLE ($name) - stateRequestTimer: Call Request Timer";
}
sub Set($$@) {
my ( $hash, $name, @aa ) = @_;
my ( $cmd, @args ) = @aa;
my $handle;
my $value;
if ( $cmd eq 'desired-temp' or $cmd eq 'controlManu' ) {
return
'CometBlueBTLE: desired-temp requires <temperature> in degrees celsius as additional parameter'
if ( @args != 1 );
#return 'CometBlueBTLE: desired-temp supports temperatures from 6.0 - 28.0 degrees' if($args[0]<8.0 or $args[0]>28.0 or $args[0] ne 'on' or $args[0] ne 'off');
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'tempEco' ) {
return
'CometBlueBTLE: tempEco requires <temperature> in degrees celsius as additional parameter'
if ( @args != 1 );
return
'CometBlueBTLE: tempEco supports temperatures from 6.0 to 28.0 degrees'
if ( $args[0] < 12.0 or $args[0] > 23.0 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'tempComfort' ) {
return
'CometBlueBTLE: tempComfort requires <temperature> in degrees celsius as additional parameter'
if ( @args != 1 );
return
'CometBlueBTLE: tempComfort supports temperatures from 6.0 to 28.0 degrees'
if ( $args[0] < 12.0 or $args[0] > 23.0 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'tempOffset' ) {
return 'CometBlueBTLE: tempOffset requires an additional parameter'
if ( @args != 1 );
return 'CometBlueBTLE: tempOffset supports values from -5.0 to 5.0'
if ( $args[0] < -5.0 or $args[0] > 5.0 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'winOpnSensitivity' ) {
return
'CometBlueBTLE: winOpnSensitivity requires an additional parameter high, medium or low'
if ( @args != 1 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'winOpnPeriod' ) {
return
'CometBlueBTLE: winOpnSensitivity requires an additional parameter in minutes'
if ( @args != 1 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'};
$value = join( " ", @args );
}
elsif ( $cmd eq 'resetBatteryTimestamp' ) {
return "usage: resetBatteryTimestamp" if ( @args != 0 );
$hash->{helper}{updateTimeCallBattery} = 0;
return;
}
else {
my $list =
"desired-temp:on,off,Eco,Comfort,8.0,8.5,9.0,9.5,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0,23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0";
$list .=
" controlManu:on,off,8.0,8.5,9.0,9.5,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0,23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0";
$list .=
" tempEco:12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0";
$list .=
" tempComfort:12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0";
$list .=
" tempOffset:-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5 winOpnSensitivity:high,medium,low winOpnPeriod:slider,5,5,30 resetBatteryTimestamp:noArg";
return "Unknown argument $cmd, choose one of $list";
}
return 'another process is running, try again later'
if ( $hash->{helper}{writePin} == 1 );
CreateParamGatttool( $hash, 'write', $handle,
CreatePayloadString( $hash, $cmd, $value ) );
return undef;
}
sub Get($$@) {
my ( $hash, $name, @aa ) = @_;
my ( $cmd, @args ) = @aa;
my $handle;
if ( $cmd eq 'temperatures' ) {
return 'usage: temperatures' if ( @args != 0 );
return 'another process is running, try again later'
if ( $hash->{helper}{writePin} == 1 );
return StateRequest($hash);
}
# elsif ( $cmd eq 'temperatures2' ) {
# return 'usage: temperatures2' if ( @args != 0 );
#
# return 'another process is running, try again later'
# if ( $hash->{helper}{writePin} == 1 );
# return CreateParamGatttool( $hash, 'read',
# $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload2'} )
#
# }
elsif ( $cmd eq 'firmware' ) {
return 'usage: firmware' if ( @args != 0 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'firmware'};
}
elsif ( $cmd eq 'devicename' ) {
return 'usage: devicename' if ( @args != 0 );
$handle = $gatttChar{ AttrVal( $name, 'model', '' ) }{'devicename'};
}
elsif ( $cmd eq 'tempLists' ) {
return 'usage: tempLists' if ( @args != 0 );
if ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) == 0 )
{
foreach (
split(
',',
$gatttChar{ AttrVal( $name, 'model', '' ) }{'tempLists'}
)
)
{
unshift( @{ $hash->{tempListsHandleQueue} }, $_ );
}
}
$handle = pop( @{ $hash->{tempListsHandleQueue} } );
}
else {
my $list =
"temperatures:noArg devicename:noArg firmware:noArg tempLists:noArg";
return "Unknown argument $cmd, choose one of $list";
}
return 'another process is running, try again later'
if ( $hash->{helper}{writePin} == 1 );
CreateParamGatttool( $hash, 'read', $handle ) if ( $cmd ne 'temperatures' );
return undef;
}
sub CreateParamGatttool($@) {
my ( $hash, $mod, $handle, $value ) = @_;
my $name = $hash->{NAME};
my $mac = $hash->{BTMAC};
Log3 $name, 4,
"CometBlueBTLE ($name) - Run CreateParamGatttool with mod: $mod";
Log3 $name, 4,
"CometBlueBTLE ($name) - Run CreateParamGatttool with mod: $mod : $handle : $value"
if ( defined($value) );
Log3 $name, 5, "CometBlueBTLE ($name) - Noch in Queue nach pop: "
. scalar( @{ $hash->{tempListsHandleQueue} } );
if ( $hash->{helper}{writePin} == 0 ) {
Log3 $name, 4, "CometBlueBTLE ($name) - CreateParamGatttool erstes if";
$hash->{helper}{writePin} = 1;
$hash->{helper}{paramGatttool}{mod} = $mod;
$hash->{helper}{paramGatttool}{handle} = $handle;
$hash->{helper}{paramGatttool}{value} = $value if ( $mod eq 'write' );
$hash->{helper}{RUNNING_PID} = BlockingCall(
"CometBlueBTLE::ExecGatttool_Run",
$name . "|"
. $mac
. "|write|"
. $gatttChar{ AttrVal( $name, 'model', '' ) }{'pin'} . "|"
. ConvertPinToHexLittleEndian(
AttrVal( $name, 'pin', '00000000' )
),
"CometBlueBTLE::ExecGatttool_Done",
60,
"CometBlueBTLE::ExecGatttool_Aborted",
$hash
) unless ( exists( $hash->{helper}{RUNNING_PID} ) );
readingsSingleUpdate(
$hash, "state",
"pairing thermostat with pin: "
. ConvertPinToHexLittleEndian(
AttrVal( $name, 'pin', '00000000' )
),
1
);
Log3 $name, 4,
"CometBlueBTLE ($name) - Read CometBlueBTLE_ExecGatttool_Run $name|$mac|$mod|$handle";
}
elsif ( $mod eq 'read' ) {
Log3 $name, 4, "CometBlueBTLE ($name) - CreateParamGatttool zweites if";
$hash->{helper}{RUNNING_PID} = BlockingCall(
"CometBlueBTLE::ExecGatttool_Run",
$name . "|" . $mac . "|" . $mod . "|" . $handle,
"CometBlueBTLE::ExecGatttool_Done",
60,
"CometBlueBTLE::ExecGatttool_Aborted",
$hash
) unless ( exists( $hash->{helper}{RUNNING_PID} ) );
readingsSingleUpdate( $hash, "state", "read sensor data", 1 );
Log3 $name, 4,
"CometBlueBTLE ($name) - Read CometBlueBTLE_ExecGatttool_Run $name|$mac|$mod|$handle";
}
elsif ( $mod eq 'write' ) {
Log3 $name, 4, "CometBlueBTLE ($name) - CreateParamGatttool drittes if";
$hash->{helper}{RUNNING_PID} = BlockingCall(
"CometBlueBTLE::ExecGatttool_Run",
$name . "|" . $mac . "|" . $mod . "|" . $handle . "|" . $value,
"CometBlueBTLE::ExecGatttool_Done",
60,
"CometBlueBTLE::ExecGatttool_Aborted",
$hash
) unless ( exists( $hash->{helper}{RUNNING_PID} ) );
readingsSingleUpdate( $hash, "state", "write sensor data", 1 );
Log3 $name, 4,
"CometBlueBTLE ($name) - Write CometBlueBTLE_ExecGatttool_Run $name|$mac|$mod|$handle|$value";
}
}
sub ExecGatttool_Run($) {
my $string = shift;
my ( $name, $mac, $gattCmd, $handle, $value ) = split( "\\|", $string );
my $sshHost = AttrVal( $name, "sshHost", "none" );
my $gatttool;
my $json_notification;
$gatttool = qx(which gatttool) if ( $sshHost eq 'none' );
$gatttool = qx(ssh $sshHost 'which gatttool') if ( $sshHost ne 'none' );
chomp $gatttool;
if ( defined($gatttool) and ($gatttool) ) {
my $cmd;
my $loop;
my @gtResult;
my $wait = 1;
my $sshHost = AttrVal( $name, "sshHost", "none" );
my $hci = AttrVal( $name, "hciDevice", "hci0" );
while ($wait) {
my $grepGatttool;
my $gatttoolCmdlineStaticEscaped =
CmdlinePreventGrepFalsePositive("gatttool -i $hci -b $mac");
$grepGatttool = qx(ps ax| grep -E \'$gatttoolCmdlineStaticEscaped\')
if ( $sshHost eq 'none' );
$grepGatttool =
qx(ssh $sshHost 'ps ax| grep -E "$gatttoolCmdlineStaticEscaped"')
if ( $sshHost ne 'none' );
if ( not $grepGatttool =~ /^\s*$/ ) {
Log3 $name, 4,
"CometBlueBTLE ($name) - ExecGatttool_Run: another gatttool process is running. waiting...";
sleep(1);
}
else {
$wait = 0;
}
}
$cmd .= "ssh $sshHost '" if ( $sshHost ne 'none' );
$cmd .= "gatttool -i $hci -b $mac ";
$cmd .= "--char-read -a $handle" if ( $gattCmd eq 'read' );
$cmd .= "--char-write-req -a $handle -n $value"
if ( $gattCmd eq 'write' );
$cmd .= " 2>&1 /dev/null";
$cmd .= "'" if ( $sshHost ne 'none' );
$loop = 0;
do {
Log3 $name, 4,
"CometBlueBTLE ($name) - ExecGatttool_Run: call gatttool with command $cmd and loop $loop";
@gtResult = split( ": ", qx($cmd) );
Log3 $name, 5,
"CometBlueBTLE ($name) - ExecGatttool_Run: gatttool loop result "
. join( ",", @gtResult );
$loop++;
$gtResult[0] = 'connect error'
unless ( defined( $gtResult[0] ) );
} while ( $loop < 5 and $gtResult[0] eq 'connect error' );
Log3 $name, 4,
"CometBlueBTLE ($name) - ExecGatttool_Run: gatttool result "
. join( ",", @gtResult );
$gtResult[1] = 'no data response'
unless ( defined( $gtResult[1] ) );
$gtResult[1] = 'wrong PIN'
if ( $gtResult[1] =~ /Attribute value length is invalid/ );
$json_notification = EncodeJSON( $gtResult[1] );
if ( $gtResult[0] =~ /^connect error$/ ) {
return "$name|$mac|error|$gattCmd|$handle|$json_notification";
}
elsif ( $gtResult[1] =~ /^([0-9a-f]{2}(\s?))*$/ ) {
return "$name|$mac|ok|$gattCmd|$handle|$json_notification";
}
elsif ( $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'pin'}
and $gattCmd eq 'write' )
{
if ( $gtResult[1] eq 'wrong PIN' ) {
return "$name|$mac|error|$gattCmd|$handle|$json_notification";
}
else {
return "$name|$mac|ok|$gattCmd|$handle|$json_notification";
}
}
elsif ( $gtResult[1] eq 'no data response' and $gattCmd eq 'write' ) {
return "$name|$mac|ok|$gattCmd|$handle|$json_notification";
}
else {
return "$name|$mac|error|$gattCmd|$handle|$json_notification";
}
}
else {
$json_notification = EncodeJSON(
'no gatttool binary found. Please check if bluez-package is properly installed'
);
return "$name|$mac|error|$gattCmd|$handle|$json_notification";
}
}
sub ExecGatttool_Done($) {
my $string = shift;
my ( $name, $mac, $respstate, $gattCmd, $handle, $json_notification ) =
split( "\\|", $string );
my $hash = $defs{$name};
delete( $hash->{helper}{RUNNING_PID} );
Log3 $name, 3,
"CometBlueBTLE ($name) - ExecGatttool_Done: Helper is disabled. Stop processing"
if ( $hash->{helper}{DISABLED} );
return if ( $hash->{helper}{DISABLED} );
Log3 $name, 4,
"CometBlueBTLE ($name) - ExecGatttool_Done: gatttool return string: $string";
if (
$respstate eq 'ok'
and (
$gattCmd eq 'write'
or ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) > 0 )
)
and $handle ne $gatttChar{ AttrVal( $name, 'model', '' ) }{'pin'}
and $hash->{helper}{writePin} == 1
)
{
if ( $gattCmd eq 'write' ) {
readingsBeginUpdate($hash);
readingsBulkUpdateIfChanged( $hash, "lastChangeBy", "FHEM" );
readingsEndUpdate( $hash, 1 );
}
return CreateParamGatttool( $hash, 'read',
$hash->{helper}{paramGatttool}{handle} )
if ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) == 0 );
}
elsif ( $respstate eq 'ok'
and $gattCmd eq 'write'
and $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'pin'}
and $hash->{helper}{writePin} == 1 )
{
return CreateParamGatttool(
$hash,
$hash->{helper}{paramGatttool}{mod},
$hash->{helper}{paramGatttool}{handle}
)
if ( $handle ne $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'}
and $hash->{helper}{paramGatttool}{mod} eq 'read' );
return CreateParamGatttool(
$hash,
$hash->{helper}{paramGatttool}{mod},
$hash->{helper}{paramGatttool}{handle},
$hash->{helper}{paramGatttool}{value}
)
if ( $handle ne $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'}
and $hash->{helper}{paramGatttool}{mod} eq 'write' );
return StateRequest($hash)
if ( $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'}
and $hash->{helper}{paramGatttool}{mod} eq 'read' );
}
my $decode_json = eval { decode_json($json_notification) };
if ($@) {
Log3 $name, 3,
"CometBlueBTLE ($name) - ExecGatttool_Done: JSON error while request: $@";
}
if ( $respstate eq 'ok' ) {
ProcessingNotification( $hash, $gattCmd, $handle,
$decode_json->{gtResult} );
}
else {
ProcessingErrors( $hash, $decode_json->{gtResult} );
}
$hash->{helper}{writePin} = 0
if ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) == 0 );
}
sub ExecGatttool_Aborted($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my %readings;
delete( $hash->{helper}{RUNNING_PID} );
readingsSingleUpdate( $hash, "state", "unreachable", 1 );
$readings{'lastGattError'} =
'The BlockingCall Process terminated unexpectedly. Timedout';
WriteReadings( $hash, \%readings );
$hash->{helper}{writePin} = 0;
Log3 $name, 3,
"CometBlueBTLE ($name) - ExecGatttool_Aborted: The BlockingCall Process terminated unexpectedly. Timedout";
}
sub ProcessingNotification($@) {
my ( $hash, $gattCmd, $handle, $notification ) = @_;
my $name = $hash->{NAME};
my $readings;
Log3 $name, 4, "CometBlueBTLE ($name) - ProcessingNotification";
Log3 $name, 4,
"CometBlueBTLE ($name) - ProcessingNotification: handle "
. $handle
. " - Noch in Queue: "
. scalar( @{ $hash->{tempListsHandleQueue} } );
if ( $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'battery'} ) {
### Read Firmware and Battery Data
Log3 $name, 5,
"CometBlueBTLE ($name) - ProcessingNotification: handle $gatttChar{AttrVal($name,'model','')}{'battery'}";
$readings = HandleBattery( $hash, $notification );
}
elsif ( $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'payload'} )
{
### payload abrufen
Log3 $name, 5,
"CometBlueBTLE ($name) - ProcessingNotification: handle $gatttChar{AttrVal($name,'model','')}{'payload'}";
$readings = HandlePayload( $hash, $notification );
}
elsif ( $handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'firmware'} )
{
### firmware abrufen
Log3 $name, 5,
"CometBlueBTLE ($name) - ProcessingNotification: handle $gatttChar{AttrVal($name,'model','')}{'firmware'}";
$readings = HandleFirmware( $hash, $notification );
}
elsif (
$handle eq $gatttChar{ AttrVal( $name, 'model', '' ) }{'devicename'} )
{
### devicename abrufen
Log3 $name, 5,
"CometBlueBTLE ($name) - ProcessingNotification: handle $gatttChar{AttrVal($name,'model','')}{'devicename'}";
$readings = HandleDevicename( $hash, $notification );
}
elsif ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) > 0 )
{
Log3 $name, 4,
"CometBlueBTLE ($name) - ProcessingNotification: $notification - Noch in Queue: "
. scalar( @{ $hash->{tempListsHandleQueue} } );
### templisten abrufen
my $i = 0;
foreach (
split(
',', $gatttChar{ AttrVal( $name, 'model', '' ) }{'tempLists'}
)
)
{
if ( $handle eq $_ ) {
Log3 $name, 4,
"CometBlueBTLE ($name) - ProcessingNotification in der Schleife: handle "
. $_
. " und dayOfWeek: "
. $i;
$readings = HandleTempLists( $hash, $_, $i, $notification );
}
$i++;
}
$hash->{helper}{paramGatttool}{handle} =
pop( @{ $hash->{tempListsHandleQueue} } )
if ( defined( $hash->{tempListsHandleQueue} )
and scalar( @{ $hash->{tempListsHandleQueue} } ) > 0 );
CreateParamGatttool( $hash, 'read',
$hash->{helper}{paramGatttool}{handle} )
if ( $hash->{helper}{paramGatttool}{handle} ne 'end' );
}
WriteReadings( $hash, $readings );
}
sub HandleBattery($$) {
### Read Battery Data
my ( $hash, $notification ) = @_;
my $name = $hash->{NAME};
my %readings;
Log3 $name, 5,
"CometBlueBTLE ($name) - handle $gatttChar{AttrVal($name,'model','')}{'battery'}";
#chomp($notification);
$notification =~ s/\s+//g;
$readings{'batteryPercent'} = hex( "0x" . $notification );
$readings{'batteryState'} =
( hex( "0x" . $notification ) > 15 ? "ok" : "low" );
$hash->{helper}{CallBattery} = 1;
CallBattery_Timestamp($hash);
return \%readings;
}
sub HandlePayload($$) {