// To run shell scripts, probably works with different types of scripts too.
// Pre-condition: Full path to the script, the script file name,
// and any parameters the script needs to run
function run_script($path, $script, $parameters) {
$cmd = $path.'/'.$script.' '.$parameters;
system($cmd);
}
// Plots the small and large graphs for the file passed to it
// Pre-condition: The name of the file you want to plot minus the .txt,
// as well as the script name and full path
function plot_it($file, $script, $path) {
$small = ".png 320 240";
$large = "_lg.png 1024 768";
// If file is an array of files set the parameters to...
if (is_array($file)) {
// Name of graph
$graph = $file['name'];
// Infiles for tc330
$infile = $file['heater']." ".$file['setpoint']." ".$file['dewar']." ".$file['array']." ";
// Infiles for tc208
for ($i=0; $i < 8; $i++)
$infile .= $file['ch'.($i+1)]." ";
}
else { // or else set the parameters to...
// Name of graph
$graph = $file;
// Infile for graph
$infile = $file.".txt ";
}
// Check for which file is being passed to determin which graph parameters
// to use; 2 hours, 1 day, or one week.
if (strrpos($graph, "_2hours") === strlen($graph)-strlen("_2hours")) {
run_script($path, $script, $graph." ".$graph.$small." 3600 lines ".$infile);
run_script($path, $script, $graph." ".$graph.$large." 3600 linespoints ".$infile);
}
else if (strrpos($graph, "_1day") === strlen($graph)-strlen("_1day")) {
run_script($path, $script, $graph." ".$graph.$small." 28800 lines ".$infile);
run_script($path, $script, $graph." ".$graph.$large." 28800 lines ".$infile);
}
else if (strrpos($graph, "_1week") === strlen($graph)-strlen("_1week")) {
run_script($path, $script, $graph." ".$graph.$small." 86400 lines ".$infile);
run_script($path, $script, $graph." ".$graph.$large." 86400 lines ".$infile);
}
else
echo "Unrecognized File!";
}
// Query all of the SensorData for the set parameters and write the txt files
// Pre-condition: The path, script, database, table, how far back you want
// to look, and the name you want for the graph
function plot_tc330($path, $script, $database, $table, $sec_ago, $outfile)
{
// Set up the text files holding the data being graphed (tc330: dewar temp, array temp, heater, and setpoint)
run_script($path, $script, $database." ".$table." ".$sec_ago." 330a_cdat > ".$outfile."_cdat.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 330a_sdat > ".$outfile."_sdat.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 330a_heat > ".$outfile."_heat.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 330a_setpt > ".$outfile."_setpt.txt");
// Return an array with all of the names of the text files just created
return array('name' => $outfile, 'dewar' => $outfile.'_cdat.txt', 'array' => $outfile.'_sdat.txt', 'heater' => $outfile.'_heat.txt', 'setpoint' => $outfile.'_setpt.txt');
}
// Query all of the SensorData for the set parameters and write the txt files
// Pre-condition: The path, script, database, table, how far back you want
// to look, and the name you want for the graph
function plot_tc208($path, $script, $database, $table, $sec_ago, $outfile)
{
// Set up the text files holding the data being graphed (tc208: channels 1-8)
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch1 > ".$outfile."_ch1.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch2 > ".$outfile."_ch2.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch3 > ".$outfile."_ch3.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch4 > ".$outfile."_ch4.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch5 > ".$outfile."_ch5.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch6 > ".$outfile."_ch6.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch7 > ".$outfile."_ch7.txt");
run_script($path, $script, $database." ".$table." ".$sec_ago." 208_ch8 > ".$outfile."_ch8.txt");
// Return an array with all of the names of the text files just created
return array('name' => $outfile, 'ch1' => $outfile.'_ch1.txt', 'ch2' => $outfile.'_ch2.txt', 'ch3' => $outfile.'_ch3.txt', 'ch4' => $outfile.'_ch4.txt', 'ch5' => $outfile.'_ch5.txt', 'ch6' => $outfile.'_ch6.txt', 'ch7' => $outfile.'_ch7.txt', 'ch8' => $outfile.'_ch8.txt');
}
// Set an array with
// Pre-condition: The name of the graph
function array_outfiles($outfile)
{
// If the array is for tc208...
if (strpos($outfile, "tc208") !== False)
{
// Return an array with all of the names of the text files holding data from tc208
return array('name' => $outfile, 'ch1' => $outfile.'_ch1.txt', 'ch2' => $outfile.'_ch2.txt', 'ch3' => $outfile.'_ch3.txt', 'ch4' => $outfile.'_ch4.txt', 'ch5' => $outfile.'_ch5.txt', 'ch6' => $outfile.'_ch6.txt', 'ch7' => $outfile.'_ch7.txt', 'ch8' => $outfile.'_ch8.txt');
}
else // if not for tc208...
{
// Return an array with all of the names of the text files holding data from tc330...
return array('name' => $outfile, 'dewar' => $outfile.'_cdat.txt', 'array' => $outfile.'_sdat.txt', 'heater' => $outfile.'_heat.txt', 'setpoint' => $outfile.'_setpt.txt');
}
}
// Sets up the query
// Pre-condition: The table with your data, how far back you want to
// search, and the SensorID of the data you want.
function query_SensorData($table, $sec_ago, $sensor_id)
{
// Returns the query with the parameters passed to this function
return $query = "SELECT Timestamp, FROM_UNIXTIME(Timestamp) AS date, SensorData FROM ".$table." WHERE Timestamp > (UNIX_TIMESTAMP() - ".$sec_ago.") AND SensorID = '".$sensor_id."'";
}
// Query MySQL and update the .txt file associated
// Pre-condition: ID Prefix for the sensor you are looking up (bd, gd, spex),
// Timeframe, file name with .txt, and the table the data is in
function set_txts($query, $outfile) {
// Open the .txt file being writen to
$fh = fopen($outfile, 'w') or die("can't open file ".$outfile);
// Collect the query results
$results = mysql_query($query) or DIE("Error with Query: ".$query);
// Write the results to the .txt file
while ($row=mysql_fetch_array($results)){
$stringData = $row[0]."\t".$row[1]."\t".$row[2]."\n";
fwrite($fh, $stringData);
}
// Clear the query
mysql_free_result($results);
// Close the .txt file
fclose($fh);
}
// List the text files in the array used to make the graph
// Pre-condition: An array with your text file names
function list_txt_files($array)
{
// Start the list
$list = 'Data to make Graph:
';
// Go through each item in the array with key as $key and value as $value
foreach($array as $key => $value)
{
// If key is not equal to name
if ($key !== 'name')
{
// and value is not the last item in the array
if (end($array) !== $value)
{
// Add value to the list and add a new line
$list .= <<< DATA
{$value}
DATA;
}
else // if it is the last item
{
// Just add the value to the list
$list .= <<< DATA
{$value}
DATA;
}
}
}
// Return the list
return $list;
}
?>