webio-node/route/api/wristband.js

407 lines
14 KiB
JavaScript
Raw Normal View History

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_pool');
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 Wristband API' });
})
.get('/pushdata', async(req, res, n) => {
try {
res.db = await mysql.getConn();
} catch (e) {
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
let arr = req.query;
2017-04-05 07:10:02 +00:00
if (!arr.mac) return n('ERR0000');
if (!arr.devid) return n('ERR0000');
let query = "insert into ??.?? values (null, ?, ?,?,?,?,?,?,?,?,?,?,?,?,unix_timestamp())";
let param = [
config.db.db9,
'rawdata',
2017-03-29 10:02:51 +00:00
arr.devid || '',
2017-04-05 07:10:02 +00:00
arr.mac || '',
arr.val2 || '',
arr.val3 || '',
arr.val4 || '',
arr.val5 || '',
arr.val6 || '',
arr.val7 || '',
arr.val8 || '',
arr.val9 || '',
arr.val10 || '',
arr.val11 || '',
arr.timestamp || '',
];
res.db.query(query, param, (err, row) => {
2017-04-05 07:10:02 +00:00
if (err) return n('ERR8001');
res.api_res = {
record: []
};
let q = "select count(*) as c from ??.?? where `mac` = ?";
let p = [config.db.db9, 'wristband', arr.mac];
res.db.query(q, p, (err, row) => {
2017-04-05 07:10:02 +00:00
let pros = [];
if (!(err || row.length == 0 || row[0].c > 0)) {
2017-04-10 02:03:43 +00:00
let q = "insert into ??.?? (`mac`, `name`, `cuser`, `muser`, `ctime`, `mtime`) values (?, '', 'API', 'API', unix_timestamp(), unix_timestamp())";
2017-04-05 07:10:02 +00:00
let p = [config.db.db9, 'wristband', arr.mac];
pros.push(tool.promiseQuery(res, q, p, 'q1'));
}
2017-04-05 07:10:02 +00:00
let lq = "insert into ??.?? (`mac`, `devid`, `val2`, `val3`, `val4`, `val5`, `val6`, `val7`, `val8`, `val9`, `val10`, `val11`, `timestamp`, `ctime`, `mtime`) values \
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp()) ON DUPLICATE KEY \
update \
`devid` = ?, \
`val2` = ?, \
`val3` = ?, \
`val4` = ?, \
`val5` = ?, \
`val6` = ?, \
`val7` = ?, \
`val8` = ?, \
`val9` = ?, \
`val10` = ?, \
`val11` = ?, \
`timestamp` = ?, \
`mtime` = unix_timestamp()";
2017-04-05 07:10:02 +00:00
let lp = [config.db.db9, 'lastdata',
arr.mac,
arr.devid,
arr.val2 || '',
arr.val3 || '',
arr.val4 || '',
arr.val5 || '',
arr.val6 || '',
arr.val7 || '',
arr.val8 || '',
arr.val9 || '',
arr.val10 || '',
arr.val11 || '',
arr.timestamp || '',
arr.devid,
arr.val2 || '',
arr.val3 || '',
arr.val4 || '',
arr.val5 || '',
arr.val6 || '',
arr.val7 || '',
arr.val8 || '',
arr.val9 || '',
arr.val10 || '',
arr.val11 || '',
arr.timestamp || ''
]
2017-04-05 07:10:02 +00:00
pros.push(tool.promiseQuery(res, lq, lp, 'q2'));
2017-04-05 07:10:02 +00:00
Promise.all(pros)
.then(r => {
n()
})
.catch(err => {
console.log(err);
n();
})
})
// n();
})
})
.post('*', async(req, res, n) => {
try {
res.db = await mysql.getConn();
} catch (e) {
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
2017-04-05 07:10:02 +00:00
.post('/getstatus', (req, res, n) => {
2017-04-06 08:28:57 +00:00
if (!config.permission.wristband) return n('ERR9000');
2017-04-06 09:31:36 +00:00
let query = "select w.`name`, w.`mac` as wristband, coalesce(l.`name`, l2.`name`) as locname, ll.*\
from ??.?? w\
left join ( \
select * from ( \
select * from ??.?? \
where `timestamp` > unix_timestamp() - 30 order by conv(`val11`, 16, 10) desc limit 65535000\
) tmp group by `mac` \
) tmp2 \
on tmp2.`mac` = w.`mac` \
left join ??.?? ll \
on ll.`mac` = w.`mac` \
left join ??.?? l \
on \
l.`serialnumber` = tmp2.`devid` \
2017-04-05 08:45:58 +00:00
left join ??.?? l2\
on \
l2.`serialnumber` = ll.`devid` \
2017-04-06 09:31:36 +00:00
where \
w.`switch` = 1 ";
2017-04-05 07:10:02 +00:00
let param = [config.db.db9, 'wristband', config.db.db9, 'rawdata', config.db.db9, 'lastdata', config.db.db9, 'location', config.db.db9, 'location', ];
let sortfield = '';
let sortorder = '';
if (req.body.data && req.body.data.sort) {
if (req.body.data.sort.field) sortfield = req.body.data.sort.field;
if (req.body.data.sort.order) sortorder = req.body.data.sort.order;
}
2017-03-29 10:02:51 +00:00
2017-04-05 07:10:02 +00:00
switch (sortfield) {
case 'mac':
case 'val2':
case 'val3':
case 'val4':
case 'val5':
case 'val6':
case 'val7':
case 'val8':
case 'val9':
case 'val10':
case 'val11':
query += `order by ll.${sortfield} ${sortorder}`;
break;
2017-04-05 08:45:58 +00:00
case 'loc':
query += `order by locname ${sortorder}`;
break;
2017-04-06 08:28:57 +00:00
case 'time':
query += 'order by ll.`timestamp` ' + sortorder;
break;
2017-04-05 07:10:02 +00:00
default:
query += 'order by w.`uid`';
break;
}
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8000');
2017-03-29 10:02:51 +00:00
res.api_res = {
record: tool.checkArray(row)
}
n();
})
})
2017-04-06 08:28:57 +00:00
.post('/getwristbandlist', (req, res, n) => {
if (!config.permission.wristband) return n('ERR9000');
let query = "select * from ??.?? ";
let param = [config.db.db9, 'wristband'];
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('/delwristband', (req, res, n) => {
if (!config.permission.wristband) 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.query(`use ${config.db.db9}`);
let query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db9, 'wristband', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8003');
res.api_res = {
record: []
};
n();
});
})
.post('/addwristband', (req, res, n) => {
if (!config.permission.wristband) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000')
if (!arr.data.mac) return n('ERR0060');
2017-04-06 09:31:36 +00:00
let query = "select count(*) as c from ??.?? where `mac` = ?";
let param = [config.db.db9, 'wristband', arr.data.mac];
2017-04-06 08:28:57 +00:00
res.db.query(query, param, (err, row) => {
2017-04-06 09:31:36 +00:00
if (err || row.length == 0) return rt.err(res, err, n, "ERR8000");
if (row[0].c > 0) return n("ERR0061");
2017-04-06 08:28:57 +00:00
2017-04-06 09:31:36 +00:00
let name = arr.data.name || '';
let identity = arr.data.identity || 0;
let monitor = arr.data.monitor || 0;
let notify = arr.data.notify || 0;
let sw = arr.data.switch || 0;
2017-04-06 08:28:57 +00:00
2017-04-07 08:23:41 +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;
}
let query = "insert into ??.?? (`mac`, `name`, `identity`, `monitor`, `notify`, `switch`, `cuser`, `muser`, `ctime`, `mtime`) values \
( ?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp() )";
let param = [config.db.db9, 'wristband', arr.data.mac, name, identity, monitor, notify, sw, u, u];
2017-04-06 09:31:36 +00:00
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8001');
res.api_res = {
record: []
};
n();
});
})
2017-04-06 08:28:57 +00:00
})
.post('/editwristband', (req, res, n) => {
if (!config.permission.wristband) 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 name = arr.data.name || '';
let identity = arr.data.identity || 0;
let monitor = arr.data.monitor || 0;
let notify = arr.data.notify || 0;
let sw = arr.data.switch || 0;
2017-04-07 08:23:41 +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-04-06 08:28:57 +00:00
let query = "update ??.?? set \
`name` = ?, \
`identity` = ?, \
`monitor` = ?, \
`notify` = ?, \
`switch` = ?, \
2017-04-07 08:23:41 +00:00
`muser` = ?, \
2017-04-06 08:28:57 +00:00
`mtime` = unix_timestamp() \
where \
`uid` = ?";
2017-04-07 08:23:41 +00:00
let param = [config.db.db9, 'wristband', name, identity, monitor, notify, sw, u, arr.data.id];
2017-04-06 08:28:57 +00:00
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8002');
res.api_res = {
record: []
}
n();
})
})
2017-04-06 09:31:36 +00:00
.post('/getlocationlist', (req, res, n) => {
if (!config.permission.wristband) return n('ERR9000');
let query = "select * from ??.??";
let param = [config.db.db9, 'location'];
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('/addlocation', (req, res, n) => {
if (!config.permission.wristband) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
2017-04-06 10:17:26 +00:00
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.sn) return n("ERR0062");
if (!arr.data.name) return n('ERR0026');
let query = "select count(*) as c from ??.?? where `serialnumber` = ?";
let param = [config.db.db9, 'location', arr.data.sn];
res.db.query(query, param, (err, row) => {
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
if (row[0].c > 0) return n('ERR0063');
2017-04-07 08:23:41 +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;
}
let query = "insert into ??.?? (`name`, `serialnumber`, `cuser`, `ctime`, `muser`, `mtime`) values (?, ?, ?, unix_timestamp(), ?, unix_timestamp())";
let param = [config.db.db9, 'location', arr.data.name, arr.data.sn, u, u];
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8001');
res.api_res = {
record: []
}
n();
});
})
})
.post('/editlocation', (req, res, n) => {
if (!config.permission.wristband) 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');
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` = ?, \
`muser` = ?, \
`mtime` = unix_timestamp() \
where \
`uid` = ? ";
let param = [config.db.db9, 'location', arr.data.name, u, arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8002');
res.api_res = {
record: []
};
n();
})
})
.post('/dellocation', (req, res, n) => {
if (!config.permission.wristband) 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.query(`use ${config.db.db9}`);
let query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db9, 'location', arr.data.id];
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8003');
res.api_res = {
record: []
}
n();
2017-04-06 10:17:26 +00:00
})
2017-04-06 09:31:36 +00:00
})
.all('*', rt.send);
module.exports = router;