aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms-public.gathering.org/old/stempmap.pl
blob: ccd44fd9b122923836f4ba5924e77f71aa6fb533 (plain)
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
#!/usr/bin/perl
#
#
use strict;
use warnings;

use CGI;
use GD;
use DBI;
use File::Basename;
use lib '../../include';
use nms;

GD::Image->trueColor(1);

my $cwd = dirname($0);
my $img = GD::Image->new($cwd.'/tg15-salkart.png');
my $cgi = CGI->new;

my $dbh = nms::db_connect();

my $max_update_age = '\'8 min\'::interval';

# Henter ut de som er oppdatert for mindre enn $max_update_age siden
my $sgetpoll = $dbh->prepare('select switch,sysname,(select temp from switch_temp where switches.switch = switch_temp.switch AND temp != 0 order by time desc limit 1) AS temp,placement from switches natural join placements where now()-'.$max_update_age.' < last_updated');

my $black = $img->colorAllocate(0,0,0);
my $white = $img->colorAllocate(255,255,255);
my $grey  = $img->colorAllocate(192,192,192);
my $green = $img->colorAllocate(0,255,0);
my $blue = $img->colorAllocate(0,0,255);

my $mintemp = 10.0;
my $maxtemp = 55.0;
my $steps = 100;

for (my $i = 0; $i < $steps; $i++) {
	my $diff = $maxtemp - $mintemp;
	my $temp = $mintemp + ($maxtemp - $mintemp) * ((($diff / $steps) * $i)/$diff);
	$img->line(5, $i, 45, $i, &getcolor($temp));
}

$img->stringFT($black, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 50, 10, "Freezing!");
$img->stringFT($black, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 50, 22, "$mintemp C");
$img->stringFT($black, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 50, $steps - 12, "$maxtemp C");
$img->stringFT($black, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 50, $steps, "Too hot!");

$sgetpoll->execute();
while (my $ref = $sgetpoll->fetchrow_hashref()) {
	next if (!defined($ref->{'temp'}));

	my $sysname = $ref->{'sysname'};
	$sysname =~ s/sw$//;

	my $temp = $ref->{'temp'};
	my $color = getcolor($temp);

	$ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
	my ($x2, $y2, $x1, $y1) = ($1, $2, $3, $4);
	$img->filledRectangle($x1,$y1,$x2,$y2,$color);
	$img->rectangle($x1,$y1,$x2,$y2,$black);
	my $max_textlen = ($x2-$x1) > ($y2-$y1) ? $x2-$x1 : $y2-$y1;
	while (length($sysname) * 6 > $max_textlen) {
		# Try to abbreviate sysname if it is too long for the box
		$sysname =~ s/^(.*)[a-z]~?([0-9-]+)$/$1~$2/ or last;
	}
	if (($x2-$x1) > ($y2-$y1)) {
		$img->string(gdSmallFont,$x1+2,$y1,$sysname,$white);
	} else {
		$img->stringUp(gdSmallFont,$x1,$y2-3,$sysname,$white);
	}
	$img->string(gdGiantFont, $x2-(length("$temp") * 9), $y1, "$temp", $white);
}

print $cgi->header(-type=>'image/png');
print $img->png;

sub getcolor {
	my ($temp) = @_;

	my $t = ($temp - $mintemp) / ($maxtemp - $mintemp);
	$t = 0 if ($t < 0);
	$t = 1 if ($t > 1);

	my $colorred = 65025 * $t;
	my $colorblue = 65025 - $colorred;

	return $img->colorResolve(sqrt($colorred), 0, sqrt($colorblue) );
}