-
Notifications
You must be signed in to change notification settings - Fork 1
/
valve.php
219 lines (190 loc) · 6.27 KB
/
valve.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
include_once 'ini_write.php';
include_once 'mutex.php';
include_once 'ValveHW.php';
class Valve
{
const DATEDATEFORMAT = 'Y-m-d';
const DATETIMEFORMAT = 'H:i:s';
const DATETIMEFORMAT_NOSECONDS = 'H:i';
const DATEFORMAT = 'Y-m-d H:i:s';
const DURATIONFORMAT = '%r%H:%I';
const DURATIONLONGFORMAT = '%r%a days %H:%I:%S';
const DURATIONFORMAT_WITHSECS = '%r%H:%I:%S';
const DURATIONLONGFORMAT_PARSE = '%d days %d:%d:%d';
public $params = [
"General" => [
"Name" => "Name",
"Image" => "http://lorempixel.com/64/64",
"HWID" => 0,
],
"Auto" => [
"Duration" => "PT1H0M",
"At" => "4:30",
"Interval" => "P3D",
],
"Manual" => [
"Duration" => "PT1H0M",
"At" => "2013-01-01 12:00:00",
],
"Status" => [
"Manual" => false,
"Auto" => true,
"Start" => "",
],
"History" => [
"Dates" => [],
"Durations" => [],
],
];
public $filename;
private $HW;
function __construct($filename) {
$this->HW = new ValveHW;
$this->filename = $filename;
$this->ReadINI();
}
function ReadINI() {
$mutex = new Mutex($this->filename);
$params = parse_ini_file($this->filename, true);
// Overrides defaults with values from the ini file.
$this->params = array_replace_recursive($this->params, $params);
}
function WriteINI() {
ini_write($this->params, $this->filename, true);
}
function IsOpen() {
return $this->HW->IsOpen($this->params["General"]["HWID"]);
}
function DoOpen() {
if (!$this->IsOpen() && $this->CanOpen()) {
$this->params['Status']['Start'] = (new DateTime())->format(self::DATEFORMAT);
$this->WriteINI();
$this->HW->Open($this->params["General"]["HWID"]);
if ($this->params["Status"]["Manual"]) {
$title = $this->params["General"]["Name"] . " Manually for " . $this->FormatManualDuration();
} elseif ($this->params["Status"]["Auto"]) {
$title = $this->params["General"]["Name"] . " Automatically for " . $this->FormatAutoDuration();
} else {
$title = $this->params["General"]["Name"] . " For unknown reason";
}
//exec("echo ' ' | mail -s 'PI Open $title' micronen@gmail.com");
}
}
function DoClose() {
if ($this->IsOpen()) {
$start = DateTime::createFromFormat(self::DATEFORMAT, $this->params["Status"]["Start"]);
$duration = $start->diff(new DateTime());
$this->params['History']['Dates'][] = $this->params['Status']['Start'];
$this->params['History']['Durations'][] = $duration->format(self::DURATIONLONGFORMAT);
$this->params['Status']['Manual'] = false;
$this->WriteINI();
$this->HW->Close($this->params["General"]["HWID"]);
if ($duration->days == 0) {
$duration = $duration->format(self::DURATIONFORMAT_WITHSECS);
} else {
$duration = $duration->format(self::DURATIONLONGFORMAT);
}
//exec("echo ' ' | mail -s 'PI Close " . $this->params["General"]["Name"] . " after $duration' micronen@gmail.com");
}
}
function ShouldOpen() {
$TimeToOpen = $this->TimeToOpen();
if (is_null($TimeToOpen)) {
return false;
} else {
return !$this->IsOpen() && $this->TimeToOpen()->invert;
}
}
function CanOpen() {
return $this->HW->CanOpen($this->params["General"]["HWID"]);
}
function ShouldClose() {
return $this->IsOpen() && $this->TimeToClose()->invert;
}
function FormatAutoTime() {
$at = DateTime::createFromFormat(self::DATETIMEFORMAT, $this->params["Auto"]["At"]);
if (DateTime::getLastErrors()["error_count"] > 0) {
$at = DateTime::createFromFormat(self::DATETIMEFORMAT_NOSECONDS, $this->params["Auto"]["At"]);
}
return $at->format(self::DATETIMEFORMAT);
}
function FormatAutoInterval() {
return (new DateInterval($this->params["Auto"]['Interval']))->format("%d days");
}
function FormatAutoDuration() {
return (new DateInterval($this->params["Auto"]["Duration"]))->format(self::DURATIONFORMAT);
}
function FormatManualDuration() {
return (new DateInterval($this->params["Manual"]["Duration"]))->format(self::DURATIONFORMAT);
}
function FormatManualAt() {
return DateTime::createFromFormat(self::DATEFORMAT, $this->params["Manual"]["At"])->format(self::DATEFORMAT);
}
function FormatManualDate() {
return DateTime::createFromFormat(self::DATEFORMAT, $this->params["Manual"]["At"])->format(self::DATEDATEFORMAT);
}
function FormatManualTime() {
return DateTime::createFromFormat(self::DATEFORMAT, $this->params["Manual"]["At"])->format(self::DATETIMEFORMAT);
}
function OpenDuration() {
if ($this->params["Status"]["Manual"]) {
$duration = new DateInterval($this->params["Manual"]["Duration"]);
} else {
$duration = new DateInterval($this->params["Auto"]["Duration"]);
}
return $duration;
}
function TimeToClose() {
$duration = $this->OpenDuration();
$finish = DateTime::createFromFormat(self::DATEFORMAT, $this->params["Status"]["Start"])->add($duration);
$left = (new DateTime())->diff($finish);
return $left;
}
function TimeToOpen() {
if ($this->params["Status"]["Manual"]) {
$at = DateTime::createFromFormat(self::DATEFORMAT, $this->params["Manual"]["At"]);
} elseif ($this->params["Status"]["Auto"]) {
$at = DateTime::createFromFormat(self::DATETIMEFORMAT, $this->params["Auto"]["At"]);
if (DateTime::getLastErrors()["error_count"] > 0) {
$at = DateTime::createFromFormat(self::DATETIMEFORMAT_NOSECONDS, $this->params["Auto"]["At"]);
}
$last = end($this->params["History"]["Dates"]);
if (empty($last)) {
if ($at < new DateTime()) $at->add(new DateInterval("P1D"));
} else {
$wait = DateTime::createFromFormat(self::DATEFORMAT, $last)->add(new DateInterval($this->params["Auto"]['Interval']));
for ($i = 0; $i < 100; $i++) {
if ($at < $wait) {
$at->add(new DateInterval("P1D"));
} else {
$at->sub(new DateInterval("P1D"));
break;
}
}
}
} else {
return NULL;
}
$diff = (new DateTime())->diff($at);
return $diff;
}
function ManualOpenNow() {
$this->params['Status']['Manual'] = 1;
$this->params['Manual']['At'] = (new DateTime())->format(self::DATEFORMAT);
// $this->params['Manual']['Duration'] = 'PT1H0M';
$this->DoOpen();
$this->WriteINI();
}
}
function GetValvesList($class_name = 'Valve', $path = DEFAULTPATH) {
$files = glob($path . '*.vlv');
if (empty($files)) {
return null;
} else {
foreach($files as $file) {
$valves[] = new $class_name($file);
}
return $valves;
}
}