Extensions - findings.

David White david.white at trustpower.co.nz
Sun Mar 12 16:49:35 EDT 2017


Hi all,
        First off, I am a total noob with javascript, node, mongo etc etc. I have been playing with the extension scripts and thought I would share my findings with anyone that is interested.

I have had a heck of a time trying to get Genieacs to query  a collection from mongodb. I have made a simple provisioning collection with serialNumber, SIP user and SIP pass but this could apply to anything.


1.       For testing purposes I have set my test device to inform every 20 seconds, then apply the preset to a "2 PERIODIC" event and my device serial - means I can test end to end.

2.       Genieacs will cache your extension script. Meaning if you change the script on the fly, it will not pick up the updates. The way I have been working around this is to rename the script then update the provision to include the new name.

3.        This one makes sense but took me a while, if a script faults or errors, it will not run again until you clear the fault in the GUI. (Think this is on a per device basis)

4.       If you look at sandbox.js under genieacs/lib, there is a 'log' function. This is SUPER handy as if you start genieacs-cwmp and log to file (genieacs-cwmp >> /var/log/genieacs-cwmp.log 2>> /var/log/genieacs-cwmp-err.log) you can then tail the file and see what your script is doing.

5.       Change the group permissions on the log folder to include genieacs.

6.       This took me a LONG time to figure out - any arguments that you pass into the function ARE NOT STRINGS, I spent hours wondering why my mongo query kept returning no results, it was because I had to convert the serial to a string first.


Ok here is the meat of it. Have omitted any specific details.

Provision in Genieacs GUI:

let serial = declare("InternetGatewayDevice.DeviceInfo.SerialNumber", {value: 1});
let response = ext("a7", "getSIP", serial.value[0]);
log('provision script is running: ' + serial.value[0] + ' username= ' + response.username +' password: ' + response.password + ' device: '+ response.serial);

Here is the script genieacs/config/ext/a7.js

----------------------------------------------
function getSIP(device,callback) {
                var MongoClient,mongodb;
                mongodb = require('mongodb');
                MongoClient = mongodb.MongoClient;
                MongoClient.connect('mongodb://127.0.0.1/genieacs', function(err, db) {
                                var col;
                                var name = "serialNum";
                                var query = {};
                                query[name] = String(device);
                                if (err) {
                                                return ('Unable to connect . Error:', err);
                                } else {
                                                col = db.collection('voice');
                                                col.find(query, {
                                                                _id: 0,
                                                                serialNum: 0
                                                                }).toArray(function(err, results) {
                                                                if (err) {
                                                                                return err;
                                                                }else {
                                                                                db.close();
                                                                                if(results.length == 0){
                                                                                                callback(null,'no results');
                                                                                }
                                                                                else{
                                                                                                let SIPParams = {
                                                                                                username: results[0].voiceUser,
                                                                                                password: results[0].voicePass,
                                                                                                serial: device
                                                                                                };
                                                                                callback(null,SIPParams);
                                                                                } // end else
                                                                } // end else
                                                                }); // .toArray anon function
                                                } // end else
                }); // end MongoClient.connect
} // end getSIP function

exports.getSIP = getSIP; // make available to genieacs

----------------------------------------------

Here is the output from tail -f /var/loggenieacs-cwmp.log

2017-03-12T20:41:47.513Z [INFO] 10.203.115.1 FCB698-G%2D240G%2DP-CHORUS1621210409V01: Inform; cpeRequestId="1234" informEvent="2 PERIODIC" informRetryCount=0
2017-03-12T20:41:47.586Z [INFO] 10.203.115.1 FCB698-G%2D240G%2DP-CHORUS1621210409V01: Script: provision script is running: <device serial> username= <sip username> password: <sip password> device:  <serial again, checking to see argument passed>

Thanks,

David White


------------------------------------------------------------------------------------
The contents of this email and any attachments are confidential and may be privileged.  If you are not the intended recipient, you may not use, copy or disclose this email or its attachments.  Please notify the sender immediately by e-mail if you have received this e-mail in error and delete both emails from your system.  It is your responsibility to check this email and any attachments for viruses or other harmful code before opening or sending on.  Trustpower Limited and its subsidiaries (collectively, Trustpower) accepts no responsibility for any such virus or any effects of a virus on your systems or data.  Trustpower does not endorse anything in this email that is not related to its official business.

Please think of the environment before printing this email.

------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.genieacs.com/pipermail/users/attachments/20170312/ee5349e8/attachment-0001.html>


More information about the Users mailing list