add door last changed

master
Dirk Heilig 2023-01-09 23:54:42 +01:00
parent 31c42e1ffa
commit f3a6cecbc0
2 changed files with 100 additions and 72 deletions

View File

@ -7,47 +7,79 @@ foreach (getenv() as $k => $v) {
$search[] = "\$$k"; $search[] = "\$$k";
$replace[] = '"' . addslashes($v) . '"'; $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(); $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) { $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']) { switch ($data[$message->topic]["cast"]) {
case 'int': case "int":
$data[$message->topic]['value'] = (int)$message->payload; $data[$message->topic]["value"] = (int) $message->payload;
break; break;
case 'float': case "float":
$data[$message->topic]['value'] = (float)$message->payload; $data[$message->topic]["value"] = (float) $message->payload;
break; break;
case 'bool': case "bool":
$data[$message->topic]['value'] = (bool)$message->payload; $data[$message->topic]["value"] = (bool) $message->payload;
break; break;
default: default:
echo "error, cast type " . $data[$message->topic]['cast'] . " unknown\n"; echo "error, cast type " .
$data[$message->topic]["cast"] .
" unknown\n";
die(1); die(1);
} }
$search = []; $data[$message->topic]["changed"] = time();
$replace = []; $out = file_get_contents(__DIR__ . "/template.json");
foreach ($data as $v) { $out = preg_replace_callback(
$search[] = '"{{' . $v['name'] . '}}"'; "/{{(?<name>[^#}]+?)(#(?<modifier>[^}]+))?}}/",
$replace[] = json_encode($v['value']); 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");
} }
file_put_contents("public/spaceapi.json", $out);
$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']); $c->connect($conf["mqtt"]["host"]);
$data = []; $data = [];
foreach ($conf['variables'] as $name => $variable) { foreach ($conf["variables"] as $name => $variable) {
$data[$variable['topic']] = [ $data[$variable["topic"]] = [
"value" => null,
'value' => null, "cast" => $variable["cast"],
'cast' => $variable['cast'], "name" => $name,
'name' => $name "changed" => 0,
]; ];
$c->subscribe($variable['topic'], 2); $c->subscribe($variable["topic"], 2);
} }
$c->loopForever(); $c->loopForever();

View File

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