mirror of https://github.com/iotcat/wIoT
parent
d4417f9d93
commit
eb6c9f53f1
12 changed files with 457 additions and 93 deletions
@ -0,0 +1,33 @@ |
||||
module.exports = async (port) => { |
||||
|
||||
const { execFile } = require('child_process'); |
||||
const path = require('path'); |
||||
const ora = require('ora'); |
||||
const root = __dirname + '/../../../'; |
||||
|
||||
let cache = ''; |
||||
|
||||
const upload = () => { |
||||
return new Promise((resolve, reject) => { |
||||
let process = execFile(root + 'node_modules/.bin/nodemcu-tool.cmd', ['terminal', '-p', port], (err, stdout, stderr) => { |
||||
if(err) { |
||||
console.log(stderr); |
||||
reject(); |
||||
}else{ |
||||
resolve(stdout); |
||||
} |
||||
}); |
||||
process.stdout.on('data', (data) => { |
||||
cache += String(data); |
||||
if(data.indexOf('\n') != -1){ |
||||
console.log(cache); |
||||
cache = ''; |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
return new Promise(async (resolve, reject) => { |
||||
resolve(await upload()); |
||||
}); |
||||
} |
@ -1,89 +0,0 @@ |
||||
|
||||
function evall(obj){ return Function('"use strict";return (setImmediate(()=>{' + obj + '}))')(); } |
||||
|
||||
try{ |
||||
|
||||
t = evall('console.log("evall ok")'); |
||||
}catch(e){} |
||||
|
||||
|
||||
const express = require('express'); |
||||
const mqtt = require('mqtt'); |
||||
|
||||
|
||||
var client = mqtt.connect('mqtt://192.168.3.4'); |
||||
|
||||
|
||||
client.on('connect', function () { |
||||
client.subscribe('/t/#'); |
||||
}); |
||||
|
||||
|
||||
var que = []; |
||||
|
||||
|
||||
client.on('message', function (topic, message) { |
||||
console.log(message.toString()); |
||||
que.forEach(obj => { |
||||
if(obj.topic == topic){ |
||||
obj.func(message); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
|
||||
wiot = { |
||||
device:{ |
||||
nodemcu: (id) => { |
||||
let o = { |
||||
'id': id |
||||
} |
||||
return o; |
||||
} |
||||
}, |
||||
module: { |
||||
led: (device, pin) => { |
||||
let o = { |
||||
set: (val) => { |
||||
client.publish('/t/'device.id+'/'+pin, val); |
||||
} |
||||
} |
||||
return o; |
||||
}, |
||||
sensor: (device, pin) => { |
||||
let o = { |
||||
trigger: (func) => { |
||||
que.push({ |
||||
topic: '/t/'device.id+'/'+pin, |
||||
'func': (s) => { |
||||
func(s); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
return o; |
||||
} |
||||
} |
||||
}; |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const app = express() |
||||
const port = 3000 |
||||
|
||||
app.get('/', (req, res) => { |
||||
try{ |
||||
clearImmediate(t); |
||||
t = evall(atob(req.query.body)); |
||||
}catch(e){ |
||||
t = evall('console.log('+e+')'); |
||||
} |
||||
}) |
||||
|
||||
app.listen(port, () => { |
||||
console.log(`Example app listening at http://localhost:${port}`) |
||||
}) |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,9 @@ |
||||
{ |
||||
"info": "function() return CONFIG;end", |
||||
"restart": "function() node.restart();end", |
||||
"exec": "function(s) return loadstring(s)();end", |
||||
"list": "function() local obj = {};for k, v in pairs(w.f) do obj[k] = encoder.toHex(crypto.hash([[md5]],string.dump(v))); end;return obj;end", |
||||
"push": "function(s) local obj = sjson.decode(s);w.push(obj.hash, obj.func);return {status = true};end", |
||||
"pull": "function(s) w.pull(s);return {status = true};end", |
||||
"clear": "function() w.clear();return {status = true};end" |
||||
} |
@ -0,0 +1,27 @@ |
||||
CONFIG = { |
||||
firmware = { |
||||
version = '0.0.1' |
||||
}, |
||||
wifi = { |
||||
station = { |
||||
ssid = "yimian-iot", |
||||
pwd = "1234567890.", |
||||
save = true |
||||
} |
||||
}, |
||||
coap = { |
||||
server = { |
||||
port = 5683 |
||||
} |
||||
}, |
||||
w = { |
||||
id = node.chipid(), |
||||
director = { |
||||
ip = '192.168.3.251', |
||||
port = 5683 |
||||
}, |
||||
heartbeat = { |
||||
interval = 15000 |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,167 @@ |
||||
--CONFIG |
||||
dofile('config.lua') |
||||
|
||||
--Global Var |
||||
cs = nil --coAP Server |
||||
cc = nil --coAP Client |
||||
|
||||
--wIoT Toolbox |
||||
w = { |
||||
f = {}, |
||||
_push = function(hash, s) |
||||
w.f[hash] = function(r) |
||||
local status, msg = pcall(loadstring('return '..s)(), r) |
||||
return sjson.encode({ |
||||
status = status, |
||||
msg = msg |
||||
}) |
||||
end |
||||
_G["_"..hash] = w.f[hash] |
||||
cs:func('_'..hash, coap.JSON) |
||||
|
||||
return hash |
||||
end, |
||||
push = function(hash, s) |
||||
w._push(hash, s) |
||||
func.jsonfPush('func.json', hash, s) |
||||
return hash |
||||
end, |
||||
pull = function(hash) |
||||
w.f[hash] = nil |
||||
_G["_"..hash] = nil |
||||
func.jsonfPull('func.json', hash) |
||||
w.refresh() |
||||
end, |
||||
clear = function() |
||||
for k, v in pairs(w.f) do |
||||
loadstring('_'..k..'=nil') |
||||
end |
||||
w.f = {} |
||||
func.jsonfClear('func.json') |
||||
w.refresh() |
||||
end, |
||||
start = function() |
||||
print('w starting...') |
||||
cs = coap.Server() |
||||
cs:listen(CONFIG.coap.server.port) |
||||
local usr = func.jsonfRead('func.json') |
||||
if next(usr) ~= nil then |
||||
print('in usr') |
||||
for k, v in pairs(usr) do |
||||
print(k) |
||||
w._push(k, v) |
||||
end |
||||
else |
||||
print('in systemd') |
||||
local systemd = func.jsonfRead('FUNC.json') |
||||
for k, v in pairs(systemd) do |
||||
w.push(k, v) |
||||
end |
||||
end |
||||
end, |
||||
stop = function() |
||||
cs:close() |
||||
cs = nil |
||||
w.f = {} |
||||
collectgarbage("collect") |
||||
end, |
||||
refresh = function() |
||||
w.stop() |
||||
w.start() |
||||
for k, v in pairs(w.f) do |
||||
cs:func('_'..k, coap.JSON) |
||||
end |
||||
end |
||||
} |
||||
|
||||
--Functions |
||||
func = { |
||||
init = { |
||||
run = function() |
||||
func.init.wifi(func.init.coap, func.init.w, func.run) |
||||
end, |
||||
wifi = function(after, after2, after3) |
||||
print('Setting up WIFI...') |
||||
wifi.setmode(wifi.STATION) |
||||
wifi.sta.config(CONFIG.wifi.station) |
||||
wifi.sta.connect() |
||||
local wifiInit = tmr.create() |
||||
wifiInit:register(1000, tmr.ALARM_AUTO, function() |
||||
if wifi.sta.getip() == nil then |
||||
print('Waiting for IP ...') |
||||
else |
||||
print('IP is ' .. wifi.sta.getip()) |
||||
wifiInit:unregister() |
||||
after(after2, after3) |
||||
end |
||||
end) |
||||
wifiInit:start() |
||||
end, |
||||
coap = function (after, after2) |
||||
cc = coap.Client() |
||||
after(after2) |
||||
end, |
||||
w = function(after) |
||||
w.start() |
||||
--cc:post(coap.CON, 'coap://'..CONFIG.w.director.ip..CONFIG.w.director.port..'/reg', '{"version": "'..CONFIG.firmware.version..'", "id": "'..CONFIG.w.id..'", "ip": "'..wifi.sta.getip()..'", "port": '..CONFIG.coap.server.port..'}') |
||||
local heartbeat = tmr.create() |
||||
heartbeat:register(CONFIG.w.heartbeat.interval, tmr.ALARM_AUTO, function() |
||||
--cc:post(coap.CON, 'coap://'..CONFIG.w.director.ip..CONFIG.w.director.port..'/heartbeat', '{"id": "'..CONFIG.w.id..'", , "ip": "'..wifi.sta.getip()..'"}') |
||||
end); |
||||
heartbeat:start() |
||||
if after then after() end |
||||
end |
||||
}, |
||||
randomLetter = function(len) |
||||
local rt = "" |
||||
for i = 1, len, 1 do |
||||
rt = rt..string.char(math.random(97,122)) |
||||
end |
||||
return rt |
||||
end, |
||||
jsonfRead = function(f) |
||||
if file.open(f) then |
||||
local status, obj = pcall(sjson.decode, file.read()) |
||||
file.close() |
||||
if status then |
||||
return obj |
||||
else |
||||
return {} |
||||
end |
||||
end |
||||
return {} |
||||
end, |
||||
jsonfWrite = function(f, obj) |
||||
if file.open(f, 'w+') then |
||||
local status, json = pcall(sjson.encode, obj) |
||||
if status then |
||||
file.write(json) |
||||
else |
||||
file.write("{}") |
||||
end |
||||
file.close() |
||||
end |
||||
end, |
||||
jsonfPush = function(f, hash, s) |
||||
local obj = func.jsonfRead(f) |
||||
obj[hash] = s |
||||
func.jsonfWrite(f, obj) |
||||
end, |
||||
jsonfPull = function(f, hash) |
||||
func.jsonfPush(f, hash, nil) |
||||
end, |
||||
jsonfClear = function(f) |
||||
func.jsonfWrite(f, {}) |
||||
end |
||||
} |
||||
|
||||
--Run |
||||
func.run = function() |
||||
--run content |
||||
gpio.write(0, gpio.LOW) |
||||
end |
||||
|
||||
|
||||
|
||||
--exec Init |
||||
func.init.run() |
@ -0,0 +1,120 @@ |
||||
--CONFIG |
||||
CONFIG = { |
||||
firmware = { |
||||
version = '0.0.1' |
||||
}, |
||||
wifi = { |
||||
station = { |
||||
ssid = "yimian-iot", |
||||
pwd = "1234567890.", |
||||
save = true |
||||
} |
||||
}, |
||||
coap = { |
||||
server = { |
||||
port = 5683 |
||||
} |
||||
}, |
||||
w = { |
||||
id = {} |
||||
director = { |
||||
ip = '192.168.3.251' |
||||
}, |
||||
heartbeat = { |
||||
interval = 15000 |
||||
} |
||||
} |
||||
} |
||||
|
||||
--Global Var |
||||
cs = nil --coAP Server |
||||
cc = nil --coAP Client |
||||
|
||||
--wIoT Toolbox |
||||
w = { |
||||
f = {}, |
||||
push = function(hash, s) |
||||
w.f[hash] = loadstring(s) |
||||
cs:func('w.f.'..hash) |
||||
return hash |
||||
end, |
||||
pull = function(hash) |
||||
w.f[hash] = nil |
||||
w.refresh() |
||||
end, |
||||
clear = function() |
||||
w.f = nil |
||||
w.refresh() |
||||
end, |
||||
start = function() |
||||
cs = coap.Server() |
||||
cs:listen(CONFIG.coap.server.port) |
||||
w.push('_/info', 'function() local status, json;ok, json = pcall(sjson.encode, CONFIG);return json;end') |
||||
w.push('_/w/f', 'function() local status, json;ok, json = pcall(sjson.encode, w.f);return json;end'); |
||||
w.push('_/w/push', 'function() ') |
||||
end, |
||||
stop = function() |
||||
cs:close() |
||||
cs = nil |
||||
collectgarbage("collect") |
||||
end, |
||||
refresh = function() |
||||
w.stop() |
||||
w.start() |
||||
for k, v in pairs(w.f) do |
||||
cs:func('w.f.'..k) |
||||
end |
||||
end |
||||
} |
||||
|
||||
--Functions |
||||
func = { |
||||
init = { |
||||
run = function() |
||||
func.init.wifi(func.init.coap, func.init.w, func.run) |
||||
end, |
||||
wifi = function(after, after2, after3) |
||||
print('Setting up WIFI...') |
||||
wifi.setmode(wifi.STATION) |
||||
wifi.sta.config(CONFIG.wifi.station) |
||||
wifi.sta.connect() |
||||
local wifiInit = tmr.create() |
||||
wifiInit:register(1000, tmr.ALARM_AUTO, function() |
||||
if wifi.sta.getip() == nil then |
||||
print('Waiting for IP ...') |
||||
else |
||||
print('IP is ' .. wifi.sta.getip()) |
||||
wifiInit:unregister() |
||||
after(after2, after3) |
||||
end |
||||
end) |
||||
wifiInit:start() |
||||
end, |
||||
coap = function (after, after2) |
||||
cs = coap.Server() |
||||
cs:listen(CONFIG.coap.server.port) |
||||
cc = coap.Client() |
||||
after(after2) |
||||
end, |
||||
w = function(after) |
||||
|
||||
if after then after() |
||||
end |
||||
}, |
||||
randomLetter = function(len) |
||||
local rt = "" |
||||
for i = 1, len, 1 do |
||||
rt = rt..string.char(math.random(97,122)) |
||||
end |
||||
return rt |
||||
end |
||||
|
||||
} |
||||
|
||||
--Run |
||||
func.run = function() |
||||
gpio.write(0, gpio.LOW) |
||||
end |
||||
|
||||
--exec Init |
||||
func.init.run() |
@ -0,0 +1,45 @@ |
||||
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() |
@ -0,0 +1,41 @@ |
||||
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() |
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue