From fbab4f44e8d06adfde8456bdd97d367ced8d6633 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 4 May 2017 14:36:29 +0800 Subject: [PATCH] add event history --- app.js | 25 +++++- includes/language/zh.json | 2 + route/api/ipcam.js | 82 +++++++++++++++++++- src/components/AdminPage/IPCam/EventModal.js | 54 +++++++++++++ src/components/AdminPage/IPCam/EvtImgGrid.js | 21 +++++ src/components/AdminPage/IPCam/ListItem.js | 4 +- src/components/AdminPage/IPCam/index.js | 67 +++++++++++++++- 7 files changed, 246 insertions(+), 9 deletions(-) create mode 100644 src/components/AdminPage/IPCam/EventModal.js create mode 100644 src/components/AdminPage/IPCam/EvtImgGrid.js diff --git a/app.js b/app.js index 4643bfc..990c3bc 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ const path = require('path'); const config = require('./config'); const so = require('./includes/storeObject'); const exec = require('child_process').exec; +const fs = require('fs'); const app = express(); @@ -55,7 +56,29 @@ app.get(['/admin', '/admin/*'], (req, res) => { }); app.get('/camevent', (req, res) => { - res.send(0); + let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; + ip = ip.split(':').pop(); + let cmd = `camcam ${ip} ${Math.floor(Date.now() / 1000)}`; + exec(cmd, (err, sout, serr) => { + res.send({ ip }); + }); +}) +app.get('/viewcamimg/:dir/:img', async(req, res) => { + let dir = req.params.dir; + let img = req.params.img; + if (!dir || !img) return res.status(404).end(); + try { + let stat = await new Promise((resolve, reject) => { + fs.stat(path.resolve(config.cmdpath.ipcamsave, dir, img), (err, stats) => { + if (err) return reject(err); + return resolve(stats); + }) + }) + if (!stat.isFile()) return res.status(404).end(); + } catch (e) { + return res.status(404).end(); + } + res.sendfile(path.resolve(config.cmdpath.ipcamsave, dir, img)) }) app.get('/servcmd', (req, res) => { diff --git a/includes/language/zh.json b/includes/language/zh.json index 95145e2..e2bd22c 100644 --- a/includes/language/zh.json +++ b/includes/language/zh.json @@ -70,6 +70,8 @@ "ERR0068": "事件數量輸入錯誤", "ERR0069": "圖片數量輸入錯誤", "ERR0070": "IPCam 裝置已達上限", + "ERR0071": "無此裝置資料", + "ERR0072": "目錄資訊取得失敗", "ERR7000": "命令執行失敗", diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 71ef67b..80d8356 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -8,6 +8,7 @@ const tool = require('../../includes/apiTool'); const exec = require('child_process').exec; const so = require('../../includes/storeObject'); const crypt = require('../../libs/crypto'); +const path = require('path'); router .get('/', (req, res, n) => { @@ -25,7 +26,7 @@ router .post('/getipcamlist', (req, res, n) => { if (!config.permission.ipcam) return n('ERR9000'); - let query = "select * from ??.??"; + let query = "select * from ??.?? where `delete` = 0 order by `ctime`"; let param = [config.db.db1, 'jcioccamset']; res.db.query(query, param, (err, row) => { if (err) return rt.err(res, err, n, 'ERR8000'); @@ -92,11 +93,13 @@ router `maximg` = ?, \ `active` = ?, \ `delete` = 0, \ + `cuser` = ?, \ + `ctime` = unix_timestamp(), \ `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]; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u, idx]; await tool.promiseQuery(res, query, param); } catch (e) { return rt.err(res, e, n, 'ERR8002'); @@ -175,10 +178,17 @@ router if (!arr.data) return n('ERR0000'); if (!arr.data.id) return n('ERR0028'); + let u = ''; + let obj = so.get(req.headers['x-auth-token'] || ''); + if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; + res.db.query(`use ${config.db.db1}`); - let query = "delete from ??.?? \ + let query = "update ??.?? set \ + `delete` = 1, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ where `uid` = ?"; - let param = [config.db.db1, 'jcioccamset', arr.data.id]; + let param = [config.db.db1, 'jcioccamset', u, arr.data.id]; res.db.query(query, param, (err, row) => { if (err) return rt.err(res, err, n, 'ERR8003'); @@ -211,6 +221,70 @@ router n(); }) }) + .post('/getevents', async(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.id) return n('ERR0028'); + + try { + let query = "select * from ??.?? where `uid` = ? and `delete` = 0"; + let param = [config.db.db1, 'jcioccamset', arr.data.id]; + let cam = await tool.promiseQuery(res, query, param); + if (cam.length == 0) return n('ERR0071'); + } catch (e) { + return rt.err(res, e, n, 'ERR8000'); + } + let rp = config.cmdpath.ipcamsave; + let dirs = []; + + try { + let dirls = await new Promise((resolve, reject) => { + fs.readdir(rp, (err, list) => { + if (err) return reject(err); + return resolve(list); + }) + }) + for (let i of dirls) { + let tmp = i.split('-'); + if (tmp.length != 2) continue; + if (tmp[0] == arr.data.id) dirs.push({ + name: i, + files: { + img: [], + video: [] + } + }); + } + + for (let i in dirs) { + let p = path.resolve(rp, dirs[i].name); + let idx = i; + let files = await new Promise((resolve, reject) => { + fs.readdir(p, (err, list) => { + if (err) return reject(err); + return resolve(list); + }) + }) + for (let j of files) { + if (/^img/.test(j)) { + dirs[idx].files.img.push(j); + } else if (/^vid/.test(j)) { + dirs[idx].files.video.push(j); + } + } + } + + } catch (e) { + return rt.err(res, e, n, 'ERR0072'); + } + + res.api_res = { + record: tool.checkArray(dirs) + } + return n(); + }) .all('*', rt.send); module.exports = router; \ No newline at end of file diff --git a/src/components/AdminPage/IPCam/EventModal.js b/src/components/AdminPage/IPCam/EventModal.js new file mode 100644 index 0000000..5fbbb47 --- /dev/null +++ b/src/components/AdminPage/IPCam/EventModal.js @@ -0,0 +1,54 @@ +import React from 'react'; +import { Modal, Grid, List, Menu, Icon } from 'semantic-ui-react'; +import {convertTime} from '../../../tools'; +import ImgGrid from './EvtImgGrid'; + +const EventModal = ({ i18n, open, name, list, sel, closeEventModal, changeSelectEvent,refreshEvt }) => { + + return ( + {closeEventModal()}} size="fullscreen"> + + + + + + + + 事件列表 + {refreshEvt(-1)}}/> + + + { + list.map((t,idx) =>{ + let n = t.name; + let time = n.split('-')[1]; + return ( + {changeSelectEvent(idx)}}> + {convertTime(time, true)} + + )}) + } + + + + + + { + sel != -1 ? + ( + + ) : null + } + + + + + ) +} + +export default EventModal; \ No newline at end of file diff --git a/src/components/AdminPage/IPCam/EvtImgGrid.js b/src/components/AdminPage/IPCam/EvtImgGrid.js new file mode 100644 index 0000000..e3d93ea --- /dev/null +++ b/src/components/AdminPage/IPCam/EvtImgGrid.js @@ -0,0 +1,21 @@ +import React from 'react'; +import {Grid, Image} from 'semantic-ui-react'; + +const EvtImgGrid = ({ i18n, dname, list }) => { + + return ( + + { + list.map((t,idx) => { + return ( + + + + ) + }) + } + + ) +} + +export default EvtImgGrid; \ No newline at end of file diff --git a/src/components/AdminPage/IPCam/ListItem.js b/src/components/AdminPage/IPCam/ListItem.js index d6be16c..bfcea60 100644 --- a/src/components/AdminPage/IPCam/ListItem.js +++ b/src/components/AdminPage/IPCam/ListItem.js @@ -1,7 +1,7 @@ import React from 'react'; import {Table, Button} from 'semantic-ui-react'; -const ListItem = ({ i18n, data, swActive, openModal, delIPCam }) => { +const ListItem = ({ i18n, data, swActive, openModal, delIPCam, getEvents }) => { return ( @@ -9,10 +9,12 @@ const ListItem = ({ i18n, data, swActive, openModal, delIPCam }) => {