/
schedule.php
379 lines (327 loc) · 11.3 KB
/
schedule.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php include('layout.inc'); ?>
<?php echo layout_getPageHeader(); ?>
<script type="text/javascript">
function showControl(rowId)
{
document.getElementById('valve' + rowId + '_control').style.display = '';
document.getElementById('valve' + rowId + '_days').style.display = 'none';
}
function showAuto(rowId)
{
document.getElementById('valve' + rowId + '_control').style.display = 'none';
document.getElementById('valve' + rowId + '_days').style.display = '';
}
</script>
<?php
// include stuff
include_once('schedule.inc');
include_once('logger.inc');
include_once('operateValves.php');
// valve configuration object
$schedule_storedConfiguration = new Schedule();
// gets days of week into a valve
function schedule_getValveDays($storedSchedule, $valveIndex, $dayName)
{
$resultString = "";
$dayIndex = 0;
// iterate through days
foreach ($dayName as $day)
{
$checkboxIsChecked = "";
// get if checked
if ($storedSchedule->valves[$valveIndex]->daysToOperate[$dayIndex] != 0)
{
// set checked attribute
$checkboxIsChecked = 'checked="checked"';
}
// echo it
$resultString .= '<div style="float:left;margin-left:4px;">
<div style="float:top;text-align:center">'.$day.'</div>
<div style="float:bottom;"><input type="checkbox" name="valve'.$valveIndex.'_day'.$dayIndex.'" '.$checkboxIsChecked.'/></div>
</div>';
// next day
$dayIndex++;
}
return $resultString;
}
// get master control on/off
function schedule_getMasterControl($storedSchedule, $mode)
{
// check if equal
if ($mode == $storedSchedule->masterControl)
{
echo 'checked="checked"';
}
else echo '';
}
// creates a dropdown menue for a certain valve
function common_createDropDownListOptions($format, $startNumber, $endNumber, $increment, $selectedNumber)
{
// add items
for ($currentNumber = $startNumber; $currentNumber <= $endNumber; $currentNumber += $increment)
{
// selected
$selected = ($currentNumber == $selectedNumber ? "selected" : "");
// format the number
$formattedCurrentNumber = sprintf($format, $currentNumber);
// add item
echo '<option value='.$formattedCurrentNumber.' '.$selected.'>'.$formattedCurrentNumber.'</option>';
}
}
// outputs a valve
function schedule_outputValve($storedSchedule, $style, $valveIndex, $valveIsOpen)
{
// get days of week checkboxes
$daysOfWeek = schedule_getValveDays($storedSchedule, $valveIndex, array("S", "M", "T", "W", "T", "F", "S"));
// auto/manual
$autoRadioSelected = ""; $daysRowStyle = "";
$manualRadioSelected = ""; $controlRowStyle = "";
// get selected according to mode
if ($storedSchedule->valves[$valveIndex]->mode == 0)
{
$autoRadioSelected = "checked";
$controlRowStyle = "display:none;";
}
else
{
$manualRadioSelected = "checked";
$daysRowStyle = "display:none;";
}
// get valve status
$valveStatus = $valveIsOpen ? " (On)" : "";
// echo it
echo '<div class="valve_wrapper" style="'.$style.'">
<fieldset class="base">
<legend class="base_legend">Valve #'.($valveIndex + 1).$valveStatus.'</legend>
<div>
<table border="0">
<tr>
<td class="valve_value_name">Name:</td>
<td><input type="text" size="16" name="valve'.$valveIndex.'_name" value="'.$storedSchedule->valves[$valveIndex]->name.'" class = "styled_input"/></td>
</tr>
<tr>
<td class="valve_value_name">Mode:</td>
<td>
<label for="valve'.$valveIndex.'_modeAuto"><input type="radio" id="valve'.$valveIndex.'_modeAuto" name="valve'.$valveIndex.'_mode" value="auto" onClick="showAuto('.$valveIndex.')" '.$autoRadioSelected.'/> Auto    </label>
<label for="valve'.$valveIndex.'_modeManual"><input type="radio" id="valve'.$valveIndex.'_modeManual" name="valve'.$valveIndex.'_mode" value="manual" onClick="showControl('.$valveIndex.')" '.$manualRadioSelected.'/> Manual </label>
</td>
</tr>
<tr id="valve'.$valveIndex.'_days" style="'.$daysRowStyle.'" >
<td class="valve_value_name">Days:</td>
<td>
<div style="float:left;">'.$daysOfWeek.'</div>
<div style="clear:both;"></div>
</td>
</tr>
<tr id="valve'.$valveIndex.'_control" style="'.$controlRowStyle.'" >
<td class="valve_value_name"></td>
<td>
<input type="submit" name="valve'.$valveIndex.'_turnOn" value="Turn on" class="buttonTurnOn" />  
<input type="submit" name="valve'.$valveIndex.'_turnOff" value="Turn off" class="buttonTurnOff" />
</td>
</tr>
<tr>
<td class="valve_value_name">On for:</td>
<td><input type="text" size="2" name="valve'.$valveIndex.'_duration" value="'.$storedSchedule->valves[$valveIndex]->operationDurationInMinutes.'" class = "styled_input"/> minutes</td>
</tr>
</table>
</div>
</fieldset>
</div>';
}
// outputs all valves
function schedule_outputValves($storedSchedule)
{
// get which valves are open
$valvesCurrentlyOpen = operateValves_getCurrentlyOpenValves();
// iterate through all valves
for ($valveIndex = 0; $valveIndex < count($storedSchedule->valves); $valveIndex++)
{
// style for valve
$valveStyle = "float: left";
// output valve
schedule_outputValve($storedSchedule, $valveStyle, $valveIndex, $valvesCurrentlyOpen[$valveIndex]);
}
}
// load data from the form into a schedule
function schedule_saveScheduleToFile($schedule, $fileName)
{
// save to file
$fp = fopen($fileName, 'w');
// check
if ($fp)
{
// log
logger_logEvent('Updating configuration');
// write
fwrite($fp, serialize($schedule));
fclose($fp);
// success
return true;
}
else return false;
}
// load data from the form into a schedule
function schedule_loadPostedFormToSchedule(&$schedule)
{
// iterate over all valves
for ($valveIndex = 0; $valveIndex < count($schedule->valves); $valveIndex++)
{
// get current valve
$valve = $schedule->valves[$valveIndex];
// set name and duration
$valve->name = $_POST['valve'.$valveIndex.'_name'];
$valve->operationDurationInMinutes = $_POST['valve'.$valveIndex.'_duration'];
// set mode
if ($_POST['valve'.$valveIndex.'_mode'] == 'auto')
{
// set mode to auto and clear start time
$valve->mode = 0;
$valve->manualStartTime = -1;
}
else
{
// set mode to manual, leave start time as is
$valve->mode = 1;
}
// iterate dow
for ($dayIndex = 0; $dayIndex < 7; $dayIndex++)
{
// shove if set
$valve->daysToOperate[$dayIndex] = isset($_POST['valve'.$valveIndex.'_day'.$dayIndex]);
}
}
// get master control
$schedule->masterControl = ($_POST['masterControl'] == 'controlOn');
// get start hour
$schedule->startHour = $_POST['startHour'];
$schedule->startMinutes = $_POST['startMinutes'];
// concurrent open valves
$schedule->maxConcurrentOpenValves = $_POST['maxConcurrentOpenValves'];
}
// create default schedule
function schedule_initDefaultSchedule($schedule, $numberOfValves)
{
// iterate over valves
for ($valveIndex = 0; $valveIndex < $numberOfValves; $valveIndex++)
{
// create default valve
$valve = new ScheduleValve();
$valve->name = 'Area #'.($valveIndex + 1);
$valve->daysToOperate = array(0, 0, 0, 0, 0, 0, 0);
$valve->operationDurationInMinutes = 0;
// shove to array
$schedule->valves[$valveIndex] = $valve;
}
// init master mode and start hour
$schedule->masterControl = false; // off
$schedule->startHour = 0;
$schedule->startMinutes = 0;
$schedule->maxConcurrentOpenValves = 1;
}
// load the schedule from file
function schedule_loadScheduleFromFile(&$schedule, $fileName)
{
// check if valve configuration exists
if (file_exists($fileName))
{
// load from file
$schedule = unserialize(file_get_contents($fileName));
}
}
// stored schedule
$schedule_storedSchedule = new Schedule();
// check if anything has been posted
if ($_POST)
{
// load the schedule from file
schedule_loadScheduleFromFile($schedule_storedSchedule, $schedule_configurationFileName);
// update the schedule with data from the form
schedule_loadPostedFormToSchedule($schedule_storedSchedule);
// check if a turn on/off button has been pressed
for ($valveIndex = 0; $valveIndex < $schedule_numberOfValves; ++$valveIndex)
{
// check if a valve has been turned on
if (isset($_POST['valve'.$valveIndex.'_turnOn']))
{
// set teh valve manual time to now
$schedule_storedSchedule->valves[$valveIndex]->manualStartTime = operateValves_getCurrentMinutesOffsetIntoWeek();
break;
}
if (isset($_POST['valve'.$valveIndex.'_turnOff']))
{
// reset the valve manual time
$schedule_storedSchedule->valves[$valveIndex]->manualStartTime = -1;
break;
}
}
// save to file
if (schedule_saveScheduleToFile($schedule_storedSchedule, $schedule_configurationFileName))
{
// operate the valves now, don't wait for cron
operateValves_performActions($schedule_storedSchedule);
}
}
else
{
// check if valve configuration exists
if (!file_exists($schedule_configurationFileName))
{
// create new scedule
$schedule_storedSchedule = new Schedule();
// initialize the current schedule
schedule_initDefaultSchedule($schedule_storedSchedule, $schedule_numberOfValves);
// save the current schedule so that the configuration file will always exist
schedule_saveScheduleToFile($schedule_storedSchedule, $schedule_configurationFileName);
}
else
{
// Try to load the data from the saved configuration
schedule_loadScheduleFromFile($schedule_storedSchedule, $schedule_configurationFileName);
}
}
?>
<?php echo layout_getBodyHeader(); ?>
<form action="schedule.php" method="post">
<h1>General Stuff</h1>
<div>
<table border="0" style="margin=0 0 0 0; padding=0 0 0 0;">
<tr>
<td class="general_config_name">Schedule is:</td>
<td>
<label for="masterControlOn"><input type="radio" id="masterControlOn" name="masterControl" value="controlOn" <?php schedule_getMasterControl($schedule_storedSchedule, true); ?> /> Active </label>
<label for="masterControlOff"><input type="radio" id="masterControlOff" name="masterControl" value="controlOff" <?php schedule_getMasterControl($schedule_storedSchedule, false); ?> /> Inactive </label>
</td>
</tr>
<tr>
<td class="general_config_name">Start at:</td>
<td>
<select name="startHour">
<?php common_createDropDownListOptions('%02d', 0, 23, 1, $schedule_storedSchedule->startHour); ?>
</select> :
<select name="startMinutes">
<?php common_createDropDownListOptions('%02d', 0, 45, 15, $schedule_storedSchedule->startMinutes); ?>
</select>
</td>
<td>
<input type="submit" name="scheduleSave" value=" Save Settings " class="button" style="margin-left:295px;" />
</td>
</tr>
<tr>
<td class="general_config_name">At most, open:</td>
<td>
<select name="maxConcurrentOpenValves">
<?php common_createDropDownListOptions('%d', 1, $schedule_numberOfValves, 1, $schedule_storedSchedule->maxConcurrentOpenValves); ?>
</select>
  valve(s) at a time
</td>
</tr>
</table>
</div>
<div style="clear:both;"></div>
<h1>Irrigation Schedule</h1><br>
<div class="content">
<?php schedule_outputValves($schedule_storedSchedule); ?>
</div>
</form>
<?php echo layout_getBodyFooter(); ?>