]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/TimeFrame.pm
rewrite config log followup: move common log message out in closure
[pmg-api.git] / PMG / RuleDB / TimeFrame.pm
CommitLineData
0a580593
DM
1package PMG::RuleDB::TimeFrame;
2
3use strict;
4use warnings;
0a580593
DM
5use DBI;
6use Digest::SHA;
7
758c7b6b 8use PMG::Utils;
0a580593
DM
9use PMG::RuleDB::Object;
10
b8ea5d5d 11use base qw(PMG::RuleDB::Object);
0a580593
DM
12
13sub otype {
14 return 2000;
15}
16
17sub oclass {
18 return 'when';
19}
20
21sub otype_text {
22 return 'TimeFrame';
23}
24
e2b04306
DM
25my $hm_to_minutes = sub {
26 my ($hm) = @_;
27
28 if ($hm =~ m/^(\d+):(\d+)$/) {
29 my @tmp = split(/:/, $hm);
30 return $tmp[0]*60+$tmp[1];
31 }
32 return 0;
33};
34
35my $minutes_to_hm = sub {
36 my ($minutes) = @_;
37
38 my $hour = int($minutes/60);
39 my $rest = int($minutes%60);
40
41 return sprintf("%02d:%02d", $hour, $rest);
42};
43
0a580593
DM
44sub new {
45 my ($type, $start, $end, $ogroup) = @_;
46
47 my $class = ref($type) || $type;
48
7a2cf7e6 49 my $self = $class->SUPER::new($class->otype(), $ogroup);
0a580593
DM
50
51 $start //= "00:00";
52 $end //= "24:00";
53
e2b04306 54 # Note: allow H:i or integer format
0a580593 55 if ($start =~ m/:/) {
e2b04306 56 $start = $hm_to_minutes->($start);
0a580593 57 }
0a580593 58 if ($end =~ m/:/) {
e2b04306 59 $end = $hm_to_minutes->($end);
0a580593
DM
60 }
61
62 $self->{start} = $start;
63 $self->{end} = $end;
64
65 return $self;
66}
67
68sub load_attr {
69 my ($type, $ruledb, $id, $ogroup, $value) = @_;
70
71 my $class = ref($type) || $type;
72
73 defined($value) || return undef;
74
75 my ($sh, $sm, $eh, $em) = $value =~ m/(\d+):(\d+)-(\d+):(\d+)/;
76
77 my $start = $sh*60+$sm;
78 my $end = $eh*60+$em;
79
80 my $obj = $class->new($start, $end, $ogroup);
81 $obj->{id} = $id;
82
83 $obj->{digest} = Digest::SHA::sha1_hex ($id, $start, $end, $ogroup);
84
85 return $obj;
86}
87
88sub save {
89 my ($self, $ruledb) = @_;
90
91 defined($self->{ogroup}) || return undef;
92 defined($self->{start}) || return undef;
93 defined($self->{end}) || return undef;
94
e2b04306
DM
95 my $start = $minutes_to_hm->($self->{start});
96 my $end = $minutes_to_hm->($self->{end});
97
98 my $v = "$start-$end";
0a580593
DM
99
100 if (defined ($self->{id})) {
101 # update
102
103 $ruledb->{dbh}->do(
104 "UPDATE Object SET Value = ? WHERE ID = ?", undef, $v, $self->{id});
105
106 } else {
107 # insert
108
109 my $sth = $ruledb->{dbh}->prepare(
110 "INSERT INTO Object " .
111 "(Objectgroup_ID, ObjectType, Value) " .
112 "VALUES (?, ?, ?);");
113
114 $sth->execute($self->ogroup, $self->otype, $v);
115
758c7b6b 116 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
0a580593
DM
117 }
118
119 return $self->{id};
120}
121
122sub when_match {
123 my ($self, $t) = @_;
124
125 my ($sec,$min,$hour) = localtime($t);
126
127 my $amin = $hour*60 + $min;
128
441f9044 129 if ($self->{end} >= $self->{start}) {
0a580593 130
441f9044 131 return $amin >= $self->{start} && $amin <= $self->{end};
0a580593 132
441f9044 133 } else {
0a580593 134
441f9044 135 return ($amin <= $self->{end}) || ($amin >= $self->{start});
0a580593 136 }
0a580593
DM
137}
138
139sub short_desc {
140 my $self = shift;
141
e2b04306
DM
142 my $start = $minutes_to_hm->($self->{start});
143 my $end = $minutes_to_hm->($self->{end});
144
145 return "$start-$end";
0a580593
DM
146}
147
88d03754
DM
148sub properties {
149 my ($class) = @_;
150
151 return {
152 start => {
e2b04306
DM
153 description => "Start time in `H:i` format (00:00).",
154 type => 'string',
155 pattern => '\d?\d:\d?\d',
88d03754
DM
156 },
157 end => {
e2b04306
DM
158 description => "End time in `H:i` format (00:00).",
159 type => 'string',
160 pattern => '\d?\d:\d?\d',
88d03754
DM
161 },
162 };
163}
164
165sub get {
166 my ($self) = @_;
167
e2b04306
DM
168 return {
169 start => $minutes_to_hm->($self->{start}),
170 end => $minutes_to_hm->($self->{end}),
171 };
88d03754
DM
172}
173
174sub update {
175 my ($self, $param) = @_;
176
e2b04306
DM
177 $self->{start} = $hm_to_minutes->($param->{start});
178 $self->{end} = $hm_to_minutes->($param->{end});
88d03754
DM
179}
180
0a580593
DM
181
1821;
183
184__END__
185
186=head1 PMG::RuleDB::TimeFrame
187
188A WHEN object to check for a specific daytime.
189
190=head2 Attribues
191
192=head3 start
193
194Start time im minutes since 00:00.
195
196=head3 end
197
198End time im minutes since 00:00.
199
200=head2 Examples
201
202 $obj = PMG::RuleDB::TimeFrame->new(8*60+15, 16*60+30);
203
204Represent: 8:15 to 16:30
441f9044
DM
205
206Note: End time is allowed to be smaller that start time.