<?php
require_once ('class.davis.php');
davis::writeLog ( print_r ( $_REQUEST, true ) );
// this is the API for the davis weather station class.
/**
* the following are accepted params
*
* this API returns GOOD values as JSON strings
* Errors in plane text
*
*
* param values
* function FETCH (use to pull archives from the database) ;
* Collect and save are no longer supported. They are left in because that may still work in some cases when used with a single station ID
* !!!!!COLLECT (use to read the given sensor value directly from the given station) ;!!!!!!
* !!!!!SAVE (function to collect a sensors value and save it directly to the database)
* stationID int of valid station ID
* sensor All, BarTrend,Pressure, TempIn, HumIn, TempOut, WindSpeed, WindSpeed10Min, WindDir,
* ExtraTemps, SoilTemps, LeafTemps, HumOut, HumExtra, RainRate, UV, SolarRad, RainStorm,
* StormStartDate, RainDay, RainMonth, RainYear, ETDay, ETMonth, ETYear, SoilMoist,
* LeafWetness, AlarmIn, AlarmRain, AlarmOut, AlarmExTempHum, AlarmSoilLeaf, BatteryStatus,
* BatteryVolts, ForecastIcon, ForecastRuleNo, SunRise, SunSet
* fromDate unix time stamp or FULL ( use this with the FETCH function to set a date range of records to fetch. if left out fetch returns ALL records for the given station.)
* toDate unix time stamp or FULL (must be sent if from date is sent) NOTE: see comments in code about how these work.
*
* example usage:
* To return data array of ALL sensor data or a single sensor without saving to database
* api.php?function=COLLECT&station=<STATION ID>&sensor[]=<SENSOR NAME>
*
* To return the data for a given station from the database for the given dates.
* api.php?function=FETCH&station=<STATION ID>&sensor=ALL&fromDate=<UNIXTIMESTAMP>$toDate=<UNIXTIMESTAMP>
*
* to return ALL data for a given station from the database
* api.php?function=FETCH&station=<STATION ID>&sensor=ALL&fromDate=ALL$toDate=ALL
*
**/
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');
//collect sent values
if (isset ( $_REQUEST ['function'] ) && isset ( $_REQUEST ['stationID'] ) && isset ( $_REQUEST ['sensor'] )) {
// collect function and validate
$function = davis::isValidAPIFunction ( $_REQUEST ['function'] );
// collect and validate station ID
$stationID = davis::isValidStation ( $_REQUEST ['stationID'] );
// collect ann validate sensor
$sensor = davis::isValidSensor ( $_REQUEST ['sensor'] );
if ($function) {
// collect staion ID and validate
if ($stationID) {
// if we get to this point we have a valid stationID anf a valid function.
if ($sensor) {
if (davis::isValidDate ( $_REQUEST ['fromDate'] ) && davis::isValidDate ( $_REQUEST ['toDate'] )) {
// if we get to here we have valid data for all the passed vars
// process function requests
switch ($function) {
case 'COLLECT' :
// get the station details
$stationObj = davis::getStation ( $stationID );
$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) {
// pull out the data for the requested sensor
switch ($sensor) {
case 'ALL' :
print_r($connection->dataArray);
echo json_encode ( $connection->dataArray );
break;
default :
if (is_array ( $sensor )) {
// if more than one sensor was requested
$requestedSensorsValues = array ();
foreach ( $sensor as $s ) {
$requestedSensorsValues [$s] = $connection->dataArray [$s];
}
echo json_encode ( $requestedSensorsValues );
} else {
echo '{"' . $sensor . '":' . $connection->dataArray [$sensor] . '}';
}
}
} else {
echo "<p>Unable to collect valid data from station : " . $stationObj->stationID . "</p>";
}
} else {
echo "<p>Unable to wake station.</p>";
}
$connection->disconnect ();
}
break;
case 'FETCH' :
$stationObj = davis::getStation ( $stationID );
$connection = new davis ( $stationObj->stationIP, $stationObj->stationPort );
$fromDate = $_REQUEST ['fromDate'];
$toDate = $_REQUEST ['toDate'];
// if either date is set to FULL then do this
if ($fromDate == 'FULL' || $toDate == 'FULL') {
// depending on how these are set....
//echo $sensor;
// if both are set to FULL then fetch ALL records fo the given station.
if ($toDate == 'FULL' && $fromDate == 'FULL') {
$jsonString = json_encode ( $connection->getStationDataByDate ( $stationID, false, false, $sensor ) );
}
// if toDate is set to FULL then fetch all records after the value of fromDate
if ($toDate == 'FULL' && $fromDate != 'FULL') {
$fromDate = date ( 'Y-m-d h:i:s', $fromDate );
$jsonString = json_encode ( $connection->getStationDataByDate ( $stationID, $fromDate, false, $sensor ) );
}
// if from date is set to full then fetch all the records befor the value of todate
if ($toDate != 'FULL' && $fromDate == 'FULL') {
$toDate = date ( 'Y-m-d h:i:s', $toDate );
$jsonString = json_encode ( $connection->getStationDataByDate ( $stationID, false, $toDate, $sensor ) );
}
} else {
// convert dates to correct format
$toDate = date ( 'Y-m-d h:i:s', $toDate );
$fromDate = date ( 'Y-m-d h:i:s', $fromDate );
$jsonString = json_encode ( $connection->getStationDataByDate ( $stationID, $fromDate, $toDate, $sensor ) );
}
echo $jsonString;
break;
case 'SAVE' :
// save the sensor data for the requested station to the database.
// fetch the requested data from the database
// get the station details
$stationObj = davis::getStation ( $stationID );
$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 )) {
// return json string of saved data
echo json_encode ( $connection->dataArray );
} else {
davis::writeLog ( "<p>Unable to save data array for station : " . $stationObj->stationID . "</p>" );
}
} else {
davis::writeLog ( "<p>Unable to collect valid data from station : " . $stationObj->stationID . "</p>" );
}
} else {
davis::writeLog ( "<p>Unable to wake station.</p>" );
}
$connection->disconnect ();
}
break;
}
} else {
davis::writeLog ( "<p>Invalid date sent to api.</p>" );
}
} else {
davis::writeLog ( "<p>Invalid sensor name sent to api :" . $_REQUEST ['sensor'] . "</p>" );
}
} else {
davis::writeLog ( "<p>Invalid station ID (" . $_REQUEST ['stationID'] . ") sent to the api.</p>" );
}
} else {
davis::writeLog ( "<p>Invalid function sent to api: " . $_REQUEST ['function'] . "</p>" );
}
} else {
davis::writeLog ( "<p>Fatal Error: One or more of the required values was not sent to the API. This API REQUIRES a function, a station ID and a sensor.</p>" );
}
?>
|