diff --git a/config.json b/config.json index cd510cd..cbaae59 100644 --- a/config.json +++ b/config.json @@ -17,7 +17,8 @@ "db5": "jcmbset", "db6": "jcmbrt", "db7": "jcioclc", - "db8": "jciocln" + "db8": "jciocln", + "db9": "jcbtwristband" }, "permission": { "dio": true, diff --git a/libs/mysql_pool.js b/libs/mysql_pool.js index 0d87af9..5f68b6d 100644 --- a/libs/mysql_pool.js +++ b/libs/mysql_pool.js @@ -15,7 +15,7 @@ class MySQLPool { createPool(){ this._pool = mysql.createPool({ - connectionLimit: 20, + connectionLimit: 30, user: this._user, password: this._password, host: this._host, diff --git a/route/ResTool.js b/route/ResTool.js index 8d624e0..ed40d21 100644 --- a/route/ResTool.js +++ b/route/ResTool.js @@ -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); diff --git a/route/api/dio.js b/route/api/dio.js index 36b527f..8b1d160 100644 --- a/route/api/dio.js +++ b/route/api/dio.js @@ -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'); diff --git a/route/api/index.js b/route/api/index.js index d74ba11..5183156 100644 --- a/route/api/index.js +++ b/route/api/index.js @@ -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); diff --git a/route/api/iocmd.js b/route/api/iocmd.js index 25ea9b4..cfe6170 100644 --- a/route/api/iocmd.js +++ b/route/api/iocmd.js @@ -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'); diff --git a/route/api/iogroup.js b/route/api/iogroup.js index 3196f82..b1bad68 100644 --- a/route/api/iogroup.js +++ b/route/api/iogroup.js @@ -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; diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 8f5c2f5..155c2e7 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -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'); diff --git a/route/api/leone.js b/route/api/leone.js index 3b71095..bfd3a93 100644 --- a/route/api/leone.js +++ b/route/api/leone.js @@ -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'); diff --git a/route/api/link.js b/route/api/link.js index e95d206..fa27a49 100644 --- a/route/api/link.js +++ b/route/api/link.js @@ -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'); diff --git a/route/api/log.js b/route/api/log.js index eebc3e3..411913f 100644 --- a/route/api/log.js +++ b/route/api/log.js @@ -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; diff --git a/route/api/modbus.js b/route/api/modbus.js index 5b91c65..430de02 100644 --- a/route/api/modbus.js +++ b/route/api/modbus.js @@ -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'); diff --git a/route/api/schedule.js b/route/api/schedule.js index 16453c6..3cb5ee4 100644 --- a/route/api/schedule.js +++ b/route/api/schedule.js @@ -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; diff --git a/route/api/system.js b/route/api/system.js index d9227b8..f56ab07 100644 --- a/route/api/system.js +++ b/route/api/system.js @@ -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'); diff --git a/route/api/wristband.js b/route/api/wristband.js new file mode 100644 index 0000000..d6ca376 --- /dev/null +++ b/route/api/wristband.js @@ -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; \ No newline at end of file