From 97a8650e47ad573e95a414ac173b4dbcbe11a902 Mon Sep 17 00:00:00 2001 From: H Plato Date: Tue, 25 Nov 2014 14:51:55 -0700 Subject: [PATCH 1/3] Added communication child object, cleaned up docs modified: raZberry.pm --- lib/raZberry.pm | 113 ++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 41 deletions(-) diff --git a/lib/raZberry.pm b/lib/raZberry.pm index a9de9a526..9550b56bd 100755 --- a/lib/raZberry.pm +++ b/lib/raZberry.pm @@ -6,6 +6,7 @@ In user code: use raZberry.pm; $razberry_controller = new raZberry('192.168.0.100',1); + $razberry_comm = new raZberry_comm($razberry_controller); $family_room_fan = new raZberry_dimmer($razberry_controller,'2:0:38','force_update'); So far only raZberry_dimmer is a working child object @@ -16,43 +17,35 @@ In items.mht: =head2 DESCRIPTION -Support for the Rasberry PI raZberry GPIO card. Uses web services to poll connected -device status, as well as control zwave items. + Uses w -The controller object itself doesn't do anything, it serves as the conduit to connect MH -to the zwave network. The only real useful method is display_all_devices in which the razberry -will echo all the devices that it knows about. +=head3 LINKING ZWAVE devices -=head3 NEW OBJECT +Devices need to first linked inside the razberry using the included web interface. -To create a new object; +=head3 STATE REPORTED IN MisterHouse - $mh_object = new raZberry('set('on'); -To help control fans there are also, low, med and high states. -Turning off a device: - - $raz_dimmer->set('off'); - -To get the actual dim level: - - $raz_dimmer->level; +=head2 METHODS =over @@ -69,6 +62,8 @@ use HTTP::Request::Common qw(POST); use JSON::XS; use Data::Dumper; +#todo +# - dump_all_devices to see everything raZberry knows @raZberry::ISA = ('Generic_Item'); @@ -102,6 +97,8 @@ sub new { $self->{port} = $port if ($port); $self->{debug} = 0; $self->{lastupdate} = undef; + $self->{timeout} = 5; #300; + $self->{timer} = new Timer; $self->start_timer; @@ -175,7 +172,6 @@ sub set_dev { my ($isSuccessResponse1,$status) = _get_JSON_data($self, 'devices', $cmd); unless ($isSuccessResponse1) { &main::print_log("[raZberry] Problem retrieving data from " . $self->{host}); - $self->{data}->{retry}++; return ('0'); } @@ -192,6 +188,8 @@ sub _get_JSON_data { $self->{updating} = 1; my $ua = new LWP::UserAgent(keep_alive=>1); + $ua->timeout($self->{timeout}); + my $host = $self->{host}; my $port = $self->{port}; my $params = ""; @@ -211,10 +209,21 @@ sub _get_JSON_data { my $isSuccessResponse = $responseCode < 400; $self->{updating} = 0; if (! $isSuccessResponse ) { - &main::print_log("[raZberry] Warning, failed to get data. Response code $responseCode"); - $self->{data}->{retry}++; + &main::print_log("[raZberry] Warning, failed to get data. Response code $responseCode"); + if (defined $self->{child_object}->{comm}) { + if ($self->{child_object}->{comm}->state() ne "offline") { + main::print_log "Communication Tracking object found. Updating..." if ($self->{loglevel}); + $self->{child_object}->{comm}->set("offline",'poll'); + } + } return ('0'); } + if (defined $self->{child_object}->{comm}) { + if ($self->{child_object}->{comm}->state() ne "online") { + main::print_log "Communication Tracking object found. Updating..." if ($self->{loglevel}); + $self->{child_object}->{comm}->set("online",'poll'); + } + } return ('1') if ($mode eq "force_update"); my $response = JSON::XS->new->decode ($responseObj->content); return ($isSuccessResponse, $response) @@ -267,23 +276,18 @@ sub get_dev_status { } -sub get_error_count { - my ($self) @_; - return ($self->{data}->{retry}); -} - -sub reset_error_count { - my ($self) @_; - $self->{data}->{retry} = 0; -} - sub register { my ($self, $object, $dev, $options ) = @_; - &main::print_log("[raZberry] Registering Device ID $dev to controller"); - $self->{child_object}->{$dev} = $object; - if ($options =~ m/force_update/) { - $self->{data}->{force_update}->{$dev} = 1; - &main::print_log("[raZberry] Forcing Controller to contact Device $dev at each poll"); + if (lc $dev eq 'comm') { + &main::print_log("[raZberry] Registering Communication object to controller"); + $self->{child_object}->{'comm'} = $object; + } else { + &main::print_log("[raZberry] Registering Device ID $dev to controller"); + $self->{child_object}->{$dev} = $object; + if ($options =~ m/force_update/) { + $self->{data}->{force_update}->{$dev} = 1; + &main::print_log("[raZberry] Forcing Controller to contact Device $dev at each poll"); + } } } @@ -356,4 +360,31 @@ sub level { return ($self->{level}); } +package raZberry_comm; + +@raZberry_comm::ISA = ('Generic_Item'); + +sub new +{ + my ($class,$object) = @_; + + my $self={}; + bless $self,$class; + + $$self{master_object} = $object; + push(@{$$self{states}}, 'online','offline'); + $object->register($self,'comm'); + return $self; + +} + +sub set { + my ($self,$p_state,$p_setby) = @_; + + if ($p_setby eq 'poll') { + $self->SUPER::set($p_state); + } +} + + 1; From e4ce1228c4a2b111bd0e3355e6286ee04dbddeb5 Mon Sep 17 00:00:00 2001 From: H Plato Date: Sun, 4 Jan 2015 12:23:49 -0700 Subject: [PATCH 2/3] raZberry v2.0.0 update. Doesn't work now tieh 1.7.2 modified: raZberry.pm --- lib/raZberry.pm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/raZberry.pm b/lib/raZberry.pm index 9550b56bd..d03750654 100755 --- a/lib/raZberry.pm +++ b/lib/raZberry.pm @@ -71,7 +71,7 @@ use Data::Dumper; # -------------------- START OF SUBROUTINES -------------------- # -------------------------------------------------------------- -my $zway_vdev="ZWayVDev"; +my $zway_vdev="ZWayVDev_zway"; our %rest; $rest{api} = ""; $rest{devices} = "devices"; @@ -95,7 +95,7 @@ sub new { $self->{host} = $host; $self->{port} = 8083; $self->{port} = $port if ($port); - $self->{debug} = 0; + $self->{debug} = 5; $self->{lastupdate} = undef; $self->{timeout} = 5; #300; @@ -118,7 +118,7 @@ sub poll { for my $dev (keys %{$self->{data}->{force_update}}) { &main::print_log("[raZberry] Forcing update to device $dev to account for local changes") if ($self->{debug}); my $cmd; - my ($devid,$instance,$class) = (split /:/,$dev)[0,1,2]; + my ($devid,$instance,$class) = (split /-/,$dev)[0,1,2]; $cmd = "%5B" . $devid . "%5D.instances%5B" . $instance . "%5D.commandClasses%5B" . $class ."%5D.Get()"; &main::print_log("cmd=$cmd") if ($self->{debug} > 1); my ($isSuccessResponse0,$status) = _get_JSON_data($self, 'force_update', $cmd); @@ -135,7 +135,8 @@ sub poll { $self->{lastupdate} = $devices->{data}->{updateTime}; foreach my $item (@{$devices->{data}->{devices}}) { &main::print_log("Found:" . $item->{id} . " with level " . $item->{metrics}->{level} . " and updated " . $item->{updateTime} . ".") if ($self->{debug}); - my ($id) = (split /_/,$item->{id})[1]; + my ($id) = (split /_/,$item->{id})[2]; +print "id=$id\n"; $self->{data}->{devices}->{$id}->{level} = $item->{metrics}->{level}; $self->{data}->{devices}->{$id}->{updateTime} = $item->{updateTime}; $self->{data}->{devices}->{$id}->{devicetype} = $item->{deviceType}; From d274a71215b4d99c226c8f526119110669bfc968 Mon Sep 17 00:00:00 2001 From: H Plato Date: Mon, 16 Feb 2015 21:04:43 -0700 Subject: [PATCH 3/3] Removed some debug settings modified: raZberry.pm --- lib/raZberry.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/raZberry.pm b/lib/raZberry.pm index d03750654..133ffb4de 100755 --- a/lib/raZberry.pm +++ b/lib/raZberry.pm @@ -95,7 +95,7 @@ sub new { $self->{host} = $host; $self->{port} = 8083; $self->{port} = $port if ($port); - $self->{debug} = 5; + $self->{debug} = 0; $self->{lastupdate} = undef; $self->{timeout} = 5; #300; @@ -136,7 +136,7 @@ sub poll { foreach my $item (@{$devices->{data}->{devices}}) { &main::print_log("Found:" . $item->{id} . " with level " . $item->{metrics}->{level} . " and updated " . $item->{updateTime} . ".") if ($self->{debug}); my ($id) = (split /_/,$item->{id})[2]; -print "id=$id\n"; + #print "id=$id\n"; $self->{data}->{devices}->{$id}->{level} = $item->{metrics}->{level}; $self->{data}->{devices}->{$id}->{updateTime} = $item->{updateTime}; $self->{data}->{devices}->{$id}->{devicetype} = $item->{deviceType};