allmon2-last-heard/fetchData.php

108 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2020-07-31 00:30:36 -04:00
<?php
$streamFile = __DIR__.'/storage/stream.txt';
2020-07-31 00:30:36 -04:00
$cmd = $_GET['cmd'] ?? null;
2021-02-07 18:51:38 -05:00
$node = $_GET['node'] ?? null;
2020-07-31 00:30:36 -04:00
$type = intval($_GET['type'] ?? null);
2021-02-07 18:51:38 -05:00
header("Cache-Control: no-cache, max-age=0");
2020-07-31 00:30:36 -04:00
if ($cmd === "log" || $cmd == "logText") {
// $irlpData = file_get_contents("http://www3.winsystem.org/monitor/ajax-logtail.php");
$data = file_get_contents($streamFile);
2020-07-31 00:30:36 -04:00
// Just output the raw data
if ($cmd === "log") {
echo $data;
// echo $irlpData;
2020-07-31 00:30:36 -04:00
return;
}
} else if ($cmd === "node") {
if (empty($node)) return;
if ($type === 1) {
$info = fetchNodeInfoAllstar($node);
} else if ($type === 2) {
$info = fetchNodeInfoIRLP($node);
}
echo json_encode($info, JSON_UNESCAPED_SLASHES);
}
return;
function fetchNodeInfoAllstar($node) {
$db = fetchAllStarDb();
$db = explode("\n", $db);
foreach ($db as $row) {
$row = explode("|", $row);
if (intval($row[0]) !== $node) continue;
return [
'node' => $node,
2021-02-07 18:51:38 -05:00
'callsign' => $row[1] ?? '',
'desc' => $row[2] ?? '',
'location' => $row[3] ?? ''
2020-07-31 00:30:36 -04:00
];
}
return [
'node' => $node,
'callsign' => 'Internal'
2020-07-31 00:30:36 -04:00
];
}
function fetchNodeInfoIRLP($node) {
$db = fetchIRLPDb();
$db = explode("\n", $db);
foreach ($db as $row) {
$line = str_getcsv($row, "\t");
if ($line[0] != $node) continue;
return [
'node' => $node,
'callsign' => $line[1],
'desc' => $line[15],
'location' => "{$line[2]}, {$line[3]}"
];
}
return [
'node' => $node,
'callsign' => 'Internal',
2020-07-31 00:30:36 -04:00
'desc' => '',
'location' => ''
];
}
function fetchAllStarDb() {
$dbFile = __DIR__.'/storage/allstar.txt';
2021-01-11 09:56:52 -05:00
if (!file_exists($dbFile) || outdatedFile($dbFile)) {
2020-07-31 00:30:36 -04:00
$db = file_get_contents("http://allmondb.allstarlink.org");
file_put_contents($dbFile, $db);
return $db;
}
return file_get_contents($dbFile);
}
function fetchIRLPDb() {
$dbFile = __DIR__.'/storage/irlp.txt';
2021-01-11 09:56:52 -05:00
if (!file_exists($dbFile) || outdatedFile($dbFile)) {
2020-07-31 00:30:36 -04:00
$db = file_get_contents("http://status.irlp.net/nohtmlstatus.txt.zip");
file_put_contents("${dbFile}.zip", $db);
shell_exec("/usr/bin/unzip -p ${dbFile}.zip > ${dbFile}");
}
return file_get_contents($dbFile);
}
2021-01-11 09:56:52 -05:00
function outdatedFile($path)
{
return time() - filemtime($path) > 3600;
}