From c5c5586ff10606228ad8431c2971fe689e046d65 Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Sun, 3 Jan 2021 23:04:07 +0100 Subject: [PATCH] first commit --- .gitignore | 1 + Dockerfile | 18 +++++++++++++++++ config.yaml | 16 ++++++++++++++++ generator.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ template.json | 40 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 config.yaml create mode 100755 generator.php create mode 100644 template.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a48cf0d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +public diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..898972d --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..7fe4265 --- /dev/null +++ b/config.yaml @@ -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 + diff --git a/generator.php b/generator.php new file mode 100755 index 0000000..78f02b3 --- /dev/null +++ b/generator.php @@ -0,0 +1,53 @@ +#!/usr/bin/env php + $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(); \ No newline at end of file diff --git a/template.json b/template.json new file mode 100644 index 0000000..946188b --- /dev/null +++ b/template.json @@ -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" + ] +}