Welcome to the Cumulus Support forum.

Latest Cumulus MX release 3.9.1 (build 3096) - 31 October 2020 (please see announcement regarding releases since 3.5.0)
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 the NOAA report and Snow Index drop-down menus to 2030)

Use this Wiki link to Download the Software (Cumulus MX / Cumulus 1 and other related items).

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: 125
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: 7168
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2
Operating System: Buster 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: 125
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: 125
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: 7168
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2
Operating System: Buster 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?

Post Reply