From 2803a10254905476ae528bd76cb80f8fc57b144e Mon Sep 17 00:00:00 2001 From: Rob Vella Date: Fri, 31 Jul 2020 17:39:54 -0700 Subject: [PATCH] Trying to fix issues with buffer / allmon not sending correct data --- app.js | 6 +- composer.json | 1 + fetchData.php | 8 ++- index.html | 3 +- stream.php | 94 ---------------------------- streamServer.php | 160 +++++++++++++++++++++++++++++++++++++++++++++++ testdata.txt | 10 --- 7 files changed, 169 insertions(+), 113 deletions(-) delete mode 100644 stream.php create mode 100644 streamServer.php delete mode 100644 testdata.txt diff --git a/app.js b/app.js index ad43f61..9045501 100644 --- a/app.js +++ b/app.js @@ -99,7 +99,7 @@ let App = new Vue({ rows.forEach((v) => { // let match = v.match(/([A-Za-z]+ [0-9]+ [0-9]+\:[0-9]+\:[0-9]+) (rpt|stn)([A-Za-z0-9]+) ?.*? (?:\[(?:via) ([0-9]+))?/); - let match = v.match(/([A-Za-z]+ [0-9]+ [0-9]+\:[0-9]+\:[0-9]+) (rpt|stn)([A-Za-z0-9]+) (KEY|UNKEY)?.*? (?:\[(?:via) ([0-9]+))?/); + let match = v.match(/([A-Za-z]+ [0-9]+ [0-9]+\:[0-9]+\:[0-9]+) (rpt|stn)([A-Za-z0-9]+) (KEY|UNKEY) (?:\[(?:via) ([0-9]+))?/); if (!match) return; let type = this.getNodeType(match[2]); @@ -129,10 +129,6 @@ let App = new Vue({ fetchNodeInfo(node, type) { if (type === 0) return; - // Bind it and recurse, fetch it again - // if (typeof this.nodes[type][node] === "undefined") { - // this.nodes[type][node] = null; - // return this.fetchNodeInfo(node, type); if (this.nodes[type+node]) { // Don't even call fetchNode** return this.nodes[type+node]; diff --git a/composer.json b/composer.json index 3201c93..a2c3622 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ } ], "require": { + "ext-json": "*", "symfony/var-dumper": "^5.1", "nesbot/carbon": "^2.37" }, diff --git a/fetchData.php b/fetchData.php index 0b53690..a28ed35 100644 --- a/fetchData.php +++ b/fetchData.php @@ -1,5 +1,7 @@ Node# - + Via Key Type When @@ -35,6 +35,7 @@ + {{ row.via }} {{ row.key }} {{ row.typeLabel }} {{ row.dateTime.format('hh:mm:ss MM/DD/YY') }} diff --git a/stream.php b/stream.php deleted file mode 100644 index a8d83ae..0000000 --- a/stream.php +++ /dev/null @@ -1,94 +0,0 @@ - $v) { - foreach ($v->remote_nodes as $rn) { - $node = intval($rn->node); - if (isIgnored($node)) continue; - - // Capture previous keyed values - $keyedNow = $rn->keyed === "yes"; - $keyedBefore = isset($keyed[$node]); - - // Set current key state. In PHP null !== isset - $keyed[$node] = $keyedNow ?: null; - - if ($keyedBefore !== $keyedNow) { - // Permanently set to Allstar for now - // $nodePrefix = isIrlp($node) ? 'stn' : 'rpt'; - $nodePrefix = 'rpt'; - $keyedLabel = $keyedNow ? "KEY" : "UNKEY"; - $time = Carbon::now(); - $timeFormatted = $time->format("M d h:i:s"); - // echo "{$node},{$keyedLabel},{$time}\n"; - echo "{$timeFormatted} $nodePrefix{$node} {$keyedLabel} tx\n"; - } - } - } -} - -/** - * @param $node - * @return bool - */ -function isIgnored($node){ - global $ignore; - return in_array($node, $ignore); -} - -/** - * @return string - */ -function getAllMonUri() -{ - global $hubs; - $hubsStr = implode(",", $hubs); - // return "https://allmon.winsystem.org/server.php?nodes=2353"; - // return __DIR__ . '/test.stream'; - return "https://allmon.winsystem.org/server.php?nodes=" . $hubsStr; -} \ No newline at end of file diff --git a/streamServer.php b/streamServer.php new file mode 100644 index 0000000..ad724a3 --- /dev/null +++ b/streamServer.php @@ -0,0 +1,160 @@ +ignore = array_merge($this->hubs, $this->ignore); + $this->stream = fopen($this->getAllMonUri(), "r"); + $this->streamLoop(); + } + + /** + * @return string + */ + public function getAllMonUri() + { + $hubsStr = implode(",", $this->hubs); + // return "https://allmon.winsystem.org/server.php?nodes=2353"; + // return __DIR__ . '/test.stream'; + return "https://allmon.winsystem.org/server.php?nodes=" . $hubsStr; + return "http://kk9rob/allmon2/server.php?nodes=52003"; + } + + /** + * Main event loop + */ + public function streamLoop() { + $buffer = ''; + + while (!feof($this->stream)) { + $buffer .= stream_get_line($this->stream, 2048, "\n\n"); + + if (false !== strpos($buffer, '}}')) { + $buffer .= "\n\n"; + + if (preg_match("/event: (.*?)\ndata: (.*)\n\n/m", $buffer, $matches)) { + $buffer = ''; + if ($matches[1] !== "nodes") continue; + $this->parseStreamData($matches[2]); + } + } + } + + // TODO: Failure condition for feof to restart stream, or let supervisor handle it? + } + + /** + * @param $node + * @return bool + */ + protected function isIgnored($node) + { + return in_array($node, $this->ignore); + } + + /** + * @param $json + */ + protected function parseStreamData($json) + { + // Debugging -- sometimes allmon doesn't send the proper keyup + // file_put_contents(__DIR__.'/storage/event_nodes.txt', "$json,\n", FILE_APPEND); + $obj = json_decode($json); + + foreach ($obj as $node => $v) { + foreach ($v->remote_nodes as $remoteNode) { + $via = intval($node); + $node = intval($remoteNode->node); + if ($this->isIgnored($node)) continue; + + // Capture previous keyed values + $keyedNow = $remoteNode->keyed === "yes"; + $keyedBefore = isset($this->keyed[$node]); + + // Set current key state. In PHP null !== isset + $this->keyed[$node] = $keyedNow ?: null; + + if ($keyedBefore !== $keyedNow) { + if (filesize($this->streamOutput) >= 10240) { + file_put_contents($this->streamOutput, ''); + } + + // Permanently set to Allstar for now + // $nodePrefix = isIrlp($node) ? 'stn' : 'rpt'; + $nodePrefix = 'rpt'; + $keyedLabel = $keyedNow ? "KEY" : "UNKEY"; + $time = Carbon::now(); + $timeFormatted = $time->format("M d h:i:s"); + // echo "{$node},{$keyedLabel},{$time}\n"; + $toWrite = "{$timeFormatted} $nodePrefix{$node} {$keyedLabel} [via {$via}]\n"; + file_put_contents($this->streamOutput, $toWrite, FILE_APPEND); + echo $toWrite; + } + } + } + } +} \ No newline at end of file diff --git a/testdata.txt b/testdata.txt deleted file mode 100644 index be4de9d..0000000 --- a/testdata.txt +++ /dev/null @@ -1,10 +0,0 @@ - Jul 30 18:23:23 rpt29600 tx [via 2353] - Jul 30 18:33:11 rpt45073 tx [via 2353] - Jul 30 18:33:24 rpt48697 tx [via 2560] - Jul 30 18:33:33 stn3543 92 packets -Jul 30 18:33:43 rpt28462 tx [via 2560] - Jul 30 18:33:51 rpt28462 tx [via 2560] - Jul 30 18:33:57 rpt28462 tx [via 2560] - - -