]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/TimeFrame.pm
fix schema definition
[pmg-api.git] / PMG / RuleDB / TimeFrame.pm
1 package PMG::RuleDB::TimeFrame;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Digest::SHA;
7
8 use PMG::Utils;
9 use PMG::RuleDB::Object;
10
11 use base qw(PMG::RuleDB::Object);
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
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
44 sub new {
45 my ($type, $start, $end, $ogroup) = @_;
46
47 my $class = ref($type) || $type;
48
49 my $self = $class->SUPER::new($class->otype(), $ogroup);
50
51 $start //= "00:00";
52 $end //= "24:00";
53
54 # Note: allow H:i or integer format
55 if ($start =~ m/:/) {
56 $start = $hm_to_minutes->($start);
57 }
58 if ($end =~ m/:/) {
59 $end = $hm_to_minutes->($end);
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
95 my $start = $minutes_to_hm->($self->{start});
96 my $end = $minutes_to_hm->($self->{end});
97
98 my $v = "$start-$end";
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
116 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
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
129 if ($self->{end} >= $self->{start}) {
130
131 return $amin >= $self->{start} && $amin <= $self->{end};
132
133 } else {
134
135 return ($amin <= $self->{end}) || ($amin >= $self->{start});
136 }
137 }
138
139 sub short_desc {
140 my $self = shift;
141
142 my $start = $minutes_to_hm->($self->{start});
143 my $end = $minutes_to_hm->($self->{end});
144
145 return "$start-$end";
146 }
147
148 sub properties {
149 my ($class) = @_;
150
151 return {
152 start => {
153 description => "Start time in `H:i` format (00:00).",
154 type => 'string',
155 pattern => '\d?\d:\d?\d',
156 },
157 end => {
158 description => "End time in `H:i` format (00:00).",
159 type => 'string',
160 pattern => '\d?\d:\d?\d',
161 },
162 };
163 }
164
165 sub get {
166 my ($self) = @_;
167
168 return {
169 start => $minutes_to_hm->($self->{start}),
170 end => $minutes_to_hm->($self->{end}),
171 };
172 }
173
174 sub update {
175 my ($self, $param) = @_;
176
177 $self->{start} = $hm_to_minutes->($param->{start});
178 $self->{end} = $hm_to_minutes->($param->{end});
179 }
180
181
182 1;
183
184 __END__
185
186 =head1 PMG::RuleDB::TimeFrame
187
188 A WHEN object to check for a specific daytime.
189
190 =head2 Attribues
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
205
206 Note: End time is allowed to be smaller that start time.