worker.php hinzugefügt
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
<?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();
|
||||
print_r($config);
|
||||
|
||||
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) {
|
||||
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 = queryDb($sql);
|
||||
|
||||
// process every device found on database
|
||||
foreach( $data as $device) {
|
||||
// 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[0][2]);
|
||||
print_r($dataset);
|
||||
|
||||
// process configured monitors
|
||||
foreach($config->monitors as $monitor) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user