re commit

This commit is contained in:
Jay
2017-03-22 13:35:45 +08:00
commit 8947715671
109 changed files with 161369 additions and 0 deletions
+199
View File
@@ -0,0 +1,199 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO DIO Api' });
})
.post('/getio', (req, res, n) => {
if (!config.permission.dio) return n('ERR9000');
res.api_res = {
record: [],
rt: {
di: {},
do: {}
}
};
let pdi = new Promise((resolve, reject) => {
let id = 1;
let dis = {};
! function dichk(i) {
if (i > 8) return resolve({ data: dis, key: 'di' });
dis[i] = 0;
exec(`ditchk ${i}`, (err, sout, serr) => {
if (err) return dichk(++i);
dis[i] = sout.replace(/\n/, '');
dichk(++i);
});
}(id)
});
let pdo = new Promise((resolve, reject) => {
let id = 1;
let dos = {};
! function dochk(i) {
if (i > 8) return resolve({ data: dos, key: 'do' });
dos[i] = 0;
exec(`dotchk ${i}`, (err, sout, serr) => {
if (err) return dochk(++i);
dos[i] = sout.replace(/\n/, '');
dochk(++i);
});
}(id)
});
Promise.all([pdi, pdo])
.then(r => {
for (let i in r) {
if (r[i].key == 'di') {
res.api_res.rt.di = r[i].data;
} else if (r[i].key == 'do') {
res.api_res.rt.do = r[i].data;
}
}
return n();
})
.catch(e => n());
})
.post('/dotrun', (req, res, n) => {
if (!config.permission.dio) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.pin) return n('ERR0001');
if (!('value' in arr.data)) return n('ERR0002');
exec(`dotrun ${arr.data.pin} ${arr.data.value}`, (err, sout, serr) => {
res.api_res = {
record: []
};
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let obj = so.get(req.headers['x-auth-token']);
let u = '';
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
let query = "select * from ??.?? where `douid` = ?";
let param = [config.db.db1, 'dolist', arr.data.pin];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n();
let q = "insert into ??.?? (`eventtag`, `iolabel`, `ioname`, `iosetting1`, `iosetting2`, `iosetting3`, `ioevent`, `ioeventtst`) values ";
let p = [config.db.db2, 'jciocert'];
let doStat = sout.replace(/\n/, '') == 1 ? 1 : 0;
let str = `('WEB', 'DO${row[0].douid}', '${row[0].doname}', '${row[0].dologic}', '${doStat}', '${u}', '${doStat}', unix_timestamp())`;
res.db.query(q + str, p, (err, row) => n());
});
});
})
.post('/getdioinfo', (req, res, n) => {
if (!config.permission.dio) return n('ERR9000');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let q = "select * from ??.??";
let pdi = tool.promiseQuery(res, q, [config.db.db1, 'dilist'], 'di');
let pdo = tool.promiseQuery(res, q, [config.db.db1, 'dolist'], 'do');
res.api_res = {
record:[],
rt: {
di: [],
do: []
}
};
Promise.all([pdi,pdo])
.then(r => {
for(let i in r){
if(r[i].key == 'di') res.api_res.rt.di = r[i].data;
else if(r[i].key == 'do') res.api_res.rt.do = r[i].data;
}
return n();
})
.catch(e => n('ERR8000'));
})
.post('/setdioinfo', (req,res,n) => {
if(!config.permission.dio) return n('ERR9000');
// if(!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if(!arr.data) return n('ERR0000');
if(!arr.data.di || typeof (arr.data.di) != 'object' || Object.keys(arr.data.di).length == 0) return n('ERR0005');
if(!arr.data.do || typeof (arr.data.do) != 'object' || Object.keys(arr.data.do).length == 0) return n('ERR0006');
res.api_res = {
record: []
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "update ??.?? set ?? = unix_timestamp() , ";
let sub = " ?? = case ?? ";
let dos = [];
let dis = [];
let di_logic = "";
let di_name = "";
let do_logic = "";
let do_name = "";
let where = " where ?? in (?)";
for(let i=1; i<=8; i++){
dos.push(`do${i}`);
dis.push(`di${i}`);
if(arr.data.do[`do${i}`] && 'logic' in arr.data.do[`do${i}`]){
do_logic += ` when 'do${i}' then ${arr.data.do[`do${i}`].logic == 1 ? 1 : 0} `;
}
if(arr.data.do[`do${i}`] && 'name' in arr.data.do[`do${i}`]){
do_name += ` when 'do${i}' then ${res.db.escape(arr.data.do[`do${i}`].name)} `;
}
if(arr.data.di[`di${i}`] && 'logic' in arr.data.di[`di${i}`]){
di_logic += ` when 'di${i}' then ${arr.data.di[`di${i}`].logic == 1 ? 1 : 0} `;
}
if(arr.data.di[`di${i}`] && 'name' in arr.data.di[`di${i}`]){
di_name += ` when 'di${i}' then ${res.db.escape(arr.data.di[`di${i}`].name)} `;
}
}
do_logic += " end ";
do_name += " end ";
di_logic += " end ";
di_name += " end ";
let diq = `${query} ${sub} ${di_logic} , ${sub} ${di_name} ${where}`;
let doq = `${query} ${sub} ${do_logic} , ${sub} ${do_name} ${where}`;
let pdi = tool.promiseQuery(res, diq, [config.db.db1, 'dilist', 'di_modify_date', 'dilogic', 'diid', 'diname', 'diid', 'diid', dis]);
let pdo = tool.promiseQuery(res, doq, [config.db.db1, 'dolist', 'do_modify_date', 'dologic', 'doid', 'doname', 'doid', 'doid', dos]);
Promise.all([pdi, pdo])
.then(r => n())
.catch(e => n('ERR8002'))
})
.all('*', rt.send);
module.exports = router;
+35
View File
@@ -0,0 +1,35 @@
const express = require('express');
const router = express.Router();
const errMng = require('../../includes/errorManager');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO API System' });
})
.use('/system', require('./system.js'))
.use('/log', require('./log.js'))
.use('/leone', require('./leone.js'))
.use('/iogroup', require('./iogroup.js'))
.use('/iocmd', require('./iocmd.js'))
.use('/schedule', require('./schedule.js'))
.use('/dio', require('./dio.js'))
.use('/link', require('./link.js'))
.use('/modbus', require('./modbus.js'));
// api error handler
router.use((err, req, res, n) => {
if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close();
let lngs = req.headers['accept-language'].split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
}
res.send({
errorCode: (typeof err != 'string' ? err.toString() : err),
message: errMng(err, lng),
status: 0
});
})
module.exports = router;
+188
View File
@@ -0,0 +1,188 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const so = require('../../includes/storeObject');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO IOCmd API' });
})
.post('/iocmd', (req, res, n) => {
if (!config.permission.iocmd) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.cmd) return n('ERR0030');
if (!arr.data.devs) return n('ERR0029');
let cmds = arr.data.cmd.split(' ');
if (cmds.length != 2) return n('ERR0030');
if (cmds[0] == 2 && !(cmds[1] > 16 && cmds[1] < 30)) return n('ERR0031');
let cmd = `echo '${cmds[0]} ${cmds[1]} ${arr.data.devs}' > ${config.cmdpath.iocmd}`;
exec(cmd, (err, sout, serr) => {
let data = {};
data.record = [];
res.api_res = data;
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let dos = [];
let les = [];
let ios = [];
let devs = arr.data.devs.split(',');
let regex_do = /^do([0-9]+)$/;
let regex_le = /^le([0-9]+)$/;
let regex_io = /^iogroup([0-9]+)$/;
for (let i in devs) {
let s = devs[i];
if (!s.trim()) continue;
if (regex_do.test(s)) {
dos.push(s.replace(regex_do, '$1'));
} else if (regex_le.test(s)) {
les.push(s.replace(regex_le, '$1'));
} else if (regex_io.test(s)) {
ios.push(s.replace(regex_io, '$1'));
}
}
let obj = so.get(req.headers['x-auth-token']);
let u = '';
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
let pros = [];
if (dos.length > 0) {
let query = "select * from ??.?? where `douid` in (?)";
let param = [config.db.db1, 'dolist', dos];
pros.push(tool.promiseQuery(res, query, param, 'do'));
}
if (les.length > 0) {
let query = "select * from ??.?? where `leonelistuid` in (?)";
let param = [config.db.db1, 'leonelist', les];
pros.push(tool.promiseQuery(res, query, param, 'leone'));
}
if (ios.length > 0) {
let query = "select * from ??.?? where `iogroupuid` in (?)";
let param = [config.db.db1, 'iogroup', ios];
pros.push(tool.promiseQuery(res, query, param, 'iogroup'));
}
Promise.all(pros)
.then(r => {
let c = r.length;
let q = "insert into ??.?? (`eventtag`, `iolabel`, `ioname`, `iosetting1`, `iosetting2`, `iosetting3`, `ioevent`, `ioeventtst`) values ";
let p = [config.db.db2, 'jciocert'];
// for (let i in r) {
! function runLog(json) {
if (!json) return;
if (json.key == 'do') {
let d = json.data;
let qs = [];
for (let j in d) {
let doStat = execSync(`dotchk ${d[j].douid}`) == 1 ? 'HIGH' : 'LOW';
let str = `('WEB', 'DO${d[j].douid}', '${d[j].doname}', '${d[j].dologic}', '${doStat}', '${u}', '${doStat}', unix_timestamp())`;
qs.push(str);
}
tool.promiseQuery(res, q + qs.join(','), p)
.then(r => {
c--;
})
.catch(err => {
c--;
});
runLog(r.pop());
} else if (json.key == 'leone') {
let d = json.data;
let qs = [];
let mm = tool.getMode(req);
let act = tool.getCmd(req);
tool.getLeoneRT((rts) => {
for (let j in d) {
let st = '';
for (var k in rts) {
if (rts[k].ip == d[j].leoneip) {
for (let o in mm) {
if (rts[k].mode == mm[o].mode) {
st = mm[o].name;
break;
}
}
break;
}
}
let cn = '';
for (let k in act) {
if (act[k].cmd == `${cmds[0]} ${cmds[1]}`) {
cn = act[k].name;
break;
} else if (act[k].cmd == cmds[0]) {
cn = act[k].name;
break;
}
}
let str = `('WEB', 'LeOne', '${d[j].leonename}', '-', '${st}', '${u}', '${cn}', unix_timestamp())`;
qs.push(str);
}
tool.promiseQuery(res, q + qs.join(','), p)
.then(r => {
c--;
})
.catch(err => {
c--;
});
runLog(r.pop());
});
} else if (json.key == 'iogroup') {
let d = json.data;
let qs = [];
let act = tool.getCmd(req);
for (let j in d) {
let cn = '';
for (let k in act) {
if (act[k].cmd == `${cmds[0]} ${cmds[1]}`) {
cn = act[k].name;
break;
} else if (act[k].cmd == cmds[0]) {
cn = act[k].name;
break;
}
}
let str = `('WEB', 'IOGroup', '${d[j].iogroupname}', '-', '-', '${u}', '${cn}', unix_timestamp())`;
qs.push(str);
}
tool.promiseQuery(res, q + qs.join(','), p)
.then(r => {
c--;
})
.catch(err => {
c--;
});
runLog(r.pop());
}
}(r.pop())
! function chkFin() {
if (c <= 0) return n();
setTimeout(chkFin, 1000);
}()
})
.catch(err => {
return n();
});
});
})
.all('*', rt.send);
module.exports = router;
+183
View File
@@ -0,0 +1,183 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO IOGroup API' });
})
.post(['/getiogrouplist', '/getiogroup'], (req, res, n) => {
if (!config.permission.iogroup) return n('ERR9000');
let s = false;
let arr = req.body;
if (req.url == '/getiogroup') {
s = true;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select * from ??.?? ";
let order = " order by `iogroupuid` desc ";
let param = [config.db.db1, 'iogroup'];
if (s) {
query += " where `iogroupuid` = ? ";
param.push(arr.data.id);
}
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
let dos = [];
let les = [];
for (var i in row) {
let str = row[i].iogroupid;
let tmp = str.split(',');
if (tmp.length == 0) continue;
for (var j in tmp) {
if (/^do([0-9]+)$/.test(tmp[j])) {
let n = tmp[j].replace(/^do([0-9]+)$/, '$1');
dos.push(n);
} else if (/^le([0-9]+)$/.test(tmp[j])) {
let n = tmp[j].replace(/^le([0-9]+)$/, '$1');
les.push(n);
}
}
}
let doarr = [];
let leonearr = [];
let pros = [];
if (dos.length > 0) {
let query = "select * from ??.?? where `douid` in (?)";
let param = [config.db.db1, 'dolist', dos];
pros.push(tool.promiseQuery(res, query, param, 'do'));
}
if (les.length > 0) {
let query = "select * from ??.?? where `leonelistuid` in (?)";
let param = [config.db.db1, 'leonelist', les];
pros.push(tool.promiseQuery(res, query, param, 'leone'));
}
Promise.all(pros)
.then(r => {
for (var i in r) {
if (r[i].key == 'do') doarr = r[i].data;
else if (r[i].key == 'leone') leonearr = r[i].data;
}
let data = {};
data.record = tool.checkArray(row);
data.rt = {};
data.rt.do = tool.checkArray(doarr);
data.rt.leone = tool.checkArray(leonearr);
res.api_res = data;
return n();
})
.catch(err => {
if (err) return n('ERR8000');
});
});
})
.post('/addiogroup', (req, res, n) => {
if (!config.permission.iogroup) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
if (!arr.data.devs) return n('ERR0029');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "insert into ??.?? (`iogroupname`,`iogroupid`,`iogroup_add_date`) values (?, ?, unix_timestamp())";
let param = [config.db.db1, 'iogroup', arr.data.name, arr.data.devs];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/editiogroup', (req, res, n) => {
if (!config.permission.iogroup) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
if (!arr.data.devs) return n('ERR0029');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "update ??.?? set \
`iogroupname` = ?, \
`iogroupid` = ?, \
`iogroup_modify_date` = unix_timestamp() \
where \
`iogroupuid` = ?";
let param = [config.db.db1, 'iogroup', arr.data.name, arr.data.devs, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/deliogroup', (req, res, n) => {
if (!config.permission.iogroup) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "delete from ??.?? where `iogroupuid` = ? ";
let param = [config.db.db1, 'iogroup', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.all('*', rt.send);
module.exports = router;
+284
View File
@@ -0,0 +1,284 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
router
.get('/', (req, res) => {
res.send({name: 'WebIO Leone API'});
})
.post('/scanleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.ip || !/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/.test(arr.data.ip)) return n('ERR0010');
if (!arr.data.password) return n('ERR0017');
let ips = arr.data.ip.trim().split('.');
for (var i in ips) {
if (ips[i] < 0 || ips[i] > 255) return n('ERR0025');
}
if (fs.existsSync(config.cmdpath.scanleone)) {
fs.unlinkSync(config.cmdpath.scanleone);
}
if (fs.existsSync(config.cmdpath.scanleone_end)) {
fs.unlinkSync(config.cmdpath.scanleone_end);
}
let cmd = `echo '${arr.data.ip} ${arr.data.password}' > ${config.cmdpath.scanleone}`;
exec(cmd, (err, sout, serr) => {
if (err) return n('ERR7000');
! function chkEnd() {
fs.exists(config.cmdpath.scanleone_end, exists => {
if (exists) {
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select `leonename`, `leoneip`, `leonelistuid` from ??.?? where `temporary` = '1' ";
let param = [config.db.db1, 'leonelist'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
let data = {};
data.record = tool.checkArray(row);
res.api_res = data;
return n();
});
}else{
setTimeout(chkEnd, 1000);
}
});
}()
});
})
.post(['/getleonelist', '/getleone'], (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
let s = false;
let arr = req.body;
if (req.url == '/getleone') {
s = true;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select * from ??.?? where `temporary` = '0' ";
let param = [config.db.db1, 'leonelist'];
if (s) {
query += " and `leonelistuid` = ? ";
param.push(arr.data.id);
}
let order = " order by `leonelistuid` desc ";
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
tool.getLeoneRT(rts => {
let data = {};
data.record = tool.checkArray(row);
data.rt = {};
data.rt.status = tool.checkArray(rts);
res.api_res = data;
return n();
});
});
})
.post('/addleone', (req, res, n) => {
let arr = req.body;
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
if (!arr.data.ip) return n('ERR0010');
if (!arr.data.password) return n('ERR0017');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select count(*) as num from ??.?? where `temporary` = '0' ";
let param = [config.db.db1, 'leonelist'];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].num >= config.leone_limit) return n('ERR0048');
let query = "select count(*) from ??.?? where `leoneip` = ? ";
let p = [...param, arr.data.ip];
res.db.query(query, p, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].num > 0) return n('ERR0027');
let query = "insert into ??.?? (`leoneip`,`leonename`,`leonepassword`,`leone_add_date`,`leone_modify_date`) values (?,?,?,unix_timestamp(),unix_timestamp())";
let p = [...param, arr.data.ip, arr.data.name, arr.data.password];
res.db.query(query, p, (err, row) => {
if (err) return n('ERR8001');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
});
})
})
.post('/delleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "delete from ??.?? where `leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/editleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n("ERR0026");
if (!arr.data.password) return n('ERR0017');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "update ??.?? set \
`leonename` = ?,\
`leonepassword` = ?,\
`leone_modify_date` = unix_timestamp() \
where \
`leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.name, arr.data.password, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
let data = {};
data.record = [];
res.api_res = data;
let query = "select * from ??.?? where `leonelistuid` = ? ";
let param = [config.db.db1, 'leonelist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n();
if (row.length == 0) return n();
let { leoneip, leonename, leonepassword } = row[0];
let cmd = `sledn ${leonepassword} ${leoneip} "${leonename}"`;
exec(cmd, (err, sout, serr) => {
return n();
});
});
});
})
.post('/addscanleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id || !Array.isArray(arr.data.id)) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let ids = [];
for (var i in arr.data.id) {
let t = arr.data.id[i];
if (typeof t == 'string' && t.length == 0) continue;
ids.push(t);
}
if (ids.length == 0) {
let data = {};
data.record = [];
res.api_res = data;
return n();
}
let query = "select count(*) as num from ??.?? where `temporary` = '0' or `leonelistuid` in (?)";
let param = [config.db.db1, 'leonelist', ids];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].num >= config.leone_limit) return n('ERR0048');
let query = "update ??.?? set `temporary` = '0', `leone_modify_date` = unix_timestamp() where `leonelistuid` in (?)";
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
});
})
.post('/clearscanleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "delete from ??.?? where `temporary` = '1'";
let param = [config.db.db1, 'leonelist'];
res.db.query(query, param, (err, row) => {
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.all('*', rt.send);
module.exports = router;
+287
View File
@@ -0,0 +1,287 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Link API' });
})
.post('/getlinklist', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db8;
res.db.connect();
let query = "select * from ??.?? \
where \
`jcioclntuid` not in ( \
select n2.jcioclntuid \
from ??.?? n1 \
left join \
??.?? n2\
on \
n1.`lnlcid1` = concat('ln', n2.`jcioclntuid`) \
or n1.`lnlcid2` = concat('ln', n2.`jcioclntuid`) \
where n2.`jcioclntuid` is not null \
)";
let param = [config.db.db8, 'jcioclnt', config.db.db8, 'jcioclnt', config.db.db8, 'jcioclnt'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/getlink', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db8;
res.db.connect();
res.api_res = {
record: [{ root: arr.data.id }],
rt: {
ln: [],
lc: []
}
}
let query = "select ??.??(?) as ids";
let param = [config.db.db8, 'getLink', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
let ids = row[0].ids;
let id = ids.split(',');
id.splice(0, 1);
let ida = id.filter(t => {
if (t != '') return t;
});
let pros = [];
let lnq = "select * from ??.?? where `jcioclntuid` in (?)";
let lnp = [config.db.db8, 'jcioclnt', ida];
pros.push(tool.promiseQuery(res, lnq, lnp, 'ln'));
let lcq = "select c.* from ??.?? n \
left join ??.?? c \
on \
c.`jcioclctuid` = substr(n.`lnlcid1`, 3) \
or c.`jcioclctuid` = substr(n.`lnlcid2`, 3) \
where \
n.`jcioclntuid` in ( ? ) \
and ( \
substr(n.`lnlcid1`, 1,2) = 'lc' \
or \
substr(n.`lnlcid2`, 2,2) = 'lc' \
) \
and c.`jcioclctuid` is not null \
group by c.`jcioclctuid`";
let lcp = [config.db.db8, 'jcioclnt', config.db.db7, 'jcioclct', ida];
pros.push(tool.promiseQuery(res, lcq, lcp, 'lc'));
Promise.all(pros)
.then(r => {
for (let i in r) {
if (r[i].key == 'lc') res.api_res.rt.lc = r[i].data;
if (r[i].key == 'ln') res.api_res.rt.ln = r[i].data;
}
return n();
})
.catch(err => n('ERR8000'));
});
})
.post('/addlink', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.link || Object.keys(arr.data.link).length == 0) return n('ERR0049');
if (!('active' in arr.data)) return n('ERR0032');
if (!arr.data.action) return n('ERR0030');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db8;
res.db.connect();
! function runLoop(unit, cb) {
if (typeof unit != 'object' || Object.keys(unit).length == 0) return cb(0);
if (unit.type == 'ln') {
let p1 = new Promise((resolve, reject) => {
runLoop(unit.id1, id => {
resolve({ id, key: 'id1' });
});
});
let p2 = new Promise((resolve, reject) => {
runLoop(unit.id2, id => {
resolve({ id, key: 'id2' });
});
});
Promise.all([p1, p2])
.then(r => {
let id1 = 0;
let id2 = 0;
for (let i in r) {
if (r[i].key == 'id1') id1 = r[i].id;
if (r[i].key == 'id2') id2 = r[i].id;
}
let q = "insert into ??.?? \
(`lnname`, `lnlcid1`, `lnlcid2`, `lnlcop`, `lnactive`, `lnaddtst`, `lnmodtst`) values (?,?,?,?,'1',unix_timestamp(),unix_timestamp())";
let p = [config.db.db8, 'jcioclnt', unit.name, id1, id2, unit.op];
res.db.query(q, p, (err, row) => {
if (err) return cb(0);
return cb(`ln${row.insertId}`);
});
})
.catch(err => {
return cb(0);
});
} else if (unit.type == 'lc') {
let q = "insert into ??.?? \
(`lcname`, `lcioid`, `lciovalue`, `lcioop`, `lcactive`, `lcaddtst`, `lcmodtst`) values ('-', ?, ?, ?, '1', unix_timestamp(), unix_timestamp())";
let p = [config.db.db7, 'jcioclct', unit.id, unit.value, unit.op];
res.db.query(q, p, (err, row) => {
if (err) return cb(0);
return cb(`lc${row.insertId}`);
});
} else {
return cb(0);
}
}(arr.data.link, (id) => {
if (id == 0) return n('ERR8001');
if (typeof id != 'string') id = id.toString();
let query = "update ??.?? set \
`lnactive` = ?, \
`lnaction` = ? \
where \
`jcioclntuid` = ?";
let param = [config.db.db8, 'jcioclnt', (arr.data.active == 1 ? 1 : 0), arr.data.action, id.substr(2)];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
});
})
.post('/swlinkactive', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db8;
res.db.connect();
let query = "update ??.?? set \
`lnactive` = (case when `lnactive` = 1 then 0 else 1 end), \
`lnmodtst` = unix_timestamp() \
where \
`jcioclntuid` = ?";
let param = [config.db.db8, 'jcioclnt', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
})
.post('/dellink', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db8;
res.db.connect();
let query = "select ??.??(?) as ids";
let param = [config.db.db8, 'getLink', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
let ids = row[0].ids;
let id = ids.split(',');
id.splice(0, 1);
let ida = id.filter(t => {
if (t != '') return t;
});
let query = "delete c,n from ??.?? n \
left join ??.?? c \
on \
( \
c.`jcioclctuid` = substr(n.`lnlcid1`, 3) \
or c.`jcioclctuid` = substr(n.`lnlcid2`, 3)\
) \
and ( \
substr(n.`lnlcid1`, 1,2) = 'lc' \
or substr(n.`lnlcid2` ,1,2) = 'lc'\
) \
where \
n.`jcioclntuid` in (?)";
let param = [config.db.db8, 'jcioclnt', config.db.db7, 'jcioclct', ida];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
res.api_res = {
record: []
};
return n();
});
});
})
.all('*', rt.send);
module.exports = router;
+46
View File
@@ -0,0 +1,46 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Log API' })
})
.post('/getlog', (req, res, n) => {
if(!config.permission.log) return n('ERR9000');
let arr = req.body;
let p = arr.data && arr.data.p && isFinite(arr.data.p) && arr.data.p > 0 ? arr.data.p : 1;
let per = arr.data && arr.data.perpage && isFinite(arr.data.perpage) && arr.data.perpage > 0 ? arr.data.perpage : config.perpage;
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db2;
res.db.connect();
let query = "select count(*) as num from ??.??";
let param = [config.db.db2, 'jciocert'];
res.db.query(query, param, (err, row) => {
if(err || row.length == 0) return n('ERR0023');
let page = res.db.recordPage(row[0].num, p, per);
let query = "select * from ??.?? order by `ioeventtst` desc ";
let limit = ` limit ${page.rec_start} , ${per} `;
res.db.query(query + limit, param, (err, row) => {
if(err) return n('ERR8000');
let data = {};
data.page = page;
data.record = tool.checkArray(row);
res.api_res = data;
return n();
});
})
})
.all('*', rt.send);
module.exports = router;
+588
View File
@@ -0,0 +1,588 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Modbus API' });
})
.post('/getmodbuslist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select * from ??.?? order by `uid` desc";
let param = [config.db.db5, 'device'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
})
})
.post('/getmodbus', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
// if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select * from ??.?? where `uid` = ?";
let param = [config.db.db5, 'device', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row),
rt: {
iolist: [],
aioset: []
}
};
let pros = [];
let iq = "select * from ??.?? where `devuid` = ?";
let ip = [config.db.db5, 'iolist', row[0].uid];
pros.push(tool.promiseQuery(res, iq, ip, 'iolist'));
let aq = "select a.* from ??.?? a\
left join ??.?? i\
on \
a.`iouid`= i.`uid` \
where \
i.`devuid` = ?";
let ap = [config.db.db5, 'aioset', config.db.db5, 'iolist', row[0].uid];
pros.push(tool.promiseQuery(res, aq, ap, 'aioset'));
Promise.all(pros)
.then(r => {
for (let i in r) {
if (r[i].key == 'iolist') res.api_res.rt.iolist = r[i].data;
if (r[i].key == 'aioset') res.api_res.rt.aioset = r[i].data;
}
return n();
})
.then(err => n('ERR8000'));
});
})
.post('/getporttype', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select * from ??.?? order by `uid` ";
let param = [config.db.db5, 'porttype'];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/addmodbus', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
if (!('node' in arr.data)) return n('ERR0038');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let u = '';
let obj = so.get(req.headers['x-auth-token']);
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
let query = "insert into ??.?? (`name`, `node`, `adduser`, `moduser`, `ctime`, `mtime`) values (?,?,?,?, unix_timestamp(), unix_timestamp())";
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, u, u];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
res.api_res = {
record: []
};
return n();
});
})
.post('/editmodbus', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
if (!('node' in arr.data) || !('original_node' in arr.data)) return n('ERR0038');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select count(*) as num from ??.?? where `node` = ? and `uid` != ?";
let param = [config.db.db5, 'device', arr.data.node, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].num > 0) return n('ERR0047');
let u = '';
let obj = so.get(req.headers['x-auth-token']);
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
let query = "update ??.?? set \
`name` = ?, \
`node` = ?, \
`moduser` = ?, \
`mtime` = unix_timestamp() \
where \
`uid` = ?";
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, u, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
let q = "delete from ??.?? where `node` in (?)";
let p = [config.db.db6, 'jcmbrt', [arr.data.node, arr.data.original_node]];
res.db.query(q, p, (err, row) => {
res.api_res = {
record: []
};
return n();
});
})
})
})
.post('/delmodbus', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "delete d,i,s,rt from ??.?? d \
left join ??.?? i \
on d.`uid` = i.`devuid` \
left join ??.?? s \
on i.`uid` = s.`iouid` \
left join ??.?? rt \
on d.`node` = rt.`node` \
where \
d.`uid` = ?";
let param = [config.db.db5, 'device', config.db.db5, 'iolist', config.db.db5, 'aioset', config.db.db6, 'jcmbrt', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
res.api_res = {
record: []
};
return n();
});
})
.post('/getiostatus', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.type) return n('ERR0009');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select rt.* from ??.?? rt \
left join ??.?? d \
on d.`node` = rt.`node` \
left join ??.?? i \
on i.`devuid` = d.`uid` and i.`type` = ? \
where \
d.`uid` = ? \
and rt.`type` = ? \
and i.`uid` is not null ";
let param = [config.db.db6, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', arr.data.type, arr.data.id, arr.data.type];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/getiolist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select * from ??.?? where `uid` = ? ";
let param = [config.db.db5, 'iolist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/addiolist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.type) return n('ERR0009');
if (!arr.data.addr) return n('ERR0048');
if (!arr.data.num) return n('ERR0049');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select count(*) as c from ??.?? \
where \
`devuid` = ? \
and `type` = ? \
and `addr` = ?";
let param = [config.db.db5, 'iolist', arr.data.id, arr.data.type, arr.data.addr];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].c > 0) return n('ERR0054');
let query = "insert into ??.?? (`devuid`,`type`,`addr`,`num`,`ctime`,`mtime`) values (?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
let param = [config.db.db5, 'iolist', arr.data.id, arr.data.type, arr.data.addr, arr.data.num];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
res.api_res = {
record: []
};
return n();
});
});
})
.post('/editiolist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.addr) return n('ERR0048');
if (!arr.data.num) return n('ERR0049');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select count(*) sa c from ??.?? i \
left join ??.?? i2 \
on i2.`type` = i.`type` \
and i2.`addr` = ? \
and i2.`devuid` = i.`devuid` \
and i2.`uid` != ? \
where \
i.`uid` = ? \
and i2.`uid` is not null ";
let param = [config.db.db5, 'iolist', config.db.db5, 'iolist', arr.data.addr, arr.data.id, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].c > 0) return n('ERR0054');
let q = "update ??.?? set \
`addr` = ?, \
`num` = ?, \
`mtime` = unix_timestamp() \
where \
`uid` = ?";
let p = [config.db.db5, 'iolist', arr.data.addr, arr.data.num, arr.data.id];
res.db.query(q, p, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
});
})
.post('/deliolist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "delete i, a from ??.?? i \
left join ??.?? a \
on a.`iouid` = i.`uid` \
where \
i.`uid` = ?";
let param = [config.db.db5, 'iolist', config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
res.api_res = {
record: []
};
return n();
});
})
.post('/getaiosetlist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select a.* from ??.?? a \
left join ??.?? i \
on a.`iouid` = i.`uid` \
where \
i.`uid` = ?";
let param = [config.db.db5, 'aioset', config.db.db5, 'iolist', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/getaioset', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select * from ??.?? where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row)
};
return n();
});
})
.post('/delaioset', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
res.api_res = {
record: []
};
return n();
});
})
.post('/addaioset', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.iouid) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
if (!('portnum' in arr.data)) return n('ERR0048');
if (!('range_min' in arr.data)) return n('ERR0050');
if (!('range_max' in arr.data)) return n('ERR0051');
if (!('scale_min' in arr.data)) return n('ERR0052');
if (!('scale_max' in arr.data)) return n('ERR0053');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select count(*) as count from ??.?? where `iouid` = ? and `portnum` = ?";
let param = [config.db.db5, 'aioset', arr.data.iouid, arr.data.portnum];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].count > 0) return n('ERR0054');
let q = "insert into ??.?? (`iouid`, `name`, `portnum`, `range_min`, `range_max`, `scale_min`, `scale_max`, `ctime`, `mtime`) values \
(?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
let p = [config.db.db5, 'aioset', arr.data.iouid, arr.data.name, arr.data.portnum, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max];
res.db.query(q, p, (err, row) => {
if (err) return n('ERR8001');
res.api_res = {
record: []
};
return n();
});
});
})
.post('/editaioset', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.iouid) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
if (!arr.data.portnum) return n('ERR0048');
if (!('range_min' in arr.data)) return n('ERR0050');
if (!('range_max' in arr.data)) return n('ERR0051');
if (!('scale_min' in arr.data)) return n('ERR0052');
if (!('scale_max' in arr.data)) return n('ERR0053');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db5;
res.db.connect();
let query = "select count(*) as count from ??.?? \
where \
`iouid` = ? \
and `portnum` = ? \
and `uid` != ?";
let param = [config.db.db5, 'aioset', arr.data.iouid, arr.data.portnum, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return n('ERR8000');
if (row[0].length > 0) return n('ERR0054');
let query = "update ??.?? set \
`name` = ?, \
`portnum` = ?, \
`range_min` = ?, \
`range_max` = ?, \
`scale_min` = ?, \
`scale_max` = ?, \
`mtime` = unix_timestamp() \
where `uid` = ?";
let param = [config.db.db5, 'aioset', arr.data.name, arr.data.portnum, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
});
})
.all('*', rt.send);
module.exports = router;
+301
View File
@@ -0,0 +1,301 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Schedule API' });
})
.post(['/getschedulelist', '/getschedule'], (req, res, n) => {
if (!config.permission.schedule) return n('ERR9000');
let s = false;
let arr = req.body;
if (req.url == '/getschedule') {
s = true;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select * from ??.?? ";
let order = " order by `ioscheduleuid` desc ";
let param = [config.db.db1, 'ioschedulet'];
if (s) {
query += " where `ioschedulet` = ? ";
param.push(arr.data.id);
}
res.db.query(query + order, param, (err, row) => {
if (err) return n('ERR8000');
res.api_res = {
record: tool.checkArray(row),
rt: {
'do': [],
'leone': [],
'iogroup': []
}
};
let dos = [];
let les = [];
let ios = [];
let regex = {
do: /^do([0-9]+)$/,
leone: /^leone([0-9]+)$/,
iogroup: /^iogroup([0-9]+)$/
}
for (let i in row) {
let str = row[i].ioscheduleio;
let t = str.split(',');
if (t.length == 0) continue;
for (let j in t) {
if (regex.do.test(t[j])) {
dos.push(t[j].replace(regex.do, '$1'));
} else if (regex.leone.test(t[j])) {
les.push(t[j].replace(regex.leone, '$1'));
} else if (regex.iogroup.test(t[j])) {
ios.push(t[j].replace(regex.iogroup, '$1'));
}
}
}
let pros = [];
if (dos.length > 0) {
let query = "select * from ??.?? where `douid` in (?)";
let param = [config.db.db1, 'dolist', dos];
pros.push(tool.promiseQuery(res, query, param, 'do'));
}
if (les.length > 0) {
let query = "select * from ??.?? where `leonelistuid` in (?)";
let param = [config.db.db1, 'leonelist', les];
pros.push(tool.promiseQuery(res, query, param, 'leone'));
}
if (ios.length > 0) {
let query = "select * from ??.?? where `iogroupuid` in (?)";
let param = [config.db.db1, 'iogroup', ios];
pros.push(tool.promiseQuery(res, query, param, 'iogroup'));
}
Promise.all(pros)
.then(r => {
for (let i in r) {
if (r[i].key == 'do') {
res.api_res.rt.do = r[i].data;
} else if (r[i].key == 'leone') {
res.api_res.rt.leone = r[i].data;
} else if (r[i].key == 'iogroup') {
res.api_res.rt.iogroup = r[i].data;
}
}
return n();
})
.catch(err => {
return n();
})
});
})
.post('/delschedule', (req, res, n) => {
if (!config.permission.schedule) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "delete from ??.?? where `ioscheduleuid` = ? ";
let param = [config.db.db1, 'ioschedulet', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8003');
res.api_res = {
record: []
};
return n();
});
})
.post('/swschedule', (req, res, n) => {
if (!config.permission.schedule) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
// let active = arr.data.active == 1 ? 1 : 0;
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "update ??.?? set `ioscheduleactive` = (case when `ioscheduleactive` = '1' then '0' else '1' end) where `ioscheduleuid` = ? ";
let param = [config.db.db1, 'ioschedulet', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
})
.post('/addschedule', (req, res, n) => {
if (!config.permission.schedule) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
if (!('active' in arr.data)) return n('ERR0032');
if (!arr.data.devs) return n('ERR0029');
if (!arr.data.action) return n('ERR0030');
if (!('min' in arr.data) || !isFinite(arr.data.min) || !(arr.data.min >= 0 && arr.data.min < 60)) return n('ERR0033');
if (!('hour' in arr.data) || !isFinite(arr.data.hour) || !(arr.data.hour >= 0 && arr.data.hour < 24)) return n('ERR0034');
if (!arr.data.week) {
if (!arr.data.day || !isFinite(arr.data.day) || !(arr.data.day > 0 && arr.data.day <= 31)) return n('ERR0035');
if (!arr.data.month || !isFinite(arr.data.month) || !(arr.data.month > 0 && arr.data.month <= 12)) return n('ERR0036');
}
let tmp = arr.data.action.split(',');
if (tmp.length != 2) return n('ERR0030');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let active = arr.data.active == 1 ? 1 : 0;
let d = '';
let m = '';
let w = '';
if (arr.data.week) {
w = arr.data.week;
d = '-';
m = '-';
} else {
w = '-';
d = arr.data.day;
m = arr.data.month;
}
let query = "insert into ??.?? (\
`ioschedulename`, \
`ioscheduleactive`, \
`ioscheduleio`, \
`ioschedulecmd`, \
`ioscheduleparam1`, \
`ioscheduleparam2`, \
`ioscheduleparam3`, \
`ioscheduleparam4`, \
`ioscheduleparam5`, \
`ioschedule_add_date`) values (?,?,?,?,?,?,?,?,?,unix_timestamp())";
let param = [config.db.db1, 'ioschedulet', arr.data.name, active, arr.data.devs, arr.data.action, arr.data.min, arr.data.hour, d, m, w];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8001');
res.api_res = {
record: []
};
return n();
});
})
.post('/editschedule', (req,res,n) => {
if(!config.permission.schedule) return n('ERR9000');
if(!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if(!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
if (!('active' in arr.data)) return n('ERR0032');
if (!arr.data.devs) return n('ERR0029');
if (!arr.data.action) return n('ERR0030');
if (!('min' in arr.data) || !isFinite(arr.data.min) || !(arr.data.min >= 0 && arr.data.min < 60)) return n('ERR0033');
if (!('hour' in arr.data) || !isFinite(arr.data.hour) || !(arr.data.hour >= 0 && arr.data.hour < 24)) return n('ERR0034');
if (!arr.data.week) {
if (!arr.data.day || !isFinite(arr.data.day) || !(arr.data.day > 0 && arr.data.day <= 31)) return n('ERR0035');
if (!arr.data.month || !isFinite(arr.data.month) || !(arr.data.month > 0 && arr.data.month <= 12)) return n('ERR0036');
}
let tmp = arr.data.action.split(',');
if (tmp.length != 2) return n('ERR0030');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let active = arr.data.active == 1 ? 1 : 0;
let d = '';
let m = '';
let w = '';
if (arr.data.week) {
w = arr.data.week;
d = '-';
m = '-';
} else {
w = '-';
d = arr.data.day;
m = arr.data.month;
}
let query = "update ??.?? set \
`ioschedulename` = ? , \
`ioscheduleactive` = ? , \
`ioscheduleio` = ? , \
`ioschedulecmd` = ? , \
`ioscheduleparam1` = ? , \
`ioscheduleparam2` = ? , \
`ioscheduleparam3` = ? , \
`ioscheduleparam4` = ? , \
`ioscheduleparam5` = ? , \
`ioschedule_add_date` = unix_timestamp() \
where \
`ioscheduleuid` = ?";
let param = [config.db.db1, 'ioschedulet', arr.data.name, active, arr.data.devs, arr.data.action, arr.data.min, arr.data.hour, d, m, w, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8002');
res.api_res = {
record: []
};
return n();
});
})
.all('*', rt.send);
module.exports = router;
+452
View File
@@ -0,0 +1,452 @@
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res, n) => {
// res.db = new mysql();
// res.db.user = config.db.user;
// res.db.password = config.db.pass;
// res.db.host = config.db.host;
// res.db.port = config.db.port;
// res.db.database = config.db.db1;
// res.db.connect();
res.send({ name: 'WebIO System API' });
})
.post('/getnetwork', (req, res, n) => {
fs.exists(config.cmdpath.sysinfo, (exists) => {
if (!exists) return n('ERR0014');
fs.readFile(config.cmdpath.sysinfo, (err, d) => {
if (err) return n('ERR0014');
let str = d.toString().split(/\n/);
let arr = {};
for (var i in str) {
if (!str[i].trim()) continue;
let t = str[i].split(' ');
if (t.langth < 2) continue;
arr[t[0]] = t[1];
}
let data = {};
data.record = [arr];
res.api_res = data;
return n();
});
});
})
.post('/updatenetwork', (req, res, n) => {
let arr = req.body;
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.type) return n('ERR0009');
if (arr.data.type == 'manual') {
if (!arr.data.ip) return n('ERR0010');
if (!arr.data.netmask) return n('ERR0011');
if (!arr.data.gateway) return n('ERR0012');
if (!arr.data.dns) return n('ERR0013');
}
let cmd = '';
if (arr.data.type == 'manual') {
cmd = `echo "${arr.data.ip}" "${arr.data.gateway}" "${arr.data.netmask}" "${arr.data.dns}" > ${config.cmdpath.manualip}`;
} else {
cmd = `touch ${config.cmdpath.dhcpip}`;
}
if (cmd.length > 0) {
exec(cmd, (err, sout, serr) => {
let data = {};
data.record = [];
res.api_res = data;
return n();
});
}
})
.post('/gettime', (req, res, n) => {
let cmd = 'date +%s';
exec(cmd, (err, sout, serr) => {
let time = parseInt(sout);
let data = {};
data.record = [{ time }];
res.api_res = data;
return n();
});
})
.post('/updatetime', (req, res, n) => {
let arr = req.body;
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.time || !/^[0-9]{12}$/.test(arr.data.time)) return n('ERR0015');
let cmd = `echo "${arr.data.time}" > ${config.cmdpath.settime}`;
exec(cmd, (err, sout, serr) => {
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/login', (req, res, n) => {
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.account) return n('ERR0016');
if (!arr.data.password) return n('ERR0017');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select * from ??.?? where `account` = ? and `user_password` = ?";
res.db.query(query, [config.db.db1, 'userlist', arr.data.account, arr.data.password], (err, row) => {
if (err) return n('ERR8000');
if (row.length == 0) return n('ERR0019');
delete row[0]['user_password'];
let token = '';
while (true) {
token = crypt.random(15);
if (!so.chkKey(token)) break;
}
so.set(token, { user: row[0] });
let data = {};
data.record = row;
data.rt = {}
data.rt.permission = [];
let tmp = {};
for(let i in config.permission) {
if(config.permission[i]){
tmp[i] = true;
}
}
data.rt.permission.push(tmp);
data.token = token;
res.api_res = data;
return n();
});
})
.post('/logout', (req, res, n) => {
let token = req.headers['x-auth-token'];
if (token) {
so.del(token);
}
let data = {};
data.record = [];
res.api_res = data;
return n();
})
.post(['/getuserlist', '/getuser'], (req, res, n) => {
let s = false;
let arr = req.body;
if (req.url == '/getuser') {
s = true;
if (!arr.data) return n('ERR0000');
if (!arr.data.account) return n('ERR0016');
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "select * from ??.??";
let param = [config.db.db1, 'userlist']
if (s) {
query += " where `account` = ?";
param.push(arr.data.account);
}
res.db.query(query, param, (err, row) => {
if (err) return n('ERR8000');
for (var i in row) {
delete row[i]['user_password'];
}
let data = {};
data.record = row;
res.api_res = data;
return n();
})
})
.post('/deluser', (req, res, n) => {
let arr = req.body;
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.account) return n('ERR0016');
if (arr.data.account == 'admin') return n('ERR0037');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "delete from ??.?? where `account` = ?";
let param = [config.db.db1, 'userlist', arr.data.account];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0020');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/edituser', (req, res, n) => {
let arr = req.body;
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.account) return n('ERR0016');
let w = arr.data.write_privilege && arr.data.write_privilege == '1' ? 1 : 0;
let r = arr.data.read_privilege && arr.data.read_privilege == '1' ? 1 : 0;
let pass = typeof arr.data.password == 'string' && arr.data.password.length > 0 ? arr.data.password : '';
if(arr.data.account == 'admin') {
w = 1;
r = 1;
}
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "update ??.?? set `write_privilege` = ? , `read_privilege` = ? " +
(pass.length > 0 ? " , `user_password` = ? " : "") + " where `account` = ? ";
let param = [config.db.db1, 'userlist', w.toString(), r.toString()];
if (pass.length > 0) param.push(pass);
param.push(arr.data.account);
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0021');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/adduser', (req, res, n) => {
let arr = req.body;
if (!tool.checkPermission(req)) return n('ERR9000');
if (!arr.data) return n('ERR0000');
if (!arr.data.account) return n('ERR0016');
if (!arr.data.password) return n('ERR0017');
let w = arr.data.write_privilege && arr.data.write_privilege == '1' ? 1 : 0;
let r = arr.data.read_privilege && arr.data.read_privilege == '1' ? 1 : 0;
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let query = "insert into ??.?? (`account`,`user_password`,`write_privilege`,`read_privilege`,`user_add_date`) values (?,?,?,?,unix_timestamp())";
let param = [config.db.db1, 'userlist', arr.data.account, arr.data.password, w.toString(), r.toString()];
res.db.query(query, param, (err, row) => {
if (err) return n('ERR0022');
let data = {};
data.record = [];
res.api_res = data;
return n();
});
})
.post('/dashboard', (req, res, n) => {
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
let data = {
record: [],
rt: {}
};
data.rt['time'] = [{
time: Date.now()
}];
res.api_res = data;
let pros = [];
pros.push(new Promise((resolve, reject) => {
fs.exists(config.cmdpath.sysinfo, exists => {
if (!exists) return resolve({ data: [], key: 'sysinfo' });
fs.readFile(config.cmdpath.sysinfo, (err, d) => {
if (err) return resolve({ data: [], key: 'sysinfo' });
let s = d.toString();
let tmp = s.split(/\n/);
for (let i in tmp) {
if (!tmp[i].trim()) continue;
let tt = tmp[i].split(' ');
if (tt.length > 1 && /^ip$/i.test(tt[0])) {
return resolve({ data: [{ ip: tt[1] }], key: 'sysinfo' });
}
}
});
});
}));
pros.push(new Promise((resolve, reject) => {
fs.exists(config.cmdpath.version, exists => {
if (!exists) return resolve({ data: [], key: 'version' });
fs.readFile(config.cmdpath.version, (err, d) => {
if (err) return resolve({ data: [], key: 'version' });
return resolve({ data: [{ version: d.toString().replace(/\n/, '') }], key: 'version' });
});
});
}));
if (config.permission.dio) {
pros.push(new Promise((resolve, reject) => {
let q = "select `diname`, `diid`, `diuid` from ??.?? ";
let p = [config.db.db1, 'dilist'];
res.db.query(q, p, (err, row) => {
if (err) return resolve({ data: [], key: 'di' });
let c = row.length;
let td = [];
! function chkdi(json) {
if (!json) return;
exec(`ditchk ${json.diid.replace(/^di([0-9]+)$/, '$1')}`, (err, sout, serr) => {
if (err) {
chkdi(row.pop());
if (!--c) return resolve({ data: td, key: 'di' });
return;
}
if (sout == 1) td.push(json);
chkdi(row.pop());
if (!--c) return resolve({ data: td, key: 'di' });
return;
});
}(row.pop());
});
}));
}
if (config.permission.leone) {
pros.push(new Promise((resolve, reject) => {
tool.getLeoneRT(rts => {
let ips = []
for (let i in rts) {
if (rts[i].mode == '9999') {
ips.push(rts[i].ip);
}
}
let q = "select * from ??.?? where `leoneip` in (?) order by `leonelistuid` desc ";
let p = [config.db.db1, 'leonelist', ips];
res.db.query(q, p, (err, row) => {
if (err) return resolve({ data: [], key: 'leone' });
return resolve({ data: row, key: 'leone' });
});
});
}));
}
Promise.all(pros)
.then(r => {
for (let i in r) {
if (r[i].key == 'di') {
data.rt.di = r[i].data;
} else if (r[i].key == 'leone') {
data.rt.leone = r[i].data;
} else if (r[i].key == 'sysinfo') {
data.rt.ip = r[i].data;
} else if (r[i].key == 'version') {
data.rt.version = r[i].data;
}
}
return n();
})
.catch(e => {
return n();
});
})
.post('/getselectlist', (req, res, n) => {
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.type) return n('ERR0009');
res.db = new mysql();
res.db.user = config.db.user;
res.db.password = config.db.pass;
res.db.host = config.db.host;
res.db.port = config.db.port;
res.db.database = config.db.db1;
res.db.connect();
res.api_res = {
record: []
};
let pro = null;
let q, p;
switch (arr.data.type) {
case 'do':
q = "select `doname` as name, `douid` as id from ??.??";
p = [config.db.db1, 'dolist'];
pro = tool.promiseQuery(res, q, p, '');
break;
case 'di':
q = "select `diname` as name, `diuid` as id from ??.??";
p = [config.db.db1, 'dilist'];
pro = tool.promiseQuery(res, q, p, '');
break;
case 'leone':
q = "select `leonename` as name, `leonelistuid` as id from ??.??";
p = [config.db.db1, 'leonelist'];
pro = tool.promiseQuery(res, q, p, '');
break;
case 'iogroup':
q = "select `iogroupname` as name, `iogroupuid` as id from ??.??";
p = [config.db.db1, 'iogroup'];
pro = tool.promiseQuery(res, q, p, '');
break;
default:
return n();
}
pro.then(r => {
if('data' in r) {
res.api_res.record = tool.checkArray(r.data);
}
return n();
}).catch(e => {
return n();
})
})
.all('*', rt.send);
module.exports = router;