Add documentation to wifi library

This commit is contained in:
mmcwilliams 2017-09-17 15:50:14 -04:00
parent e3393e6e7d
commit b60334d4b7
4 changed files with 164 additions and 4 deletions

View File

@ -8,7 +8,7 @@ Object representing a fake onoff Gpio class
### onoffsim.Gpio(no, dir, additional) ⇒ <code>object</code> ### onoffsim.Gpio(no, dir, additional) ⇒ <code>object</code>
Gpio() - Gpio() -
Create's a Gpio class in the case of running on a dev machine Returns a Gpio class in the case of running on a dev machine
**Kind**: static method of [<code>onoffsim</code>](#onoffsim) **Kind**: static method of [<code>onoffsim</code>](#onoffsim)
**Returns**: <code>object</code> - Fake Gpio object **Returns**: <code>object</code> - Fake Gpio object

View File

@ -12,7 +12,6 @@ const onoffsim = {
* @returns {object} Fake Gpio object * @returns {object} Fake Gpio object
*/ */
Gpio : function (no, dir = 'in', additional = 'none') { Gpio : function (no, dir = 'in', additional = 'none') {
//
return { return {
no : no, no : no,
dir : dir, dir : dir,

View File

@ -0,0 +1,109 @@
<a name="Wifi"></a>
## Wifi
Class representing the wifi features
**Kind**: global class
* [Wifi](#Wifi)
* [.list(callback)](#Wifi+list)
* [._readConfigCb(err, data)](#Wifi+_readConfigCb)
* [._writeConfigCb(err)](#Wifi+_writeConfigCb)
* [._reconfigureCb(err, stdout, stderr)](#Wifi+_reconfigureCb)
* [._refreshCb(err, stdout, stderr)](#Wifi+_refreshCb)
* [.setNetwork(ssid, pwd, callback)](#Wifi+setNetwork)
* [.getNetwork(callback)](#Wifi+getNetwork)
<a name="Wifi+list"></a>
### wifi.list(callback)
Wifi.list() -
List available wifi access points
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| callback | <code>function</code> | Function which gets invoked after list is returned |
<a name="Wifi+_readConfigCb"></a>
### wifi._readConfigCb(err, data)
Wifi._readConfigCb() -
(internal function) Invoked after config file is read,
then invokes file write on the config file
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| err | <code>object</code> | (optional) Error object only present if problem reading config file |
| data | <code>string</code> | Contents of the config file |
<a name="Wifi+_writeConfigCb"></a>
### wifi._writeConfigCb(err)
Wifi._writeConfigCb() -
(internal function) Invoked after config file is written,
then executes reconfiguration command
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| err | <code>object</code> | (optional) Error object only present if problem writing config file |
<a name="Wifi+_reconfigureCb"></a>
### wifi._reconfigureCb(err, stdout, stderr)
Wifi._reconfigureCb() -
(internal function) Invoked after reconfiguration command is complete
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| err | <code>object</code> | (optional) Error object only present if configuration command fails |
| stdout | <code>string</code> | Standard output from reconfiguration command |
| stderr | <code>string</code> | Error output from command if fails |
<a name="Wifi+_refreshCb"></a>
### wifi._refreshCb(err, stdout, stderr)
Wifi._refreshCb() -
(internal function) Invoked after wifi refresh command is complete
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| err | <code>object</code> | (optional) Error object only present if refresh command fails |
| stdout | <code>string</code> | Standard output from refresh command |
| stderr | <code>string</code> | Error output from command if fails |
<a name="Wifi+setNetwork"></a>
### wifi.setNetwork(ssid, pwd, callback)
Wifi.setNetwork() -
Function which initializes the processes for adding a wifi access point authentication
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| ssid | <code>string</code> | SSID of network to configure |
| pwd | <code>string</code> | Password of access point, plaintext |
| callback | <code>function</code> | Function invoked after process is complete, or fails |
<a name="Wifi+getNetwork"></a>
### wifi.getNetwork(callback)
Wifi.getNetwork() -
Executes command which gets the currently connected network
**Kind**: instance method of [<code>Wifi</code>](#Wifi)
| Param | Type | Description |
| --- | --- | --- |
| callback | <code>function</code> | Function which is invoked after command is completed |

View File

@ -12,12 +12,19 @@ const iwgetid = '/sbin/iwgetid'
const exec = require('child_process').exec const exec = require('child_process').exec
const fs = require('fs') const fs = require('fs')
class wifi { /** Class representing the wifi features */
class Wifi {
constructor () { constructor () {
this._callback = () => {} this._callback = () => {}
this._entry = null this._entry = null
this._ssid = null this._ssid = null
} }
/**
* Wifi.list() -
* List available wifi access points
*
* @param {function} callback Function which gets invoked after list is returned
*/
list (callback) { list (callback) {
exec(iwlist, (err, stdout, stderr) => { exec(iwlist, (err, stdout, stderr) => {
if (err) { if (err) {
@ -37,6 +44,14 @@ class wifi {
return callback(null, output) return callback(null, output)
}) })
} }
/**
* Wifi._readConfigCb() -
* (internal function) Invoked after config file is read,
* then invokes file write on the config file
*
* @param {object} err (optional) Error object only present if problem reading config file
* @param {string} data Contents of the config file
*/
_readConfigCb (err, data) { _readConfigCb (err, data) {
if (err) { if (err) {
console.error(err) console.error(err)
@ -50,6 +65,13 @@ class wifi {
this._entry = null this._entry = null
fs.writeFile(filePath, data, 'utf8', this._writeConfigCb) fs.writeFile(filePath, data, 'utf8', this._writeConfigCb)
} }
/**
* Wifi._writeConfigCb() -
* (internal function) Invoked after config file is written,
* then executes reconfiguration command
*
* @param {object} err (optional) Error object only present if problem writing config file
*/
_writeConfigCb (err) { _writeConfigCb (err) {
if (err) { if (err) {
console.error(err) console.error(err)
@ -57,6 +79,14 @@ class wifi {
} }
exec(reconfigure, this._reconfigureCb) exec(reconfigure, this._reconfigureCb)
} }
/**
* Wifi._reconfigureCb() -
* (internal function) Invoked after reconfiguration command is complete
*
* @param {object} err (optional) Error object only present if configuration command fails
* @param {string} stdout Standard output from reconfiguration command
* @param {string} stderr Error output from command if fails
*/
_reconfigureCb (err, stdout, stderr) { _reconfigureCb (err, stdout, stderr) {
if (err) { if (err) {
console.error(err) console.error(err)
@ -65,6 +95,14 @@ class wifi {
console.log('Wifi reconfigured') console.log('Wifi reconfigured')
exec(refresh, this._refreshCb) exec(refresh, this._refreshCb)
} }
/**
* Wifi._refreshCb() -
* (internal function) Invoked after wifi refresh command is complete
*
* @param {object} err (optional) Error object only present if refresh command fails
* @param {string} stdout Standard output from refresh command
* @param {string} stderr Error output from command if fails
*/
_refreshCb (err, stdout, stderr) { _refreshCb (err, stdout, stderr) {
if (err) { if (err) {
console.error(err) console.error(err)
@ -74,12 +112,26 @@ class wifi {
//this._callback(null, { ssid : ssid, pwd : pwd.length }) //this._callback(null, { ssid : ssid, pwd : pwd.length })
this._callback = () => {} this._callback = () => {}
} }
/**
* Wifi.setNetwork() -
* Function which initializes the processes for adding a wifi access point authentication
*
* @param {string} ssid SSID of network to configure
* @param {string} pwd Password of access point, plaintext
* @param {function} callback Function invoked after process is complete, or fails
*/
setNetwork (ssid, pwd, callback) { setNetwork (ssid, pwd, callback) {
this._entry = `network={\n\tssid="${ssid}"\n\tpsk="${pwd}"\n}\n` this._entry = `network={\n\tssid="${ssid}"\n\tpsk="${pwd}"\n}\n`
this._callback = callback this._callback = callback
this._ssid = ssid this._ssid = ssid
fs.readFile(filePath, 'utf8', this._readConfigCb) fs.readFile(filePath, 'utf8', this._readConfigCb)
} }
/**
* Wifi.getNetwork() -
* Executes command which gets the currently connected network
*
* @param {function} callback Function which is invoked after command is completed
*/
getNetwork (callback) { getNetwork (callback) {
exec(iwgetid, (err, stdout, stderr) => { exec(iwgetid, (err, stdout, stderr) => {
if (err) { if (err) {
@ -90,4 +142,4 @@ class wifi {
} }
} }
module.exports = new wifi() module.exports = new Wifi()