mirror of https://github.com/iotcat/wIoT
parent
735d91f723
commit
6a54429165
16 changed files with 507 additions and 441 deletions
@ -1,22 +1,30 @@ |
||||
#!/usr/bin/env node |
||||
var yargs = require('yargs'); |
||||
|
||||
//set language to english |
||||
yargs.locale('en'); |
||||
|
||||
/* CLI code path */ |
||||
const src = __dirname + '/../src/cli/'; |
||||
|
||||
/* help */ |
||||
yargs = require(src + 'help.js')(yargs); |
||||
|
||||
/* version */ |
||||
yargs = require(src + 'version.js')(yargs); |
||||
/* init */ |
||||
require(src + 'init.js')(yargs); |
||||
|
||||
/* config */ |
||||
//yargs = require(src + 'config.js')(yargs); |
||||
require(src + 'config.js')(yargs); |
||||
|
||||
/* node */ |
||||
require(src + 'node.js')(yargs); |
||||
|
||||
/* push */ |
||||
yargs = require(src + 'push.js')(yargs); |
||||
/* wifi */ |
||||
require(src + 'wifi.js')(yargs); |
||||
|
||||
/* device */ |
||||
yargs = require(src + 'device.js')(yargs); |
||||
|
||||
argv = yargs.epilogue("-----------------------").argv; |
||||
|
||||
/* version */ |
||||
require(src + 'version.js')(yargs); |
||||
|
||||
/* help */ |
||||
require(src + 'help.js')(yargs); |
||||
|
||||
yargs.epilogue("-----------------------").argv; |
||||
|
@ -1,89 +1,50 @@ |
||||
module.exports = (yargs) => { |
||||
|
||||
|
||||
|
||||
var o = { |
||||
email: { |
||||
set: s => { |
||||
if(!o.email.check(s)){ |
||||
return 'Illegal Email!!'; |
||||
} |
||||
data.config.email(s); |
||||
return; |
||||
}, |
||||
get: () => data.config.email(), |
||||
check: s => { |
||||
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; |
||||
return reg.test(s); |
||||
} |
||||
}, |
||||
remote: { |
||||
set: s => { |
||||
if(!o.remote.check(s)){ |
||||
return 'Illegal URL!!'; |
||||
} |
||||
data.config.remote(s); |
||||
return; |
||||
}, |
||||
get: () => data.config.remote(), |
||||
check: s => { |
||||
var reg = /./;///^((https|http|ftp|rtsp|mms):\/\/)(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)$/;
|
||||
return reg.test(s); |
||||
} |
||||
}, |
||||
show: () => { |
||||
let configInfo = new table(); |
||||
configInfo.push({email: o.email.get()},{remote: o.remote.get()}); |
||||
console.log(configInfo.toString()); |
||||
} |
||||
}; |
||||
|
||||
var data = require(__dirname + '/../utilities/data.js')(); |
||||
const colors = require('colors'); |
||||
const boxen = require('boxen'); |
||||
const table = require('cli-table'); |
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
|
||||
yargs = yargs |
||||
.command("config", "mksec config".green + " Config Email and Server URL..", yargs => { |
||||
return yargs |
||||
.option("e", { |
||||
alias: "email", |
||||
default: "", |
||||
describe: "Your Email to login.", |
||||
demand: false, |
||||
type: 'string' |
||||
}) |
||||
.option("r", { |
||||
alias: "remote", |
||||
default: "", |
||||
describe: "Remote mksec server URL.", |
||||
demand: false, |
||||
type: 'string' |
||||
}) |
||||
.help() |
||||
.version(false) |
||||
.argv; |
||||
.command("config", "display or update project configuration", yargs => { |
||||
return yargs |
||||
.option('name', { |
||||
alias: 'n', |
||||
type: 'string', |
||||
describe: 'project name' |
||||
}) |
||||
.option('director', { |
||||
alias: 'd', |
||||
type: 'string', |
||||
describe: 'director request URL' |
||||
}) |
||||
.option('entrance', { |
||||
alias: 'e', |
||||
type: 'string', |
||||
describe: 'default entrance of your project' |
||||
}) |
||||
.example([ |
||||
['$0 config', 'display current project configuration'], |
||||
['$0 config --name newproject', 'update project name to "newproject"'], |
||||
['$0 config --director http://xxx/', 'change director access URL'], |
||||
['$0 config --entrance main.js', 'change entrance file to main.js'] |
||||
]) |
||||
}, argv => { |
||||
if(argv.e == "" && argv.r == ""){ |
||||
o.show(); |
||||
console.error(boxen('Please use '+'mksec config -h '.red+'to get Help!!', {padding: 1, margin: 1, borderStyle: 'double'})); |
||||
return; |
||||
} |
||||
if(argv.e != ""){ |
||||
var m = o.email.set(argv.e); |
||||
if(m){ |
||||
console.error(boxen(m, {padding: 1, margin: 1, borderStyle: 'double'})); |
||||
return; |
||||
} |
||||
} |
||||
if(argv.r != ""){ |
||||
var m = o.remote.set(argv.r); |
||||
if(m){ |
||||
console.error(boxen(m, {padding: 1, margin: 1, borderStyle: 'double'})); |
||||
return; |
||||
} |
||||
} |
||||
o.show(); |
||||
|
||||
let config = require(__dirname + '/modules/getConfig.js')(); |
||||
if(!config) return; |
||||
|
||||
let raw_config = JSON.parse(fs.readFileSync(config.root + 'config.json', 'utf-8')); |
||||
|
||||
raw_config.name = argv.name || raw_config.name; |
||||
raw_config.director = argv.director || raw_config.director; |
||||
raw_config.entrance = argv.entrance || raw_config.entrance; |
||||
|
||||
|
||||
fs.writeFileSync(config.root + 'config.json', JSON.stringify(raw_config, null, 2), 'utf-8'); |
||||
|
||||
console.log(raw_config); |
||||
}) |
||||
|
||||
return yargs; |
||||
|
@ -1,66 +0,0 @@ |
||||
module.exports = (yargs) => { |
||||
var o = { |
||||
flash: async (argv) => new Promise(async resolve => { |
||||
await winFlash(argv._[1], __dirname+'/../drivers/nodemcu/bin/full.bin'); |
||||
resolve() |
||||
}), |
||||
upload: async (argv) => new Promise(async resolve => { |
||||
ban = new ora(`Preparing NodeMCU...0%`).start(); |
||||
await reset(argv._[1]); |
||||
ban.info('Preparing NodeMCU...10%'); |
||||
await upload(argv._[1], __dirname+'/../drivers/nodemcu/lua/init.lua'); |
||||
ban.info('Preparing NodeMCU...30%'); |
||||
await upload(argv._[1], __dirname+'/../drivers/nodemcu/lua/lfs.img'); |
||||
ban.info('Preparing NodeMCU...60%'); |
||||
await upload(argv._[1], __dirname+'/../drivers/nodemcu/lua/config.json'); |
||||
ban.info('Preparing NodeMCU...70%'); |
||||
await reset(argv._[1]); |
||||
ban.info('Preparing NodeMCU...100%'); |
||||
ban.succeed('NodeMCU on '+argv._[1]+' is ready!! You can pull it out now~'); |
||||
resolve() |
||||
}), |
||||
terminal: async (argv) => new Promise(async resolve => { |
||||
terminal(argv._[1]) |
||||
resolve(); |
||||
}) |
||||
} |
||||
|
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
const winDevList = require(__dirname + '/modules/winDevList.js'); |
||||
const winFlash = require(__dirname + '/modules/winFlash.js'); |
||||
const upload = require(__dirname + '/modules/upload.js'); |
||||
const terminal = require(__dirname + '/modules/terminal.js'); |
||||
const reset = require(__dirname + '/modules/reset.js'); |
||||
|
||||
yargs = yargs |
||||
.command('flash', "wiot flash <PortsName>".green + " Flash a NodeMCU board..", yargs => yargs, async argv => { |
||||
await o.flash(argv); |
||||
}) |
||||
.command('upload', "wiot upload <PortsName>".green + " Upload a NodeMCU board..", yargs => yargs, async argv => { |
||||
await o.upload(argv); |
||||
}) |
||||
.command('terminal', "wiot terminal <PortsName>".green + " Open a NodeMCU terminal..", yargs => yargs, async argv => { |
||||
await o.terminal(argv); |
||||
}) |
||||
.command('init', "wiot init <PortsName>".green + " Init a NodeMCU board..", yargs => yargs, async argv => { |
||||
ban = new ora(`Checking port ${argv._[1]}...`).start(); |
||||
let deviceList = await winDevList(); |
||||
if(deviceList.indexOf(argv._[1]) == -1){ |
||||
ban.fail('No devices on '+argv._[1]+'!!'); |
||||
return; |
||||
} |
||||
ban.succeed('Found '+argv._[1]+'!!'); |
||||
await o.flash(argv); |
||||
await o.upload(argv); |
||||
}) |
||||
.command('lsp', "wiot lsp".green + " List all available devices.", yargs => yargs, async argv => { |
||||
ban = new ora(`Collecting information...`).start(); |
||||
let deviceList = await winDevList(); |
||||
ban.succeed('Found ' + deviceList.length + ' devices!!'); |
||||
console.log(deviceList); |
||||
}) |
||||
|
||||
|
||||
return yargs; |
||||
} |
@ -1,78 +1,57 @@ |
||||
module.exports = (yargs) => { |
||||
var o = { |
||||
init: async (argv) => new Promise(async resolve => { |
||||
ban = new ora(`Creating new wIoT project...`).start(); |
||||
let path = './'; |
||||
if(argv._.length > 2){ |
||||
path = argv._[2]; |
||||
if(path.substring(-1) != '/' && path.substring(-1) != '\\'){ |
||||
path += '/'; |
||||
} |
||||
} |
||||
fs.mkdir(path + argv._[1], function(err){ |
||||
if (err) { |
||||
ban.fail('Creating failure!!'); |
||||
console.error(err); |
||||
resolve(); |
||||
} |
||||
o.copyFolder(__dirname + '/../../dist', path + argv._[1], function(err){ |
||||
if (err) { |
||||
ban.fail('Creating failure!!'); |
||||
console.error(err); |
||||
resolve(); |
||||
} |
||||
ban.success('Initiate successfully!!'); |
||||
resolve() |
||||
}) |
||||
}); |
||||
}), |
||||
copyFolder: function(srcDir, tarDir, cb) { |
||||
fs.readdir(srcDir, function(err, files) { |
||||
var count = 0 |
||||
var checkEnd = function() { |
||||
++count == files.length && cb && cb() |
||||
} |
||||
|
||||
if (err) { |
||||
checkEnd() |
||||
return |
||||
} |
||||
|
||||
files.forEach(function(file) { |
||||
var srcPath = path.join(srcDir, file) |
||||
var tarPath = path.join(tarDir, file) |
||||
|
||||
fs.stat(srcPath, function(err, stats) { |
||||
if (stats.isDirectory()) { |
||||
console.log('mkdir', tarPath) |
||||
fs.mkdir(tarPath, function(err) { |
||||
if (err) { |
||||
console.log(err) |
||||
return |
||||
} |
||||
|
||||
copyFolder(srcPath, tarPath, checkEnd) |
||||
}) |
||||
} else { |
||||
copyFile(srcPath, tarPath, checkEnd) |
||||
} |
||||
}) |
||||
}) |
||||
files.length === 0 && cb && cb() |
||||
}) |
||||
} |
||||
} |
||||
|
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
const path = require('path'); |
||||
|
||||
|
||||
yargs = yargs |
||||
.command('ini', "wiot ini <ProjectName> [path]".green + " Create and initiate a new wIoT Project folder", yargs => yargs, async argv => { |
||||
await o.init(argv); |
||||
yargs |
||||
.command('init <ProjectName> [path]', "Create and initiate a new wIoT Project folder", yargs => { |
||||
return yargs |
||||
.option('director', { |
||||
alias: 'd', |
||||
default: 'http://127.0.0.1:3000/', |
||||
type: 'string', |
||||
describe: 'director URL' |
||||
}) |
||||
.option('entrance', { |
||||
alias: 'e', |
||||
default: 'index.js', |
||||
type: 'string', |
||||
describe: 'default entrance' |
||||
}) |
||||
.example([ |
||||
['$0 init myproject', 'Create a wiot project named "myproject" at current folder'] |
||||
]) |
||||
}, async argv => { |
||||
let ban = new ora(`Creating new wIoT project...`).start(); |
||||
let path = './'; |
||||
|
||||
if(argv.hasOwnProperty('path')){ |
||||
path = argv.path; |
||||
if(path.substring(-1) != '/' && path.substring(-1) != '\\'){ |
||||
path += '/'; |
||||
} |
||||
} |
||||
let dir = path + argv.ProjectName; |
||||
if(fs.existsSync(dir)){ |
||||
ban.fail('Folder already exists!!'); |
||||
return; |
||||
} |
||||
fs.mkdir(dir, function(err){ |
||||
if (err) { |
||||
ban.fail('Creating failure!!'); |
||||
throw err; |
||||
} |
||||
fs.mkdirSync(dir+'/.wiot'); |
||||
fs.writeFileSync(dir+'/.wiot/__hash', require('crypto').createHash('sha256').update(Math.random().toString()).digest('hex')); |
||||
fs.writeFileSync(dir+'/config.json', JSON.stringify({ |
||||
name: argv.ProjectName, |
||||
entrance: argv.entrance, |
||||
director: argv.director, |
||||
nodes: {}, |
||||
wifi: [] |
||||
}, null, 2)); |
||||
ban.succeed('Initiate successfully!!'); |
||||
}); |
||||
}) |
||||
|
||||
|
||||
return yargs; |
||||
} |
||||
|
@ -0,0 +1,7 @@ |
||||
module.exports = (s) => { |
||||
|
||||
const colors = require('colors'); |
||||
const boxen = require('boxen'); |
||||
|
||||
console.error(boxen(s, {padding: 1, margin: 1, borderStyle: 'double'})); |
||||
} |
@ -0,0 +1,30 @@ |
||||
module.exports = () => { |
||||
|
||||
const colors = require('colors'); |
||||
const boxen = require('boxen'); |
||||
const table = require('cli-table'); |
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
|
||||
|
||||
let path = './'; |
||||
|
||||
|
||||
for(let i = 0; i < 30; i ++){ |
||||
if(fs.readdirSync(path).indexOf('.wiot') !== -1 && fs.existsSync(path + 'config.json') && fs.existsSync(path + '.wiot/__hash')){ |
||||
if(fs.readdirSync(path + '.wiot').indexOf('cache') == -1){ |
||||
fs.mkdirSync(path + '.wiot/cache'); |
||||
fs.writeFileSync(path + '.wiot/cache/.gitignore', '*', 'utf-8'); |
||||
} |
||||
|
||||
let config = JSON.parse(fs.readFileSync(path + 'config.json')); |
||||
config.root = path; |
||||
config.hash = fs.readFileSync(path + '.wiot/__hash', 'utf-8'); |
||||
return config; |
||||
} |
||||
path = '../' + path; |
||||
} |
||||
|
||||
require(__dirname + '/error.js')('No wIoT project selected!\nAre you in a wIoT project folder?'); |
||||
return null; |
||||
} |
@ -0,0 +1,245 @@ |
||||
module.exports = (yargs) => { |
||||
var o = { |
||||
check: async (argv) => new Promise(async resolve => { |
||||
ban = new ora(`Checking port ${argv.port}...`).start(); |
||||
let deviceList = await winDevList(); |
||||
if(deviceList.indexOf(argv.port) == -1){ |
||||
ban.fail('No NodeMCU device on '+argv.port+'!!'); |
||||
return; |
||||
} |
||||
ban.succeed('Found NodeMCU device on '+argv.port+'!!'); |
||||
resolve(); |
||||
}), |
||||
flash: async (argv) => new Promise(async resolve => { |
||||
await winFlash(argv.port, __dirname+'/../drivers/nodemcu/bin/full.bin'); |
||||
resolve() |
||||
}), |
||||
upload: async (argv) => new Promise(async resolve => { |
||||
let config = require(__dirname + '/modules/getConfig.js')(); |
||||
if(!config) return; |
||||
|
||||
if(!config.wifi[argv.wifiIndex]){ |
||||
error('Invalid WiFi index '+argv.wifiIndex+'!\nSee "'+argv.$0+' wifi ls" to find a wifi index.'); |
||||
return; |
||||
} |
||||
|
||||
ban = new ora(`Preparing ${argv.NodeName}...0%`).start(); |
||||
await reset(argv.port); |
||||
ban.info('Preparing '+argv.NodeName+'...10%'); |
||||
if(argv.config){ |
||||
|
||||
Object.keys(config.nodes).forEach(nid => { |
||||
if(config.nodes[nid].nickname == argv.nickname){ |
||||
error('Nickname "'+argv.nickname+'"" has already been used by '+nid); |
||||
throw null; |
||||
} |
||||
}) |
||||
|
||||
let nid = o.nidGen(); |
||||
config.nodes[nid] = { |
||||
nickname: argv.nickname, |
||||
msgport: argv.msgport, |
||||
wifiIndex: argv.wifiIndex |
||||
} |
||||
let raw_config = JSON.parse(fs.readFileSync(config.root + 'config.json', 'utf-8')); |
||||
raw_config.nodes = config.nodes; |
||||
fs.writeFileSync(config.root + 'config.json', JSON.stringify(raw_config, null, 2), 'utf-8'); |
||||
|
||||
let config_path = config.root + '.wiot/cache/config.json'; |
||||
let config_obj = { |
||||
nid: nid, |
||||
wifi: config.wifi[config.nodes[nid].wifiIndex], |
||||
msg: { |
||||
port: config.nodes[nid].msgport |
||||
}, |
||||
director: { |
||||
ip: '192.168.3.100', |
||||
port: 6789, |
||||
HeartbeatInterval: argv.heartbeat |
||||
} |
||||
} |
||||
fs.writeFileSync(config_path, JSON.stringify(config_obj), 'utf-8'); |
||||
await upload(argv.port, config_path); |
||||
} |
||||
ban.info('Preparing '+argv.NodeName+'...30%'); |
||||
if(argv.img) await upload(argv.port, __dirname+'/../drivers/nodemcu/lua/lfs.img'); |
||||
ban.info('Preparing '+argv.NodeName+'...60%'); |
||||
if(argv.lua) await upload(argv.port, __dirname+'/../drivers/nodemcu/lua/init.lua'); |
||||
ban.info('Preparing '+argv.NodeName+'...70%'); |
||||
await reset(argv.port); |
||||
ban.info('Preparing '+argv.NodeName+'...100%'); |
||||
ban.succeed(argv.NodeName+' on '+argv.port+' is ready for online!!'); |
||||
resolve() |
||||
}), |
||||
terminal: async (argv) => new Promise(async resolve => { |
||||
terminal(argv.port) |
||||
resolve(); |
||||
}), |
||||
nidGen: () => require('crypto').createHash('sha256').update(Math.random().toString()).digest('hex').substring(0, 5) |
||||
} |
||||
|
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
const winDevList = require(__dirname + '/modules/winDevList.js'); |
||||
const winFlash = require(__dirname + '/modules/winFlash.js'); |
||||
const upload = require(__dirname + '/modules/upload.js'); |
||||
const terminal = require(__dirname + '/modules/terminal.js'); |
||||
const reset = require(__dirname + '/modules/reset.js'); |
||||
const error = require(__dirname + '/modules/error.js'); |
||||
|
||||
yargs = yargs |
||||
|
||||
|
||||
.command('node', "operate NodeMCU devices connected on this computer. Use \""+yargs.$0+" node -h\" for more information. ", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 node search', 'search for all NodeMCUs connected to this computer'] |
||||
]) |
||||
|
||||
|
||||
.command('search', "List all available ports", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 node search', 'search for all NodeMCUs connected to this computer'] |
||||
]) |
||||
}, async argv => { |
||||
ban = new ora(`Collecting port information...`).start(); |
||||
let deviceList = await winDevList(); |
||||
ban.succeed('Found ' + deviceList.length + ' NodeMCU devices!!'); |
||||
console.log(deviceList); |
||||
}) |
||||
|
||||
.command('init <port>', "flash and prepare a NodeMCU device..", yargs => { |
||||
return yargs |
||||
.option('nickname', { |
||||
alias: 'n', |
||||
demandOption: true, |
||||
type: 'string', |
||||
describe: 'nickname for this node' |
||||
}) |
||||
.option('wifiIndex', { |
||||
alias: 'w', |
||||
default: 0, |
||||
type: 'number', |
||||
describe: 'determine which wifi config to use' |
||||
}) |
||||
.option('msgport', { |
||||
alias: 'm', |
||||
default: 6789, |
||||
type: 'number', |
||||
describe: 'udp port on NodeMCU for MSG communication' |
||||
}) |
||||
.option('heartbeat', { |
||||
default: 10000, |
||||
type: 'number', |
||||
describe: 'heartbeat interval' |
||||
}) |
||||
.option('lua', { |
||||
alias: 'l', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update init.lua' |
||||
}) |
||||
.option('img', { |
||||
alias: 'i', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update lfs.img' |
||||
}) |
||||
.option('config', { |
||||
alias: 'c', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update config.json' |
||||
}) |
||||
.example([ |
||||
['$0 node init COM3 -n firstnode', 'flash and prepare the NodeMCU device on COM3 and name it as "firstnode"'] |
||||
]) |
||||
}, async argv => { |
||||
await o.check(argv); |
||||
await o.flash(argv); |
||||
await o.upload(argv); |
||||
}) |
||||
|
||||
|
||||
.command('flash <port>', "flash a NodeMCU device.", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 node flash COM3', 'flash the NodeMCU device on COM3 port'] |
||||
]) |
||||
}, async argv => { |
||||
await o.check(argv); |
||||
await o.flash(argv); |
||||
}) |
||||
|
||||
.command('prepare <port>', "prepare wIoT system environment on the NodeMCU", yargs => { |
||||
return yargs |
||||
.option('nickname', { |
||||
alias: 'n', |
||||
demandOption: true, |
||||
type: 'string', |
||||
describe: 'nickname for this node' |
||||
}) |
||||
|
||||
.option('wifiIndex', { |
||||
alias: 'w', |
||||
default: 0, |
||||
type: 'number', |
||||
describe: 'determine which wifi config to use' |
||||
}) |
||||
.option('msgport', { |
||||
alias: 'm', |
||||
default: 6789, |
||||
type: 'number', |
||||
describe: 'udp port on NodeMCU for MSG communication' |
||||
}) |
||||
.option('heartbeat', { |
||||
default: 10000, |
||||
type: 'number', |
||||
describe: 'heartbeat interval' |
||||
}) |
||||
.option('lua', { |
||||
alias: 'l', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update init.lua' |
||||
}) |
||||
.option('img', { |
||||
alias: 'i', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update lfs.img' |
||||
}) |
||||
.option('config', { |
||||
alias: 'c', |
||||
default: true, |
||||
type: 'boolean', |
||||
describe: 'update config.json' |
||||
}) |
||||
.example([ |
||||
['$0 node prepare COM3 -n firstnode', 'prepare the NodeMCU device on COM3 and name it as "firstnode"'] |
||||
]) |
||||
}, async argv => { |
||||
await o.check(argv); |
||||
await o.upload(argv); |
||||
}) |
||||
|
||||
.command('terminal <port>', "open a NodeMCU UART terminal for debug purpose", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 node terminal COM3', 'open the UART terminal on COM3 port'] |
||||
]) |
||||
}, async argv => { |
||||
await o.check(argv); |
||||
await o.terminal(argv); |
||||
}) |
||||
|
||||
|
||||
}, async argv => { |
||||
throw null; |
||||
}) |
||||
|
||||
|
||||
|
||||
return yargs; |
||||
} |
@ -0,0 +1,118 @@ |
||||
module.exports = (yargs) => { |
||||
|
||||
const ora = require('ora'); |
||||
const fs = require('fs'); |
||||
const error = require(__dirname + '/modules/error.js'); |
||||
const colors = require('colors'); |
||||
const boxen = require('boxen'); |
||||
const Table = require('cli-table'); |
||||
|
||||
|
||||
|
||||
const listWiFi = () => { |
||||
let table = new Table({ |
||||
head: ['index', 'ssid', 'password'] |
||||
}); |
||||
let config = require(__dirname + '/modules/getConfig.js')(); |
||||
if(!config) return; |
||||
|
||||
config.wifi.forEach((item, index) => { |
||||
if(item !== null) table.push([index, item.ssid, item.pwd]) |
||||
}); |
||||
console.log(table.toString()); |
||||
} |
||||
|
||||
|
||||
|
||||
yargs = yargs |
||||
|
||||
|
||||
.command('wifi', "set wifi configuration. Use \""+yargs.$0+" node -h\" for more information. ", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 node search', 'search for all NodeMCUs connected to this computer'] |
||||
]) |
||||
|
||||
|
||||
.command('ls', "List all wifi configuration", yargs => { |
||||
return yargs. |
||||
example([ |
||||
['$0 wifi ls', 'List all wifi configuration'] |
||||
]) |
||||
}, async argv => { |
||||
listWiFi(); |
||||
}) |
||||
|
||||
.command('set', "set wifi configuration", yargs => { |
||||
return yargs |
||||
.option('ssid', { |
||||
alias: 's', |
||||
type: 'string', |
||||
demandOption: true, |
||||
describe: 'wifi ssid' |
||||
}) |
||||
.option('pwd', { |
||||
alias: 'p', |
||||
type: 'string', |
||||
demandOption: true, |
||||
describe: 'wifi password' |
||||
}) |
||||
.option('index', { |
||||
alias: 'i', |
||||
type: 'number', |
||||
demandOption: false, |
||||
default: 0, |
||||
describe: 'wifi index in the configuration' |
||||
}) |
||||
.example([ |
||||
['$0 wifi set --ssid=abc --pwd=123 --index=0', 'Set ssid and password of a wifi at index 0'] |
||||
]) |
||||
}, async argv => { |
||||
let config = require(__dirname + '/modules/getConfig.js')(); |
||||
if(!config) return; |
||||
|
||||
let raw_config = JSON.parse(fs.readFileSync(config.root + 'config.json', 'utf-8')); |
||||
raw_config.wifi[argv.index] = { |
||||
ssid: argv.ssid, |
||||
pwd: argv.pwd |
||||
}; |
||||
fs.writeFileSync(config.root + 'config.json', JSON.stringify(raw_config, null, 2), 'utf-8'); |
||||
|
||||
listWiFi(); |
||||
}) |
||||
|
||||
.command('remove', "remove wifi configuration", yargs => { |
||||
return yargs |
||||
.option('index', { |
||||
alias: 'i', |
||||
type: 'number', |
||||
demandOption: true, |
||||
describe: 'wifi index in the configuration' |
||||
}) |
||||
.example([ |
||||
['$0 wifi remove --index=0', 'remove the wifi configuration at index 0'] |
||||
]) |
||||
}, async argv => { |
||||
let config = require(__dirname + '/modules/getConfig.js')(); |
||||
if(!config) return; |
||||
|
||||
let raw_config = JSON.parse(fs.readFileSync(config.root + 'config.json', 'utf-8')); |
||||
raw_config.wifi[argv.index] = null; |
||||
if(!raw_config.wifi.some(i=>i)) raw_config.wifi = []; |
||||
fs.writeFileSync(config.root + 'config.json', JSON.stringify(raw_config, null, 2), 'utf-8'); |
||||
|
||||
|
||||
|
||||
listWiFi(); |
||||
}) |
||||
|
||||
|
||||
|
||||
}, async argv => { |
||||
throw null; |
||||
}) |
||||
|
||||
|
||||
|
||||
return yargs; |
||||
} |
@ -1,2 +0,0 @@ |
||||
--dofile('led.lua') |
||||
dofile('wifi.lua') |
@ -1,18 +0,0 @@ |
||||
--设置控制针脚 |
||||
LED_PIN = 0 |
||||
--设置针脚模式 |
||||
gpio.mode(LED_PIN, gpio.OUTPUT) |
||||
|
||||
--设置计时器0每0.5秒执行一次程序 |
||||
print("hey ther") |
||||
mytimer = tmr.create() |
||||
mytimer:register(1000, tmr.ALARM_AUTO, function() |
||||
print("hey there") |
||||
if gpio.read(LED_PIN) == 0 then |
||||
gpio.write(LED_PIN, gpio.HIGH) |
||||
else |
||||
gpio.write(LED_PIN, gpio.LOW) |
||||
end |
||||
end) |
||||
mytimer:start() |
||||
|
@ -1,66 +0,0 @@ |
||||
|
||||
m = mqtt.Client("clientid", 120) |
||||
|
||||
|
||||
m:lwt("/lwt", "offline", 0, 0) |
||||
|
||||
m:on("connect", function(client) print ("connected") end) |
||||
m:on("connfail", function(client, reason) print ("connection failed", reason) end) |
||||
m:on("offline", function(client) print ("offline") end) |
||||
|
||||
|
||||
idd = 'n1'; |
||||
|
||||
-- pwm control |
||||
local function pwmCtl(pin, duty) |
||||
pwm.setduty(pin, duty) |
||||
end |
||||
|
||||
for i=1,4 do |
||||
pwm.setup(i, 1000, 0) |
||||
pwm.start(i) |
||||
end |
||||
|
||||
m:on("message", function(client, topic, data) |
||||
print(topic .. ":" ) |
||||
if data ~= nil then |
||||
print(data) |
||||
end |
||||
if topic == '/t/'..idd..'/D0' then |
||||
--mytimer:stop() |
||||
if data == '0' then |
||||
gpio.write(0, gpio.LOW) |
||||
print('D0 low') |
||||
else |
||||
gpio.write(0, gpio.HIGH) |
||||
print('D0 high') |
||||
end |
||||
else |
||||
print(string.sub(topic, -2, -2)) |
||||
if string.sub(topic, -2, -2) == 'D' then |
||||
if tonumber(string.sub(topic, -1)) >0 and tonumber(string.sub(topic, -1)) <= 12 then |
||||
print(tonumber(string.sub(topic, -1))) |
||||
pwmCtl(tonumber(string.sub(topic, -1)), tonumber(data)) |
||||
end |
||||
end |
||||
end |
||||
end) |
||||
|
||||
m:on("overflow", function(client, topic, data) |
||||
print(topic .. " partial overflowed message: " .. data ) |
||||
end) |
||||
|
||||
m:connect("192.168.3.4", 1883, false, function(client) |
||||
print("connected") |
||||
client:subscribe("/t/"..idd.."/#", 0, function(client) print("subscribe success") end) |
||||
client:publish("/t/"..idd, "hello", 0, 0, function(client) print("sent") end) |
||||
|
||||
for i=5,8 do |
||||
gpio.mode(i,gpio.INT) |
||||
gpio.trig(i, "up", function() client:publish("/t/"..idd.."/D"..i, gpio.read(i), 0, 0, function(client) print(i.." high") end) end) |
||||
end |
||||
|
||||
end, |
||||
function(client, reason) |
||||
print("failed reason: " .. reason) |
||||
end) |
@ -1,45 +0,0 @@ |
||||
o = {} |
||||
|
||||
|
||||
|
||||
function random(n, m) |
||||
math.randomseed(os.clock()*math.random(1000000,90000000)+math.random(1000000,90000000)) |
||||
return math.random(n, m) |
||||
end |
||||
|
||||
function randomLetter(len) |
||||
local rt = "" |
||||
for i = 1, len, 1 do |
||||
rt = rt..string.char(random(97,122)) |
||||
end |
||||
return rt |
||||
end |
||||
|
||||
|
||||
o._new = function(f, r) |
||||
local hash = randomLetter(8) |
||||
o[tag] = { |
||||
f = loadstring(f), |
||||
r = loadstring(r) |
||||
} |
||||
return hash |
||||
end |
||||
|
||||
|
||||
o._del = function(hash) |
||||
o[hash] = nil |
||||
collectgarbage("collect") |
||||
end |
||||
|
||||
|
||||
|
||||
|
||||
l = o._new("print('ff')", "print('rr')") |
||||
|
||||
o[l].f() |
||||
|
||||
|
||||
|
||||
print(collectgarbage("count")) |
||||
|
||||
--o._list() |
@ -1,6 +0,0 @@ |
||||
//heartbeat LED
|
||||
let b = wiot.device.nodemcu('n1'); |
||||
|
||||
let l = wiot.module.led(b, 'D2'); |
||||
|
||||
l.heartbeat(0, 1000, 600, 3000); |
@ -1,7 +0,0 @@ |
||||
let b = wiot.device.nodemcu('n1'); |
||||
let b2 = wiot.device.nodemcu('n2'); |
||||
|
||||
let l = wiot.module.led(b, 'D2'); |
||||
let s = wiot.module.sensor(b2, 'D5'); |
||||
|
||||
s.trigger(m=>l.set(m*400)); |
@ -1,41 +0,0 @@ |
||||
o = {} |
||||
|
||||
|
||||
|
||||
function random(n, m) |
||||
math.randomseed(os.clock()*math.random(1000000,90000000)+math.random(1000000,90000000)) |
||||
return math.random(n, m) |
||||
end |
||||
|
||||
function randomLetter(len) |
||||
local rt = "" |
||||
for i = 1, len, 1 do |
||||
rt = rt..string.char(random(97,122)) |
||||
end |
||||
return rt |
||||
end |
||||
|
||||
|
||||
o._new = function(f, r) |
||||
local hash = randomLetter(8) |
||||
o[hash] = {} |
||||
o[hash].f = loadstring(f) |
||||
o[hash].r = loadstring(r) |
||||
return hash |
||||
end |
||||
|
||||
|
||||
o._del = function(hash) |
||||
o[hash] = nil |
||||
end |
||||
|
||||
|
||||
|
||||
|
||||
l = o._new("print('ff')", "print('rr')") |
||||
|
||||
o[l].f() |
||||
|
||||
|
||||
|
||||
|
@ -1,31 +0,0 @@ |
||||
print('Setting up WIFI...') |
||||
wifi.setmode(wifi.STATION) |
||||
|
||||
station_cfg={} |
||||
station_cfg.ssid="yimian-iot" |
||||
station_cfg.pwd="1234567890." |
||||
station_cfg.save=true |
||||
wifi.sta.config(station_cfg) |
||||
wifi.sta.connect() |
||||
|
||||
|
||||
mytime = tmr.create() |
||||
mytime:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end) |
||||
mytime:start() |
||||
|
||||
tmp = 0 |
||||
|
||||
mytimer = tmr.create() |
||||
mytimer:register(1000, tmr.ALARM_AUTO, function() |
||||
print('Setting up WIFI...') |
||||
if wifi.sta.getip() == nil then |
||||
print('Waiting for IP ...') |
||||
loadstring("tmp=tmp+1 print(tmp)")() |
||||
else |
||||
print('IP is ' .. wifi.sta.getip()) |
||||
mytimer:unregister() |
||||
dofile('mqtt.lua') |
||||
end |
||||
end) |
||||
mytimer:start() |
||||
|
Loading…
Reference in new issue