sensor graphs for homepage
authorBernhard Tittelbach <xro@realraum.at>
Tue, 23 Mar 2010 17:46:24 +0000 (17:46 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 23 Mar 2010 17:46:24 +0000 (17:46 +0000)
sensor_graph/get_realraum_picture.pl [new file with mode: 0755]
sensor_graph/get_sensor_light.pl [new file with mode: 0644]
sensor_graph/get_sensor_temp.pl [new file with mode: 0755]
sensor_graph/sensor-light.cgi [new file with mode: 0755]
sensor_graph/sensor-temp.cgi [new file with mode: 0755]

diff --git a/sensor_graph/get_realraum_picture.pl b/sensor_graph/get_realraum_picture.pl
new file mode 100755 (executable)
index 0000000..bd97148
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/perl -w
+#
+#
+
+use strict;
+use utf8;
+require LWP::UserAgent;
+my $ua = LWP::UserAgent->new(env_proxy => 1,
+                              keep_alive => 1,
+                              timeout => 30,
+                             );
+use HTTP::Cookies;
+use LWP;
+$ua->cookie_jar({});
+
+###############
+$cam::url = "http://slug.realraum.at:8088/?action=snapshot";
+#$cam::get = {action=>"snapshot"};
+$cam::localpath = "/tmp/realraum.jpg";
+$cam::mintime = 0.00025;
+###############
+
+sub output_saved_image
+{
+  return 0 if ( not -e $cam::localpath);
+  my $fh;  
+  print STDOUT "Content-type: image/jpeg\n\n";
+  open($fh,"<$cam::localpath") or exit;
+  while (<$fh>) {print $_};
+  close($fh);
+  return 1;
+}
+
+sub output_remote_image_and_save
+{
+  my $response;
+  $response = $ua->get($cam::url);
+  if (defined $response and $response->content =~ /^\xff\xd8/)
+  {    
+    my $fh;
+    open($fh,">$cam::localpath");
+    print $fh $response->content;
+    close($fh);
+    print STDOUT "Content-type: image/jpeg\n\n";
+    print $response->content;
+    $response->clear;
+    return 1;
+  }
+  return 0;
+}
+
+sub output_error
+{
+  print STDOUT "Status: 404 Not Found\n";
+  print STDOUT "Content-type: text/html\n\n";
+  print STDOUT "<html><body><h1>Sorry</h1><h2>The picture you requested could not be found</h2></body></html>\n"; 
+  return 1;
+}
+
+if (-e $cam::localpath and -M $cam::localpath < $cam::mintime)
+{
+  exit if (&output_saved_image);
+}
+else
+{
+  exit if (&output_remote_image_and_save);
+  exit if (&output_saved_image);
+}
+&output_error;
diff --git a/sensor_graph/get_sensor_light.pl b/sensor_graph/get_sensor_light.pl
new file mode 100644 (file)
index 0000000..85fe638
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -w
+#
+#
+
+use strict;
+use utf8;
+require LWP::UserAgent;
+my $ua = LWP::UserAgent->new(env_proxy => 1,
+                              keep_alive => 1,
+                              timeout => 30,
+                             );
+use HTTP::Cookies;
+use LWP;
+$ua->cookie_jar({});
+
+###############
+$sensor::url_refresh = "http://slug.realraum.at/cgi-bin/sensor-light.cgi";
+$sensor::url_image = "http://slug.realraum.at/light0.png";
+$sensor::localpath = "/tmp/light0.png";
+$sensor::mintime = 0.0015;
+###############
+
+sub output_saved_image
+{
+  return 0 if ( not -e $sensor::localpath);
+  my $fh;  
+  print STDOUT "Content-type: image/png\n\n";
+  open($fh,"<$sensor::localpath") or exit;
+  while (<$fh>) {print $_};
+  close($fh);
+  return 1;
+}
+
+sub output_remote_image_and_save
+{
+  my $response;
+  $response = $ua->get($sensor::url_refresh);
+  return 0 unless (defined $response);
+  $response = $ua->get($sensor::url_image);
+  if (defined $response and $response->content =~ /^\x89PNG/)
+  {
+    my $fh;
+    open($fh,">$sensor::localpath");
+    print $fh $response->content;
+    close($fh);
+    print STDOUT "Content-type: image/png\n\n";
+    print $response->content;
+    $response->clear;
+    return 1;
+  }
+  return 0;
+}
+
+sub output_error
+{
+  print STDOUT "Status: 404 Not Found\n";
+  print STDOUT "Content-type: text/html\n\n";
+  print STDOUT "<html><body><h1>Sorry</h1><h2>The picture you requested could not be found</h2></body></html>\n"; 
+  return 1;
+}
+
+if (-e $sensor::localpath and -M $sensor::localpath < $sensor::mintime)
+{
+  exit if (&output_saved_image);
+}
+else
+{
+  exit if (&output_remote_image_and_save);
+  exit if (&output_saved_image);
+}
+&output_error;
diff --git a/sensor_graph/get_sensor_temp.pl b/sensor_graph/get_sensor_temp.pl
new file mode 100755 (executable)
index 0000000..39b5d1f
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -w
+#
+#
+
+use strict;
+use utf8;
+require LWP::UserAgent;
+my $ua = LWP::UserAgent->new(env_proxy => 1,
+                              keep_alive => 1,
+                              timeout => 30,
+                             );
+use HTTP::Cookies;
+use LWP;
+$ua->cookie_jar({});
+
+###############
+$sensor::url_refresh = "http://slug.realraum.at/cgi-bin/sensor-temp.cgi";
+$sensor::url_image = "http://slug.realraum.at/temp0.png";
+$sensor::localpath = "/tmp/temp0.png";
+$sensor::mintime = 0.0015;
+###############
+
+sub output_saved_image
+{
+  return 0 if ( not -e $sensor::localpath);
+  my $fh;  
+  print STDOUT "Content-type: image/png\n\n";
+  open($fh,"<$sensor::localpath") or exit;
+  while (<$fh>) {print $_};
+  close($fh);
+  return 1;
+}
+
+sub output_remote_image_and_save
+{
+  my $response;
+  $response = $ua->get($sensor::url_refresh);
+  return 0 unless (defined $response);
+  $response = $ua->get($sensor::url_image);
+  if (defined $response and $response->content =~ /^\x89PNG/)
+  {
+    my $fh;
+    open($fh,">$sensor::localpath");
+    print $fh $response->content;
+    close($fh);
+    print STDOUT "Content-type: image/png\n\n";
+    print $response->content;
+    $response->clear;
+    return 1;
+  }
+  return 0;
+}
+
+sub output_error
+{
+  print STDOUT "Status: 404 Not Found\n";
+  print STDOUT "Content-type: text/html\n\n";
+  print STDOUT "<html><body><h1>Sorry</h1><h2>The picture you requested could not be found</h2></body></html>\n"; 
+  return 1;
+}
+
+if (-e $sensor::localpath and -M $sensor::localpath < $sensor::mintime)
+{
+  exit if (&output_saved_image);
+}
+else
+{
+  exit if (&output_remote_image_and_save);
+  exit if (&output_saved_image);
+}
+&output_error;
diff --git a/sensor_graph/sensor-light.cgi b/sensor_graph/sensor-light.cgi
new file mode 100755 (executable)
index 0000000..bf3f019
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/rrdcgi
+<HTML>
+<BODY>
+<RRD::GOODFOR 30>
+<RRD::GRAPH ../light0.png
+   --lazy --imginfo '<IMG SRC="/%s" WIDTH="%lu" HEIGHT="%lu" >'
+   --title="Room Illumination"
+   DEF:cel=/home/sensorlight.rrd:light:LAST
+   LINE2:cel#00a000:"0 dark to 1023 bright">
+</BODY>
+</HTML>
diff --git a/sensor_graph/sensor-temp.cgi b/sensor_graph/sensor-temp.cgi
new file mode 100755 (executable)
index 0000000..ee2bf34
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/rrdcgi
+<HTML>
+<BODY>
+<RRD::GOODFOR 30>
+<RRD::GRAPH ../temp0.png
+   --lazy --imginfo '<IMG SRC="/%s" WIDTH="%lu" HEIGHT="%lu" >'
+   --title="Temperatures"
+   DEF:cel=/home/sensortemp.rrd:temp:LAST
+   LINE2:cel#00a000:"D. Celsius">
+</BODY>
+</HTML>