2009-08-25 20:56:00 +00:00
< ? php
2010-03-05 15:40:22 +00:00
//
// logger.drush.inc : drush support for the logger module
// Copyright (c) 2008-2009 jokamajo.org
// 2010 flukso.net
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
2009-08-25 20:56:00 +00:00
// $Id$
2010-03-05 15:40:22 +00:00
//
2009-08-25 20:56:00 +00:00
/**
* Implementation of hook_drush_command () .
*/
function logger_drush_command () {
$items = array ();
2010-01-11 09:27:38 +00:00
$items [ 'logger create node' ] = array (
'callback' => '_logger_create_node' ,
2009-08-25 20:56:00 +00:00
'description' => 'Create a new sensor node entry.' ,
'arguments' => array (
'serial' => 'Sensor node serial number.' ,
2009-10-28 22:16:46 +00:00
'country' => 'Destination country.' ,
2009-08-25 20:56:00 +00:00
'uid' => 'User ID.' ,
2010-01-11 09:27:38 +00:00
),
'options' => array (
'--pipe' => 'Returns a space delimited list of created entries.' ,
2009-08-25 20:56:00 +00:00
),
);
2010-01-11 09:27:38 +00:00
$items [ 'logger assign node' ] = array (
'callback' => '_logger_assign_node' ,
2009-08-25 20:56:00 +00:00
'description' => 'Assign a sensor node to a user.' ,
'arguments' => array (
'serial' => 'Sensor node serial number.' ,
'uid' => 'User ID.' ,
),
);
2010-01-11 09:27:38 +00:00
$items [ 'logger config meter' ] = array (
'callback' => '_logger_config_meter' ,
'description' => 'Configure a specific meter.' ,
'arguments' => array (
'meter' => 'Meter ID.' ,
'type' => 'Meter type.' ,
'function' => 'Meter function.' ,
'phase' => 'Meter phase. Only applicable in case of type = electricity.' ,
'constant' => 'Meter constant. Only applicable in case of type = electricity.' ,
'unit' => 'Meter unit.' ,
),
'options' => array (
'--pipe' => 'Returns ok.' ,
),
);
2010-03-13 04:07:22 +00:00
$items [ 'logger token' ] = array (
'callback' => '_logger_token' ,
'description' => 'Create tokens for meter/sensorIDs.' ,
2009-11-30 19:56:15 +00:00
'arguments' => array (
2010-03-13 04:07:22 +00:00
'meter' => 'Create a token for one specific meterID.' ,
'permissions' => 'Set non-default permissions for this token.' ,
2009-11-30 19:56:15 +00:00
),
);
2009-08-25 20:56:00 +00:00
return $items ;
}
/**
* Drush command callbacks .
*/
2010-01-11 09:27:38 +00:00
function _logger_create_node ( $serial , $country = " " , $uid = 0 ) {
2009-08-25 20:56:00 +00:00
// 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 ) {
drush_set_error ( 'LOGGER_CREATE_SERIAL_DUPLICATE' , dt ( 'The S/N: @serial already exists.' , array ( '@serial' => $serial )));
}
else {
2009-12-07 09:21:28 +00:00
if ( $uid > 0 ) {
$result = db_query ( " INSERT INTO { logger_users} (uid, private) VALUES (%d, %d) " , $uid , 0 );
if ( ! $result ) drush_set_error ( 'LOGGER_CREATE_USERS_ENTRY' , dt ( 'Error creating a user entry for @uid.' , array ( '@uid' => $uid )));
}
2009-08-25 20:56:00 +00:00
// create an entry in the {logger_devices} table
$device = md5 ( uniqid ( rand (), TRUE ));
$sha = md5 ( uniqid ( rand (), TRUE ));
$created = time ();
2009-10-28 22:16:46 +00:00
$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 );
2009-08-25 20:56:00 +00:00
if ( ! $result ) drush_set_error ( 'LOGGER_CREATE_DEVICE_ENTRY' , dt ( 'Error creating a device entry for @device.' , array ( '@device' => $device )));
2010-01-11 09:27:38 +00:00
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' );
$pipe [] .= 'DEVICE=' . $device ;
$pipe [] .= 'KEY=' . $sha ;
}
2009-10-28 22:16:46 +00:00
2009-08-25 20:56:00 +00:00
// create an entry in the {logger_meters} table
2009-10-28 22:16:46 +00:00
for ( $i = 0 ; $i < 4 ; $i ++ ) {
2009-11-30 19:56:15 +00:00
$permissions = 62 ;
2009-10-28 22:16:46 +00:00
$meter = md5 ( uniqid ( rand (), TRUE ));
2010-03-13 04:07:22 +00:00
$token = md5 ( uniqid ( rand (), TRUE ));
2009-11-30 19:56:15 +00:00
2009-10-28 22:16:46 +00:00
$path = new stdClass ();
$path -> root = DRUPAL_ROOT . '/' . drupal_get_path ( 'module' , 'logger' );
$path -> base = $path -> root . '/data/base/' ;
$path -> night = $path -> root . '/data/night/' ;
2009-11-30 19:56:15 +00:00
2010-01-11 09:27:38 +00:00
$result = db_query ( " INSERT INTO { logger_meters} (meter, uid, device, created) VALUES ('%s', %d, '%s', %d) " , $meter , $uid , $device , $created );
2010-03-13 04:07:22 +00:00
$insert = db_query ( " INSERT INTO { logger_tokens} (token, meter, permissions) VALUES ('%s', '%s', %d) " , $token , $meter , $permissions );
2009-11-30 19:56:15 +00:00
if ( ! ( $result && $insert )) drush_set_error ( 'LOGGER_CREATE_METER_ENTRY' , dt ( 'Error creating meter entry for @meter.' , array ( '@meter' => $meter )));
2009-08-25 20:56:00 +00:00
2009-10-28 22:16:46 +00:00
// create the meter base rrd
if ( ! file_exists ( $path -> base . $meter . '.rrd' )) {
2010-09-18 07:33:35 +00:00
$command = $path -> root . '/rrdtool create ' . $path -> base . $meter . '.rrd -b 1199487600 -s 60 DS:meter:DERIVE:8640000:0:20 RRA:AVERAGE:0.5:1:1440 RRA:AVERAGE:0.5:15:672 RRA:AVERAGE:0.5:1440:365 RRA:AVERAGE:0.5:10080:520' ;
2009-10-28 22:16:46 +00:00
system ( $command , $return );
2010-03-13 04:07:22 +00:00
if ( ! ( $return == 0 )) {
2009-11-30 19:56:15 +00:00
drush_set_error ( 'LOGGER_CREATE_RRD_BASE_ERROR' , dt ( 'Error creating the base @meter rrd.' , array ( '@meter' => $meter )));
}
2009-10-28 22:16:46 +00:00
}
// create the meter night rrd
if ( ! file_exists ( $path -> night . $meter . '.rrd' )) {
2010-09-18 07:33:35 +00:00
$command = $path -> root . '/rrdtool create ' . $path -> night . $meter . '.rrd -b 1199487600 -s 86400 DS:meter:GAUGE:8640000:0:20 RRA:AVERAGE:0.5:1:365 RRA:AVERAGE:0.5:7:520' ;
2009-10-28 22:16:46 +00:00
system ( $command , $return );
2010-03-13 04:07:22 +00:00
if ( ! ( $return == 0 )) {
2009-11-30 19:56:15 +00:00
drush_set_error ( 'LOGGER_CREATE_RRD_NIGHT_ERROR' , dt ( 'Error creating the night @meter rrd.' , array ( '@meter' => $meter )));
}
2009-10-28 22:16:46 +00:00
}
2010-01-11 09:27:38 +00:00
if ( ! drush_get_error ()) {
2010-03-13 04:07:22 +00:00
drush_log ( dt ( 'Successfully created the meter: @meter with token: @token' , array ( '@meter' => $meter , '@token' => $token )), 'ok' );
2010-01-11 09:27:38 +00:00
$pipe [] .= 'SENSOR' . $i . '=' . $meter ;
}
2009-08-25 20:56:00 +00:00
}
2010-01-11 09:27:38 +00:00
// Space delimited list for use by other scripts. Set the --pipe option.
drush_print_pipe ( implode ( ' ' , $pipe ));
2009-08-25 20:56:00 +00:00
}
}
2010-01-11 09:27:38 +00:00
function _logger_assign_node ( $serial , $country , $uid ) {
2009-08-25 20:56:00 +00:00
// check the existence of S/N
$device = db_result ( db_query ( " SELECT device FROM { logger_devices} WHERE serial = %d " , $serial ));
if ( $device == '' ) {
drush_set_error ( 'LOGGER_ASSIGN_SERIAL_NON_EXISTENT' , dt ( 'The S/N: @serial does not exist.' , array ( '@serial' => $serial )));
}
else {
2009-12-01 10:33:32 +00:00
db_query ( " UPDATE { logger_devices} SET uid = %d, country = '%s' WHERE serial = %d " , $uid , $country , $serial );
2009-08-27 18:29:29 +00:00
db_query ( " UPDATE { logger_meters} SET uid = %d WHERE device = '%s' " , $uid , $device );
2009-12-07 09:21:28 +00:00
$result = db_query ( " INSERT INTO { logger_users} (uid, private) VALUES (%d, %d) " , $uid , 0 );
if ( ! $result ) drush_log ( dt ( 'uid: @uid already exists in the {logger_users} table' , array ( '@uid' => $uid )), 'notice' );
2009-08-25 20:56:00 +00:00
}
if ( ! drush_get_error ()) drush_log ( dt ( 'Successfully assigned uid: @uid to S/N: @serial' , array ( '@uid' => $uid , '@serial' => $serial )), 'ok' );
}
2009-11-30 19:56:15 +00:00
2010-01-11 09:27:38 +00:00
function _logger_config_meter ( $meter , $type , $function , $phase , $constant , $unit ) {
$result = db_query ( " UPDATE { logger_meters} SET type = '%s', function = '%s', phase = %d, constant = %d, unit = '%s' WHERE meter = '%s' " , $type , $function , $phase , $constant , $unit , $meter );
drush_log ( dt ( 'Successfully updated meter: @meter' , array ( '@meter' => $result )), 'ok' );
drush_print_pipe ( 'ok' );
}
2010-03-13 04:07:22 +00:00
function _logger_tokens ( $meter = " " , $permissions = 62 ) {
2009-11-30 19:56:15 +00:00
if ( $meter == " " ) {
$result = db_query ( " SELECT meter FROM { logger_meters} " );
while ( $meter = db_fetch_object ( $result )) {
2010-03-13 04:07:22 +00:00
$count = db_result ( db_query ( " SELECT COUNT(meter) FROM { logger_tokens} WHERE meter = '%s' " , $meter -> meter ));
2009-11-30 19:56:15 +00:00
if ( $count == 0 ) {
2010-03-13 04:07:22 +00:00
$token = md5 ( uniqid ( rand (), TRUE ));
$insert = db_query ( " INSERT INTO { logger_tokens} (token, meter, permissions) VALUES ('%s', '%s', %d) " , $token , $meter -> meter , $permissions );
2009-11-30 19:56:15 +00:00
if ( ! $insert ) {
2010-03-13 04:07:22 +00:00
drush_set_error ( 'LOGGER_CREATE_TOKEN_ENTRY' , dt ( 'Error creating token entry for @meter.' , array ( '@meter' => $meter -> meter )));
2009-11-30 19:56:15 +00:00
}
else {
2010-03-13 04:07:22 +00:00
drush_log ( dt ( 'Created an entry in {logger_tokens} with token: @token and meter: @meter' , array ( '@token' => $token , '@meter' => $meter -> meter )), 'ok' );
2009-11-30 19:56:15 +00:00
}
}
}
}
}