-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanting_device.php
68 lines (55 loc) · 1.53 KB
/
planting_device.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
<?php
/**
* Planting Device Daemon
*
* @category Daemons
* @package Automated Planting 1.0
* @author Marcin Polak <mapoart@gmail.com>
* @license http://MarcinPolak.eu
* @version Release: 1.0
* @link http://MarcinPolak.eu/AutomatedPlanting
*/
require_once "bootstrap.php";
use Mapos\Daemon\Daemon;
use Mapos\Device\DeviceSerial;
use Mapos\Planting\Planting;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
//We need to do it in one iteration because serial reseting Arduino,
//So we calculating sleepCollect Data
$daemon = false;
$sleepUpdate = 5;//seconds
$sleepCollectData = 600; // 10 mins
if ($daemon) {
$daemon = new Daemon("plantdevice");
$daemon->setDelay($sleepUpdate); //10 minutes
$daemon->start();
}
$logPath = 'logs/planting_device.log';
$logger = new Logger('Serial');
$logger->pushHandler(new StreamHandler($logPath, Logger::INFO));
$device = new DeviceSerial();
$device->setLog($logger);
$planting = new Planting();
$planting->setSerialDevice($device);
$logger = new Logger('Planting');
$logger->pushHandler(new StreamHandler($logPath, Logger::INFO));
$planting->setLog($logger);
$planting->boot();
$run = 0;
$collectDataTimer = 0;
while (1) {
if ($run == 0 || $collectDataTimer > $sleepCollectData) {
$planting->collectData();
$collectDataTimer = 0;
}
$planting->updateDevice();
$collectDataTimer += $sleepUpdate;
if ($daemon) {
$daemon->info("run " . $run++);
//sleep(5);
$daemon->delay();
} else {
sleep($sleepUpdate);
}
}