]> git.sesse.net Git - nms/blob - web/stempmap.pl
2399b31677dcb977519315d3db09ac5ff9ac663c
[nms] / web / stempmap.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 use CGI;
6 use GD;
7 use DBI;
8 use lib '../include';
9 use nms;
10 use strict;
11 use warnings;
12
13 GD::Image->trueColor(1);
14
15 my $img = GD::Image->new('bg2.png');
16 #my $img = GD::Image->new(100,100);
17 my $cgi = CGI->new;
18
19 my $dbh = nms::db_connect();
20
21 my $sgetpoll = $dbh->prepare('select switch,(select temp from temppoll where switches.switch=temppoll.switch AND temp != 0 order by id desc limit 1) AS temp,placement from switches natural join placements');
22
23 my $black = $img->colorAllocate(0,0,0);
24 my $white = $img->colorAllocate(255,255,255);
25 my $green = $img->colorAllocate(0,255,0);
26 my $blue = $img->colorAllocate(0,0,255);
27
28 my $avrgtemp = 54.5;
29 my $mintemp = 30.0;
30 my $maxtemp = 55.0;
31 my $steps = 100;
32
33 my $color = &getcolor($avrgtemp);
34
35 open (OUT, "> /tmp/outtemps");
36 for (my $i = 0; $i < $steps; $i++) {
37         my $diff = $maxtemp - $mintemp;
38         my $temp = $mintemp + ($maxtemp - $mintemp) * ((($diff / $steps) * $i)/$diff);
39         $img->line(5, $i, 45, $i, &getcolor2($temp));
40 }
41 close(OUT);
42
43 $img->string(gdMediumBoldFont, 50, 0, "Freezing!", $black);
44 $img->string(gdMediumBoldFont, 50, $steps - 11, "Too hot!", $black);
45 $img->string(gdMediumBoldFont, 50, 12, "$mintemp C", $black);
46 $img->string(gdMediumBoldFont, 50, $steps - 22, "$maxtemp C", $black);
47
48 #$img->string(gdMediumBoldFont, 600, 200, "Freezing!$mintemp", $black);
49 #$img->string(gdMediumBoldFont, 600, 210, "$mintemp C", $black);
50 #$img->string(gdMediumBoldFont, 600, 220 + $maxtemp, "Too hot! $maxtemp", $black);
51 #$img->string(gdMediumBoldFont, 600, 210 + $maxtemp, "$maxtemp C", $black);
52
53
54 $sgetpoll->execute();
55 while (my $ref = $sgetpoll->fetchrow_hashref()) {
56         next if (!defined($ref->{'temp'}));
57
58         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
59         $avrgtemp = $ref->{'temp'};
60         my $px = $3;
61         my $py = $4 + 16;
62         my $roundtemp = sprintf ("%.0f", $avrgtemp);
63         $color = getcolor($avrgtemp);
64         $img->filledRectangle($px - 0, $py - 17, $px + 12, $py + 5, $white);
65         $img->stringUp(gdGiantFont,$px - 1,$py - 1,"$roundtemp",$blue);
66         $img->rectangle($px, $py, $px + 10,$py + 10,$black);
67         $img->filledRectangle($px + 1, $py + 1, $px + 9, $py + 9,$color);
68 }
69
70 print $cgi->header(-type=>'image/png');
71 print $img->png;
72
73 sub getcolor {
74         my ($temp) = @_;
75
76         my $t = ($temp - $mintemp) / ($maxtemp - $mintemp);
77         $t = 0 if ($t < 0);
78         $t = 1 if ($t > 1);
79
80         my $colorred = 255 * $t;
81         my $colorblue = 255 - $colorred;
82
83         return $img->colorResolve($colorred, 0, $colorblue);
84 }
85
86 sub getcolor2 {
87         my ($temp) = @_;
88
89         my $t = ($temp - $mintemp) / ($maxtemp - $mintemp);
90         $t = 0 if ($t < 0);
91         $t = 1 if ($t > 1);
92
93         my $colorred = 255 * $t;
94         my $colorblue = 255 - $colorred;
95
96         return $img->colorResolve($colorred, 0, $colorblue);
97 }