Compare commits

...

3 Commits

Author SHA1 Message Date
IoTcat d78602c150 classify 3 years ago
IoTcat 32ba89dbdf v0.3.9 3 years ago
IoTcat 36492d3787 v0.3.8 3 years ago
  1. 3
      .gitmodules
  2. 6
      package.json
  3. 1
      src/cli/compiler/good.lua
  4. 1
      src/cli/compiler/good3.lua
  5. 1
      src/cli/compiler/good4.lua
  6. 20
      src/cli/compiler/test.js
  7. 23
      src/cli/run.js
  8. 5
      src/cli/test.js
  9. 40
      src/cli/ttt.js
  10. 25
      src/compiler/index.js
  11. 9
      src/compiler/modules/breathing.js
  12. 5
      src/compiler/operators/if.js
  13. 5
      src/compiler/operators/strIndexOf.js
  14. 5
      src/compiler/operators/strSubStr.js
  15. 11
      src/compiler/primitives/adc.js
  16. 17
      src/compiler/primitives/bigiot.js
  17. 19
      src/compiler/primitives/gpio.js
  18. 12
      src/compiler/primitives/http.js
  19. 10
      src/compiler/primitives/mail.js
  20. 12
      src/compiler/primitives/memobird.js
  21. 10
      src/compiler/primitives/print.js
  22. 13
      src/compiler/primitives/pwm.js
  23. 20
      src/compiler/primitives/redis.js
  24. 10
      src/compiler/primitives/sjson_decode.js
  25. 10
      src/compiler/primitives/sjson_encode.js
  26. 23
      src/compiler/primitives/tcp.js
  27. 18
      src/compiler/primitives/udp.js
  28. 34
      src/compiler/primitives/webpage.js
  29. 238
      src/compiler/wiot-core.js
  30. 93
      src/controller/test.js
  31. 1
      src/director
  32. BIN
      src/drivers/nodemcu/lua/lfs.img
  33. BIN
      src/drivers/nodemcu/lua/lfs.zip
  34. 1
      src/drivers/nodemcu/lua/lfs/bigiot.lua

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "src/director"]
path = src/director
url = https://github.com/IoTcat/wIoT-director.git

@ -1,8 +1,8 @@
{
"name": "wiot",
"version": "0.3.6",
"description": "A software-defined IoT OS basing on NodeJS.",
"main": "src/cli/compiler.js",
"version": "0.3.9",
"description": "A software-defined IoT Network OS for NodeMCU",
"main": "src/compiler/index.js",
"repository": "git@github.com:iotcat/wIoT",
"author": "IoTcat <i@iotcat.me>",
"license": "MIT",

@ -1 +0,0 @@
gpio.mode(3,gpio.INPUT);tmr.create():alarm(100, tmr.ALARM_AUTO, function()a66=gpio.read(3);if not (f_a66==a66) then msg.send('good3','a66',a66);msg.send('good4','a66',a66);end;f_a66=a66; end);

@ -1 +0,0 @@
gpio.mode(4,gpio.OUTPUT);a66=0;tmr.create():alarm(100, tmr.ALARM_AUTO, function()gpio.write(4,a66); end);msg.onSend('a66',function(f,b) a66=b; end);

@ -1 +0,0 @@
gpio.mode(4,gpio.OUTPUT);a66=0;tmr.create():alarm(100, tmr.ALARM_AUTO, function()gpio.write(4,a66); end);msg.onSend('a66',function(f,b) a66=b; end);

@ -1,20 +0,0 @@
const wiot =
let nodemcu1 = wiot.node.nodemcu('good');
let nodemcu2 = wiot.node.nodemcu('good3');
let nodemcu3 = wiot.node.nodemcu('good4');
let w1 = wiot.udp.wire()//,
// w2 = wiot.udp.wire();
//let buf = wiot.udp.buffer(w1, w2, nodemcu1);
let pin1 = wiot.udp.gpio(wiot.INPUT, nodemcu1.D3, w1, nodemcu1);
let pin2 = wiot.udp.gpio(wiot.OUTPUT, nodemcu2.D4, w1, nodemcu2);
let pin3 = wiot.udp.gpio(wiot.OUTPUT, nodemcu3.D4, w1, nodemcu3);

@ -1,23 +0,0 @@
module.exports = (yargs) => {
const request = require('request');
const ora = require('ora');
const fs = require('fs');
yargs = yargs
.command('run', "wiot run".green + " Run the selected file on NodeMCU cluster..", yargs => yargs, async argv => {
let s = fs.readFileSync(argv._[1], 'utf-8');
let b = new Buffer(s).toString('base64');
ban = new ora(`Pushing...`).start();
request('http://192.168.3.251:3000/?body='+b, (err)=>{
if(err){
ban.fail('fail');
}else{
ban.succeed('success');
}
});
})
return yargs;
}

@ -1,5 +0,0 @@
const t = require('./modules/upload.js');
(async ()=>{
await t('COM3', __dirname + '/../../test/init.lua')
})();

@ -1,40 +0,0 @@
const wiot = require('./compiler.js')();
;(async () => {
// declare two nodemcu board by ID
let NodeTest1 = wiot.device.nodemcu('test1');
let NodeTest2 = wiot.device.nodemcu('test2');
// declare a led
let led = wiot.module.led(NodeTest1, NodeTest1.pin.D2);
// declare a sensor
let sensor = wiot.module.sensor(NodeTest2, NodeTest2.pin.A0);
// let the frequency of breathing is controled by sensor
led.breath(sensor);
// push above to the two nodemcu
await NodeTest1.service.push()
await NodeTest2.service.push()
})();
/*
//breathing led
;(async () => {
// declear new nodemcu board by ID
let NodeTest1 = wiot.device.nodemcu('test1');
//declear new led module at D2 on nodemcu test1
let led = wiot.module.led(NodeTest1, NodeTest1.pin.D2);
// let led breathe with 2s
led.breath(2000);
// push the above functions to nodemcu
await NodeTest1.service.push()
})();
*/

@ -0,0 +1,25 @@
const wiot = require(__dirname + '/wiot-core.js');
/* Primitives */
require(__dirname + '/primitives/adc.js')(wiot)
require(__dirname + '/primitives/bigiot.js')(wiot)
require(__dirname + '/primitives/gpio.js')(wiot)
require(__dirname + '/primitives/http.js')(wiot)
require(__dirname + '/primitives/mail.js')(wiot)
require(__dirname + '/primitives/memobird.js')(wiot)
require(__dirname + '/primitives/print.js')(wiot)
require(__dirname + '/primitives/pwm.js')(wiot)
require(__dirname + '/primitives/redis.js')(wiot)
require(__dirname + '/primitives/sjson_decode.js')(wiot)
require(__dirname + '/primitives/sjson_encode.js')(wiot)
require(__dirname + '/primitives/tcp.js')(wiot)
require(__dirname + '/primitives/udp.js')(wiot)
require(__dirname + '/primitives/webpage.js')(wiot)
/* operators */
require(__dirname + '/operators/if.js')(wiot)
require(__dirname + '/operators/strIndexOf.js')(wiot)
require(__dirname + '/operators/strSubStr.js')(wiot)
/* modules */
require(__dirname + '/modules/breathing.js')(wiot)

@ -0,0 +1,9 @@
module.exports = wiot => {
wiot.newModule('breathing', (node, period_s, wire_output) => {
let w1 = new wiot.wire(0),
w2 = new wiot.wire(10);
wiot.operate(node, `math.abs($1%2047-1023)`, wire_output, w2);
wiot.operate(node, `($1+math.floor(2048*0.01/${period_s}))%2048`, w2, w1);
wiot.buffer(node, w1, w2, .01, true);
});
}

@ -0,0 +1,5 @@
module.exports = wiot => {
wiot.newOperator('if', (condition, ifTrue, ifFalse) => {
return `((${condition})and{${ifTrue}}or{${ifFalse}})[1]`;
});
}

@ -0,0 +1,5 @@
module.exports = wiot => {
wiot.newOperator('strIndexOf', (str, segment) => {
return `string.find(${str},${segment})`;
});
}

@ -0,0 +1,5 @@
module.exports = wiot => {
wiot.newOperator('strSubStr', (str, pos, length) => {
return `string.sub(${str},${pos}${length?`,${length}`:``})`;
});
}

@ -0,0 +1,11 @@
module.exports = wiot => {
wiot.newPrimitive('adc', (node, wire_output) => ({
node: node,
pin: 0,
output: [wire_output]
}), (o, method) => {
let node = o.node;
node.always(`${o.output[0].reg}=adc.read(${o.pin});`);
});
}

@ -0,0 +1,17 @@
module.exports = wiot => {
wiot.newPrimitive('bigiot', (node, DeviceID, APIkey, wire_output, wire_trigger) => ({
node: node,
DeviceID: DeviceID,
APIkey: APIkey,
output: [wire_output, wire_trigger]
}), (o, method) => {
let node = o.node;
let obj = method.Reg()
node.setReg(obj, '{}');
node.always(`${o.output[0].reg}=${obj}.reg;`);
node.always(`${o.output[1].reg}=${obj}.trigger;`);
node.finale(`node.LFS.bigiot()('${o.DeviceID}','${o.APIkey}',${obj});`);
});
}

@ -0,0 +1,19 @@
module.exports = wiot => {
/* pre-installed udp defination */
wiot.newPrimitive('gpio', (node, mode, pin, wire) => ({
node: node,
pin: pin,
mode: mode,
[(mode==wiot.INPUT)?'output':'input']: [wire]
}), (o, method) => {
node = o.node;
node.init(`gpio.mode(${o.pin},${o.mode});`);
if(o.mode == wiot.INPUT){
node.always(`${o.output[0].reg}=gpio.read(${o.pin});`);
}
if(o.mode == wiot.OUTPUT){
node.prepare(`gpio.write(${o.pin},${o.input[0].reg}%2);`);
node.trigger(o.input[0], `gpio.write(${o.pin},${o.input[0].reg}%2);`);
}
});
}

@ -0,0 +1,12 @@
module.exports = wiot => {
wiot.newPrimitive('http', (node, wire_statusCode, wire_res, wire_url, wire_body = new wiot.wire(`''`), header = 'nil', method = 'GET') => ({
node: node,
header: header,
method: method,
output: [wire_statusCode, wire_res],
input: [wire_url, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `http.request(${o.input[0].reg},'${o.method}','${o.header}',${o.input[1].reg},function(c,d)${o.output[0].reg}=c;${o.output[1].reg}=d;end);`);
});
}

@ -0,0 +1,10 @@
module.exports = wiot => {
wiot.newPrimitive('mail', (node, wire_en, wire_to, wire_subject, wire_body, from = 'wiot') => ({
node: node,
from: from,
input: [wire_en, wire_to, wire_subject, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input[0], `if ${o.input[0].reg}==1 then http.request('http://api.yimian.xyz/mail/?to='..tostring(${o.input[1].reg})..'&subject='..tostring(${o.input[2].reg})..'&body='..tostring(${o.input[3].reg})..'&from=${o.from}','GET','','',function(c,d)end);end;`);
});
}

@ -0,0 +1,12 @@
module.exports = wiot => {
wiot.newPrimitive('memobird', (node, AppKey, UserID, memobirdID, wire_en, wire_body) => ({
node: node,
ak: AppKey,
uid: UserID,
memobirdID: memobirdID,
input: [wire_en, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input[0], `if ${o.input[0].reg}==1 then http.request('http://api.yimian.xyz/gugu/?ak=${o.ak}&userID=${o.uid}&memobirdID=${o.memobirdID}&body='..tostring(${o.input[1].reg}),'GET','','',function(c,d)end);end;`);
});
}

@ -0,0 +1,10 @@
module.exports = wiot => {
wiot.newPrimitive('print', (node, ...wires_input) => ({
node: node,
input: wires_input
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `print(${o.input.map(wire=>wire.reg).join(',')});`);
});
}

@ -0,0 +1,13 @@
module.exports = wiot => {
wiot.newPrimitive('pwm', (node, pin, wire_duty, wire_clock) => ({
node: node,
pin: pin,
input: [wire_duty, wire_clock]
}), (o, method) => {
let node = o.node;
node.prepare(`pwm.setup(${o.pin},${o.input[1].reg},${o.input[0].reg});`);
node.prepare(`pwm.setduty(${o.pin},${o.input[0].reg});`);
node.trigger(o.input[0], `pwm.setduty(${o.pin},${o.input[0].reg});`);
node.trigger(o.input[1], `pwm.setclock(${o.pin},${o.input[1].reg});`);
});
}

@ -0,0 +1,20 @@
module.exports = wiot => {
wiot.newPrimitive('redis', (node, host, channel, wire_res, wire_send, port = 6379) => ({
node: node,
host: host,
port: port,
channel: channel,
output: [wire_res],
input: [wire_send]
}), (o, method) => {
let node = o.node;
let vpub = method.Reg(),
vsub = method.Reg();
node.setReg(vpub);
node.setReg(vsub);
node.prepare(`${vpub}=node.LFS.redis().connect('${o.host}',${o.port});`);
node.prepare(`${vsub}=node.LFS.redis().connect('${o.host}',${o.port});`);
node.finale(`${vsub}:subscribe('${o.channel}',function(c,m)${o.output[0].reg}=m;end);`);
node.trigger(o.input, `${vpub}:publish('${o.channel}',tostring(${o.input[0].reg}));`);
});
}

@ -0,0 +1,10 @@
module.exports = wiot => {
wiot.newPrimitive('sjson_decode', (node, wire_output, wire_input) => ({
node: node,
input: wire_input,
output: wire_output
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `tmp,${o.output[0].reg}=pcall(sjson.decode,${o.input[0].reg});`);
});
}

@ -0,0 +1,10 @@
module.exports = wiot => {
wiot.newPrimitive('sjson_encode', (node, wire_output, wire_input) => ({
node: node,
input: wire_input,
output: wire_output
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `tmp,${o.output[0].reg}=pcall(sjson.encode,${o.input[0].reg});`);
});
}

@ -0,0 +1,23 @@
module.exports = wiot => {
wiot.newPrimitive('tcp', (node, host, port, wire_res_data, wire_send_data) => ({
node: node,
host: host,
port: port,
output: [wire_res_data],
input: [wire_send_data]
}), (o, method) => {
let node = o.node;
let v = method.Reg()
let status = method.Reg();
node.setReg(v);
node.setReg(status, false);
node.prepare(`${v}=net.createConnection(net.TCP,0);`);
node.finale(`${v}:connect(${o.port},'${o.host}');`);
node.trigger(o.input[0], `if ${status} then ${v}:send(tostring(${o.input[0].reg}));end;`);
node.prepare(`${v}:on('receive',function(s,d)${o.output[0].reg}=d;end);`);
node.prepare(`${v}:on('connection',function()${status}=true;end);`);
node.prepare(`${v}:on('disconnection',function()${status}=false;tmr.create():alarm(1000,tmr.ALARM_SINGLE,function()${v}:connect(${o.port},'${o.host}');end);end);`);
});
}

@ -0,0 +1,18 @@
module.exports = wiot => {
wiot.newPrimitive('udp', (node, wire_res_ip, wire_res_port, wire_res_data, wire_send_ip, wire_send_port, wire_send_data, localport = parseInt(Math.random()*20000)+10000) => ({
node: node,
localport: localport,
output: [wire_res_ip, wire_res_port, wire_res_data],
input: [wire_send_ip, wire_send_port, wire_send_data]
}), (o, method) => {
let node = o.node;
let v = method.Reg()
node.setReg(v);
node.prepare(`${v}=net.createUDPSocket();`);
node.prepare(`${v}:listen(${o.localport});`);
node.trigger(o.input[2], `${v}:send(${o.input[1].reg},${o.input[0].reg},tostring(${o.input[2].reg}));`);
node.finale(`${v}:on('receive',function(s,d,p,i)${o.output[2].reg}=d;${o.output[1].reg}=p;${o.output[0].reg}=i;end);`);
});
}

@ -0,0 +1,34 @@
module.exports = wiot => {
wiot.newPrimitive('webpage', (node, key, template, wires_output_map = {}, wires_input_map = {}, interval_s = 0.4, host = 'http://wiot-webpage.yimian.xyz/') => ({
node: node,
host: host,
key: key,
interval_s: interval_s,
template: template,
output: Object.keys(wires_output_map).map(id => wires_output_map[id]),
input: Object.keys(wires_input_map).map(id => wires_input_map[id]),
wires_output_map: wires_output_map,
wires_input_map: wires_input_map
}), (o, method) => {
let node = o.node;
let url = `${o.host}${(o.host.substring(o.host.length-1)=='/'?'':'/')}SET?key=${o.key}`;
url += '&template='+JSON.stringify(o.template);
got(url, {
responseType: 'json'
});
let url2 = `${o.host}${(o.host.substring(o.host.length-1)=='/'?'':'/')}SYNC?key=${o.key}`;
Object.keys(o.wires_input_map).forEach(id => {
url2 += `&${id}='..${o.wires_input_map[id].reg}..'`;
});
let s = '';
Object.keys(o.wires_output_map).forEach(id => {
s += `${o.wires_output_map[id].reg}=obj['${id}'];`;
});
node.prepare(`tmr.create():alarm(${o.interval_s*1000},tmr.ALARM_AUTO,function()http.request('${url2}','GET','','',function(c,d)if c==200 then local st,obj=pcall(sjson.decode,d);if st then ${s}end;end;end);end)`);
});
}

@ -4,7 +4,7 @@ const fs = require('fs');
const md5 = require('md5');
const got = require('got');
let config = require(__dirname + '/modules/getConfig.js')();
let config = require(__dirname + '/../cli/modules/getConfig.js')();
if(!config) return;
/* system private method */
@ -311,25 +311,6 @@ wiot.newPrimitive('wire', (default_value = 0, isPersist = false) => ({
/* pre-installed udp defination */
wiot.newPrimitive('gpio', (node, mode, pin, wire) => ({
node: node,
pin: pin,
mode: mode,
[(mode==wiot.INPUT)?'output':'input']: [wire]
}), (o, method) => {
node = o.node;
node.init(`gpio.mode(${o.pin},${o.mode});`);
if(o.mode == wiot.INPUT){
node.always(`${o.output[0].reg}=gpio.read(${o.pin});`);
}
if(o.mode == wiot.OUTPUT){
node.prepare(`gpio.write(${o.pin},${o.input[0].reg}%2);`);
node.trigger(o.input[0], `gpio.write(${o.pin},${o.input[0].reg}%2);`);
}
});
wiot.newPrimitive('buffer', (node, wire_output, wire_input, delay_s = 0, isSyncAtInit = false) => ({
node: node,
delay_s: delay_s,
@ -361,223 +342,6 @@ wiot.newPrimitive('operate', (node, expression, wire_output, ...wires_input) =>
node.trigger(o.input, `${o.output[0].reg}=${s};`);
});
wiot.newPrimitive('print', (node, ...wires_input) => ({
node: node,
input: wires_input
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `print(${o.input.map(wire=>wire.reg).join(',')});`);
});
wiot.newPrimitive('sjson_encode', (node, wire_output, wire_input) => ({
node: node,
input: wire_input,
output: wire_output
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `tmp,${o.output[0].reg}=pcall(sjson.encode,${o.input[0].reg});`);
});
wiot.newPrimitive('sjson_decode', (node, wire_output, wire_input) => ({
node: node,
input: wire_input,
output: wire_output
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `tmp,${o.output[0].reg}=pcall(sjson.decode,${o.input[0].reg});`);
});
wiot.newPrimitive('pwm', (node, pin, wire_duty, wire_clock) => ({
node: node,
pin: pin,
input: [wire_duty, wire_clock]
}), (o, method) => {
let node = o.node;
node.prepare(`pwm.setup(${o.pin},${o.input[1].reg},${o.input[0].reg});`);
node.prepare(`pwm.setduty(${o.pin},${o.input[0].reg});`);
node.trigger(o.input[0], `pwm.setduty(${o.pin},${o.input[0].reg});`);
node.trigger(o.input[1], `pwm.setclock(${o.pin},${o.input[1].reg});`);
});
wiot.newPrimitive('adc', (node, wire_output) => ({
node: node,
pin: 0,
output: [wire_output]
}), (o, method) => {
let node = o.node;
node.always(`${o.output[0].reg}=adc.read(${o.pin});`);
});
wiot.newPrimitive('http', (node, wire_statusCode, wire_res, wire_url, wire_body = new wiot.wire(`''`), header = 'nil', method = 'GET') => ({
node: node,
header: header,
method: method,
output: [wire_statusCode, wire_res],
input: [wire_url, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input, `http.request(${o.input[0].reg},'${o.method}','${o.header}',${o.input[1].reg},function(c,d)${o.output[0].reg}=c;${o.output[1].reg}=d;end);`);
});
wiot.newPrimitive('redis', (node, host, channel, wire_res, wire_send, port = 6379) => ({
node: node,
host: host,
port: port,
channel: channel,
output: [wire_res],
input: [wire_send]
}), (o, method) => {
let node = o.node;
let vpub = method.Reg(),
vsub = method.Reg();
node.setReg(vpub);
node.setReg(vsub);
node.prepare(`${vpub}=node.LFS.redis().connect('${o.host}',${o.port});`);
node.prepare(`${vsub}=node.LFS.redis().connect('${o.host}',${o.port});`);
node.finale(`${vsub}:subscribe('${o.channel}',function(c,m)${o.output[0].reg}=m;end);`);
node.trigger(o.input, `${vpub}:publish('${o.channel}',tostring(${o.input[0].reg}));`);
});
wiot.newPrimitive('udp', (node, wire_res_ip, wire_res_port, wire_res_data, wire_send_ip, wire_send_port, wire_send_data, localport = parseInt(Math.random()*20000)+10000) => ({
node: node,
localport: localport,
output: [wire_res_ip, wire_res_port, wire_res_data],
input: [wire_send_ip, wire_send_port, wire_send_data]
}), (o, method) => {
let node = o.node;
let v = method.Reg()
node.setReg(v);
node.prepare(`${v}=net.createUDPSocket();`);
node.prepare(`${v}:listen(${o.localport});`);
node.trigger(o.input[2], `${v}:send(${o.input[1].reg},${o.input[0].reg},tostring(${o.input[2].reg}));`);
node.finale(`${v}:on('receive',function(s,d,p,i)${o.output[2].reg}=d;${o.output[1].reg}=p;${o.output[0].reg}=i;end);`);
});
wiot.newPrimitive('tcp', (node, host, port, wire_res_data, wire_send_data) => ({
node: node,
host: host,
port: port,
output: [wire_res_data],
input: [wire_send_data]
}), (o, method) => {
let node = o.node;
let v = method.Reg()
let status = method.Reg();
node.setReg(v);
node.setReg(status, false);
node.prepare(`${v}=net.createConnection(net.TCP,0);`);
node.finale(`${v}:connect(${o.port},'${o.host}');`);
node.trigger(o.input[0], `if ${status} then ${v}:send(tostring(${o.input[0].reg}));end;`);
node.prepare(`${v}:on('receive',function(s,d)${o.output[0].reg}=d;end);`);
node.prepare(`${v}:on('connection',function()${status}=true;end);`);
node.prepare(`${v}:on('disconnection',function()${status}=false;tmr.create():alarm(1000,tmr.ALARM_SINGLE,function()${v}:connect(${o.port},'${o.host}');end);end);`);
});
wiot.newPrimitive('webpage', (node, key, template, wires_output_map = {}, wires_input_map = {}, interval_s = 0.4, host = 'http://wiot-webpage.yimian.xyz/') => ({
node: node,
host: host,
key: key,
interval_s: interval_s,
template: template,
output: Object.keys(wires_output_map).map(id => wires_output_map[id]),
input: Object.keys(wires_input_map).map(id => wires_input_map[id]),
wires_output_map: wires_output_map,
wires_input_map: wires_input_map
}), (o, method) => {
let node = o.node;
let url = `${o.host}${(o.host.substring(o.host.length-1)=='/'?'':'/')}SET?key=${o.key}`;
url += '&template='+JSON.stringify(o.template);
got(url, {
responseType: 'json'
});
let url2 = `${o.host}${(o.host.substring(o.host.length-1)=='/'?'':'/')}SYNC?key=${o.key}`;
Object.keys(o.wires_input_map).forEach(id => {
url2 += `&${id}='..${o.wires_input_map[id].reg}..'`;
});
let s = '';
Object.keys(o.wires_output_map).forEach(id => {
s += `${o.wires_output_map[id].reg}=obj['${id}'];`;
});
node.prepare(`tmr.create():alarm(${o.interval_s*1000},tmr.ALARM_AUTO,function()http.request('${url2}','GET','','',function(c,d)if c==200 then local st,obj=pcall(sjson.decode,d);if st then ${s}end;end;end);end)`);
});
wiot.newPrimitive('bigiot', (node, DeviceID, APIkey, wire_output) => ({
node: node,
DeviceID: DeviceID,
APIkey: APIkey,
output: [wire_output]
}), (o, method) => {
let node = o.node;
let obj = method.Reg()
node.setReg(obj, '{}');
node.always(`${o.output[0].reg}=${obj}.reg;`);
node.finale(`node.LFS.bigiot()('${o.DeviceID}','${o.APIkey}',${obj});`);
});
wiot.newPrimitive('mail', (node, wire_en, wire_to, wire_subject, wire_body, from = 'wiot') => ({
node: node,
from: from,
input: [wire_en, wire_to, wire_subject, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input[0], `if ${o.input[0].reg}==1 then http.request('http://api.yimian.xyz/mail/?to='..tostring(${o.input[1].reg})..'&subject='..tostring(${o.input[2].reg})..'&body='..tostring(${o.input[3].reg})..'&from=${o.from}','GET','','',function(c,d)end);end;`);
});
wiot.newPrimitive('memobird', (node, AppKey, UserID, memobirdID, wire_en, wire_body) => ({
node: node,
ak: AppKey,
uid: UserID,
memobirdID: memobirdID,
input: [wire_en, wire_body]
}), (o, method) => {
let node = o.node;
node.trigger(o.input[0], `if ${o.input[0].reg}==1 then http.request('http://api.yimian.xyz/gugu/?ak=${o.ak}&userID=${o.uid}&memobirdID=${o.memobirdID}&body='..tostring(${o.input[1].reg}),'GET','','',function(c,d)end);end;`);
});
wiot.newOperator('if', (condition, ifTrue, ifFalse) => {
return `((${condition})and{${ifTrue}}or{${ifFalse}})[1]`;
});
/*
wiot.newOperator('strIndexOf', (str, segment) => {
return `string.find(${str},${segment})`;
});
wiot.newOperator('strSubStr', (str, pos, length) => {
return `string.sub(${str},${pos}${length?`,${length}`:``})`;
});
*/
wiot.newModule('breathing', (node, period_s, wire_output) => {
let w1 = new wiot.wire(0),
w2 = new wiot.wire(10);
wiot.operate(node, `math.abs($1%2047-1023)`, wire_output, w2);
wiot.operate(node, `($1+math.floor(2048*0.01/${period_s}))%2048`, w2, w1);
wiot.buffer(node, w1, w2, .01, true);
});
module.exports = wiot;

@ -1,93 +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}`)
})
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));

@ -0,0 +1 @@
Subproject commit ea08544224469767221154aa9560a77d678ff8af

Binary file not shown.

Binary file not shown.

@ -9,6 +9,7 @@ local F = function(DEVICEID, APIKEY, ctl_wire_obj)
isConnect = true
local r = sjson.decode(c)
if r.M == 'say' then
ctl_wire_obj.trigger = tmr.time();
ctl_wire_obj.reg = r.C;
end
end)

Loading…
Cancel
Save