From f3a6cecbc0ccf9bfd70ff9b042ccd5e4f43f009c Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Mon, 9 Jan 2023 23:54:42 +0100 Subject: [PATCH] add door last changed --- generator/generator.php | 92 +++++++++++++++++++++++++++-------------- generator/template.json | 80 +++++++++++++++++------------------ 2 files changed, 100 insertions(+), 72 deletions(-) diff --git a/generator/generator.php b/generator/generator.php index 78f02b3..d6413c0 100755 --- a/generator/generator.php +++ b/generator/generator.php @@ -7,47 +7,79 @@ foreach (getenv() as $k => $v) { $search[] = "\$$k"; $replace[] = '"' . addslashes($v) . '"'; } -$conf = yaml_parse(str_replace($search, $replace, file_get_contents(__DIR__ . '/config.yaml'))); +$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']); +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; + if (!isset($data[$message->topic])) { + return; + } - switch ($data[$message->topic]['cast']) { - case 'int': - $data[$message->topic]['value'] = (int)$message->payload; + switch ($data[$message->topic]["cast"]) { + case "int": + $data[$message->topic]["value"] = (int) $message->payload; break; - case 'float': - $data[$message->topic]['value'] = (float)$message->payload; + case "float": + $data[$message->topic]["value"] = (float) $message->payload; break; - case 'bool': - $data[$message->topic]['value'] = (bool)$message->payload; + case "bool": + $data[$message->topic]["value"] = (bool) $message->payload; break; default: - echo "error, cast type " . $data[$message->topic]['cast'] . " unknown\n"; + 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']); + $data[$message->topic]["changed"] = time(); + $out = file_get_contents(__DIR__ . "/template.json"); + $out = preg_replace_callback( + "/{{(?[^#}]+?)(#(?[^}]+))?}}/", + function ($m) use ($data, $conf) { + $value = $data[$conf["variables"][$m["name"]]["topic"]]["value"]; + $modifier = isset($m["modifier"]) ? $m["modifier"] : "noop"; + switch ($modifier) { + case "timestamp": + $value = + $data[$conf["variables"][$m["name"]]["topic"]][ + "changed" + ]; + break; + case "noop": + break; + + default: + echo "error, modifier " . $m["modifier"] . " unknown\n"; + die(1); + } + return json_encode($value); + }, + $out + ); + + $out = json_encode( + json_decode($out), + JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE + ); + if (!is_dir("public")) { + mkdir("public"); } - - $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); + file_put_contents("public/spaceapi.json", $out); }); -$c->connect($conf['mqtt']['host']); +$c->connect($conf["mqtt"]["host"]); $data = []; -foreach ($conf['variables'] as $name => $variable) { - $data[$variable['topic']] = [ - - 'value' => null, - 'cast' => $variable['cast'], - 'name' => $name +foreach ($conf["variables"] as $name => $variable) { + $data[$variable["topic"]] = [ + "value" => null, + "cast" => $variable["cast"], + "name" => $name, + "changed" => 0, ]; - $c->subscribe($variable['topic'], 2); + $c->subscribe($variable["topic"], 2); } -$c->loopForever(); \ No newline at end of file +$c->loopForever(); + diff --git a/generator/template.json b/generator/template.json index d0e1b9b..dbe41d5 100644 --- a/generator/template.json +++ b/generator/template.json @@ -1,44 +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}}", - "icon": { - "open":"https://c3re.de/doorstatus/open.png", - "closed":"https://c3re.de/doorstatus/closed.png" - } - }, - "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" - ] + "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}}", + "lastchange": "{{doorstatus#timestamp}}", + "icon": { + "open": "https://c3re.de/doorstatus/open.png", + "closed": "https://c3re.de/doorstatus/closed.png" + } + }, + "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"] }