]>
Commit | Line | Data |
---|---|---|
0a580593 DM |
1 | package PMG::RuleDB::TimeFrame; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
0a580593 DM |
5 | use DBI; |
6 | use Digest::SHA; | |
7 | ||
758c7b6b | 8 | use PMG::Utils; |
0a580593 DM |
9 | use PMG::RuleDB::Object; |
10 | ||
b8ea5d5d | 11 | use base qw(PMG::RuleDB::Object); |
0a580593 DM |
12 | |
13 | sub otype { | |
14 | return 2000; | |
15 | } | |
16 | ||
17 | sub oclass { | |
18 | return 'when'; | |
19 | } | |
20 | ||
21 | sub otype_text { | |
22 | return 'TimeFrame'; | |
23 | } | |
24 | ||
e2b04306 DM |
25 | my $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 | ||
35 | my $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 |
44 | sub 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 | ||
68 | sub 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 | ||
88 | sub 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 | ||
122 | sub 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 | ||
139 | sub 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 |
148 | sub 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 | ||
165 | sub 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 | ||
174 | sub 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 | |
182 | 1; | |
183 | ||
184 | __END__ | |
185 | ||
186 | =head1 PMG::RuleDB::TimeFrame | |
187 | ||
188 | A WHEN object to check for a specific daytime. | |
189 | ||
1359baef | 190 | =head2 Attributes |
0a580593 DM |
191 | |
192 | =head3 start | |
193 | ||
194 | Start time im minutes since 00:00. | |
195 | ||
196 | =head3 end | |
197 | ||
198 | End time im minutes since 00:00. | |
199 | ||
200 | =head2 Examples | |
201 | ||
202 | $obj = PMG::RuleDB::TimeFrame->new(8*60+15, 16*60+30); | |
203 | ||
204 | Represent: 8:15 to 16:30 | |
441f9044 DM |
205 | |
206 | Note: End time is allowed to be smaller that start time. |