PHP Classes
elePHPant
Icontem

File: cron.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Leigh Edwards  >  Davis Weather  >  cron.php  >  Download  
File: cron.php
Role: Example script
Content type: text/plain
Description: file to run as cron to collect the data
Class: Davis Weather
Communicate with Davis weather stations
Author: By
Last change: typos
Date: 2 years ago
Size: 1,678 bytes
 

Contents

Class file image Download
Davis Weather Station Cron Script.
<br />
V1.1
<br />
Writen By Leigh Edwards 2011
<br />
<?php
// this script requires a my SQL database object
// use a version of class.mysql > 1.2
require_once ('class.mysql.v.1.55.php');
$sqlObj = new mysql ();
$sqlObj->connect ( '<HOST NAME>', '<USER NAME>', '<PASSWORD>' );
$sqlObj->select ( '<DB NAME>' );
require_once (
'class.davis.php');

foreach (
davis::getAllStations () as $stationObj ) {
   
$connection = new davis ( $stationObj->stationIP, $stationObj->stationPort );
    if (
$connection->connect () == 0) { // Make connection counter intuative, but if connection === 0 then we are good to go
       
if ($connection->doCommand ( WAKE, 1 )) { // wake the console
           
$goodData = false;
           
$tries = 0;
           
// loop until we get good data from the station or max attempts is reached.
           
while ( ! $goodData || $tries <= $connection->tries ) {
               
$goodData = $connection->loopToArray ( $connection->doCommand ( "LOOP 1", 3 ) ); // process the data and put it in the output array
               
$tries ++;
            }
            if (
$goodData) {
                if (
davis::saveDataArray ( $stationObj->stationID, $connection->dataArray )) {
                    echo
"<p>Data array saved for station" . $stationObj->stationID . "</p>";
                } else {
                    echo
"<p>Unable to save data array for station : " . $stationObj->stationID . "</p>";
                }
            } else {
                echo
"<p>Unable to collect valid data from station : " . $stationObj->stationID ."</p>";
            }
        } else {
            echo
"<p>Unable to wake station.</p>";
        }
       
$connection->disconnect ();
    } else {
        echo
"<p>Unable to make connection to Weather Station at " . $stationObj->stationIP . "</p>";
    }
}
?>