-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolarcharger.perl
executable file
·133 lines (105 loc) · 4.91 KB
/
solarcharger.perl
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
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl
use strict;
use FindBin; use lib "$FindBin::Bin/perl5/lib/perl5","$FindBin::Bin","$FindBin::Bin/JNX";
use utf8;
use English;
use JNX::JLog;
use JNX::UDPServer;
use JNX::HTTPToUDPServer;
use JNX::SolarWorker;
use JNX::Configuration;
my %commandlineoption = JNX::Configuration::newFromDefaults(
'localaddress' => [ '0.0.0.0','string'],
'localport' => [5145,'number'],
'cars' => ['','string'],
'basecarurl' => ['','string'],
);
{
my $localaddress = $commandlineoption{'localaddress'};
my $serverport = $commandlineoption{'localport'};
my $fatherprocess = $$;
my %children;
sub killchildren {
my @childprocesses = keys %children;
JNX::JLog::fatal "$$ children died ",join(',',@childprocesses).'args:'.join(',', @_);
kill('KILL',@childprocesses) if @childprocesses > 0;
exit;
};
$SIG{CHLD} = sub {
# don't change $! and $? outside handler
local ($!, $?);
while ( (my $pid = waitpid(-1, WNOHANG)) > 0 )
{
if( defined $children{$pid} )
{
JNX::JLog::fatal "known child died - killing everything";
killchildren();
}
else
{
JNX::JLog::error "unknown child died doing nothing.";
}
}
};
$SIG{'HUP'} = sub { JNX::JLog::error "Sig HUP received"; killchildren(); };
$SIG{'INT'} = sub { JNX::JLog::error "Sig INT received"; killchildren(); };
$SIG{'QUIT'} = sub { JNX::JLog::error "Sig QUIT received"; killchildren(); };
$SIG{'ILL'} = sub { JNX::JLog::error "Sig ILL received"; killchildren(); };
$SIG{'TRAP'} = sub { JNX::JLog::error "Sig TRAP received"; killchildren(); };
$SIG{'ABRT'} = sub { JNX::JLog::error "Sig ABRT received"; killchildren(); };
$SIG{'TERM'} = sub { JNX::JLog::error "Sig TERM received"; killchildren(); };
$SIG{'IGABRT'} = sub { JNX::JLog::error "Sig IGABRT received"; killchildren(); };
$SIG{'SIGABRT'} = sub { JNX::JLog::error "Sig SIGABRT received"; killchildren(); };
if( my $childid = createChildProcess('JNX::UDPServer',\&runUDPServer,$localaddress,$serverport) ) { $children{$childid}=1; }
if( my $childid = createChildProcess('TCPServer',\&runTCPServer,$localaddress,$serverport) ) { $children{$childid}=1; }
JNX::JLog::trace "Father: waiting for children to exit";
my $child = wait();
JNX::JLog::trace "Father: child died $child";
exit;
}
exit;
sub createChildProcess
{
my($processname,$subroutine,@args) = @_;
JNX::JLog::trace "Creating child: $processname $subroutine";
if( my $childprocessid = fork() )
{
JNX::JLog::trace "$processname father $$ $childprocessid";
return $childprocessid;
}
JNX::JLog::trace "$processname child $$";
$SIG{'CHLD'} = sub { wait(); };
$PROGRAM_NAME = $PROGRAM_NAME.' '.$processname;
$subroutine->(@args);
exit;
}
sub runUDPServer
{
my($localaddress,$serverport) = @_;
JNX::JLog::trace();
my @carConnectors;
for my $carname (split(/,/,$commandlineoption{'cars'}))
{
my $carurl = $commandlineoption{'basecarurl'}.$carname.'.json';
JNX::JLog::debug("creating carconnector: $carurl");
my $carConnector = JNX::BMWConnector->new( url => $carurl ) || die "Could not create BMWConnector $carname";
JNX::JLog::debug("Created carConnector: ".$carConnector->{url});
push(@carConnectors,$carConnector);
}
my $evCharger = JNX::PhoenixCharger->new() || die "Could not create PhoenixCharger";
my $pvReader = JNX::SMAReader->new() || die "Could not create SMAReader";
my $solarworker = JNX::SolarWorker->new( JNX::SolarWorker::Options::pvReader => $pvReader,
JNX::SolarWorker::Options::evCharger => $evCharger,
JNX::SolarWorker::Options::carConnectors => \@carConnectors,
) || die "Could not create JNX::SolarWorker";
my $udpserver = JNX::UDPServer->new( LocalAddr => $localaddress ,LocalPort => $serverport ,Worker => $solarworker);
$udpserver->run();
}
sub runTCPServer
{
my($localaddress,$serverport) = @_;
JNX::JLog::trace();
my $httpToJNXUDPServer = JNX::HTTPToUDPServer->new( LocalAddr => $localaddress ,LocalPort => $serverport );
$httpToJNXUDPServer->run();
exit;
}