first commit

master
Dirk Heilig 2021-01-03 23:04:07 +01:00
commit c5c5586ff1
5 changed files with 128 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
public

18
Dockerfile 100644
View File

@ -0,0 +1,18 @@
FROM debian:10
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y \
php7.3-cli php-json \
php-pear php-dev \
libmosquitto-dev libmosquitto1 mosquitto-clients \
php-yaml \
locales-all
RUN pecl install Mosquitto-alpha
RUN echo "extension=mosquitto.so" >/etc/php/7.3/mods-available/mosquitto.ini
RUN ln -s /etc/php/7.3/mods-available/mosquitto.ini /etc/php/7.3/cli/conf.d/20-mosquitto.ini
ADD . /app
VOLUME /app/public
ENTRYPOINT /app/generator.php

16
config.yaml 100644
View File

@ -0,0 +1,16 @@
mqtt:
host: $MQTT_HOST
user: $MQTT_USER
password: $MQTT_PASSWORD
variables:
doorstatus:
topic: c3re/hhdst
cast: bool
clubraum_temp:
topic: c3re/clubraumtemp
cast: float
werkstatt_temp:
topic: c3re/werkstatttemp
cast: float

53
generator.php 100755
View File

@ -0,0 +1,53 @@
#!/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();

40
template.json 100644
View File

@ -0,0 +1,40 @@
{
"api":"0.13",
"space":"Chaostreff Recklinghausen c3RE",
"logo":"https://c3re.de/wp-content/uploads/logo/Logo_weiss_rund_schwarzer_Rand.png",
"url":"https://c3re.de",
"location":{
"address":"Westcharweg 101, 45659 Recklinghausen",
"lon":7.1690101828426,
"lat":51.62435592244
},
"contact":{
"twitter":"@c3_re",
"email":"kontakt@c3RE.de",
"issue_mail":"kontakt@c3RE.de"
},
"issue_report_channels":[
"email"
],
"state":{
"open": "{{doorstatus}}"
},
"sensors": {
"temperature": [
{
"location": "Clubraum",
"unit": "°C",
"value": "{{clubraum_temp}}"
},
{
"location": "Werkstatt",
"unit": "°C",
"value": "{{werkstatt_temp}}"
}
]
},
"projects":[
"https://github.com/c3re",
"https://git.c3re.de"
]
}