<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:674499072;
        mso-list-type:hybrid;
        mso-list-template-ids:805066718 336134159 336134169 336134171 336134159 336134169 336134171 336134159 336134169 336134171;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-NZ" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hi all,<o:p></o:p></p>
<p class="MsoNormal">        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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>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.<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>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.
<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">3.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]> 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)<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">4.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>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. <o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">5.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>Change the group permissions on the log folder to include genieacs.<o:p></o:p></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span style="mso-list:Ignore">6.<span style="font:7.0pt "Times New Roman"">      
</span></span><![endif]>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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Ok here is the meat of it. Have omitted any specific details.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Provision in Genieacs GUI:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">let serial = declare("InternetGatewayDevice.DeviceInfo.SerialNumber", {value: 1});<o:p></o:p></p>
<p class="MsoNormal">let response = ext("a7", "getSIP", serial.value[0]);<o:p></o:p></p>
<p class="MsoNormal">log('provision script is running: ' + serial.value[0] + ' username= ' + response.username +' password: ' + response.password + ' device: '+ response.serial);<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here is the script genieacs/config/ext/a7.js<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">----------------------------------------------<o:p></o:p></p>
<p class="MsoNormal">function getSIP(device,callback) {<o:p></o:p></p>
<p class="MsoNormal">                var MongoClient,mongodb;<o:p></o:p></p>
<p class="MsoNormal">                mongodb = require('mongodb');<o:p></o:p></p>
<p class="MsoNormal">                MongoClient = mongodb.MongoClient;<o:p></o:p></p>
<p class="MsoNormal">                MongoClient.connect('mongodb://127.0.0.1/genieacs', function(err, db) {<o:p></o:p></p>
<p class="MsoNormal">                                var col;<o:p></o:p></p>
<p class="MsoNormal">                                var name = "serialNum";<o:p></o:p></p>
<p class="MsoNormal">                                var query = {};<o:p></o:p></p>
<p class="MsoNormal">                                query[name] = String(device);<o:p></o:p></p>
<p class="MsoNormal">                                if (err) {<o:p></o:p></p>
<p class="MsoNormal">                                                return ('Unable to connect . Error:', err);<o:p></o:p></p>
<p class="MsoNormal">                                } else {<o:p></o:p></p>
<p class="MsoNormal">                                                col = db.collection('voice');<o:p></o:p></p>
<p class="MsoNormal">                                                col.find(query, {<o:p></o:p></p>
<p class="MsoNormal">                                                                _id: 0,<o:p></o:p></p>
<p class="MsoNormal">                                                                serialNum: 0<o:p></o:p></p>
<p class="MsoNormal">                                                                }).toArray(function(err, results) {<o:p></o:p></p>
<p class="MsoNormal">                                                                if (err) {<o:p></o:p></p>
<p class="MsoNormal">                                                                                return err;<o:p></o:p></p>
<p class="MsoNormal">                                                                }else {<o:p></o:p></p>
<p class="MsoNormal">                                                                                db.close();<o:p></o:p></p>
<p class="MsoNormal">                                                                                if(results.length == 0){<o:p></o:p></p>
<p class="MsoNormal">                                                                                                callback(null,'no results');<o:p></o:p></p>
<p class="MsoNormal">                                                                                }<o:p></o:p></p>
<p class="MsoNormal">                                                                                else{<o:p></o:p></p>
<p class="MsoNormal">                                                                                                let SIPParams = {<o:p></o:p></p>
<p class="MsoNormal">                                                                                                username: results[0].voiceUser,<o:p></o:p></p>
<p class="MsoNormal">                                                                                                password: results[0].voicePass,<o:p></o:p></p>
<p class="MsoNormal">                                                                                                serial: device<o:p></o:p></p>
<p class="MsoNormal">                                                                                                };<o:p></o:p></p>
<p class="MsoNormal">                                                                                callback(null,SIPParams);<o:p></o:p></p>
<p class="MsoNormal">                                                                                } // end else<o:p></o:p></p>
<p class="MsoNormal">                                                                } // end else<o:p></o:p></p>
<p class="MsoNormal">                                                                }); // .toArray anon function<o:p></o:p></p>
<p class="MsoNormal">                                                } // end else<o:p></o:p></p>
<p class="MsoNormal">                }); // end MongoClient.connect<o:p></o:p></p>
<p class="MsoNormal">} // end getSIP function<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">exports.getSIP = getSIP; // make available to genieacs<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">----------------------------------------------<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here is the output from tail -f /var/loggenieacs-cwmp.log<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">2017-03-12T20:41:47.513Z [INFO] 10.203.115.1 FCB698-G%2D240G%2DP-CHORUS1621210409V01: Inform; cpeRequestId="1234" informEvent="2 PERIODIC" informRetryCount=0<o:p></o:p></p>
<p class="MsoNormal">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><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b><span style="font-size:13.0pt;color:#7F1399;mso-fareast-language:EN-NZ">David White</span></b><span style="font-size:10.0pt;color:#1F497D;mso-fareast-language:EN-NZ"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>

<FONT size=2 face=Arial>
<HR>
</FONT><SPAN style="COLOR: #1f497d; FONT-SIZE: 10pt"><FONT face=Arial><FONT 
size=1><FONT color=#808080>The contents of this email and any attachments are 
confidential and may be privileged.<SPAN style="mso-spacerun: yes">  
</SPAN>If you are not the intended recipient, you may not use, copy or disclose 
this email or its attachments.<SPAN style="mso-spacerun: yes">  
</SPAN>Please notify the sender immediately by e-mail if you have received this 
e-mail in error and delete both emails from your system.<SPAN 
style="mso-spacerun: yes">  </SPAN>It is your responsibility to check this 
email and any attachments for viruses or other harmful code before opening or 
sending on.<SPAN style="mso-spacerun: yes">  </SPAN>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.<SPAN 
style="mso-spacerun: yes">  </SPAN>Trustpower does not endorse anything in 
this email that is not related to its official 
business.<O:P></O:P></FONT></FONT></FONT></SPAN> 
<P style="MARGIN: 0cm 0cm 0pt 36pt" class=MsoNormal><SPAN 
style="COLOR: #1f497d; FONT-SIZE: 10pt"><O:P><FONT color=#808080 size=1 
face=Arial></FONT></O:P></SPAN></P><SPAN 
style="FONT-FAMILY: 'Calibri','sans-serif'; COLOR: #1f497d; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-NZ; mso-bidi-language: AR-SA"><FONT 
color=#004000 size=1 face=Arial>Please think of the environment before printing 
this email.</FONT></SPAN><FONT size=2 face=Arial> 
<HR>
</FONT>
</body>
</html>