53 lines
1.8 KiB
PHP
Executable File
53 lines
1.8 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
chdir(__DIR__);
|
|
$search = [];
|
|
$replace = [];
|
|
foreach (getenv() as $k => $v) {
|
|
$search[] = "\$$k";
|
|
$replace[] = '"' . addslashes($v) . '"';
|
|
}
|
|
$conf = yaml_parse(str_replace($search, $replace, file_get_contents(__DIR__ . '/config.yaml')));
|
|
$c = new \Mosquitto\Client();
|
|
if (isset($conf['mqtt']['user'])) $c->setCredentials($conf['mqtt']['user'], $conf['mqtt']['password']);
|
|
$c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) {
|
|
if (!isset($data[$message->topic])) return;
|
|
|
|
switch ($data[$message->topic]['cast']) {
|
|
case 'int':
|
|
$data[$message->topic]['value'] = (int)$message->payload;
|
|
break;
|
|
case 'float':
|
|
$data[$message->topic]['value'] = (float)$message->payload;
|
|
break;
|
|
case 'bool':
|
|
$data[$message->topic]['value'] = (bool)$message->payload;
|
|
break;
|
|
default:
|
|
echo "error, cast type " . $data[$message->topic]['cast'] . " unknown\n";
|
|
die(1);
|
|
}
|
|
$search = [];
|
|
$replace = [];
|
|
foreach ($data as $v) {
|
|
$search[] = '"{{' . $v['name'] . '}}"';
|
|
$replace[] = json_encode($v['value']);
|
|
}
|
|
|
|
$out = str_replace($search, $replace, file_get_contents(__DIR__ . '/template.json'));
|
|
$out = json_encode(json_decode($out), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
if (!is_dir('public')) mkdir('public');
|
|
file_put_contents('public/spaceapi.json', $out);
|
|
});
|
|
$c->connect($conf['mqtt']['host']);
|
|
$data = [];
|
|
foreach ($conf['variables'] as $name => $variable) {
|
|
$data[$variable['topic']] = [
|
|
|
|
'value' => null,
|
|
'cast' => $variable['cast'],
|
|
'name' => $name
|
|
];
|
|
$c->subscribe($variable['topic'], 2);
|
|
}
|
|
$c->loopForever(); |