Skip to content

Commit

Permalink
Better URL management and config tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
nigeljohnson73 committed Mar 26, 2021
1 parent 592d15f commit 8932733
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 169 deletions.
16 changes: 8 additions & 8 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"name" => "PI",
"label" => "PI",
"type" => "PI",
"colour" => "#660000"
"colour" => "#009900"
];
// $sensors [] = ( object ) [
// "name" => "ZONE1",
Expand Down Expand Up @@ -45,12 +45,12 @@
// "name" => "ZONE2", // Generic air zone
// "type" => "EMPTY" // EMPTY or MH-Z19B
// ];
$sensors [] = ( object ) [ // This sensor is for display purposes
"name" => "EXPECT",
"label" => "EXPECT",
"type" => "EXPECT",
"colour" => "#fac" // Pink
];
// $sensors [] = ( object ) [ // This sensor is for display purposes
// "name" => "EXPECT",
// "label" => "EXPECT",
// "type" => "EXPECT",
// "colour" => "#fac" // Pink
// ];

$triggers = [ ];
// $triggers [] = ( object ) [
Expand Down Expand Up @@ -126,7 +126,7 @@
/**
* START REBUILD
*/
$rebuild_from = "Simulation"; // 'Simulation', 'Demands' or filename in the locations directory, eg 'SZ.Malkerns.json'
$rebuild_from = "Simulation"; // 'Demand', 'Simulation' or filename in the locations directory, eg 'SZ.Malkerns.json'
/**
* END REBUILD
*/
Expand Down
100 changes: 54 additions & 46 deletions include/_app.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?php

function setTrigger($name, $state) {
function getRemoteData($url, $timeout = 5) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec ( $ch );
curl_close ( $ch );
return $response;
}

function setTrigger($name, $state, $timeout = 2) {
global $mysql;
$ch = curl_init ();

Expand All @@ -9,17 +19,18 @@ function setTrigger($name, $state) {
) );
if ($ips and count ( $ips )) {
$ip = $ips [0] ["ip"];
$url = "http://" . $ip . "/api/trigger/set";

curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt ( $ch, CURLOPT_URL, "http://" . $ip . "/api/trigger/set" );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, "name=" . urlencode ( $name ) . "&state=" . urlencode ( $state ) );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/x-www-form-urlencoded'
) );

// receive server response ...
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );

$server_output = curl_exec ( $ch );

curl_close ( $ch );
Expand All @@ -30,8 +41,15 @@ function setTrigger($name, $state) {
// } else {

// }
if ($server_output === false) {
logger ( LL_INF, "Set trigger '" . $name . "' to '" . (($state) ? ("ON") : ("OFF")) . "' - '" . $url . "' timed out" );
return false;
} else {
logger ( LL_INF, "Set trigger '" . $name . "' to '" . (($state) ? ("ON") : ("OFF")) . "' - success" );
return true;
}
} else {
echo "FAILED\n";
// echo "FAILED\n";
return false;
}
return true;
Expand Down Expand Up @@ -436,14 +454,14 @@ function setupTables() {
// )";
// $mysql->query ( $str );

// $str = "
// CREATE TABLE IF NOT EXISTS expects (
// event BIGINT UNSIGNED NOT NULL,
// param VARCHAR(255) NOT NULL,
// value VARCHAR(255) NOT NULL,
// KEY(event),
// KEY(param)
// )";
// $str = "
// CREATE TABLE IF NOT EXISTS expects (
// event BIGINT UNSIGNED NOT NULL,
// param VARCHAR(255) NOT NULL,
// value VARCHAR(255) NOT NULL,
// KEY(event),
// KEY(param)
// )";
$mysql->query ( $str );

clearLogs ();
Expand All @@ -453,7 +471,7 @@ function clearLogs() {
global $mysql, $logger;
$tsnow = timestampNow ();
$ts_delete = timestampAddDays ( $tsnow, - 1 );
$mysql->query ( "DELETE FROM expects where event < " . $ts_delete );
// $mysql->query ( "DELETE FROM expects where event < " . $ts_delete );
$mysql->query ( "DELETE FROM sensors where event < " . $ts_delete );
// $mysql->query ( "DELETE FROM triggers where event < " . $ts_delete );
$logger->clearLogs ();
Expand Down Expand Up @@ -1087,7 +1105,7 @@ function rebuildDataModel() {
// $model[timestampFormat(time2Timestamp($sunrise), "md")] = $v;
}
$location = $obj->location;
} else if (strtoupper ( $rebuild_from ) == "EXPECTS") {
} else if (strtoupper ( $rebuild_from ) == "DEMAND") {
if (count ( $expect )) {
logger ( LL_INFO, "rebuildDataModel(): Rebuilding from Demand Ramping" );
$location->name = "Demand Ramping";
Expand Down Expand Up @@ -1849,16 +1867,6 @@ enumerateSensors ();
}
}

function getRemoteData($url, $timeout = 4) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$response = curl_exec ( $ch );
curl_close ( $ch );
return $response;
}

function checkRegistration() {
global $mysql;
$tsnow = timestampNow ();
Expand Down Expand Up @@ -2187,8 +2195,7 @@ function tick() {
$nowOffset = timestampFormat ( $tsnow, "H" ) * 60 * 60 + timestampFormat ( $tsnow, "i" ) * 60 + timestampFormat ( $tsnow, "s" );

$model = getModel ( $tsnow );
echo "\nModel data:\n";
print_r ( $model );
echo "\nModel data: " . ob_print_r ( $model ) . "\n";

// Prepare the storage for later
$data = array (); // Whre we will store sensors and trigger data
Expand Down Expand Up @@ -2308,24 +2315,6 @@ function tick() {

echo "\n";

echo "Checking sensor registrations\n";
checkRegistration ();
echo "Complete\n\n";

echo "Gathering sensor data\n";
$sensors = gatherSensors ();
$data = array_merge ( $data, $sensors );
echo "Complete\n\n";

echo "Checking alarms\n";
$data = checkAlarms ( $data );
echo "Complete\n\n";
// echo "Writing LASTCHECK: '" . @$data ["INFO.LASTCHECK"] . "'\n";

echo "Checking conditions\n";
$data = checkConditions ( $data );
echo "Complete\n\n";

$retvar = 0;
$output = "";
$cmd = "hostname 2>/dev/null";
Expand All @@ -2348,7 +2337,26 @@ function tick() {
$data ["INFO.HOSTNAME"] = $hostname;
$data ["INFO.NEXTSUN"] = $next_sun;

echo "Checking sensor registrations\n";
checkRegistration ();
echo "Complete\n\n";

echo "Gathering sensor data\n";
$sensors = gatherSensors ();
$data = array_merge ( $data, $sensors );
echo "Complete\n\n";

echo "Checking alarms\n";
$data = checkAlarms ( $data );
echo "Complete\n\n";
// echo "Writing LASTCHECK: '" . @$data ["INFO.LASTCHECK"] . "'\n";

echo "Checking conditions\n";
$data = checkConditions ( $data );
echo "Complete\n\n";

ksort ( $data );
echo "Environment: " . ob_print_r ( $data );
$estr = json_encode ( $data );
setConfig ( "env", $estr );
file_put_contents ( "/tmp/env.gstation.json", $estr );
Expand Down
Loading

0 comments on commit 8932733

Please sign in to comment.