App-EventStreamr
view release on metacpan or search on metacpan
bin/station-mgr.pl view on Meta::CPAN
# set umask
umask 0027;
# EventStremr Modules
use App::EventStreamr::Devices;
our $devices = App::EventStreamr::Devices->new();
use App::EventStreamr::Utils;
our $utils = App::EventStreamr::Utils->new();
# Load/Build Local Config
my $localconfig;
if (-e "$Bin/../settings.json") {
$localconfig = Config::JSON->new("$Bin/../settings.json");
$localconfig = $localconfig->{config};
} else {
$localconfig = Config::JSON->create("$Bin/../settings.json");
$localconfig->{config} = blank_settings();
$localconfig->write;
$localconfig = $localconfig->{config};
}
# Station Config
our $stationconfig;
if (-e "$Bin/../station.json") {
$stationconfig = Config::JSON->new("$Bin/../station.json");
} else {
$stationconfig = Config::JSON->create("$Bin/../station.json");
$stationconfig->{config} = blank_station();
$stationconfig->write;
}
# Commands
my $commands = Config::JSON->new("$Bin/../commands.json");
# Own data
our $self;
$self->{config} = $stationconfig->{config};
$self->{config}{macaddress} = getmac();
$self->{devices} = $devices->all();
$self->{commands} = $commands->{config};
$self->{dvswitch}{check} = 1; # check until dvswitch is found
$self->{dvswitch}{running} = 0;
$self->{settings} = $localconfig;
$self->{date} = strftime "%Y%m%d", localtime;
if ($self->{config}{run} == 2) {$self->{config}{run} = 1;}
# Logging
unless ( $DEBUG ) {
$self->{loglevel} = 'INFO, LOG1' ;
} else {
$self->{loglevel} = 'DEBUG, LOG1, SCREEN' ;
}
unless (-d "$Bin/../logs/") {
make_path("$Bin/../logs/");
}
my $log_conf = qq(
log4perl.rootLogger = $self->{loglevel}
log4perl.appender.SCREEN = Log::Log4perl::Appender::Screen
log4perl.appender.SCREEN.stderr = 0
log4perl.appender.SCREEN.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.SCREEN.layout.ConversionPattern = %m %n
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.utf8 = 1
log4perl.appender.LOG1.filename = $Bin/../logs/station-mgr.log
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
);
Log::Log4perl::init(\$log_conf);
our $logger = Log::Log4perl->get_logger();
$logger->info("manager starting: pid=$$, station_id=$self->{config}->{macaddress}");
$daemons->{main}{run} = 1;
# HTTP
our $http = HTTP::Tiny->new(timeout => 15);
# Start the API
api();
# Register with controller
$logger->info("Registering with controller $localconfig->{controller}/api/station/$self->{config}{macaddress}");
my $response = $http->post("$localconfig->{controller}/api/station/$self->{config}{macaddress}");
# Controller responds with created 201, post our config
if ($response->{status} == 201) {
$logger->info("Posting config $localconfig->{controller}");
# Status Post Data
my $json = to_json($self->{config});
my %headers = (
'station-mgr' => 1,
'Content-Type' => 'application/json',
);
my %post_data = (
content => $json,
headers => \%headers,
);
$response = $http->post("$localconfig->{controller}/api/station", \%post_data);
$logger->debug({filter => \&Data::Dumper::Dumper,
value => $response}) if ($logger->is_debug());
}
if ($response->{status} == 200 ) {
my $content = from_json($response->{content});
$self->{controller}{running} = 1;
$logger->debug({filter => \&Data::Dumper::Dumper,
value => $content}) if ($logger->is_debug());
if (defined $content && $content ne 'true') {
$self->{config} = $content->{settings};
write_config();
} else {
write_config();
}
( run in 2.956 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )