From af1d96062afffa63344a56b3aeff8b5b65346bfc Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 3 May 2017 17:15:08 +0800 Subject: [PATCH] modify add ipcam api --- app.js | 4 ++ config.json | 6 ++- includes/language/zh.json | 1 + route/api/ipcam.js | 79 ++++++++++++++++++++++++++++++--------- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 521bff2..4643bfc 100644 --- a/app.js +++ b/app.js @@ -54,6 +54,10 @@ app.get(['/admin', '/admin/*'], (req, res) => { res.sendFile(path.resolve(__dirname, 'views', 'admin.html')); }); +app.get('/camevent', (req, res) => { + res.send(0); +}) + app.get('/servcmd', (req, res) => { res.send({ status: `exit time ${Date.now()}` }); setTimeout(() => { process.exit(1) }, 2000); diff --git a/config.json b/config.json index bc936bd..8deb913 100644 --- a/config.json +++ b/config.json @@ -4,7 +4,8 @@ }, "uni_token": "webiounitoken", "perpage": 10, - "leone_limie": 40, + "leone_limit": 40, + "ipcam_limit": 4, "db": { "user": "root", "pass": "16055536", @@ -46,6 +47,7 @@ "iocmd": "/home/www/cmd/cmdio", "leonert": "/home/www/tmp/rtles", "htsrt": "/home/www/tmp/rthts", - "version": "/factory/webioa_version" + "version": "/factory/webioa_version", + "ipcamsave": "/home/www/cam" } } \ No newline at end of file diff --git a/includes/language/zh.json b/includes/language/zh.json index 0d681d7..95145e2 100644 --- a/includes/language/zh.json +++ b/includes/language/zh.json @@ -69,6 +69,7 @@ "ERR0067": "型號輸入錯誤", "ERR0068": "事件數量輸入錯誤", "ERR0069": "圖片數量輸入錯誤", + "ERR0070": "IPCam 裝置已達上限", "ERR7000": "命令執行失敗", diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 0279cde..71ef67b 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -55,26 +55,69 @@ router let active = arr.data.active == 1 ? 1 : 0; - let query = "select count(*) as c from ??.?? where `ip` = ?"; - let param = [config.db.db1, 'jcioccamset', arr.data.ip]; - res.db.query(query, param, (err, row) => { - if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000'); + !async function runSync() { + // data flag 0 is insert, 1 is update + let flag = 0; + let idx = 1; + // query db data + try { + let query = "select `uid` from ??.?? order by `uid` desc limit 1"; + let param = [config.db.db1, 'jcioccamset']; + let id = await tool.promiseQuery(res, query, param); - if (row[0].c >= 1) return n('ERR0027'); - - let query = "insert into ??.?? (`ip`, `name`, `model`, `maxevents`, `maximg`, `active`, `cuser`, `muser`, `ctime`, `mtime` ) \ - values (?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())"; - let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u]; - - res.db.query(query, param, (err, row) => { - if (err) return rt.err(res, err, n, 'ERR8001'); - - res.api_res = { - record: [] + if (id.data.length > 0 && id.data[0].uid) idx = id.data[0].uid + 1; + if (idx > config.ipcam_limit) { + query = "select `uid` from ??.?? where `delete` = 1 limit 1"; + flag = 1; + let id = await tool.promiseQuery(res, query, param); + if (id.data.length > 0 && id.data[0].uid) idx = id.data[0].uid; + else return n('ERR0070'); } - n(); - }) - }) + + query = "select count(*) as c from ??.?? where `ip` = ?"; + param = [config.db.db1, 'jcioccamset', arr.data.ip]; + let count = await tool.promiseQuery(res, query, param); + if (count.data.length == 0) return n('ERR8000'); + if (count.data[0].c >= 1) return n('ERR0027'); + } catch (e) { + return rt.err(res, e, n, 'ERR8000'); + } + if (flag == 1) { + try { + let query = "update ??.?? set \ + `ip` = ?, \ + `name` = ?, \ + `model` = ?, \ + `maxevents` = ?, \ + `maximg` = ?, \ + `active` = ?, \ + `delete` = 0, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ + where \ + `uid` = ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, arr.data.id]; + await tool.promiseQuery(res, query, param); + } catch (e) { + return rt.err(res, e, n, 'ERR8002'); + } + } else { + try { + let query = "insert into ??.?? (`uid`, `ip`, `name`, `model`, `maxevents`, `maximg`, `active`, `cuser`, `muser`, `ctime`, `mtime` ) \ + values (?, ?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())"; + let param = [config.db.db1, 'jcioccamset', idx, arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u]; + await tool.promiseQuery(res, query, param); + } catch (e) { + return rt.err(res, e, n, 'ERR8001'); + } + } + + res.api_res = { + record: [] + } + return n(); + }() + }) .post('/editipcam', (req, res, n) => { if (!config.permission.ipcam) return n('ERR9000');