Skip to content
Browse files

initial work on directives: understands message and led directives

  • Loading branch information...
1 parent 7dd6271 commit 35e5344508980f629dd2454d169992b74e3e4b2a @windley committed
Showing with 61 additions and 12 deletions.
  1. +3 −1 README.md
  2. +58 −11 thermometer.pl
View
4 README.md
@@ -1,4 +1,6 @@
radiostat
=========
-KRE endpoint for radiostat thermometers
+KRE endpoint for radiostat thermometers.
+
+A blog post about this: http://www.windley.com/archives/2012/07/a_kynetx_endpoint_for_radio_thermostats.shtml
View
69 thermometer.pl
@@ -69,11 +69,16 @@
# provided by Log::Report. Output still also to the screen.
dispatcher SYSLOG => 'syslog', accept => 'INFO-', identity => 'thermometer daemon', facility => 'local0';
dispatcher mode => $mode, 'ALL' if $mode;
+#warn "Mode is $mode";
+
+my $daemon;
+$daemon = Any::Daemon->new(%os_opts);
-my $daemon = Any::Daemon->new(%os_opts);
foreach my $k (keys %{ $config->{devices} }) {
+# info "starting device $k";
+
if ($config->{devices}->{$k}->{type} eq 'thermometer') {
$daemon->run(child_task => &run_temperature_task($k, $config->{devices}->{$k}),
%run_opts
@@ -116,7 +121,7 @@ (@)
return sub {
- info "Starting temperature daemon";
+ warning "Starting temperature daemon";
while(1) {
@@ -125,20 +130,32 @@ (@)
if ($res->is_success) {
my $scalar = from_json($res->content);
- info "Temperature is " . $scalar->{ 'temp' } . "\n";
+# info "Temperature is " . $scalar->{ 'temp' } . "\n";
+ warning "Temperature is " . $scalar->{ 'temp' } . "\n";
my $response = $event->raise({'temperature' => $scalar->{ 'temp' },
}
);
- # foreach my $d (@{$response->{'directives'}}) {
- # if ($d->{'name'} eq 'thermostat') {
- # foreach my $o ($d->{'options'}) {
- # info $o
- # }
- # }
- # }
-
+# warning Dumper $response->{'directives'};
+
+ foreach my $d (@{$response->{'directives'}}) {
+ if ($d->{'name'} eq 'radstat') {
+ my $o = $d->{'options'};
+ if ($o->{'message'}) {
+ radstat_request($ua, "$thermo_url/pma", "message set", {message => $o->{'message'}} );
+ } elsif ($o->{'led'}) {
+ my $color = ($o->{'led'} eq "green") ? 1 :
+ ($o->{'led'} eq "yellow") ? 2 :
+ ($o->{'led'} eq "red") ? 4 :
+ 0 ; # off
+ radstat_request($ua, "$thermo_url/led", "LED color change ($color)" , {energy_led => $color});
+ }
+ }
+ }
+
+ } else {
+ warning "Can't connect to thermostat: " + $res->message;
}
sleep $sleep_sec;
@@ -164,4 +181,34 @@ sub read_config {
}
+#
+# radstat functions
+#
+
+sub radstat_request {
+ my ($ua, $resource_uri, $message, $content ) = @_;
+ my $post = HTTP::Request->new(POST => $resource_uri);
+ $post->content(to_json($content));
+ my $res = $ua->request($post);
+ if ($res->is_success()) {
+ parse_response($res, $message);
+ } else {
+ warning "HTTP request to $resource_uri failed: " . $res->message();
+ }
+
+}
+
+sub parse_response {
+ my($res, $message) = @_;
+ my $content = from_json($res->content());
+ if (defined $content->{'success'}) {
+ warning "$message succeeded";
+ } elsif (defined $content->{'error'}) {
+ warning "$message failed";
+ } else {
+ warning "No status for $message";
+ warning Dumper $res;
+ }
+}
+
1;

0 comments on commit 35e5344

Please sign in to comment.
Something went wrong with that request. Please try again.