web: drush logger create now assigns 4 meters per device

This commit is contained in:
Bart Van Der Meerssche 2009-10-28 22:16:46 +00:00
parent 8367d43828
commit cb6e20bdcc
1 changed files with 34 additions and 22 deletions

View File

@ -12,6 +12,7 @@ function logger_drush_command() {
'description' => 'Create a new sensor node entry.',
'arguments' => array(
'serial' => 'Sensor node serial number.',
'country' => 'Destination country.',
'uid' => 'User ID.',
),
);
@ -31,7 +32,7 @@ function logger_drush_command() {
/**
* Drush command callbacks.
*/
function _logger_create($serial, $uid = 0) {
function _logger_create($serial, $country, $uid = 0) {
// guard against duplicating entries for the same S/N
$count = db_result(db_query("SELECT COUNT(device) FROM {logger_devices} WHERE serial = %d", $serial));
if ($count > 0) {
@ -44,16 +45,26 @@ function _logger_create($serial, $uid = 0) {
$sha = md5(uniqid(rand(), TRUE));
$created = time();
$result = db_query("INSERT INTO {logger_devices} (device, serial, uid, sha, created) VALUES ('%s', %d, %d, '%s', %d)", $device, $serial, $uid, $sha, $created);
$result = db_query("INSERT INTO {logger_devices} (device, serial, uid, sha, created, country) VALUES ('%s', %d, %d, '%s', %d, '%s')", $device, $serial, $uid, $sha, $created, $country);
if (!$result) drush_set_error('LOGGER_CREATE_DEVICE_ENTRY', dt('Error creating a device entry for @device.', array('@device' => $device)));
if (!drush_get_error()) drush_log(dt('Successfully created the device/key: @device / @key for S/N: @serial', array('@device' => $device, '@key' => $sha, '@serial' => $serial)), 'ok');
// create an entry in the {logger_meters} table
for ($i = 0; $i < 4; $i++) {
if ($i == 0) {
$type = 'electricity';
}
else {
$type = 'undefined';
}
$meter = md5(uniqid(rand(), TRUE));
$path = new stdClass();
$path->root = DRUPAL_ROOT .'/'. drupal_get_path('module', 'logger');
$path->base = $path->root .'/data/base/';
$path->night = $path->root .'/data/night/';
$result = db_query("INSERT INTO {logger_meters} (meter, uid, device, created) VALUES ('%s', %d, '%s', %d)", $meter, $uid, $device, $created);
$result = db_query("INSERT INTO {logger_meters} (meter, uid, device, created, type) VALUES ('%s', %d, '%s', %d, '%s')", $meter, $uid, $device, $created, $type);
if (!$result) drush_set_error('LOGGER_CREATE_METER_ENTRY', dt('Error creating meter entry for @meter.', array('@meter' => $meter)));
// create the meter base rrd
@ -69,9 +80,10 @@ function _logger_create($serial, $uid = 0) {
system($command, $return);
if ($return <> 0) drush_set_error('LOGGER_CREATE_RRD_NIGHT_ERROR', dt('Error creating the night @meter rrd.', array('@meter' => $meter)));
}
}
if (!drush_get_error()) drush_log(dt('Successfully created the device/key: @device / @key and meter: @meter entries for S/N: @serial', array('@device' => $device, '@key' => $sha, '@meter' => $meter, '@serial' => $serial)), 'ok');
if (!drush_get_error()) drush_log(dt('Successfully created the meter: @meter with type: @type', array('@meter' => $meter, '@type' => $type)), 'ok');
}
}
}
function _logger_assign($serial, $uid) {