add initial project files.

This commit is contained in:
2020-06-09 16:02:25 +09:00
commit b692151385
22 changed files with 2851 additions and 0 deletions

51
sbin/check_alert.php Normal file
View File

@ -0,0 +1,51 @@
<?php
define('APPDIR', dirname(__DIR__));
require_once APPDIR.'/vendor/autoload.php';
$config = json_decode(file_get_contents(APPDIR.'/etc/config.json'), true);
$digitemp = new \Orrisroot\DigiTemp($config['digitemp'], APPDIR.'/'.$config['digitemp_config']);
$rrdtemp = new \Orrisroot\Rrd\Temperature(APPDIR.'/'.$config['database_dir']);
$num = $digitemp->getNumSensors();
$sensors = [];
for ($i = 0; $i < $num; ++$i) {
$id = $digitemp->getSensorId($i);
$rrdtemp->addSensor($id);
$sensors[] = $rrdtemp->readLastData($id);
}
$upper = $config['mail_alert_upper'];
$lower = $config['mail_alert_lower'];
$do_send = false;
foreach ($sensors as $sensor) {
if ($sensor['latest'] < $lower || $sensor['latest'] > $upper) {
$do_send = true;
break;
}
}
if ($do_send) {
$from = $config['mail_from'];
$from_name = $config['mail_from_name'];
$to = $config['mail_alert_to'];
$subject = $config['mail_alert_subject'];
$loader = new \Twig\Loader\FilesystemLoader(APPDIR.'/'.$config['templates_dir']);
$twig = new \Twig\Environment($loader);
$data = [
'range' => [
'upper' => $upper,
'lower' => $lower,
],
'sensors' => $sensors,
'from' => [
'name' => $from_name,
'email' => $from,
],
'url' => $config['url'],
];
$body = $twig->render($config['mail_alert_template'], $data);
$from = new \Orrisroot\Mail\Address($from_name, $from);
$tos = [new \Orrisroot\Mail\Address($to, $to)];
\Orrisroot\Mail\UTF8_Mailer::sendMail($from, $tos, $subject, $body);
}

37
sbin/send_info.php Normal file
View File

@ -0,0 +1,37 @@
<?php
define('APPDIR', dirname(__DIR__));
require_once APPDIR.'/vendor/autoload.php';
$config = json_decode(file_get_contents(APPDIR.'/etc/config.json'), true);
$digitemp = new \Orrisroot\DigiTemp($config['digitemp'], APPDIR.'/'.$config['digitemp_config']);
$rrdtemp = new \Orrisroot\Rrd\Temperature(APPDIR.'/'.$config['database_dir']);
$num = $digitemp->getNumSensors();
$sensors = [];
for ($i = 0; $i < $num; ++$i) {
$id = $digitemp->getSensorId($i);
$rrdtemp->addSensor($id);
$sensors[] = $rrdtemp->readLastData($id);
}
$from = $config['mail_from'];
$from_name = $config['mail_from_name'];
$to = $config['mail_info_to'];
$subject = $config['mail_info_subject'];
$loader = new \Twig\Loader\FilesystemLoader(APPDIR.'/'.$config['templates_dir']);
$twig = new \Twig\Environment($loader);
$data = [
'from' => [
'name' => $from_name,
'email' => $from,
],
'sensors' => $sensors,
'url' => $config['url'],
];
$body = $twig->render($config['mail_info_template'], $data);
$from = new \Orrisroot\Mail\Address($from_name, $from);
$tos = [new \Orrisroot\Mail\Address($to, $to)];
\Orrisroot\Mail\UTF8_Mailer::sendMail($from, $tos, $subject, $body);

22
sbin/update.php Normal file
View File

@ -0,0 +1,22 @@
<?php
define('APPDIR', dirname(__DIR__));
require_once APPDIR.'/vendor/autoload.php';
$config = json_decode(file_get_contents(APPDIR.'/etc/config.json'), true);
$digitemp = new \Orrisroot\DigiTemp($config['digitemp'], APPDIR.'/'.$config['digitemp_config']);
$rrdtemp = new \Orrisroot\Rrd\Temperature(APPDIR.'/'.$config['database_dir']);
$num = $digitemp->getNumSensors();
for ($i = 0; $i < $num; ++$i) {
$id = $digitemp->getSensorId($i);
$value = $digitemp->readSensor($i);
$rrdtemp->addSensor($id);
$now = time();
$rrdtemp->update($id, $now, $value);
}
foreach ($types = ['hour', 'day', 'week', 'month', 'year', '3year'] as $type) {
$fpath = APPDIR.'/'.$config['images_dir'].'/graph-'.$type.'.png';
$rrdtemp->outputGraph($type, $fpath);
}