2017-03-22 05:35:45 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const rt = require('../ResTool');
|
|
|
|
const config = require('../../config.json');
|
2017-03-28 08:34:50 +00:00
|
|
|
const mysql = require('../../libs/mysql_pool');
|
2017-03-22 05:35:45 +00:00
|
|
|
const tool = require('../../includes/apiTool');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const so = require('../../includes/storeObject');
|
|
|
|
|
|
|
|
router
|
|
|
|
.get('/', (req, res) => {
|
|
|
|
res.send({ name: 'WebIO Modbus API' });
|
|
|
|
})
|
2017-04-06 08:28:57 +00:00
|
|
|
.post('*', async(req, res, n) => {
|
|
|
|
try {
|
2017-03-28 08:34:50 +00:00
|
|
|
res.db = await mysql.getConn();
|
2017-04-06 08:28:57 +00:00
|
|
|
} catch (e) {
|
2017-03-28 08:34:50 +00:00
|
|
|
console.log(`Get DB Connection ERROR ${e}`);
|
|
|
|
return n('ERR8100');
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
2017-03-22 05:35:45 +00:00
|
|
|
.post('/getmodbuslist', (req, res, n) => {
|
|
|
|
if (!config.permission.modbus) return n('ERR9000');
|
|
|
|
|
|
|
|
let query = "select * from ??.?? order by `uid` desc";
|
|
|
|
let param = [config.db.db5, 'device'];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
let query = "select * from ??.?? where `uid` = ?";
|
|
|
|
let param = [config.db.db5, 'device', arr.data.id];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
})
|
2017-04-06 08:28:57 +00:00
|
|
|
.catch(err => rt.err(res, err, n, 'ERR8000'));
|
2017-03-22 05:35:45 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.post('/getporttype', (req, res, n) => {
|
|
|
|
if (!config.permission.modbus) return n('ERR9000');
|
|
|
|
|
|
|
|
let query = "select * from ??.?? order by `uid` ";
|
|
|
|
let param = [config.db.db5, 'porttype'];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
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');
|
|
|
|
|
2017-03-28 05:53:16 +00:00
|
|
|
let type = arr.data.type && isFinite(arr.data.type) ? arr.data.type : 0;
|
|
|
|
|
2017-03-22 05:35:45 +00:00
|
|
|
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;
|
|
|
|
|
2017-03-28 05:53:16 +00:00
|
|
|
let query = "insert into ??.?? (`name`, `node`, `type`, `adduser`, `moduser`, `ctime`, `mtime`) values (?,?,?,?,?, unix_timestamp(), unix_timestamp())";
|
|
|
|
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, type, u, u];
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8001');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2017-03-28 05:53:16 +00:00
|
|
|
let type = arr.data.type && isFinite(arr.data.type) ? arr.data.type : 0;
|
|
|
|
|
2017-03-22 05:35:45 +00:00
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
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` = ?, \
|
2017-03-28 05:53:16 +00:00
|
|
|
`type` = ?, \
|
2017-03-22 05:35:45 +00:00
|
|
|
`moduser` = ?, \
|
|
|
|
`mtime` = unix_timestamp() \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
2017-03-28 05:53:16 +00:00
|
|
|
let param = [config.db.db5, 'device', arr.data.name, arr.data.node, type, u, arr.data.id];
|
2017-03-22 05:35:45 +00:00
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8002');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2017-04-06 08:28:57 +00:00
|
|
|
res.db.query(`use ${config.db.db5}`);
|
|
|
|
|
2017-03-22 05:35:45 +00:00
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8003');
|
2017-03-22 05:35:45 +00:00
|
|
|
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');
|
|
|
|
|
2017-03-27 05:18:31 +00:00
|
|
|
let query = "select rt.*, a.`name` as name from ??.?? rt \
|
2017-03-22 05:35:45 +00:00
|
|
|
left join ??.?? d \
|
|
|
|
on d.`node` = rt.`node` \
|
|
|
|
left join ??.?? i \
|
|
|
|
on i.`devuid` = d.`uid` and i.`type` = ? \
|
2017-03-27 05:18:31 +00:00
|
|
|
left join ??.?? a \
|
|
|
|
on a.`iouid` = i.`uid` \
|
|
|
|
and a.`portnum` = rt.`port`\
|
2017-03-22 05:35:45 +00:00
|
|
|
where \
|
|
|
|
d.`uid` = ? \
|
|
|
|
and rt.`type` = ? \
|
2017-03-28 05:53:16 +00:00
|
|
|
and i.`uid` is not null \
|
2017-03-29 10:02:51 +00:00
|
|
|
and ( a.`name` is not null or rt.`type` in (1,2)) \
|
2017-03-28 05:53:16 +00:00
|
|
|
order by rt.`port` asc";
|
2017-03-27 05:18:31 +00:00
|
|
|
let param = [config.db.db6, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', arr.data.type, config.db.db5, 'aioset', arr.data.id, arr.data.type];
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(row)
|
|
|
|
};
|
|
|
|
return n();
|
|
|
|
});
|
|
|
|
})
|
2017-04-12 06:26:01 +00:00
|
|
|
.post('/getalliostatus', (req, res, n) => {
|
|
|
|
if (!config.permission.modbus) return n('ERR9000');
|
|
|
|
|
|
|
|
let pros = [];
|
|
|
|
|
|
|
|
let query = "select rt.*, a.`name` as name \
|
|
|
|
from ??.?? rt \
|
|
|
|
left join ??.?? d \
|
|
|
|
on d.`node` = rt.`node` \
|
|
|
|
left join ??.?? i \
|
|
|
|
on i.`devuid` = d.`uid` \
|
|
|
|
and i.`type` = rt.`type` \
|
|
|
|
left join ??.?? a \
|
|
|
|
on a.`iouid` = i.`uid` \
|
|
|
|
and a.`portnum` = rt.`port`\
|
|
|
|
where \
|
|
|
|
i.`uid` is not null \
|
|
|
|
and ( a.`name` is not null or rt.`type` in (1,2)) \
|
|
|
|
order by rt.`type` asc, rt.`port` asc";
|
|
|
|
let param = [config.db.db6, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', config.db.db5, 'aioset'];
|
|
|
|
|
|
|
|
pros.push(tool.promiseQuery(res, query, param, 'record'));
|
|
|
|
|
|
|
|
let rtq = "select * from ??.?? ";
|
|
|
|
let rtp = [config.db.db5, 'device'];
|
|
|
|
|
|
|
|
pros.push(tool.promiseQuery(res, rtq, rtp, 'rt'));
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: [],
|
|
|
|
rt: {
|
|
|
|
device: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all(pros)
|
|
|
|
.then(d => {
|
|
|
|
for (let i in d) {
|
|
|
|
if (d[i].key == 'record') {
|
|
|
|
res.api_res.record = tool.checkArray(d[i].data);
|
|
|
|
}
|
|
|
|
if (d[i].key == 'rt') {
|
|
|
|
res.api_res.rt.device = tool.checkArray(d[i].data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
|
|
|
.catch(err => rt.err(res, err, n, 'ERR8000'));
|
|
|
|
})
|
2017-03-22 05:35:45 +00:00
|
|
|
.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');
|
|
|
|
|
|
|
|
let query = "select * from ??.?? where `uid` = ? ";
|
|
|
|
let param = [config.db.db5, 'iolist', arr.data.id];
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2017-03-28 05:53:16 +00:00
|
|
|
let dlen = arr.data.datalen || 2;
|
|
|
|
|
2017-03-22 05:35:45 +00:00
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
if (row[0].c > 0) return n('ERR0054');
|
|
|
|
|
2017-03-31 01:19:16 +00:00
|
|
|
let query = "insert into ??.?? (`devuid`,`type`,`addr`,`num`,`datalen`,`ctime`,`mtime`) values (?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
|
2017-03-28 05:53:16 +00:00
|
|
|
let param = [config.db.db5, 'iolist', arr.data.id, arr.data.type, arr.data.addr, arr.data.num, dlen];
|
2017-03-22 05:35:45 +00:00
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8001');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2017-03-28 05:53:16 +00:00
|
|
|
let dlen = arr.data.datalen || 2;
|
|
|
|
|
2017-03-27 08:26:58 +00:00
|
|
|
let query = "select count(*) as c from ??.?? i \
|
2017-03-22 05:35:45 +00:00
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
if (row[0].c > 0) return n('ERR0054');
|
|
|
|
|
|
|
|
let q = "update ??.?? set \
|
|
|
|
`addr` = ?, \
|
|
|
|
`num` = ?, \
|
2017-03-28 05:53:16 +00:00
|
|
|
`datalen` = ?, \
|
2017-03-22 05:35:45 +00:00
|
|
|
`mtime` = unix_timestamp() \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
2017-03-28 05:53:16 +00:00
|
|
|
let p = [config.db.db5, 'iolist', arr.data.addr, arr.data.num, dlen, arr.data.id];
|
2017-03-22 05:35:45 +00:00
|
|
|
res.db.query(q, p, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8002');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2017-03-31 01:19:16 +00:00
|
|
|
res.db.query(`use ${config.db.db5}`);
|
|
|
|
|
2017-03-22 05:35:45 +00:00
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8003');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
let query = "select * from ??.?? where `uid` = ?";
|
|
|
|
let param = [config.db.db5, 'aioset', arr.data.id];
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
let query = "delete from ??.?? where `uid` = ?";
|
|
|
|
let param = [config.db.db5, 'aioset', arr.data.id];
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8003');
|
2017-03-22 05:35:45 +00:00
|
|
|
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');
|
2017-03-31 01:19:16 +00:00
|
|
|
if (!('type' in arr.data)) return n('ERR0009');
|
2017-03-22 05:35:45 +00:00
|
|
|
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');
|
|
|
|
|
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
if (row[0].count > 0) return n('ERR0054');
|
|
|
|
|
2017-03-31 01:19:16 +00:00
|
|
|
let q = "insert into ??.?? (`iouid`, `name`, `portnum`, `type`, `range_min`, `range_max`, `scale_min`, `scale_max`, `ctime`, `mtime`) values \
|
2017-03-31 07:35:30 +00:00
|
|
|
(?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
|
2017-03-31 01:19:16 +00:00
|
|
|
let p = [config.db.db5, 'aioset', arr.data.iouid, arr.data.name, arr.data.portnum, arr.data.type, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max];
|
2017-03-22 05:35:45 +00:00
|
|
|
res.db.query(q, p, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8001');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
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');
|
2017-03-31 01:19:16 +00:00
|
|
|
if (!('portnum' in arr.data)) return n('ERR0048');
|
|
|
|
if (!('type' in arr.data)) return n('ERR0009');
|
2017-03-22 05:35:45 +00:00
|
|
|
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');
|
|
|
|
|
|
|
|
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) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
2017-03-22 05:35:45 +00:00
|
|
|
if (row[0].length > 0) return n('ERR0054');
|
|
|
|
|
|
|
|
let query = "update ??.?? set \
|
|
|
|
`name` = ?, \
|
|
|
|
`portnum` = ?, \
|
2017-03-31 01:19:16 +00:00
|
|
|
`type` = ?, \
|
2017-03-22 05:35:45 +00:00
|
|
|
`range_min` = ?, \
|
|
|
|
`range_max` = ?, \
|
|
|
|
`scale_min` = ?, \
|
|
|
|
`scale_max` = ?, \
|
|
|
|
`mtime` = unix_timestamp() \
|
|
|
|
where `uid` = ?";
|
2017-03-31 01:19:16 +00:00
|
|
|
let param = [config.db.db5, 'aioset', arr.data.name, arr.data.portnum, arr.data.type, arr.data.range_min, arr.data.range_max, arr.data.scale_min, arr.data.scale_max, arr.data.id];
|
2017-03-22 05:35:45 +00:00
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8002');
|
2017-03-22 05:35:45 +00:00
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
};
|
|
|
|
return n();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
2017-04-06 08:28:57 +00:00
|
|
|
.post('/modbuscmd', (req, res, n) => {
|
2017-03-27 02:07:41 +00:00
|
|
|
if (!config.permission.modbus) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
|
|
|
let arr = req.body;
|
|
|
|
if (!arr.data) return n('ERR0000');
|
2017-04-06 08:28:57 +00:00
|
|
|
if (!arr.data.ip) return n('ERR0010');
|
|
|
|
if (!('node' in arr.data) || arr.data.node.length == 0) return n('ERR0038');
|
|
|
|
if (!('func' in arr.data) || arr.data.func.length == 0) return n('ERR0055');
|
|
|
|
if (!('addr' in arr.data) || arr.data.addr.length == 0) return n('ERR0048');
|
|
|
|
if (!('value' in arr.data) || arr.data.value.length == 0) return n('ERR0049');
|
2017-03-27 02:07:41 +00:00
|
|
|
|
|
|
|
let cmd = `mbtcpm ${arr.data.ip} ${arr.data.node} ${arr.data.func} ${arr.data.addr} ${arr.data.value}`;
|
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
};
|
|
|
|
exec(cmd, (err, stdout, stderr) => {
|
2017-04-06 08:28:57 +00:00
|
|
|
let msg = err ? err.toString() : `${stdout}${stderr}`;
|
|
|
|
res.api_res.record.push({ msg });
|
2017-03-27 02:07:41 +00:00
|
|
|
|
|
|
|
n();
|
|
|
|
})
|
|
|
|
})
|
2017-04-06 08:28:57 +00:00
|
|
|
.post('/getmodbuslog', (req, res, n) => {
|
2017-03-29 07:06:50 +00:00
|
|
|
if (!config.permission.modbus) return n('ERR9000');
|
|
|
|
let arr = req.body;
|
2017-04-06 08:28:57 +00:00
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.ids || !Array.isArray(arr.data.ids)) return n('ERR0028');
|
|
|
|
if (!arr.data.stime || !arr.data.etime) return n('ERR0015');
|
2017-03-29 07:06:50 +00:00
|
|
|
|
|
|
|
let query = "select log.`node`, log.`type`, log.`port`, log.`value2` as value, log.`tst`, dev.`name` as devname, aio.`name` as aioname \
|
|
|
|
from ??.?? log \
|
|
|
|
left join ??.?? dev \
|
|
|
|
on dev.`node` = log.`node`\
|
|
|
|
left join ??.?? io \
|
|
|
|
on io.`devuid` = dev.`uid` \
|
|
|
|
and io.`type` = log.`type` \
|
|
|
|
left join ??.?? aio \
|
|
|
|
on aio.`iouid` = io.`uid` \
|
|
|
|
and aio.`portnum` = log.`port` \
|
|
|
|
where \
|
|
|
|
dev.`uid` in (?) \
|
|
|
|
and ( log.`tst` >= ? \
|
|
|
|
and log.`tst` <= ? ) \
|
2017-03-31 01:19:16 +00:00
|
|
|
and io.`uid` is not null \
|
|
|
|
and ( aio.`name` is not null or log.`type` in (1,2)) \
|
2017-03-29 07:06:50 +00:00
|
|
|
order by log.`uid` desc, log.`tst` desc \
|
|
|
|
limit 0, 100";
|
|
|
|
let param = [config.db.db10, 'jcmbrt', config.db.db5, 'device', config.db.db5, 'iolist', config.db.db5, 'aioset', arr.data.ids, arr.data.stime, arr.data.etime];
|
|
|
|
|
2017-04-06 08:28:57 +00:00
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-29 07:06:50 +00:00
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(row)
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
|
|
|
})
|
2017-04-18 06:16:58 +00:00
|
|
|
.post('/getmodbustype', (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');
|
|
|
|
|
|
|
|
let query = "select io.`type` \
|
|
|
|
from ??.?? io \
|
|
|
|
left join ??.?? dev \
|
|
|
|
on dev.`uid` = io.`devuid` \
|
|
|
|
where \
|
|
|
|
dev.`uid` = ? \
|
|
|
|
group by io.`type` \
|
|
|
|
order by io.`type` ";
|
|
|
|
let param = [config.db.db5, 'iolist', config.db.db5, 'device', arr.data.id];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
n();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.post('/getmodbusport', (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');
|
|
|
|
|
|
|
|
let type = arr.data.type;
|
|
|
|
if (type == 5) type = 1;
|
|
|
|
if (type == 6) type = 3;
|
|
|
|
|
|
|
|
if (type == 3 || type == 4) {
|
|
|
|
let query = "select a.`name`, a.`portnum` \
|
|
|
|
from ??.?? a \
|
|
|
|
left join ??.?? io \
|
|
|
|
on io.`uid` = a.`iouid` \
|
|
|
|
left join ??.?? dev \
|
|
|
|
on dev.`uid` = io.`devuid` \
|
|
|
|
where \
|
|
|
|
io.`type` = ? \
|
|
|
|
and dev.`uid` = ? ";
|
|
|
|
let param = [config.db.db5, 'aioset', config.db.db5, 'iolist', config.db.db5, 'device', type, arr.data.id];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
n();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 1 || type == 2) {
|
|
|
|
let query = "select * \
|
|
|
|
from ??.?? io \
|
|
|
|
left join ??.?? dev \
|
|
|
|
on dev.`uid` = io.`devuid` \
|
|
|
|
where \
|
|
|
|
dev.`uid` = ? \
|
|
|
|
and io.`type` = ?";
|
|
|
|
let param = [config.db.db5, 'iolist', config.db.db5, 'device', arr.data.id, type];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
|
|
|
let arr = [];
|
|
|
|
let num = 0;
|
|
|
|
for (let i in row) {
|
|
|
|
num += parseInt(row[i].num);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < num; i++) {
|
|
|
|
arr.push({ portnum: (i + 1), name: '' })
|
|
|
|
}
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: arr
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n('ERR0009');
|
|
|
|
})
|
2017-03-22 05:35:45 +00:00
|
|
|
.all('*', rt.send);
|
|
|
|
|
|
|
|
module.exports = router;
|