add event history

This commit is contained in:
Jay 2017-05-04 14:36:29 +08:00
parent af1d96062a
commit fbab4f44e8
7 changed files with 246 additions and 9 deletions

25
app.js
View File

@ -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) => {

View File

@ -70,6 +70,8 @@
"ERR0068": "事件數量輸入錯誤",
"ERR0069": "圖片數量輸入錯誤",
"ERR0070": "IPCam 裝置已達上限",
"ERR0071": "無此裝置資料",
"ERR0072": "目錄資訊取得失敗",
"ERR7000": "命令執行失敗",

View File

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

View File

@ -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 (
<Modal open={open} onClose={()=>{closeEventModal()}} size="fullscreen">
<Modal.Header content={`IPCam: ${name}`} />
<Modal.Content>
<Grid>
<Grid.Column width={4}>
<Menu vertical>
<Menu.Item>
<Menu.Header>
事件列表
<Icon name="refresh" style={{
cursor: "pointer",
float: 'right'
}} onClick={()=>{refreshEvt(-1)}}/>
</Menu.Header>
<Menu.Menu>
{
list.map((t,idx) =>{
let n = t.name;
let time = n.split('-')[1];
return (
<Menu.Item key={idx} active={sel == idx} onClick={()=>{changeSelectEvent(idx)}}>
{convertTime(time, true)}
</Menu.Item>
)})
}
</Menu.Menu>
</Menu.Item>
</Menu>
</Grid.Column>
<Grid.Column width={12}>
{
sel != -1 ?
(
<ImgGrid i18n={i18n}
dname={list[sel].name}
list={list[sel].files.img} />
) : null
}
</Grid.Column>
</Grid>
</Modal.Content>
</Modal>
)
}
export default EventModal;

View File

@ -0,0 +1,21 @@
import React from 'react';
import {Grid, Image} from 'semantic-ui-react';
const EvtImgGrid = ({ i18n, dname, list }) => {
return (
<Grid columns={3}>
{
list.map((t,idx) => {
return (
<Grid.Column key={idx}>
<Image fluid src={`/viewcamimg/${dname}/${t}`} href={`/viewcamimg/${dname}/${t}`} target="_blank"/>
</Grid.Column>
)
})
}
</Grid>
)
}
export default EvtImgGrid;

View File

@ -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 (
<Table.Row>
@ -9,10 +9,12 @@ const ListItem = ({ i18n, data, swActive, openModal, delIPCam }) => {
<Button basic content="修改" type="button" onClick={()=>openModal(1, data)} />
<Button basic content="刪除" type="button" onClick={()=>delIPCam(data.uid)}/>
<Button basic content={data.active == 1 ? '停用' : '啟用'} type="button" onClick={()=>swActive(data.uid)} />
<Button basic content="事件紀錄" type="button" onClick={()=>getEvents(data.uid, data.name)} />
</Table.Cell>
<Table.Cell>{data.name}</Table.Cell>
<Table.Cell>{data.ip}</Table.Cell>
<Table.Cell>{data.model}</Table.Cell>
<Table.Cell>{data.model == 'AXIS-M1124' ? `/${data.model}-${data.uid}` : '/'}</Table.Cell>
<Table.Cell>{data.active == 1? '啟用' : '停用'}</Table.Cell>
</Table.Row>
)

View File

@ -1,9 +1,9 @@
import React from 'react';
import { Container, Segment, Table, Button, Label } from 'semantic-ui-react';
import { Container, Segment, Table, Button, Label, Message } from 'semantic-ui-react';
import {getRequest} from '../../../actions';
import ListItem from './ListItem';
import IPCamModal from './IPCamModal';
import EvtModal from './EventModal';
const stateDefault = ()=>({
list: [],
@ -11,6 +11,13 @@ const stateDefault = ()=>({
type: 0,
open: false,
data: {}
},
evt: {
open: false,
list: [],
name: '',
sel: -1,
id: -1
}
})
@ -101,11 +108,55 @@ class IPCamPage extends React.Component {
});
}
getEvents = (id, name = '') => {
if(!id) return ;
if(id==-1) id = this.state.evt.id;
let {callConfirm, toggleLoading, showDialog} = this.props;
toggleLoading(1);
fetch('/api/ipcam/getevents', getRequest({id}))
.then(response=>response.json())
.then(json => {
toggleLoading(0);
if(json.status != 1) return showDialog(json.message);
this.setState({
evt: {
open: true,
list: json.data.record || [],
name: name != '' ? name : this.state.evt.name,
sel: -1,
id
}
})
})
}
closeEventModal = () => {
this.setState({
evt: {
...stateDefault().evt
}
})
}
changeSelectEvent = (idx) => {
if(!isFinite(idx) || idx < 0) return ;
this.setState({
evt:{
...this.state.evt,
sel: idx
}
})
}
render() {
let {i18n} = this.props;
return (
<Container>
<Segment className="clearfix">
<Message>
<Message.Header>攝影機事件通知位址</Message.Header>
<p>
{`${location.protocol}//${location.host}${location.port != '' ? `:${location.port}` : ''}/camevent`}
</p>
</Message>
<Button basic content="新增" icon="plus" floated="right" type="button" style={{marginBottom: '15px'}} color="green" onClick={()=>{this.openModal(0)}} />
<Table>
<Table.Header>
@ -114,6 +165,7 @@ class IPCamPage extends React.Component {
<Table.HeaderCell>名稱</Table.HeaderCell>
<Table.HeaderCell>IP</Table.HeaderCell>
<Table.HeaderCell>型號</Table.HeaderCell>
<Table.HeaderCell>FTP路徑</Table.HeaderCell>
<Table.HeaderCell>啟用狀態</Table.HeaderCell>
</Table.Row>
</Table.Header>
@ -125,7 +177,8 @@ class IPCamPage extends React.Component {
data={t}
swActive={this.swActive}
delIPCam={this.delIPCam}
openModal={this.openModal} />
openModal={this.openModal}
getEvents={this.getEvents} />
))
}
</Table.Body>
@ -137,6 +190,14 @@ class IPCamPage extends React.Component {
type={this.state.modal.type}
closeModal={this.closeModal}
submitModal={this.submitModal} />
<EvtModal i18n={i18n}
open={this.state.evt.open}
list={this.state.evt.list}
sel={this.state.evt.sel}
name={this.state.evt.name}
refreshEvt={this.getEvents}
closeEventModal={this.closeEventModal}
changeSelectEvent={this.changeSelectEvent} />
</Container>
)
}