Files
VE.Direct-notify/worker.php
T
2026-06-02 23:58:59 +02:00

71 lines
2.1 KiB
PHP

<?php
/*
* worker.php
*
* This file is part of the VE.Direct notify service
* https://git.schauaus.at/VE-Tools/VE.Direct-notify
* It is licensed under the GPL3.0 or later.
* You should have received a copy of the license with this software
*/
$config = readConfig();
queryMonitors();
function readConfig() {
return json_decode( file_get_contents('config.json') );
}
function sendMessage($message) {
global $config;
$authorization = "";
if( defined($config->service->username) && defined($config->service->password) )
$authorization = 'Basic ' + base64_encode($config->service->username + ':' + $config->service->password);
elseif( defined($config->service->token) )
$authorization = 'Bearer ' + $config->service->token;
$result = file_get_contents( $config->service->url + '/' + $config->service->topic, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' =>
'Content-Type: text/plain\r\n' .
'Authorization: ' + $authorization,
'content' => $message
]
]));
return true;
}
function queryMonitors() {
global $config;
// establish database connection
$dbPath = (str_starts_with($config->database, '/')) ? $config->database : dirname(__FILE__) . '/' . $config->database;
try {
$db = new SQLite3($dbPath, SQLITE3_OPEN_READONLY);
}
catch (Exception $e) {
print ('error detected: ' . $e->getMessage() . "\r\n");
return false;
}
// query devices from database
$sql = "SELECT name FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' AND name <> 'meta' ORDER BY name ASC;";
$data = $db->query($sql);
// process every device found on database
while( $device = $data->fetchArray(SQLITE3_ASSOC) ) {
// query device data from database
$sql = "SELECT * FROM '" . $device['name'] . "' ORDER BY date DESC, time DESC LIMIT 1;";
$res = $db->query($sql)->fetchArray(SQLITE3_NUM);
$dataset = json_decode($res[2]);
print_r($dataset);
// process configured monitors
foreach($config->monitors as $monitor) {
}
}
}
?>