diff --git a/README.md b/README.md index 9792c75..27f63ba 100644 --- a/README.md +++ b/README.md @@ -103,13 +103,20 @@ flashair.command.getFileList("/", function (err, files) { }); ``` +Resources +--------- + +* [FlashAir Developper Home](https://flashair-developers.com/) +* [FlashAir Developpers Home](https://flashair-developers.com/en/support/forum/) +* [FlashAir StackOverflow channel](http://stackoverflow.com/questions/tagged/flashair) + License ------- The MIT License (MIT) Copyright (c) 2013 Takenori Nakagawa -Copyright (c) 2016 Francois-Xavier Kowalski +Copyright (c) 2016-2017 Francois-Xavier Kowalski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 0000000..1ba10f3 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,31 @@ +#!/usr/bin/env node + +var FlashAir = require('../lib') +var argv = require('minimist')(process.argv.slice(2)) +var flashair = new FlashAir('flashair', 'IPT') +try { + if (argv._[0] === 'config') { + var homeNet = { + ssid: argv['home-ssid'], + key: argv['home-key'] + } + var flashairNet = { + ssid: argv['card-ssid'], + key: argv['card-key'] + } + flashair.ini.runInPassThroughMode(homeNet, flashairNet).save() + } else if (argv._[0] === 'ls') { + flashair.command.getFileList(argv._[1] || '/', function (err, data) { + if (err) { + console.console.error(err) + } else { + console.log('data:', data) + } + }) + } else { + throw new Error('unknown cli command "' + argv._[0] + '"') + } +} catch (e) { + console.error('***', e.toString()) + process.exit(1) +} diff --git a/lib.js b/lib.js index c54ccd0..e7a94bd 100644 --- a/lib.js +++ b/lib.js @@ -9,12 +9,12 @@ // default var APPNAME = 'flashair' -var APPMODE = 'STA' // or AP +var APPMODE = 'STA' // or AP or IPT var Libs = require('./libs') var hostname = require('os').hostname() -module.exports = function (appname, appmode) { +module.exports = function (appname, appmode, configPath) { // API End-point var endpoint = appname || APPNAME APPMODE = appmode || APPMODE @@ -24,7 +24,7 @@ module.exports = function (appname, appmode) { endpoint += '.local' } - return new Libs(endpoint) + return new Libs(endpoint, configPath) } -module.exports.sd = require('./libs/sd') +console.log(12) diff --git a/libs/index.js b/libs/index.js index 65a0739..f54c436 100644 --- a/libs/index.js +++ b/libs/index.js @@ -9,12 +9,14 @@ var Command = require('./command') var Config = require('./config') +var Ini = require('./ini') var Thumbnail = require('./thumbnail') exports = module.exports = Libs -function Libs (endpoint) { +function Libs (endpoint, configPath) { this.command = new Command(endpoint) this.config = new Config(endpoint) + this.ini = new Ini(configPath) this.thumbnail = new Thumbnail(endpoint) }; diff --git a/libs/ini.js b/libs/ini.js index 1ab29d3..6852235 100644 --- a/libs/ini.js +++ b/libs/ini.js @@ -3,31 +3,13 @@ */ var fs = require('fs') +var path = require('path') var ini = require('ini') -var debug = require('debug')('flashair:config') -var _ = require('underscore') +var debug = require('debug')('flashair:ini') -var _hwConfigFile = '/Volumes/NO NAME/SD_WLAN/CONFIG' -var _hwConfig // hardware => private singleton +var volumePath = process.platform === 'darwin' ? '/Volumes/NO NAME' : undefined -function load () { - _hwConfig = ini.parse(fs.readFileSync(_hwConfigFile, 'utf-8')) - _hwConfig.WLANSD = _hwConfig.WLANSD || {} - debug('loaded hwConfig:', _hwConfig) -} - -function save () { - debug('saving hwConfig:', _hwConfig) - var iniConfig = ini.encode(_hwConfig, { - whitespace: false, - newline: true, - platform: 'win32' - }) - debug('as:', iniConfig) - fs.writeFileSync(_hwConfigFile, iniConfig, 'utf-8') -} - -load() +var defaultConfigPath = 'SD_WLAN/CONFIG' // https://flashair-developers.com/en/documents/api/config/#APPMODE @@ -37,125 +19,122 @@ var appModes = { 'IPT': 6 } -module.exports = { - - save: function _save () { - save() - return this - }, - - getHw: function _getHw () { - return _.clone(_hwConfig) - }, - - setHw: function _setHw (hwConfig) { - _hwConfig.Vendor = _.extend(_hwConfig.Vendor, hwConfig.Vendor) - _hwConfig.WLANSD = _.extend(_hwConfig.WLANSD, hwConfig.WLANSD) - return this - }, - - runAsAccessPoint: function _runAsAccessPoint (flashairNet) { - debug('runAsAccessPoint: flashairNet:', flashairNet) - _checkNetwork(flashairNet) - var hwConfig = { - Vendor: { - APPMODE: appModes.AP, - APPSSID: flashairNet.ssid, - APPNETWORKKEY: flashairNet.key - } +module.exports = IniConfig + +function IniConfig (configPath) { + configPath = configPath || path.join(volumePath, defaultConfigPath) + + this.load = function () { + debug('loading hwConfig from "%s"', configPath) + this._hwConfig = ini.parse(fs.readFileSync(configPath, 'utf-8')) + debug('loaded hwConfig: %O', this._hwConfig) + } + + this.load() + + this.save = function () { + var iniConfig = ini.encode(this._hwConfig, { + whitespace: false, + newline: true, + platform: 'win32' + }) + debug('saving hwConfig %O as "%s"', iniConfig, configPath) + fs.writeFileSync(configPath, iniConfig, 'utf-8') + } +} + +IniConfig.prototype.runAsAccessPoint = function _runAsAccessPoint (flashairNet) { + debug('runAsAccessPoint: flashairNet: %s', flashairNet) + _checkNetwork(flashairNet) + var hwConfig = { + Vendor: { + APPMODE: appModes.AP, + APPSSID: flashairNet.ssid, + APPNETWORKKEY: flashairNet.key } - debug('runAsAccessPoint: Setting hw:', hwConfig) - this.setHw(hwConfig) - return this - }, - - runInPassThroughMode: function _runInPassThroughMode (homeNet, flashairNet) { - debug('runInPassThroughMode: homeNet:', homeNet, 'flashairNet:', flashairNet) - _checkNetwork(flashairNet) - _checkNetwork(homeNet) - var hwConfig = { - Vendor: { - APPMODE: appModes.IPT, - APPSSID: flashairNet.ssid, - APPNETWORKKEY: flashairNet.key, - BRGSSID: homeNet.ssid, - BRGNETWORKKEY: homeNet.key - }, - WLANSD: { - ID: flashairNet.ssid, - DHCP_Enabled: 'YES' - } + } + debug('runAsAccessPoint: Setting hw: %O', hwConfig) + this._hwConfig.Vendor = hwConfig.Vendor + return this +} + +IniConfig.prototype.runInPassThroughMode = function _runInPassThroughMode (homeNet, flashairNet) { + debug('runInPassThroughMode: homeNet: %O, flashairNet: %O', homeNet, flashairNet) + _checkNetwork(flashairNet) + _checkNetwork(homeNet) + var hwConfig = { + Vendor: { + APPMODE: appModes.IPT, + APPSSID: flashairNet.ssid, + APPNETWORKKEY: flashairNet.key, + BRGSSID: homeNet.ssid, + BRGNETWORKKEY: homeNet.key + }, + WLANSD: { + ID: flashairNet.ssid, + DHCP_Enabled: 'YES' } - debug('runInPassThroughMode: Setting hw:', hwConfig) - this.setHw(hwConfig) - return this - }, - - get: function _get () { - var config = { - appMode: this.getAppMode(), - wlanProtocol: this.getWlanProtocol() + } + debug('runInPassThroughMode: Setting hw: %O', hwConfig) + this._hwConfig.Vendor = hwConfig.Vendor + this._hwConfig.WLANSD = hwConfig.WLANSD + return this +} + +IniConfig.prototypesetAppMode = function _setAppMode (accessMode) { + if (accessMode.accessPoint && accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.IPT + } else if (accessMode.accessPoint && !accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.AP + } else if (!accessMode.accessPoint && accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.STA + } else { + throw new Error('Invalid accessMode' + JSON.stringify(accessMode)) + } +} + +IniConfig.prototype.getAppMode = function _getAppMode () { + if (this._hwConfig.Vendor.APPMODE === appModes.IPT) { + return { + accessPoint: true, + station: { + ssid: this._hwConfig.Vendor.BRGSSID || '', + key: this._hwConfig.Vendor.BRGNETWORKKEY || '' + } } - debug('config:', config) - return config - }, - - setAppMode: function _setAppMode (accessMode) { - if (accessMode.accessPoint && accessMode.station) { - _hwConfig.Vendor.APPMODE = appModes.IPT - } else if (accessMode.accessPoint && !accessMode.station) { - _hwConfig.Vendor.APPMODE = appModes.AP - } else if (!accessMode.accessPoint && accessMode.station) { - _hwConfig.Vendor.APPMODE = appModes.STA - } else { - throw new Error('Invalid accessMode' + JSON.stringify(accessMode)) + } else if (this._hwConfig.Vendor.APPMODE === appModes.AP) { + return { + accessPoint: true, + station: false } - }, - - getAppMode: function _getAppMode () { - if (_hwConfig.Vendor.APPMODE === appModes.IPT) { - return { - accessPoint: true, - station: { - ssid: _hwConfig.Vendor.BRGSSID || '', - key: _hwConfig.Vendor.BRGNETWORKKEY || '' - } - } - } else if (_hwConfig.Vendor.APPMODE === appModes.AP) { - return { - accessPoint: true, - station: false - } - } else if (_hwConfig.Vendor.APPMODE === appModes.STA) { - return { - accessPoint: false, - station: true - } + } else if (this._hwConfig.Vendor.APPMODE === appModes.STA) { + return { + accessPoint: false, + station: true } - }, + } +} - // https://flashair-developers.com/en/documents/api/config/#WLANAPMODE +// https://flashair-developers.com/en/documents/api/config/#WLANAPMODE - setWlanProtocol: function _setWlanStd (wlanProtocol) { - var WLANAPMODES = { - '802.11b': '0x01', - '802.11g': '0x02', - '802.11bg': '0x03', - '802.11ng': '0x82' - } - _hwConfig.Vendor.WLANAPMODE = WLANAPMODES[wlanProtocol] || _hwConfig.Vendor.WLANAPMODE - }, - - getWlanProtocol: function _getWlanProtocol () { - var WLANAPMODES = { - '0x01': '802.11b', - '0x02': '802.11g', - '0x03': '802.11bg', - '0x82': '802.11ng' - } - return WLANAPMODES[_hwConfig.Vendor.WLANAPMODE || '0x03'] +IniConfig.prototype.setWlanProtocol = function _setWlanStd (wlanProtocol) { + var WLANAPMODES = { + '802.11b': '0x01', + '802.11g': '0x02', + '802.11bg': '0x03', + '802.11ng': '0x82' } + this._hwConfig.Vendor.WLANAPMODE = WLANAPMODES[wlanProtocol] || this._hwConfig.Vendor.WLANAPMODE +} +IniConfig.prototype.getWlanProtocol = function _getWlanProtocol () { + var WLANAPMODES = { + '0x01': '802.11b', + '0x02': '802.11g', + '0x03': '802.11bg', + '0x82': '802.11ng' + } + return WLANAPMODES[this._hwConfig.Vendor.WLANAPMODE || '0x03'] } function _checkNetwork (net) { diff --git a/libs/tools.js b/libs/tools.js index 44d3ddb..30aecde 100644 --- a/libs/tools.js +++ b/libs/tools.js @@ -16,5 +16,32 @@ module.exports = { data = JSON.stringify(data) } require('fs').writeFileSync('data.log', data) + }, + + // https://github.com/jashkenas/underscore/issues/162 + // http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object/728694#728694 + clone: function (obj) { + if (obj === null || typeof obj !== 'object') { + return obj + } + var copy = obj.constructor() + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) { + copy[attr] = obj[attr] + } + } + return copy + }, + + // http://stackoverflow.com/questions/14843815/recursive-deep-extend-assign-in-underscore-js + extend: function (target, source) { + for (var prop in source) { + if (prop in target) { + this.extend(target[prop], source[prop]) + } else { + target[prop] = source[prop] + } + } + return target } } diff --git a/package.json b/package.json index d246258..e632e9e 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,9 @@ "standard-fix": "standard --fix" }, "dependencies": { - "debug": "^2.2.0", + "debug": "^2.6.3", "ini": "^1.3.4", + "minimist": "^1.2.0", "underscore": "^1.8.3" }, "repository": { diff --git a/test/command.js b/test/command.js index 2cb8d5e..9b15add 100644 --- a/test/command.js +++ b/test/command.js @@ -7,7 +7,7 @@ var expect = require('chai').expect var FlashAir = require('../lib') -var flashair = new FlashAir('flashair', 'STA') +var flashair = new FlashAir('flashair', 'IPT') describe('flashair.command', function () { describe('.getFileList', function () {