Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4018) - 28 March 2024

Legacy Cumulus 1 release v1.9.4 (build 1099) - 28 November 2014 (a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

PHP scripts problems

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
The PIT
Posts: 260
Joined: Thu 10 Dec 2009 6:15 pm
Weather Station: Davis VP2 Wireless
Operating System: Windows 7 32 bit
Location: England
Contact:

PHP scripts problems

Post by The PIT »

Most of the time they work without issues.

I had a problem with the windows scheduler starting two copies or more of the script which seems to be bug of the scheduler.

However other times it fails and generates one or more corrupted png files.

One of the culprits is this one cloudbase



<?php
/*!
* Simple static trend graphs for Cumulus MX
*
* Created by Mark Crossley, Febraury 2015
*
* Released under GNU GENERAL PUBLIC LICENSE, Version 2, June 1991
* See the enclosed License file
*
* File encoding = UTF-8
*
*/

require_once 'graphSettings.php';

$name = basename($_SERVER['PHP_SELF'], '.php').'.png';

// Create the graph and set a scale.
$graph = new Graph($GRAPH['width'], $GRAPH['height'], $name, $GRAPH['cachetime']);
$graph->SetScale('datint');

//fetch the data
$data = get_data(array('cloudbasevalue'));

graph_common($graph);

$graph->title->Set('Cloudbase (' .$GRAPH['uom']['cloudbase']. ')');

// Create the linear plot
$lineplot1 = new LinePlot($data['cloudbasevalue'], $data['time']);
$lineplot1->SetWeight(2);

if (strtolower(substr($GRAPH['uom']['cloudbase'], 0, 1)) === 'm') {
// Force labels to only be displayed every 500 m, tick every 100
$graph->yaxis->scale->ticks->Set(500, 100);
} else {
// Force labels to only be displayed every 1000 ft, tick every 500
$graph->yaxis->scale->ticks->Set(1000, 500);
}
$graph->xaxis->scale->setGrace(5, 0);
$graph->yaxis->scale->setAutoMin(0);

// Add the plot to the graph
$graph->Add($lineplot1);

$lineplot1->SetColor("#B22222:1.3");

// Display the graph
@unlink(CACHE_DIR . $name);
$graph->Stroke();
exit;
?>


The graphics file is. The only I altered is the date as it came back wrong

<?php
/*!
* Simple static trend graphs for Cumulus MX
*
* Created by Mark Crossley, January 2015
*
* Released under GNU GENERAL PUBLIC LICENSE, Version 2, June 1991
* See the enclosed License file
*
* File encoding = UTF-8
*
*/

$GRAPH = array();
############################################################################
# CONFIGURATION INFORMATION
############################################################################
$GRAPH['version'] = '4.3.2';
$GRAPH['width'] = 600;
$GRAPH['height'] = 300;
$GRAPH['jsonloc'] = 'c:/Program Files (X86)/CumulusMx/web';
$GRAPH['jpgraphloc'] = 'c:/php/includes/'; // Path to jpgraph, or put it in your PHP path
// NOTE: This is the *server* operating system path to the file, so unless you have added
// the web folder to your PHP path, it will be something like "/home/<userid>/www/jpgraph/"
$GRAPH['cachetime'] = 10; // number of minutes to cache images
$GRAPH['hours'] = 24; // number of hours to graph
$GRAPH['uom'] = array('temp'=>'C', 'rain'=>'mm', 'wind'=>'mph', 'baro'=>'hPa', 'cloudbase'=>'feet');
$GRAPH['rosePoints'] = 16; // 8 or 16
$GRAPH['roseSize'] = 400;
// Localised Compass point array
$GRAPH['compass'] = array('N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW');

#---------------------------------------------------------------------------

############################################################################
# Includes for JpGraph
############################################################################
include $GRAPH['jpgraphloc'] . 'jpgraph.php';
include $GRAPH['jpgraphloc'] . 'jpgraph_line.php';
include $GRAPH['jpgraphloc'] . 'jpgraph_scatter.php';
include $GRAPH['jpgraphloc'] . 'jpgraph_date.php';
include $GRAPH['jpgraphloc'] . 'jpgraph_plotline.php';
# include $GRAPH['jpgraphloc'] . 'jpgraph_windrose.php';

// Set the TZ to UTC so times display in 'station' time
//define('TIMEZONE', 'Europe/London');
// date_default_timezone_set('UTC'); Removed as set the incorrect time
// echo date(DATE_RFC2822);
// Current field names (matches tag fields) used
$GRAPH['cvalues'] = array(
'date','time','temp','hum','dew','wspeed','wlatest','bearing','rrate',
'rfall','press','currentwdir','beaufortnumber','windunit','tempunitnodeg','pressunit','rainunit',
'windrun','presstrendval','rmonth','ryear','rfallY','intemp','inhum','wchill',
'temptrend','tempTH','TtempTH','tempTL','TtempTL',
'windTM','TwindTM','wgustTM','TwgustTM',
'pressTH','TpressTH','pressTL','TpressTL',
'version','build',
'wgust','heatindex','humidex','UV','ET','SolarRad','avgbearing',
'rhour','forecastnumber','isdaylight','SensorContactLost','wdir','cloudbasevalue','cloudbaseunit',
'apptemp','SunshineHours','CurrentSolarmax','IsSunny');


############################################################################
# COMMON FUNCTIONS
############################################################################

function graph_common($graph) {
// global $graph;

// Remove the default theme
$graph->graph_theme = null;

// Ensure anti-aliasing is off.
$graph->img->SetAntiAliasing(false);

// Setup margin and titles
$graph->SetMargin(50, 20, 20, 55);

$graph->xaxis->HideLine(false);
$graph->xaxis->HideTicks(false, false);
$graph->xaxis->SetPos('min');
$graph->xgrid->SetFill(false);
$graph->xgrid->SetColor('gray');
$graph->xgrid->SetLineStyle('dotted');
$graph->xgrid->Show(true, false);

$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false, false);
$graph->yaxis->scale->SetGrace(5, 5);
//$graph->yaxis->SetTitleMargin(32);
$graph->ygrid->SetFill(false);
$graph->ygrid->SetColor('gray');
$graph->ygrid->SetLineStyle('dotted');

// Plot area settings
$graph->SetBox($aDrawPlotFrame=true, $aPlotFrameColor=array(100,100,100), $aPlotFrameWeight=1);
$graph->SetBackgroundGradient($aFrom=array(200,220,220), $aTo=array(250,255,255), $aGradType=GRAD_MIDHOR, $aStyle=BGRAD_FRAME);

$graph->legend->SetFillColor('#d0d0d0');
$graph->legend->SetFrameWeight(1);
$graph->legend->SetPos(0.5, 0.98, 'center', 'bottom');
$graph->legend->SetLayout(LEGEND_HOR);

// Adjust the start time for an "even" 6 hours
//$graph->xaxis->scale->SetTimeAlign(HOURADJ_6);

// Force labels to only be displayed every 6 hours, tick every hour
$graph->xaxis->scale->ticks->Set(6*60*60, 1*60*60);

// Use hour:minute format for the labels
$graph->xaxis->scale->SetDateFormat('H:i');

}

function get_data($fields) {
global $GRAPH;
$retVal = array();

include 'db_ro_details.php';

// Connect to the database
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($mysqli->connect_errno) {
die('Failed to connect to the database server - ' . $mysqli->connect_error);
}

$cols = '';
foreach ($fields as $fld) {
if (array_search($fld, $GRAPH['cvalues'])) {
$cols .= $fld . ',';
} else {
die("!!Failed to match field: $fld");
}
}
$cols = rtrim($cols, ',');

$query = "SELECT unix_timestamp(LogDateTime) AS time, $cols
FROM realtime
WHERE LogDateTime >= now() - INTERVAL " .$GRAPH['hours']. " HOUR
ORDER BY time";

$result = $mysqli->query($query);
if (!$result) {
die('ERROR - Bad Select Statement: ' . $mysqli->error . '<br><br>' . $query);
}


// get the field names in teh returned data
$flds = $result->fetch_fields();
$keys = array();
for ($i = 0; $i < count($flds); $i++) {
$keys[] = $flds[$i]->name;
}

// fetch the SQL data
while ($row = $result->fetch_assoc()) {
foreach($keys as $key) {
$retVal[$key][] = $row[$key];
}
}

// close connection
//$mysqli->close();
mysqli_close($mysqli);

return($retVal);
}

############################################################################
# END OF SCRIPT
############################################################################
?>

the php error log shows this

[21-Nov-2020 05:45:03 Europe/London] PHP Warning: fopen(C:/Program Files (x86)/CumulusMx/web/jpgraph_cache/graphCloudbase.png): failed to open stream: Permission denied in C:\php\includes\gd_image.inc.php on line 2177
[21-Nov-2020 05:45:03 Europe/London] PHP Warning: flock() expects parameter 1 to be resource, boolean given in C:\php\includes\gd_image.inc.php on line 2178
[21-Nov-2020 05:45:03 Europe/London] PHP Warning: flock() expects parameter 1 to be resource, boolean given in C:\php\includes\gd_image.inc.php on line 2186
[21-Nov-2020 05:45:03 Europe/London] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\php\includes\gd_image.inc.php on line 2187

However I feel this is a red herring and the issue is more that php is still open from the previous run
So I added the exit; to the end of the cloudbase.php file.

Running the file manually and it will run fine.

At the moment it's running fine on the scheduler.
Based in Sunny Old Sheffield South Yorkshire England

www.sheffieldweather.co.uk
User avatar
mcrossley
Posts: 12692
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: PHP scripts problems

Post by mcrossley »

Installing Cumulus into Program Files is not recommended as that is normally a write protected folder. Hence the permission denied? Normally writes will be redirected into your user profile to avoid the write failure.

I'd check where your data files are being written as well.

Then copy everything to a location outside the Program Files folders.
The PIT
Posts: 260
Joined: Thu 10 Dec 2009 6:15 pm
Weather Station: Davis VP2 Wireless
Operating System: Windows 7 32 bit
Location: England
Contact:

Re: PHP scripts problems

Post by The PIT »

Don't worry I know about permissions they are all set correctly.
If it was a permission issue they would always fail.
I do need to set php permissions properly though as at the moment they can run everywhere but then since this system isn't public facing it shouldn't be an issue. On my web server php is limited to a few folders with none admin access and Apache runs on none admin account as well. This reminds me when I mess around with my new server I got set those appropriately.
Yesterday I added exit; on the relevant scripts and this morning they have all run as I would have expected.
So I may have fixed it.
Just in case it hasn't I've also just added max execution time to the script.
Based in Sunny Old Sheffield South Yorkshire England

www.sheffieldweather.co.uk
The PIT
Posts: 260
Joined: Thu 10 Dec 2009 6:15 pm
Weather Station: Davis VP2 Wireless
Operating System: Windows 7 32 bit
Location: England
Contact:

Re: PHP scripts problems

Post by The PIT »

Still acting will run perfectly for a couple of days then for reasons unknown the task scheduler will suddenly end up launching two tasks which works for a while then it falls over in a heap.
Trying an alternative at the moment too see if that's more reliable.
Based in Sunny Old Sheffield South Yorkshire England

www.sheffieldweather.co.uk
User avatar
mcrossley
Posts: 12692
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: PHP scripts problems

Post by mcrossley »

I think if there was a bug in the Windows Scheduler double launching tasks we'd have heard about it by now?

You haven't really explained what you are trying to do?

It looks like you have taken the PHP graphs scripts and are running them locally and saving the resulting image files and serving those rather the invoking the script each time a client requests it?
The PIT
Posts: 260
Joined: Thu 10 Dec 2009 6:15 pm
Weather Station: Davis VP2 Wireless
Operating System: Windows 7 32 bit
Location: England
Contact:

Re: PHP scripts problems

Post by The PIT »

I'm keeping the scripts away from the web server as it's faff enough keeping up to date and patched. They are then running twice a day to create 30 day graphs, 15 day graphs and 7 day graphs. Cumulusmx just loads them up to the web server so there's no regeneration when the user views the web page.

I'm not skilled enough to use php scripts and generate them on demand so an image file is a lot easier. it was mean adding mysql to the server and then uploading the database onto that.

Anyway using a free third party program and split the tasks. The ones using the windows scheduler after a couple of days started re-launching the same task before it finished trying to run them in parallel and this option was switched off. The third party ran a different ran it's selected scripts without issues without any duplicates starting.

I know there's an issue if you disable a task then re-enable it it creates a new instance and then both instances will run when the time comes. There's a few ways to fix that in theory one telling the task manger not to start a new task which didn't fix it, Stop the first instance which didn't fix and then end all existing instances which is supposed to stop this from happening.

At the moment I've now switched over to the third party scheduler.
Based in Sunny Old Sheffield South Yorkshire England

www.sheffieldweather.co.uk
The PIT
Posts: 260
Joined: Thu 10 Dec 2009 6:15 pm
Weather Station: Davis VP2 Wireless
Operating System: Windows 7 32 bit
Location: England
Contact:

Re: PHP scripts problems

Post by The PIT »

Working fine with a third party scheduler no issues what so ever.
Based in Sunny Old Sheffield South Yorkshire England

www.sheffieldweather.co.uk
Post Reply