此文章转载于 http://sourceforge.net/p/openacs/wiki/Scripting/
OpenACS supports configuration scripts written in JavaScript. If you want to use scripts to configure CPE you must have a script named ‘Default’ because this is the script the server will try to run on CPE Inform request when it has no configuration files or firmware update requests pending .
Function provides access to standard application server logger facility.
logger('My message'); // message with default severity WARNING is logged.
logger('severe', 'Error message');
logger('warning', 'Warning message');
logger('info', 'Informational message');
Function lets to run another script.
call('script_name');
cpedb global variable provides acces to CPE bean in database, namely to properties field. They can be accessed as usual javascript properties. To save them back to database method Save should be used. This object is intended to assign configuration properties for user.
var someprop = cpedb.someprop;
cpedb.someprop = 'new_value_for_someprop';
cpedb.Save();
cpe global variable provides access to all the CPE variables and RPC calls specified in TR-069.
Inform - exposes Inform request which triggered configuration script to run.
GetRPCMethods () - return arrays of methods supported by CPE.
// this will output supported methods to server console
var methods = cpe.GetRPCMethods();
for (i = 0; i < methods.length; i++) {
logger('Method: '+methods[i]);
}
Download(commandKey, what_to_download, url, username, password, file_size, file_name) - Tells CPE to start downloading configuration or firmware depending on what_to_download parameter (possible values as specified in TR-069)
var response = cpe.Download("daCommand", "3 Vendor Configuration File","http://192.168.1.1:8080/kkonf", "", "",000,"user.ini");
logger("st="+response.StartTime+" ct="+CompleteTime+" status="+response.Status);
GetParameterValues (array_of_parameter_names) - return array of object with properties name and value.
var parameters = new Array();
parameters[0] = 'InternetGatewayDevice.DeviceSummary';
var response = cpe.GetParameterValues(parameters);
logger(response[0].name+'='+response[0].value);
SetParameterValues (params_array, commandKey) - sets parameter values. Params_array is array of objects having properties name and value.
var parameters = new Array();
parameters[0] = {name: 'InternetGatewayDevice.IPPingDiagnostics.Host', value: '192.168.0.1'};
// Encode parameter using default type xsd:string
parameters[1] = {name: 'InternetGatewayDevice.IPPingDiagnostics.NumberOfRepetitions', value: '2'};
// set encoding type
parameters[2] = {name: 'InternetGatewayDevice.IPPingDiagnostics.NumberOfRepetitions', value: '2', type: 'xsd:unsignedInt'};
cpe.SetParameterValues(parameters, "commandKey");
AddObject (top_object_name, parameterKey) - creates new object instance. Returns object with properties Status and InstanceNumber.
DeleteObject (object_name, parameterKey) - delete object. Returns integer status.
SetParameterAttributes (params_array) - set attributes for parameters. Returns nothing.
var parameters = new Array();
parameters[0]=new Object;
parameters[0].Name='InternetGatewayDevice.IPPingDiagnostics.NumberOfRepetitions';
parameters[0].Notification=0;
parameters[0].NotificationChange=true;
parameters[0].AccessListChange=true;
parameters[0].AccessList= new Array();
parameters[0].AccessList[0]='subscriber';
cpe.SetParameterAttributes(parameters);
GetParameterAttributes (parameter_names_array)
var p=new Array();
p[0]='InternetGatewayDevice.ManagementServer.PeriodicInformEnable';
var r = cpe.GetParameterAttributes (p);
Reboot (commandKey) - reboots cpe.
cpe.Reboot("commandKey");
FactoryReset - resets cpe to factory default settings. Method is optional in cpe so not all of them has support for it.
cpe.FactoryReset();
ScheduleInform (seconds_to_delay, commandKey) - request the CPE to schedule a one-time Inform method call (separate from its periodic Inform method calls) sometime in the future. Method is optional in cpe so not all of them has support for it.
cpe.ScheduleInform(3600, "commandKey"); // schedule inform in one hour
X_00000C_ShowStatus (array_of_show_commands) - Cisco proprietary method to run several show commands and get their output.
var commands = new Array();
commands[0] = "show version";
commands[1] = "show running";
var response = cpe.X_00000C_ShowStatus(commands);
var thecommand = response[0].Command;
var itsoutput = response[0].Response;
X_JUNGO_COM_RGCommand (telnet_command) - jungo.com OpenRG based firmware proprietary method to run command.
var response = cpe.X_JUNGO_COM_RGCommand("net ifconfig");
var result = response.Result; // output of command
var status = response.Status; // status - 0 if succes
SyncParameterValues - check and synchronize parameter values on cpe.
var parameters = new Array();
parameters[0] = {Name: 'InternetGatewayDevice.ManagementServer.PeriodicInformEnable', Value: '1'};
parameters[1] = {Name: 'InternetGatewayDevice.ManagementServer.PeriodicInformInterval', Value: '300'};
cpe.SyncParameterValues(parameters);
This object allows to work with database. The database can be the same the openacs uses to store data as well as external datasource configured similar to datasource in [1] step 3
This would run SELECT query on datasource named the data_source_name.
db.Query(data_source_name, select_statement)
This would run query on openacs datasource “java:ACS”.
db.Query(select_statement)
it is equivalent to
db.Query("java:ACS", select_statement)
returns array of objects with fields name as in query e.g.
try {
var rs = db.Query("SELECT id,serialno FROM hostsbean")
logger("Rows found = "+rs.length)
for (i = 0; i < rs.length; i++) {
logger("id = "+rs[i].id+" serialno= "+rs[i].serialno);
}
} catch (e) {
logger("DS exception: "+e.message)
}
To run INSERT or UPDATE queries use Update function.
db.Update(data_source_name, update_statement)
This would run query on openacs datasource “java:ACS”.
db.Update(select_statement)
returns count of rows affected e.g.
try {
var rs = db.Update("INSERT id,serialno INTO hostsbean VALUES (NULL,'12345')")
} catch (e) {
logger("DS exception: "+e.message)
}