-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdm72-read.pl
executable file
·124 lines (98 loc) · 3.48 KB
/
sdm72-read.pl
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
$| = 1;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
use lib "lib";
use Device::Modbus::TCP::Client;
use sdm630;
my $file = shift || 'sdm630.conf';
my $ref_config = SDM630::read_config($file);
$SIG{CHLD} = \&REAPER;
# TODO: catch connection timeout error and retry the connection
while (1==1) {
my $now = qx/date/;
chomp $now;
my %values;
my $ref_client = Device::Modbus::TCP::Client->new(host => $ref_config->{'IP_ADDRESS'}, timeout => $ref_config->{'TIMEOUT'});
printf("\n@%s", $now);
my @output = ();
foreach my $ref_device (@{$ref_config->{'DEVICE'}}) {
# data retrieval with some exception handling
my $ref_values;
eval {
$ref_values = SDM630::retrieve_all($ref_client, $ref_device->{'UNIT'}, $ref_device->{'TYPE'});
1; # ok
} or do {
my $eval_error= $@ || "error";
warn "Warning: device connection lost - reconnecting ...\n";
eval {
$ref_client = Device::Modbus::TCP::Client->new(host => $ref_config->{'IP_ADDRESS'}, timeout => $ref_config->{'TIMEOUT'});
$ref_values = SDM630::retrieve_all($ref_client, $ref_device->{'UNIT'}, $ref_device->{'TYPE'});
} or do {
my $eval_error= $@ || "error";
print "ERROR: connection unstable.\n";
}; # needs a semicolon
}; # needs a semicolon
$values{$ref_device->{'UNIT'}} = {
'type' => $ref_device->{'TYPE'},
'data' => $ref_values,
};
printf(" - %s (%s)", $ref_device->{'NAME'}, $ref_device->{'TYPE'});
my $prefix = sprintf("%s - %-6s (%-6s) - ", $now, $ref_device->{'NAME'}, $ref_device->{'TYPE'});
push @output, prefix_lines($prefix, SDM630::output_values($ref_values));
push @output, prefix_lines($prefix, SDM630::feed_rrds($ref_values, $ref_device->{'NAME'}));
if ($ref_device->{'TYPE'} eq 'SDM630') {
push @output, prefix_lines($prefix, SDM630::feed_rrds($ref_values, 'test'));
}
}
$ref_client->disconnect;
print "\n";
if ((time % (60*$ref_config->{'INDEX_TIME'})) < 10) {
foreach my $ref_device (@{$ref_config->{'DEVICE'}}) {
if (! -f $ref_config->{'WEBDIR'}.'/'.$ref_device->{'NAME'}.'/index.html') {
SDM630::generate_indexes($ref_config->{'WEBDIR'}, $ref_device->{'TYPE'}, $ref_device->{'NAME'});
}
}
}
if ((time % (60*$ref_config->{'DIAGRAM_TIME'})) < 10) {
my $pid = fork();
if ($pid == 0) {
print "Forked - starting diagram generator...\n";
foreach my $ref_device (@{$ref_config->{'DEVICE'}}) {
SDM630::generate_diagrams($ref_config->{'WEBDIR'}, $ref_device->{'TYPE'}, $ref_device->{'NAME'});
}
exit 0;
} else {
print "Forked graph creation (PID=$pid)\n";
}
}
# every 5 minutes, generate a new summary file with all current readings
if ((time % (60*$ref_config->{'SUMMARY_TIME'})) < 10) {
open(my $h_file, '>', 'web/readings.txt');
print $h_file $now, "\n", @output;
close($h_file);
}
my $sleeptime = 10 - time % 10;
print join('', $now, "\n", @output, 'sleep: '.$sleeptime.'s ... ');
sleep $sleeptime;
}
use POSIX ":sys_wait_h";
sub REAPER {
local $!; # don't let waitpid() overwrite current error
while ((my $pid = waitpid(-1, WNOHANG)) > 0 && WIFEXITED($?)) {
print "reaped $pid" . ($? ? " with exit $?" : "");
}
$SIG{CHLD} = \&REAPER; # loathe SysV
}
sub prefix_lines {
my $prefix = shift;
my @output;
foreach my $line (@_) {
push @output, $prefix.$line;
}
return @output;
}