1 ###RUN_PERL: #!/usr/bin/perl
3 # ong.pl is generated from ong.1.pl.
7 # Copyright (C) 2016, 2017, 2023, 2024 Balthasar SzczepaĆski
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Affero General Public License for more details.
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # use Encode::Locale ('decode_argv');
25 use Encode ('encode', 'decode');
27 ###PERL_LIB: use lib /botm/lib/bsta
29 'read_data_file', 'write_data_file',
37 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
38 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
40 binmode STDIN, ':encoding(UTF-8)';
41 binmode STDOUT, ':encoding(UTF-8)';
42 binmode STDERR, ':encoding(UTF-8)';
62 $ongtime = int($time / 3600) * 3600; # check time as if it was last full hour!
64 print $time.' - '.$ongtime."\n";
66 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
67 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
69 unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
70 print "NO STATEFILE\n";
73 unless (flock($fh, 2)) {
74 print "NO STATELOCK\n";
77 %state = read_data_file($fh);
78 $ongstate = int($state{'state'});
79 print 'state: '.$ongstate."\n";
80 unless ($ongstate > STATE->{'inactive'}) {
84 $next_ong = int($state{'nextong'});
85 print 'ongtime: '.$next_ong."\n";
87 unless ($ongtime >= $next_ong) {
91 %settings = read_data_file(DATA_SETTINGS_PATH());
92 $static_timer = int($settings{'ongtime'});
93 $timer = int($settings{'dynamicongtime'});
94 $last = int($settings{'last'});
95 $frame = int($state {'last'})+1;
97 if (($timer > 0) && ($frame < $last)) {
98 $timer = int($timer / ($last - $frame));
104 if ($static_timer > $timer) {
105 $timer = $static_timer;
107 $next_ong = $ongtime + ($timer*3600);
108 $state{'nextong'} = $next_ong;
109 print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
110 $state{'ongtime'}=$timer;
112 if ($ongstate == STATE->{'ready'}) {
113 print 'next frame: '.$frame."\n";
118 $timer, # timer to next ONG
128 $state{'last'} = $frame;
133 $state{'ongtime'}= $timer;
140 write_data_file($fh, \%state);