function farm_sensor_listener_process_data in farmOS 7
Process data posted to a sensor.
1 call to farm_sensor_listener_process_data()
- farm_sensor_listener_page_callback in modules/
farm/ farm_sensor/ farm_sensor_listener/ farm_sensor_listener.module - Callback function for processing GET and POST requests to a listener. Handles receiving JSON over HTTP and storing data to the {farm_sensor_data} table. Serves data back via API requests with optional parameters.
File
- modules/
farm/ farm_sensor/ farm_sensor_listener/ farm_sensor_listener.module, line 230
Code
function farm_sensor_listener_process_data($sensor, $data) {
// If the data is an array of multiple data points, iterate over each and
// recursively process.
if (is_array(reset($data))) {
foreach ($data as $point) {
farm_sensor_listener_process_data($sensor, $point);
}
return;
}
// Generate a timestamp from the request time. This will only be used if a
// timestamp is not provided in the JSON data.
$timestamp = REQUEST_TIME;
// If a timestamp is provided, ensure that it is in UNIX timestamp format.
if (!empty($data['timestamp'])) {
// If the timestamp is numeric, we're good!
if (is_numeric($data['timestamp'])) {
$timestamp = $data['timestamp'];
}
else {
$strtotime = strtotime($data['timestamp']);
if (!empty($strtotime)) {
$timestamp = $strtotime;
}
}
}
// Iterate over the JSON properties.
foreach ($data as $key => $value) {
// If the key is "timestamp", skip to the next property in the JSON.
if ($key == 'timestamp') {
continue;
}
// If the value is not numeric, skip it.
if (!is_numeric($value)) {
continue;
}
// Process notifications.
farm_sensor_listener_process_notifications($sensor, $key, $value);
// Create a row to store in the database;
$row = array(
'id' => $sensor->id,
'timestamp' => $timestamp,
'name' => $key,
);
// Convert the value to a fraction.
$fraction = fraction_from_decimal($value);
$row['value_numerator'] = $fraction
->getNumerator();
$row['value_denominator'] = $fraction
->getDenominator();
// Enter the reading into the {farm_sensor_data} table.
drupal_write_record('farm_sensor_data', $row);
// Invoke hook_farm_sensor_listener_data().
module_invoke_all('farm_sensor_listener_data', $sensor, $key, $value);
}
}