This commit is contained in:
Jay 2017-03-27 16:27:35 +08:00
parent 57c8083012
commit 404fb8c346
3 changed files with 149 additions and 2 deletions

91
libs/mysql_pool.js Normal file
View File

@ -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;

View File

@ -30,7 +30,8 @@ router
.use('/schedule', require('./schedule.js')) .use('/schedule', require('./schedule.js'))
.use('/dio', require('./dio.js')) .use('/dio', require('./dio.js'))
.use('/link', require('./link.js')) .use('/link', require('./link.js'))
.use('/modbus', require('./modbus.js')); .use('/modbus', require('./modbus.js'))
.use('/ipcam', require('./ipcam.js'));
// api error handler // api error handler
router.use((err, req, res, n) => { router.use((err, req, res, n) => {

View File

@ -3,7 +3,7 @@ const router = express.Router();
const rt = require('../ResTool'); const rt = require('../ResTool');
const config = require('../../config.json'); const config = require('../../config.json');
const fs = require('fs'); const fs = require('fs');
const mysql = require('../../libs/mysql_cls'); const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool'); const tool = require('../../includes/apiTool');
const exec = require('child_process').exec; const exec = require('child_process').exec;
const so = require('../../includes/storeObject'); const so = require('../../includes/storeObject');
@ -21,6 +21,61 @@ router
res.send({ name: 'WebIO IPCam API' }); 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); .all('*', rt.send);
module.exports = router; module.exports = router;