From 404fb8c346311805c268490fa19d6523e004e15a Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 27 Mar 2017 16:27:35 +0800 Subject: [PATCH] update --- libs/mysql_pool.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++ route/api/index.js | 3 +- route/api/ipcam.js | 57 ++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 libs/mysql_pool.js diff --git a/libs/mysql_pool.js b/libs/mysql_pool.js new file mode 100644 index 0000000..0d87af9 --- /dev/null +++ b/libs/mysql_pool.js @@ -0,0 +1,91 @@ +const mysql = require('mysql'); +const config = require('../config.json'); + +class MySQLPool { + constructor() { + this._user = ''; + this._password = ''; + this._host = ''; + this._port = 3306; + this._database = ''; + this._socketPath = '/var/lib/mysql/mysql.sock' + this._pool = null; + this.autoclose = false; + } + + createPool(){ + this._pool = mysql.createPool({ + connectionLimit: 20, + user: this._user, + password: this._password, + host: this._host, + port: this._port, + socketPath: this._socketPath + }); + } + + async getConn() { + return new Promise((resolve, reject) => { + this._pool.getConnection((err, conn) => { + if(err) return reject(err); + return resolve(conn) + }) + }) + } + + formatQuery(query, arg) { + return mysql.format(query, arg); + } + + escape(val) { + return mysql.escape(val); + } + + recordPage(rows, page, maxPage) { + if (!page || !isFinite(page) || page < 1) page = 1; + + let totalpage = Math.ceil(rows / maxPage); + + let prevpage = page - 1; + let nextpage = page + 1; + + if (prevpage < 1) prevpage = 1; + if (nextpage > totalpage) nextpage = totalpage; + + let rec_start = (page - 1) * maxPage + 1 + let rec_end = (rec_start + maxPage - 1); + if (rec_end > rows) rec_end = rows; + + let json = { + rec_start, + rec_end, + total: rows, + prevpage, + nextpage, + totalpage, + page + }; + + return json; + } + + set user(str) { this._user = str; } + set host(str) { this._host = str; } + set password(str) { this._password = str; } + set port(str) { this._port = str; } + set database(str) { this._database = str; } +} + + +let Pool = (()=>{ + let pool = new MySQLPool(); + pool.user = config.db.user; + pool.password = config.db.pass; + pool.host = config.db.host; + pool.port = config.db.port; + pool.createPool(); + + return pool +})() + +module.exports = Pool; \ No newline at end of file diff --git a/route/api/index.js b/route/api/index.js index 77d5f5f..17ba639 100644 --- a/route/api/index.js +++ b/route/api/index.js @@ -30,7 +30,8 @@ router .use('/schedule', require('./schedule.js')) .use('/dio', require('./dio.js')) .use('/link', require('./link.js')) - .use('/modbus', require('./modbus.js')); + .use('/modbus', require('./modbus.js')) + .use('/ipcam', require('./ipcam.js')); // api error handler router.use((err, req, res, n) => { diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 9ba44dd..96b470f 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.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,61 @@ router res.send({ name: 'WebIO IPCam API' }); }) + .post('/getipcamlist', async (req,res,n) => { + if(!config.permission.ipcam) 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 = "select * from ??.??"; + // let param = [config.db.db1, 'ipcam']; + // res.db.query(query, param, (err, row) => { + // if(err) return n('ERR8000'); + + // res.api_res = { + // record: tool.checkArray(row) + // } + + // n(); + // }); + try{ + res.pdb = await mysql.getConn(); + res.pdb.query('select * from ??.??', [config.db.db1, 'dilist'], (err, row) => { + console.log(err, row) + if(err) return n('ERR8000') + res.api_res = { + record: row + } + n(); + }) + }catch(e){ + return n('ERR8100'); + } + }) + .post('/addipcam', (req,res,n) => { + if(!config.permission.ipcam) return n('ERR9000'); + if(!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if(!arr.data) return n('ERR0000'); + if(!arr.data.ip) return n('ERR0010'); + if(!arr.data.name) return n('ERR0026'); + + if(!/^\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); + + // 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('aaa'); + }) .all('*', rt.send); module.exports = router; \ No newline at end of file