-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
96 lines (82 loc) · 2.85 KB
/
cron.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
<?php
declare(strict_types=1);
/**
* Reads and store data from all attached sensors.
*/
use steinmb\BrewSession;
use steinmb\BrewSessionConfig;
use steinmb\Logger\Handlers\BrewersFriendHandler;
use steinmb\Logger\Curl;
use steinmb\Logger\JsonDecode;
use steinmb\Logger\Handlers\TelegramHandler;
use steinmb\Onewire\SensorFactory;
use steinmb\RuntimeEnvironment;
use steinmb\Logger\Handlers\FileStorageHandler;
use steinmb\Logger\Logger;
use steinmb\Onewire\OneWire;
use steinmb\SystemClock;
include_once __DIR__ . '/vendor/autoload.php';
RuntimeEnvironment::init();
$oneWire = new OneWire();
$sensorFactory = new SensorFactory($oneWire);
$brewSessionConfig = new BrewSessionConfig(RuntimeEnvironment::getSetting('BATCH'));
$sensors =$sensorFactory->allSensors();
$loggerService = new Logger('temperature');
$fileLogger = new Logger('Files');
$clockService = new SystemClock();
if (RuntimeEnvironment::getSetting('BREWERS_FRIEND')) {
$loggerService->pushHandler(
new BrewersFriendHandler(
RuntimeEnvironment::getSetting('BREWERS_FRIEND')['SESSION_ID'],
RuntimeEnvironment::getSetting('BREWERS_FRIEND')['TOKEN'],
new JsonDecode(),
new Curl()
)
);
}
if (RuntimeEnvironment::getSetting('TELEGRAM')) {
$loggerService->pushHandler(
new TelegramHandler(
RuntimeEnvironment::getSetting('TELEGRAM')['TOKEN'],
RuntimeEnvironment::getSetting('TELEGRAM')['CHANNEL'],
new JsonDecode(),
new Curl()
)
);
}
/**
* Create a logfile pr. sensor with ambient sensor temperature.
* Write logs to file, Telegram, Brewers Friend etc.
*/
foreach ($sensors as $probe) {
$brewSession = $brewSessionConfig->sessionIdentity($probe);
if ($brewSession instanceof BrewSession) {
$ambientId = $brewSession->ambient;
$ambientSensor = '';
foreach ($sensors as $sensor) {
if ($sensor->id === $ambientId) {
$ambientSensor = $sensor;
}
}
if (!$ambientSensor) {
throw new RuntimeException(
'Ambient sensor: ' . $brewSession->ambient . ' in session: ' . $brewSession->sessionId . ' not found');
}
$context = [
'brewSession' => $brewSession,
'sensor' => $probe,
'ambient_sensor' => $ambientSensor,
'ambient' => $ambientSensor->temperature(),
'temperature' => $probe->temperature(),
'clock' => $clockService->currentTime(),
];
$loggerService->write('', $context);
$fileLogger = new Logger('Files');
$fileLogger->pushHandler(new FileStorageHandler(
$probe->id . '.csv',
RuntimeEnvironment::getSetting('LOG_DIRECTORY') . '/' . $brewSession->sessionId
));
$fileLogger->write('', $context);
}
}
//$loggerService->close();