]> bicyclesonthemoon.info Git - ott/bsta/blob - ong.1.pl
ONG will use generic function
[ott/bsta] / ong.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # ong.pl is generated from ong.1.pl.
4 #
5 # The ONG bot
6 #
7 # Copyright (C) 2016, 2017, 2023  Balthasar SzczepaƄski
8 #
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.
13 #
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.
18 #
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/>.
21
22 use strict;
23 use utf8;
24 # use Encode::Locale ('decode_argv');
25 use Encode ('encode', 'decode');
26
27 ###PERL_LIB: use lib /botm/lib/bsta
28 use botm_common (
29         'read_data_file', 'write_data_file'
30 );
31 use bsta_lib (
32         'STATE',
33         'ong'
34 );
35
36 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
37 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
38
39 my $time = time();
40 srand ($time-$$);
41
42 my %settings;
43 my %state;
44 my %goto_list;
45
46 my $fh;
47 my $ongstate;
48 my $frame;
49 my $next_ong;
50 my $ongtime;
51 my $static_timer;
52 my $timer;
53 my $last;
54 my $r = 0;
55
56 $ongtime = int($time / 3600) * 3600; # check time as if it was last full hour!
57
58 print $time.' - '.$ongtime."\n";
59
60 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
61 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
62
63 unless (open ($fh, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STATE_PATH()))) {
64         print "NO STATEFILE\n";
65 }
66 else {
67         unless (flock($fh, 2)) {
68                 print "NO STATELOCK\n";
69         }
70         else {
71                 %state = read_data_file($fh);
72                 $ongstate = int($state{'state'});
73                 print 'state: '.$ongstate."\n";
74                 unless ($ongstate > STATE->{'inactive'}) {
75                         print "INACTIVE\n";
76                 }
77                 else {
78                         $next_ong = int($state{'nextong'});
79                         print 'ongtime: '.$next_ong."\n";
80                         
81                         unless ($ongtime >= $next_ong) {
82                                 print "WAIT\n";
83                         }
84                         else {
85                                 %settings = read_data_file(DATA_SETTINGS_PATH());
86                                 $static_timer = int($settings{'ongtime'});
87                                 $timer        = int($settings{'dynamicongtime'});
88                                 $last         = int($settings{'last'});
89                                 $frame        = int($state   {'last'})+1;
90                                 
91                                 if (($timer > 0) && ($frame < $last)) {
92                                         $timer = int($timer / ($last - $frame));
93                                 }
94                                 else {
95                                         $timer=0;
96                                 }
97                                 
98                                 if ($static_timer > $timer) {
99                                         $timer = $static_timer;
100                                 }
101                                 $next_ong = $ongtime + ($timer*3600);
102                                 $state{'nextong'} = $next_ong;
103                                 print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
104                                 $state{'ongtime'}=$timer;
105                                 
106                                 if ($ongstate == STATE->{'ready'}) {
107                                         print 'next frame: '.$frame."\n";
108                                         
109                                         $r = ong (
110                                                 $frame, # frame ID
111                                                 $time,  # ONG time
112                                                 $timer, # timer to next ONG
113                                                 0,      # update
114                                                 1,      # print
115                                                 \%settings,
116                                                 '',     # %default
117                                                 '',     # %frame_data
118                                                 ''      # %goto_list
119                                         );
120                                         
121                                         if ($r) {
122                                                 $state{'last'}   = $frame;
123                                                 $state{'state'}  = 1;
124                                                 $state{'ip1'}    = '';
125                                                 $state{'ip2'}    = '';
126                                                 $state{'ip3'}    = '';
127                                                 $state{'ongtime'}= $timer;
128                                                 print "ONG\n";
129                                         }
130                                         else {
131                                                 print "NO ONG\n";
132                                         }
133                                 }
134                                 write_data_file($fh, '', '', \%state);
135                         }
136                 }
137         }
138         close ($fh);
139 }
140
141 print "\n";