|
|||||||||||||||
Code-in-actionClick here to see actual sites based on OpenBasin technology.
Sample Code:
The Creating new OpenBasin objects HOWTO contains more information on how to customize OpenBasin scripts for your own purposes. The Creating webpages with OpenBasin HOWTO contains more information on how to build websites using OpenBasin technology Sample Code:
Creating an OpenBasin Object
<?php
require_once("ob_config.php");
require_once("$OPENBASIN_OBJ_DIR/OpenBasin.php");
// $DB_DBNAME is not necessary if $DB_DBNAME is 'openbasin'
$OpenBasin =& new OpenBasin($DB_USERNAME, $DB_PASSWORD, $DB_DBNAME);
?>
Next we will explore two different ways to access the data. The first is to use the newly-created $OpenBasin object directly to pull information, the second is to use the $OpenBasin object to create station objects which will be used to access the data.
Accessing the data using the $OpenBasin object
<?php
// $stationColumnsID is either a single station_columns_id or an array of station_columns_ids
// A station_columns_id is a unique identifier for each specific column in the database
// To get the latest database values:
$dataset = $OpenBasin->getDatasetByStationColumnsID($stationColumnsID);
// To get a time span of database values:
$dataspan=$OpenBasin->getDataspanByStationColumnsID($stationColumnsID,'',$beginTime, $endTime);
?>
However, since using station_columns_ids is not very easy nor is it intuitive, this approach is most commonly used in programs where the data to be displayed is stored in the database by station_columns_id.
Accessing the data using a station object
<?php
// $stationIdentifier is a short unique name that identifies a station from all others
$station = $OpenBasin->makeStationObject($stationIdentifier);
// $keyword is the name of a key created in the database and assigned to a number of different columns
// For example, 'battery', 'water_flow', etc. could all be $keyword values.
// To get the latest database values:
$dataset = $station->getDatasetByKeyword($keyword);
// To get a time span of database values:
$dataspan = $station->getDataspanByKeyword($keyword, '', $beginTime, $endTime);
?>
Qualifying the data returnedUsing a station object to access data will automatically limit the data returned to data contained within that station. You may impose other restrictions by using any combination of qualifiers as demonstrated below:
<?php
// Only return hourly data
$station->timespanQualifier = 3600;
// Only return data from tables specified as 'data' or 'calc' tables
$station->tableDataQualifier = Array('data','calc');
// Only return information from the 'station_hour' table
$station->tableQualifier= 'station_hour';
// Only return columns specified as 'min' or 'max'
$station->dataTypeQualifier= Array('min','max');
// Only return columns named 'h_avg_dg_batt_1'
$station->columnQualifier = 'h_avg_dg_batt_1';
?>
As you may notice either a single value or an array of values can be assigned to each qualifier. Also, these qualifiers must be set before the call to getDatasetByKeyword() or getDataspanByKeyword() is made.
Returning data from multiple stations
<?php
require_once("$OPENBASIN_OBJ_DIR/Key.php");
// $keyword is the name of a key created in the database and assigned to a number of different columns
// For example, 'battery', 'water_flow', etc. could all be $keyword values.
$key =& new Key($keyword,$OpenBasin);
?>
Then similar to adding qualifiers to the station object, you may add qualifiers directly to the key object, however, there are two additional qualifiers:
<?php
// Only return data from the database 'data_db'
$key->databaseQualifier = 'data_db';
// Only return data from either 'station_1' or 'station_2'
$key->stationQualifer = Array('station_1','station_2');
?>
In fact, you can create a key object without a $keyword variable, and all columns that match the requirements in the qualifiers will be returned. The creation of a key object without a $keyword variable is shown below:
<?php
require_once("$OPENBASIN_OBJ_DIR/Key.php");
$key =& new Key('','',$OpenBasin);
?>
Finally, to return data from the key object we do the following:
<?php
require_once("$OPENBASIN_OBJ_DIR/Dataset.php");
require_once("$OPENBASIN_OBJ_DIR/Dataspan.php");
// To get the latest database values:
$dataset =& new Dataset($key->getLatestValues(), $key->getGroups());
// To get a time span of database values:
$dataspan =& new Dataspan($key->getDataspan($beginTime, $endTime), $key->getGroups());
?>
Using a Dataset
<?php
while($value = $dataset1->getValue()){
// Do something here
}
while(list($value, $units, $timestamp) = $dataset2->getData()){
// Do something here
}
while($dataArray = $dataset3->getExtendedData()){
list($value, $units, $timestamp, $timespan, $columnName, $stationColumnsID) = $dataArray;
// Do something here
}
$value = $dataset->getValue($stationColumnsID);
list($value, $units, $timestamp) = $dataset->getData($stationColumnsID);
$dataArray = $dataset->getExtendedData($stationColumnsID);
list($value, $units, $timestamp, $timespan, $columnName, $stationColumnsID) = $dataArray;
?>
Using a Dataspan
<?php
while($dataset = $dataspan->getDataset()){
// Do something
}
while($array = $dataspan->getValue()){
foreach($array as $tmp){
$value = $tmp;
}
}
while($array = $dataspan->getData()){
foreach($array as $tmp){
list($value, $units) = $tmp;
}
}
while($array = $dataspan->getExtendedData()){
foreach($array as $tmp){
list($value, $units, $timestamp, $timespan, $columnName, $stationColumnsID) = $tmp;
}
}
$dataset = $dataspan->getDataset($timestamp);
$array = $dataspan->getValue($timestamp);
$array = $dataspan->getData($timestamp);
$array = $dataspan->getExtendedData($timestamp);
?>
Features:
Data grouping
<?php
$station =& $OpenBasin->makeStationObject('station');
$station->timespanQualifier = 3600; // Hourly data only
$station->datatypeQualifier = Array('min','avg','max'); // Retrieve minimum, average, and maximum values
$dataset = $station->getDatasetByKeyword('battery');
$min_dataset = $dataset->getGroup('min');
$avg_dataset = $dataset->getGroup('avg');
$max_dataset = $dataset->getGroup('max');
?>
Using this method you can easily return large amounts of differing data, yet still be able to distinguish which data represents what. This feature (combined with other OpenBasin features) allows you to intuitively specify data by what the data represents, not where it is located in the database.
Data buffering
Automatic data conversions This means that regardless of the type of measurement made by the sensor, OpenBasin can be set up to return all the information in a common format. For example, if you have an air temperature sensor that measures in Farenheit and a soil temperature sensor that measures in Celcius, by simply making a few configurations to OpenBasin you will be able to return both values in either Farenheit or Celcius without additional programming. In fact, the website programmer does not even need to know which sensor measures Farenheit or Celcius. This feature does a great deal to simplify the programming process, and provide a more unified, standard data set. You can learn more about creating custom sensor objects in the Creating new OpenBasin objects HOWTO or more about creating PostgreSQL stored procedures in the Creating new PostgreSQL stored procedures HOWTO. You may also find custom sensor objects and custom PostgreSQL stored procedures in the Plugins section of this website.
Know of another site? Let us know by emailing webmaster@openbasin.org.
technical contact: Matthew Maxwell |