forked from krkeegan/misterhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwfs_Item.pm
1778 lines (1501 loc) · 53 KB
/
Owfs_Item.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
=begin comment
Owfs_Item.pm
03/10/2007 Created by Jim Duda ([email protected])
Use this module to interface with the OWFS (one-wire filesystem) software.
The OWFS software handles all the real-time processing of the one-wire itself,
offering a simple PERL API interface. The Owfs_Item only requires the owserver
portion of owfs to be accessable.
Requirements:
Download and install OWFS (tested against release owfs-2.9p0).
Only the owserver portion is required for Misterhouse.
http://www.owfs.org
Setup:
In your code module, instantation the Owfs_Item class (or extension) to interface with some
one-wire element. The one-wire device can be found using the OWFS html interface.
configure mh.private.ini
owfs_port = 4304 # defined port where the owfs server is listening
# (owserver defaults to 4304)
Example Usage:
$item = new Owfs_Item ( "<device_id>", <location> );
<device_id> - of the form family.address; identifies the one-wire device
<location> - ASCII string identifier providing a useful name for device_id
$frontDoorBell = new Owfs_Item ( "12.487344000000", "Front DoorBell");
$sensor = new Owfs_Item ( "05.4D212A000000");
Any of the fields in the one-wire device can be access via the set and get methods.
$sensor->set ( "power", 1 );
$sensor->get ( "alarm" );
The get method only "requests" the property be fetched. The property will be
placed into the object state and can be accessed via:
if (my $state = said $sensor) {
...
}
or;
if (my $state = state_now $sensor) {
...
}
if (my $state = state_changed $sensor) {
...
}
or;
my $state = $sensor->state( );
Owfs_Item can be used as a baseclass and extended for specific one wire devices.
For example, refer to package Owfs_DS2450 which describes a one wire A/D device.
Extended devices will have different API routines and will typically not use
the set/get methods.
=cut
# TODO
# maintain inventory
# dump inventory
# inventory of all items, not just those requested
# table A
#=======================================================================================
#
# Generic Owfs_Item
#
# Owfs_Item should handle any Owfs device, and provides access to any individual field.
#
#=======================================================================================
use Timer;
use Socket_Item;
package Owfs_Item;
use strict;
@Owfs_Item::ISA = ('Generic_Item');
our (%objects_by_id); # database of all discovered objects by id
our $socket; # single Socket_Item which serves all Owfs_Item objects
our @queue; # Queue of commands for owserver, commands handled one at a time
our $socket_state = 0; # State variable for handling socket interface
our $socket_inactive = 0; # State variable for handling socket interface
###################################################################################
# Static variables used for owserver interface
###################################################################################
our $msg_read = 2 ; # Command codes for owserver interface
our $msg_write = 3 ;
our $msg_dir = 4 ;
our $msg_presence = 6 ;
our $msg_dirall = 7 ;
our $msg_get = 8 ;
our $persistence_bit = 0x04 ;
# PresenceCheck, Return bus list, and apply aliases
our $default_sg = 0x100 + 0x2 + 0x8 ;
our $default_block = 4096 ;
our $tempscale = 0 ;
our $addr = "";
TEMPSCALE: {
$tempscale = 0x00000 , last TEMPSCALE if $addr =~ /-C/ ;
$tempscale = 0x10000 , last TEMPSCALE if $addr =~ /-F/ ;
$tempscale = 0x20000 , last TEMPSCALE if $addr =~ /-K/ ;
$tempscale = 0x30000 , last TEMPSCALE if $addr =~ /-R/ ;
}
our $format = 0 ;
FORMAT: {
$format = 0x2000000 , last FORMAT if $addr =~ /-ff\.i\.c/ ;
$format = 0x4000000 , last FORMAT if $addr =~ /-ffi\.c/ ;
$format = 0x3000000 , last FORMAT if $addr =~ /-ff\.ic/ ;
$format = 0x5000000 , last FORMAT if $addr =~ /-ffic/ ;
$format = 0x0000000 , last FORMAT if $addr =~ /-ff\.i/ ;
$format = 0x1000000 , last FORMAT if $addr =~ /-ffi/ ;
$format = 0x2000000 , last FORMAT if $addr =~ /-f\.i\.c/ ;
$format = 0x4000000 , last FORMAT if $addr =~ /-fi\.c/ ;
$format = 0x3000000 , last FORMAT if $addr =~ /-f\.ic/ ;
$format = 0x5000000 , last FORMAT if $addr =~ /-fic/ ;
$format = 0x0000000 , last FORMAT if $addr =~ /-f\.i/ ;
$format = 0x1000000 , last FORMAT if $addr =~ /-fi/ ;
}
###################################################################################
# BaseClass constructor
sub new {
my ($class, $device, $location) = @_;
my $self = { };
bless $self,$class;
# Create one Socket_Item for ALL Owfs_Item devices to share
if (!$socket) {
&::MainLoop_pre_add_hook(\&Owfs_Item::_run_loop, 'persistent');
my $host = "localhost";
my $port = "4304";
$host = "$main::config_parms{owfs_host}" if exists $main::config_parms{owfs_host};
$port = "$main::config_parms{owfs_port}" if exists $main::config_parms{owfs_port};
&main::print_log ("Owfs_Item::new Initializing host:port: $host:$port") if $main::Debug{owfs};
$socket = new Socket_Item(undef, undef, "$host:$port", undef, 'tcp', 'raw', undef);
@queue = ();
}
# Object identification
$device =~ /(.*)\.(.*)/;
$self->{device} = $device;
$self->{location} = $location;
$self->{family} = $1;
$self->{id} = $2;
$self->{root} = undef;
# State variables for _discovery
$self->{dir_tokens} = ();
$self->{dir_level} = 0;
$self->{present} = 0;
$self->{failcnt} = 0;
# State variables for get/set operations
$self->{path} = undef;
$self->{token} = undef;
$self->{value} = undef;
# State variables for debug
$self->{debug} = 0;
# Initialize object state
$self->{state} = ''; # Will only be listed on web page if state is defined
# Schedule item discovery
$self->{discover_timer} = new Timer;
$self->{discover_timer}->set(5, sub {Owfs_Item::_discover($self);});
my $uom = "F";
$uom = "$main::config_parms{owfs_uom_temp}" if exists $main::config_parms{owfs_uom_temp};
my $tempscale = 0;
if ($uom =~ /C/) {
$tempscale = 0x00000;
} elsif ($uom =~ /F/) {
$tempscale = 0x10000;
} elsif ($uom =~ /K/) {
$tempscale = 0x20000;
} elsif ($uom =~ /R/) {
$tempscale = 0x30000;
}
# owserver state variables
$self->{PERSIST} = $persistence_bit;
$self->{SG} = $default_sg + $tempscale + $format;
$self->{VER} = 0 ;
return $self;
}
sub get_device {
my ($self) = @_;
return $self->{device};
}
sub get_location {
my ($self) = @_;
return $self->{location};
}
sub get_family {
my ($self) = @_;
return $self->{family};
}
sub get_id {
my ($self) = @_;
return $self->{id};
}
sub get_present {
my ($self) = @_;
return $self->{present};
}
sub set_key {
my ($self, $key, $data) = @_;
$self->{$key} = $data;
}
sub get_key {
my ($self, $key) = @_;
return ($self->{$key});
}
# This method is called when the response for a read request to owserver returns.
sub process_read_response {
my ($self, $response) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $debug = $self->{debug} || $main::Debug{owfs};
&main::print_log ("Owfs_Item::process_read_response $device $location response: $response") if $debug;
if ($response ne $self->state( )) {
$self->SUPER::set($response, 'owfs');
}
}
# This method is called when the response for a write request to owserver returns.
sub process_write_response {
my ($self, $response) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $token = $self->{token};
my $value = $self->{value};
my $type = $self->isa('Owfs_Item');
if ($response) {
if ($value ne $self->state( )) {
$self->SUPER::set($value, 'owfs');
}
}
my $debug = $self->{debug} || $main::Debug{owfs};
&main::print_log ("Owfs_Item::process_write_response type: $type $device $location response: $response token: $token value: $value") if $debug;
}
# This method is called when the response for a directory request to owserver returns. This method is
# used during device discovery.
sub process_dir_response {
my ($self, $response) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $family = $self->{family};
my $level = $self->{dir_level};
my $id = $self->{id};
my $path = $self->{dir_path};
my @tokens = split(',',$response);
push @{$self->{dir_tokens}}, @tokens;
&main::print_log ( "Owfs_Item::process_dir_response family: $family id: $id level: $level path: $path tokens: @tokens") if $main::Debug{owfs};
while ( scalar(@{$self->{dir_tokens}}) ) {
my $token = shift @{$self->{dir_tokens}};
&main::print_log ( "Owfs_Item::process_dir_response family: $family id: $id level: $level path: $path token: $token") if $main::Debug{owfs};
if ( $token =~ /\/([0123456789abcdefABCDEF\.]+|aux|main)$/ ) {
my $leaf = $1;
&main::print_log ( "Owfs_Item::process_dir_response family: $family id: $id level: $level path: $path token: $token leaf: $leaf") if $main::Debug{owfs};
$leaf =~ /(.+)\.(.+)$/;
if ((uc $family eq uc $1) && (uc $id eq uc $2)) {
$self->{root} = $path;
if ($self->{root} ne "/") {
$self->{root} .= "/";
}
$self->{path} = $token . "/";
$self->{present} = 1;
$objects_by_id{$id} = $self;
&main::print_log ("Owfs_Item::DEVICE_DISCOVERY: device: $device location: $location family: $family id: $id root: $path path: $token"); # if $main::Debug{owfs};
$self->discovered( );
return;
} elsif (($1 eq "1F") || ($leaf =~ /aux$/) || ($leaf =~ /main$/)) {
my $val = $self->_find ($family, $id, $level+1, $token);
}
}
}
}
# This method is called to schedule a write command be sent to the owserver for the object.
sub set {
my ($self, $token, $value) = @_;
return if (!defined $self->{path});
my $path = $self->{path} . $token;
my $debug = $self->{debug} || $main::Debug{owfs};
&main::print_log ("Owfs_Item::set path: $path value: $value") if $debug;
my $path_length = length($path)+1;
#$value .= ' ';
my $value_length = length($value);
my $payload = pack( 'Z'.$path_length.'A'.$value_length,$path,$value );
$self->{token} = $token;
$self->{value} = $value;
$self->_ToServer($path,length($payload)+1, $msg_write, $value_length, 0, $payload);
}
# This method is called to schedule a read command be sent to the owserver for the object.
sub get {
my ($self, $token) = @_;
return if (!defined $self->{path});
my $path = $self->{path} . $token;
&main::print_log ("Owfs_Item::get path: $path") if $main::Debug{owfs};
$self->{token} = $token;
$self->{value} = undef;
$self->_ToServer($path,length($path)+1,$msg_read,$default_block,0,$path);
}
# This method is called to schedule a directory command be sent to the owserver for the object.
# This method is used for item discovery.
sub _dir {
my ($self, $path) = @_;
# new msg_dirall method -- single packet
&main::print_log ("Owfs_Item::dir path: $path") if $main::Debug{owfs};
$self->{dir_path} = $path;
$self->{token} = undef;
$self->{value} = undef;
$self->_ToServer($path,length($path)+1,$msg_dirall,$default_block,0,$path);
}
# This method is called to schedule a write command be sent to the owserver for the object.
# The path used will be the device root instead of the device itself. If the $token value
# is preceeded with a "/", then the token value will be used as a raw path.
sub _set_root {
my ($self, $token, $value) = @_;
my $root = $self->{root};
return if (!defined $root);
my $path = $self->{root} . $token;
if ($token =~ /^\//) {
$path = $token;
}
&main::print_log ("Owfs_Item::_set_root root: $root path: $path value: $value") if $main::Debug{owfs};
my $path_length = length($path)+1 ;
#$value .= ' ';
my $value_length = length($value) ;
my $payload = pack( 'Z'.$path_length.'A'.$value_length,$path,$value ) ;
$self->{token} = $token;
$self->{value} = $value;
$self->_ToServer($path,length($payload)+1, $msg_write, $value_length, 0, $payload);
}
# This method is called to schedule a read command be sent to the owserver for the object.
# The path used will be the device root instead of the device itself. If the $token value
# is preceeded with a "/", then the token value will be used as a raw path.
sub _get_root {
my ($self, $token) = @_;
my $root = $self->{root};
return if (!defined $root);
my $path = $self->{root} . $token;
&main::print_log ("Owfs_Item::_get_root path: $path") if $main::Debug{owfs};
$self->{token} = $token;
$self->{value} = undef;
$self->_ToServer($path,length($path)+1,$msg_read,$default_block,0,$path) ;
}
# This method is used to search the one-wire tree for the specific object as defined
# by the $device passed during construction.
sub _discover {
my ($self) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $family = $self->{family};
my $id = $self->{id};
&main::print_log ( "Owfs_Item::_discover family: $family id: $id") if $main::Debug{owfs};
return if (defined $self->{path});
$self->_find ( $family, $id, 0, "/" );
if (!$self->get_present( )) {
$self->{discover_timer}->set(5, sub {Owfs_Item::_discover($self);});
}
}
# This method is called whenever the object has been discovered. Useful for initialization.
sub discovered {
my ($self) = @_;
}
# This method is part of _discover. It provides an interim method to allow for recursion to work.
sub _find {
my ($self,$family,$id,$level,$path) = @_;
&main::print_log ( "Owfs_Item::_find family: $family id: $id") if $main::Debug{owfs};
$self->{dir_level} = $level;
$self->_dir($path);
}
# This method is called when a device which had been previously discovered has been lost. This
# method will schedule the _discover mechanism to run again. The _lost and _discover methods
# allow for dynamic removal and insertion.
sub _lost {
my ($self) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $family = $self->{family};
my $id = $self->{id};
my $path = $self->{path};
&main::print_log ("Owfs_Item::DEVICE_LOST: device: $device location: $location family: $family id: $id path: $path"); # if $main::Debug{owfs};
$self->{root} = undef;
$self->{path} = undef;
$self->{present} = 0;
$self->{discover_timer}->set(5, sub {Owfs_Item::_discover($self);});
$self->{failcnt} = 0;
$self->{state} = undef;
}
# The method can be used to dump the state of any Owfs_Item object.
sub _dump {
my ($self) = @_;
&main::print_log ( "\n") if $main::Debug{owfs};
&main::print_log ( "path: \t\t$$self{path}") if $main::Debug{owfs};
&main::print_log ( "family: \t$$self{family}") if $main::Debug{owfs};
&main::print_log ( "id: \t\t$$self{id}") if $main::Debug{owfs};
&main::print_log ( "type: \t\t$$self{type}") if $main::Debug{owfs};
for my $key (sort keys %$self) {
next if ($key eq "root");
next if ($key eq "path");
next if ($key eq "family");
next if ($key eq "id");
next if ($key eq "type");
&main::print_log ( "$key:\t\t$$self{$key}") if $main::Debug{owfs};
}
&main::print_log ( "\n") if $main::Debug{owfs};
}
# This is a helper method to remove extra white space characters.
sub _chomp_plus {
my ($self,$string) = @_;
$string =~ s/^\s+//;
chomp $string;
return $string;
}
# This is a helper method to convert states as required. This method is overloaded
# in the sub class as necessary.
sub convert_state {
my ($self, $value) = @_;
return $value;
}
# This method is a direct port from the OWNet.pm module from owfs. This is the lower layer interface
# to the owserver socket port.
sub _ToServer {
my ($self, $path, $payload_length, $msg_type, $size, $offset, $payload_data) = @_ ;
my $f = "N6Z$payload_length";
#$f .= 'Z'.$payload_length if ( $payload_length > 0 ) ;
my $message = pack($f,$self->{VER},$payload_length,$msg_type,$self->{SG}|$self->{PERSIST},$size,$offset,$payload_data);
&main::print_log ( "Owfs_Item::_ToServer path: $path payload_length: $payload_length payload_data: $payload_data message: $message") if $main::Debug{owfs};
my $token = $self->{token};
my $value = $self->{value};
my $hashref = { msg_type => $msg_type, self => $self, path => $path, token => $token, value => $value, message => $message };
push @queue, $hashref;
my $num = scalar(@queue);
&main::print_log ( "Owfs_Item::_ToServer num: $num") if $main::Debug{owfs};
if ($num > 100) {
&main::print_log ( "Owfs_Item::_ToServer high outstanding requests! num: $num");
}
if ($main::Debug{owfs} && (scalar(@queue) > 1)) {
foreach my $ref (@queue) {
my $msg_type = $ref->{msg_type};
my $path = $ref->{path};
&main::print_log ( "Owfs_Item::_ToServer msg_type: $msg_type path: $path");
}
}
if (scalar(@queue) eq 1) {
&main::print_log ( "Owfs_Item::_ToServer path: $path message: $message sending socket....") if $main::Debug{owfs};
start $socket unless active $socket;
$socket->set ( $message );
}
}
# This method is a direct port from the OWNet.pm module from owfs. This is the lower layer interface
# to the owserver socket port.
sub _FromServerLow {
my ($self, $length_wanted) = @_;
my $length = length($self->{record});
&main::print_log ( "Owfs_Item::_FromServerLow length_wanted: $length_wanted length: $length") if $main::Debug{owfs};
return '' if $length_wanted == 0;
my $remaininglength = $length_wanted;
my $fullread = '';
return if length($self->{record}) < $length_wanted;
my $result = substr($self->{record},0,$length_wanted);
$self->{record} = substr($self->{record},$length_wanted);
return $result;
}
# This method is a direct port from the OWNet.pm module from owfs. This is the lower layer interface
# to the owserver socket port.
sub _FromServer {
my ($self) = @_;
my ( $version, $payload_length, $return_status, $sg, $size, $offset, $payload_data );
while (active $socket) {
&main::print_log ( "Owfs_Item::_FromServer socket_state: $socket_state") if $main::Debug{owfs};
if ($socket_state == 0) {
do {
my $r = _FromServerLow( $self, 24 );
if (!defined $r) {
&main::print_log ( "Owfs_Item::_FromServer Trouble getting header") if $main::Debug{owfs};
return;
}
($version, $payload_length, $return_status, $sg, $size, $offset) = unpack('N6', $r );
my @things = ($version, $payload_length, $return_status, $sg, $size, $offset);
&main::print_log ( "Owfs_Item::_FromServer things: @things") if $main::Debug{owfs};
# returns unsigned (though originals signed
# assume anything above 66000 is an error
if ($return_status > 66000) {
&main::print_log ( "Owfs_Item::_FromServer Trouble getting payload") if $main::Debug{owfs};
return ($version, $payload_length, $return_status, $sg, $size, $offset, $payload_data ) ;
}
} while ($payload_length > 66000);
$socket_state = 1;
}
else {
$payload_data = $self->_FromServerLow( $payload_length ) ;
if (!defined $payload_data) {
&main::print_log ( "Owfs_Item::_FromServer Trouble getting payload") if $main::Debug{owfs};
return;
}
$payload_data = substr($payload_data,0,$size) ;
$socket_state = 0;
return ($version, $payload_length, $return_status, $sg, $size, $offset, $payload_data ) ;
}
}
}
# This method runs one during the misterhouse main loop. Its purpose is to handle the return responses
# from the Socket_Item attached to the owserver. The results are dispatched back to the objects using
# the process_write/read/dir_response methods. After each response is handled by the object, the next
# command will be popped from the queue and sent to owserver. The owserver interface is only given
# one command at a time to process.
sub _run_loop {
# State Machine
return if !scalar(@queue);
my $hashref = $queue[0];
my $self = $hashref->{self};
my $msg_type = $hashref->{msg_type};
my $path = $hashref->{path};
my $token = $hashref->{token};
my $value = $hashref->{value};
my $device = $self->{device};
my $location = $self->{location};
my $popped = 0;
# Detect state change from inactive to active
if ($socket->inactive_now( )) {
$socket_inactive = 1;
$socket_state = 0;
&main::print_log ( "Owfs_Item::_run_loop socket INACTIVE") if $main::Debug{owfs};
}
if ($socket->active_now( )) {
$socket_state = 0;
if ($socket_inactive) {
$popped = 1;
}
$socket_inactive = 0;
&main::print_log ( "Owfs_Item::_run_loop socket ACTIVE") if $main::Debug{owfs};
}
start $socket unless active $socket;
# Read Response
if ($msg_type eq $msg_read) {
if (my $record = said $socket) {
$self->{record} .= $record;
my $len1 = length($record);
my $len2 = length($self->{record});
&main::print_log ( "Owfs_Item::_run_loop len1: $len1 len2: $len2") if $main::Debug{owfs};
my @response = _FromServer($self) ;
if (!@response) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path EMPTY") if $main::Debug{owfs};
} else {
if ($response[2] > 66000) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path ERROR"); # if $main::Debug{owfs};
$self->{failcnt}++;
if ($self->{failcnt} >= 5) {
$self->_lost( );
}
$self->process_read_response( );
} else {
# process response
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path response: $response[6]") if $main::Debug{owfs};
$self->{failcnt} = 0;
$self->process_read_response($response[6]);
}
shift @queue;
$popped = 1;
}
}
}
# Write Response
elsif ($msg_type eq $msg_write) {
if (my $record = said $socket) {
$self->{record} .= $record;
my $len1 = length($record);
my $len2 = length($self->{record});
&main::print_log ( "Owfs_Item::_run_loop len1: $len1 len2: $len2") if $main::Debug{owfs};
my @response = _FromServer($self) ;
if (!@response) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path EMPTY") if $main::Debug{owfs};
} else {
if ($response[2] > 66000) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path ERROR"); # if $main::Debug{owfs};
$self->{failcnt}++;
if ($self->{failcnt} >= 5) {
$self->_lost( );
}
$self->process_write_response( );
} else {
# process response
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path response: $response[2]") if $main::Debug{owfs};
$self->{failcnt} = 0;
$self->process_write_response($response[2] >= 0);
}
shift @queue;
$popped = 1;
}
}
}
# Dirall Response
elsif ($msg_type eq $msg_dirall) {
if (my $record = said $socket) {
$self->{record} .= $record;
my @response = _FromServer($self) ;
if (!@response) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path EMPTY") if $main::Debug{owfs};
} else {
if ($response[2] > 66000) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path ERROR"); # if $main::Debug{owfs};
$self->{failcnt}++;
if ($self->{failcnt} >= 5) {
$self->_lost( );
}
$self->process_write_response( );
} else {
# process response
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path response: $response[6]") if $main::Debug{owfs};
$self->{failcnt} = 0;
$self->process_dir_response($response[6]);
}
shift @queue;
$popped = 1;
# } else {
# old msg_dir method -- many packets
# $self->{dirlist} = '';
# $self->_ToServer($path,length($path)+1,$msg_dir,$default_block,0,$path) || return ;
}
}
}
# Dir response
elsif ($msg_type eq $msg_dir) {
if (my $record = said $socket) {
$self->{record} .= $record;
my @response = _FromServer($self);
if (!@response) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path EMPTY") if $main::Debug{owfs};
} else {
if ($response[2] > 66000) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path ERROR"); # if $main::Debug{owfs};
$self->{failcnt}++;
if ($self->{failcnt} >= 5) {
$self->_lost( );
}
$self->process_write_response( );
} else {
$self->{failcnt} = 0;;
if ( $response[1] == 0 ) { # last null packet
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path response: $response[6]") if $main::Debug{owfs};
$self->process_dir_response(substr($self->{dirlist},1));
shift @queue;
$popped = 1;
} else {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path response: $response[6]") if $main::Debug{owfs};
$self->{dirlist} .= ','.$response[6] ;
}
}
}
}
}
# Handle Unknown response
else {
&main::print_log ( "Owfs_Item::_run_loop unknown msg_type: $msg_type") if $main::Debug{owfs};
if (my $record = said $socket) {
$self->{record} .= $record;
my @response = _FromServer($self) ;
if (!@response) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path EMPTY") if $main::Debug{owfs};
} else {
if ($response[2] > 66000) {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type path: $path ERROR"); # if $main::Debug{owfs};
} else {
&main::print_log ( "Owfs_Item::_run_loop msg_type: $msg_type (UNKNOWN) path: $path") if $main::Debug{owfs};
}
shift @queue;
$popped = 1;
}
}
}
if ($popped and scalar(@queue)) {
my $hashref = $queue[0];
my $msg_type = $hashref->{msg_type};
my $path = $hashref->{path};
my $message = $hashref->{message};
&main::print_log ( "Owfs_Item::run_loop path: $path message: $message sending socket....") if $main::Debug{owfs};
start $socket unless active $socket;
$socket->set ( $message );
}
}
#=======================================================================================
#
# Owfs_Switch
#
# This package is a common base class for many OWFS Switch like devices which
# have PIO, Latch, and Sense.
#
#=======================================================================================
=begin comment
Usage:
$sensor = new Owfs_Switch ( "<device_id>", <location>, <channel>, <interval>, <mode> );
<device_id> - of the form family.address; identifies the one-wire device
<location> - ASCII string identifier providing a useful name for device_id
<channel> - "0", "1", "2", "3", "4", "5", "6", "7"
<mode> - Identifies what is stored in <state> 0: PIO (Relay) 1: Sense 2: Latch
<interval> - Optional (defaults to 2). Number of seconds between input samples.
Examples:
# RELAY
my $relay = new Owfs_Switch ( "20.DB2506000000", "Some Relay", "0", 0 );
$relay->set_pio("1"); # Turn on Relay
$relay->set_pio("0"); # Turn off Relay
my $state = $state->state( ); # 0: Relay off 1: Relay on
# LATCH
my $doorbell = new Owfs_Switch ( "20.DB2506000000", "Front Door Bell", "1", 1 );
if (my $state = said $doorbell) {
print_log ("notice,,, someone is at the front door");
speak (rooms=>"all", text=> "notice,,, someone is at the front door");
}
or
if (my $state = state_now $doorbell) {
print_log ("notice,,, someone is at the front door");
speak (rooms=>"all", text=> "notice,,, someone is at the front door");
}
or
if (my $state = state_changed $doorbell) {
print_log ("notice,,, someone is at the front door");
speak (rooms=>"all", text=> "notice,,, someone is at the front door");
}
=cut
package Owfs_Switch;
use strict;
use constant;
use constant ON => 'on';
use constant OFF => 'off';
use constant PIO => 0;
use constant SENSE => 1;
use constant LATCH => 2;
@Owfs_Switch::ISA = ('Owfs_Item');
our (%latch_mask);
our (%latch_store);
sub new {
my ($class, $device, $location, $channel, $interval, $mode) = @_;
my $self = new Owfs_Item ( $device, $location );
bless $self,$class;
$self->{channel} = undef;
if (defined $channel) {
$self->{channel} = $channel;
}
$self->{interval} = 1;
if (defined $interval && ($interval >= 1)) {
$self->{interval} = $interval;
}
$self->{mode} = PIO;
if (defined $mode) {
$self->{mode} = $mode;
}
@{$$self{states}} = ('on','off');
$self->{pio} = undef;
$self->{latch} = 0;
$self->{sensed} = undef;
$latch_store{$device} = 0;
if (!exists $latch_mask{$device}) {
$latch_mask{$device} = 0;
}
if (defined $channel) {
$latch_mask{$device} |= (1 << $channel);
}
$self->{loop_timer} = new Timer;
$self->{loop_timer}->set($self->{interval}, sub {Owfs_Switch::run_loop($self);});
&::Reload_pre_add_hook(\&Owfs_Switch::reload_hook, 1);
return $self;
}
sub set_interval {
my ($self,$interval) = @_;
$self->{interval} = $interval if defined $interval;
}
sub get_interval {
my ($self) = @_;
return $self->{interval};
}
sub set {
my ($self,$value) = @_;
my $mode = $self->{mode};
my $debug = $self->{debug} || $main::Debug{owfs};
&main::print_log ("Owfs_Switch::set mode: $mode value: $value") if $debug;
if ($mode eq PIO) {
my $channel = $self->{channel};
my $pio = "PIO";
if (defined $channel) {
$pio .= ".$channel";
}
my $state = $self->convert_state($value);
$self->SUPER::set ($pio, $state);
}
}
sub get {
my ($self) = @_;
return $self->state( );
}
sub set_pio {
my ($self,$value) = @_;
$self->set($value);
}
sub get_pio {
my ($self) = @_;
return unless $self->get_present( );
return $self->{pio};
}
sub get_latch {
my ($self) = @_;
my $device = $self->{device};
my $channel = $self->{channel};
$channel = 0 if (!defined $channel);
return unless $self->get_present( );
return $self->convert_state($self->{latch});
}
sub get_sensed {
my ($self) = @_;
return unless $self->get_present( );
return $self->{sensed};
}
sub set_debug {
my ($self,$debug) = @_;
$self->{debug} = $debug;
}
# This is a helper method to convert states to 'on' and 'off'
sub convert_state {
my ($self, $value) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $channel = $self->{channel};
my $state = $value;
$state = ON if ($value eq 1);
$state = OFF if ($value eq 0);
$state = ON if ($value eq 'yes');
$state = OFF if ($value eq 'no');
if (($state ne ON) && ($state ne OFF)) {
&main::print_log ( "Owfs_Item::convert_state Unknown state device: $device location: $location channel: $channel value: $value state: $state");
}
return $state;
}
# This method is called whenever the object has been discovered. Useful for initialization.
sub discovered {
my ($self) = @_;
my $channel = $self->{channel};
my $mode = $self->{mode};
if ($mode ne PIO) {
my $pio = "PIO";
if (defined $channel) {
$pio .= ".$channel";
}
$self->SUPER::set( $pio, OFF );
}
}
sub process_read_response {
my ($self, $response) = @_;
my $device = $self->{device};
my $location = $self->{location};
my $channel = $self->{channel};
my $pio = "PIO";
my $debug = $self->{debug} || $main::Debug{owfs};
if (defined $channel) {
$pio .= ".$channel";
}
my $sensed = "sensed";
if (defined $channel) {
$sensed .= ".$channel";
}
my $latchstr = "latch";
if (defined $channel) {
$latchstr .= ".$channel";
}
my $mode = $self->{mode};
my $token = $self->{token};
&main::print_log ("Owfs_Switch::process_read_response device: $device location: $location channel: $channel mode: $mode token: $token response: $response") if $debug;
if (defined $response) {
if ($self->{token} =~ /PIO/) {
if ($mode == PIO) {
if ($response ne $self->state( )) {
my $state = $self->convert_state($response);
$self->SUPER::process_read_response($state);
}
}
$self->{pio} = $response;
&main::print_log ("Owfs_Switch::process_read_response $device $location $channel pio: $response") if $debug;
$self->{token} = undef;
$self->{value} = undef;
} elsif ($self->{token} =~ /sensed/) {
if ($mode == SENSE) {
if ($response ne $self->state( )) {
my $state = $self->convert_state($response);
$self->SUPER::process_read_response($state);
}
}
$self->{sensed} = $response;
&main::print_log ("Owfs_Switch::process_read_response $device $location $channel sensed: $response") if $debug;
$self->{token} = undef;
$self->{value} = undef;
} elsif ($self->{token} =~ /latch/) {
$latch_store{$device} |= ($latch_mask{$device} & $response);
my $ls = $latch_store{$device};
my $lm = sprintf ("%x", $latch_mask{$device});
my $chanidx = $channel;
$chanidx = 0 if (!defined $channel);
my $latch = ($latch_store{$device} >> $chanidx) & 1;
my $slatch = $self->{latch};
&main::print_log ("Owfs_Switch::process_read_response device: $device location: $location channel: $channel latch: $latch slatch: $slatch ls: $ls lm: $lm chanidx: $chanidx") if $debug;
if ($mode == LATCH) {
if ($latch ne $self->{latch}) {
my $state = $self->convert_state($latch);
$self->SUPER::process_read_response($state);
$self->{latch} = $latch;
}