Skip to content

Commit

Permalink
Tiltbridge and multi-api working
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Dec 12, 2019
1 parent 3aee402 commit 2c7e76b
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 3 deletions.
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ function receiveControlSettings(callback) {
if (typeof (controlSettings.dataLogging) !== 'undefined') {
var $loggingState = $("span.data-logging-state");
if (controlSettings.dataLogging === 'paused') {
$loggingState.text("(logging paused)");
$loggingState.text("(paused)");
$loggingState.show();
}
else if (controlSettings.dataLogging === 'stopped') {
$loggingState.text("(logging stopped)");
$loggingState.text("(stopped)");
$loggingState.show();
}
else {
Expand Down
164 changes: 164 additions & 0 deletions multi-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
/* Copyright (C) 2019 Lee C. Bussy (@LBussy)
*
* This file is part of LBussy's BrewPi WWW Remix (BrewPi-WWW-RMX).
*
* BrewPi WWW RMX is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* BrewPi WWW RMX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi WWW RMX. If not, see <https://www.gnu.org/licenses/>.
*/

$debug = true; // Write log file if true
$logfile = "multi-apilog.txt"; // API Debug Log
$subTarget = "brewpi-api.php"; // Target file in multi-chamber
$json = file_get_contents('php://input'); // Get incoming post

function writeLog($logLine) { // Log file writer (if $debug == true)
global $debug;
if ($debug) {
// Get timestamp
$date = date('Y-m-j H:m:s ', time());
//Open the File Stream
global $logfile;
$handle = fopen($logfile, "a");

//Lock File, error if unable to lock
if (flock($handle, LOCK_EX)) {
fwrite($handle, $date);
fwrite($handle, $logLine);
fwrite($handle, "\n");
flock($handle, LOCK_UN);
}
}
}

function getFileList($dir, $recurse = FALSE, $depth = FALSE) { // Get list of API files
global $subTarget;
$retval = [];

if(substr($dir, -1) != "/") { // Add trailing slash if missing
$dir .= "/";
}

// Open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading");
while(FALSE !== ($entry = $d->read())) {
// Skip hidden files
if($entry{0} == ".") continue;
if(is_dir("{$dir}{$entry}")) {
// Skip directory;
if($recurse && is_readable("{$dir}{$entry}/")) {
if($depth === FALSE) {
$retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE));
} elseif($depth > 0) {
$retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE, $depth-1));
}
}
} elseif(is_readable("{$dir}{$entry}")) {
if ($entry == $subTarget) {
$retval[] = [
'name' => "{$dir}{$entry}",
];
}
}
}
$d->close();
return $retval;
}

function postToUrl($url, $jsonString) { // Post JSON to URL and return result
$stream = stream_context_create(
array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'Content-Length' => strlen($jsonString),
'content' => $jsonString,
),
)
);

$result = file_get_contents(
$url,
false,
$stream
);

return $result;
}

function getHeader($result) { // Determine header type
$header = "";
switch (intval(substr($result, 0, 3))) {
case 200:
$header = "HTTP/1.1 200 OK";
break;
case 400:
$header = "HTTP/1.1 400 Bad Request";
break;
case 403:
$header = "HTTP/1.1 403 Forbidden";
break;
case 500:
$header = "HTTP/1.1 500 Internal Server Error";
break;
default:
$header = "HTTP/1.1 520 Unknown Error";
break;
}
return $header;
}

function postToTargets($json) {
$jsonString = json_decode($json);
if (json_last_error() === JSON_ERROR_NONE) { // JSON is valid
header("HTTP/1.1 200 OK");
echo "Ok.";
// JSON is good, do the posts
writeLog("Received JSON: " . $json);
// Get file list
$dirlist = getFileList("./", TRUE, 1);
// Sort the array
sort($dirlist);
foreach($dirlist as $file => $value) { // First, count targets
$target = $value["name"];
if (substr_count($target, '/') >= 2) {
// Write to target
$url = "http://localhost" . substr($target, 1);

$result = postToUrl($url, $json); // Send string not decoded (array)

$retval = intval(substr($result, 0, 3));
$header = getHeader($result);

writeLog("Result from " . $url . ": " . $retval);
echo $url . ": " . $result . "\n";
}
}
return true;
} else {
// Unable to decode JSON
return false;
}
}

if (postToTargets($json)) {
writeLog("JSON received and processed.");
header("HTTP/1.1 200 OK");
echo("OK");
} else {
writeLog("Invalid JSON received.");
header("HTTP/1.1 400 Bad Request");
echo "Invalid JSON received.";
}

?>
2 changes: 1 addition & 1 deletion top-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
echo $logo;
?>
<div id=beer-name-container>
<span>Showing: </span>
<span>Logging: </span>
<a href='#' id="beer-name"><?php echo urldecode($beerName); ?></a>
<?php
// If we are in multi-chamber, print ' in {chamber name}' after
Expand Down

0 comments on commit 2c7e76b

Please sign in to comment.