fix mysql pool release and add wristband api

This commit is contained in:
Jay 2017-03-28 16:34:50 +08:00
parent eeead43baf
commit 77bf5f58d2
15 changed files with 170 additions and 18 deletions

View File

@ -17,7 +17,8 @@
"db5": "jcmbset",
"db6": "jcmbrt",
"db7": "jcioclc",
"db8": "jciocln"
"db8": "jciocln",
"db9": "jcbtwristband"
},
"permission": {
"dio": true,

View File

@ -15,7 +15,7 @@ class MySQLPool {
createPool(){
this._pool = mysql.createPool({
connectionLimit: 20,
connectionLimit: 30,
user: this._user,
password: this._password,
host: this._host,

View File

@ -4,7 +4,8 @@ function send(req, res) {
if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close();
if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release();
let lngs = req.headers['accept-language'].split(',');
let lngs = req.headers['accept-language'] || '';
lngs = lngs.split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);

View File

@ -13,6 +13,15 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO DIO Api' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getio', (req, res, n) => {
if (!config.permission.dio) return n('ERR9000');

View File

@ -8,7 +8,7 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO API System' });
})
.all('*', async(req,res,n)=>{
.all('*', (req,res,n)=>{
if('x-auth-token' in req.headers) {
let token = req.headers['x-auth-token'];
if(so.chkKey(token)){
@ -18,11 +18,6 @@ router
}
}
}
try{
res.db = await mysql.getConn();
}catch(e){
return n('ERR8100');
}
n();
})
.get('/showallso', (req,res)=>{
@ -37,14 +32,16 @@ router
.use('/dio', require('./dio.js'))
.use('/link', require('./link.js'))
.use('/modbus', require('./modbus.js'))
.use('/ipcam', require('./ipcam.js'));
.use('/ipcam', require('./ipcam.js'))
.use('/wristband', require('./wristband.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();
if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release();
let lngs = req.headers['accept-language'].split(',');
let lngs = req.headers['accept-language'] || '';
lngs = lngs.split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);

View File

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
@ -12,6 +12,15 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO IOCmd API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/iocmd', (req, res, n) => {
if (!config.permission.iocmd) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');

View File

@ -2,13 +2,22 @@ const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO IOGroup API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post(['/getiogrouplist', '/getiogroup'], (req, res, n) => {
if (!config.permission.iogroup) return n('ERR9000');
let s = false;

View File

@ -13,6 +13,15 @@ router
.get('/', (req, res, n) => {
res.send({ name: 'WebIO IPCam API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getipcamlist', (req,res,n) => {
if(!config.permission.ipcam) return n('ERR9000');

View File

@ -3,7 +3,7 @@ const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
@ -11,6 +11,15 @@ router
.get('/', (req, res) => {
res.send({name: 'WebIO Leone API'});
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/scanleone', (req, res, n) => {
if (!config.permission.leone) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');

View File

@ -2,13 +2,22 @@ const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
router
.get('/', (req, res) => {
res.send({ name: 'WebIO Link API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getlinklist', (req, res, n) => {
if (!config.permission.link) return n('ERR9000');

View File

@ -9,6 +9,15 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO Log API' })
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getlog', (req, res, n) => {
if(!config.permission.log) return n('ERR9000');
let arr = req.body;

View File

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
@ -11,6 +11,15 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO Modbus API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getmodbuslist', (req, res, n) => {
if (!config.permission.modbus) return n('ERR9000');

View File

@ -3,7 +3,7 @@ const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
@ -13,6 +13,15 @@ router
.get('/', (req, res) => {
res.send({ name: 'WebIO Schedule API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post(['/getschedulelist', '/getschedule'], (req, res, n) => {
if (!config.permission.schedule) return n('ERR9000');
let s = false;

View File

@ -3,7 +3,7 @@ const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_cls');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
@ -21,6 +21,15 @@ router
res.send({ name: 'WebIO System API' });
})
.post('*', async(req,res,n) => {
try{
res.db = await mysql.getConn();
}catch(e){
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getnetwork', (req, res, n) => {
fs.exists(config.cmdpath.sysinfo, (exists) => {
if (!exists) return n('ERR0014');

63
route/api/wristband.js Normal file
View File

@ -0,0 +1,63 @@
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;
if(!arr.mac) return n('ERR0000');
let query = "insert into ??.?? values (null, ?, ?,?,?,?,?,?,?,?,?,?,?,?,unix_timestamp())";
let param = [
config.db.db9,
'rawdata',
arr.mac || '',
arr.val1 || '',
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) => {
if(err) return n('ERR8001');
res.api_res = {
record: []
};
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();
})
.all('*', rt.send);
module.exports = router;