diff --git a/app.js b/app.js index 0370a1d..6e4fc79 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(); @@ -54,8 +55,34 @@ app.get(['/admin', '/admin/*'], (req, res) => { res.sendFile(path.resolve(__dirname, 'views', 'admin.html')); }); +app.get('/camevent', (req, res) => { + 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) => { - res.send({ status: `exit time ${Date.now()}` }); + res.send({ status: `exit time ${(new Date()).toString()}` }); setTimeout(() => { process.exit(1) }, 2000); }); @@ -67,7 +94,7 @@ app.get('/fixmysql', (req, res) => { restart: '' }; exec(cmd, (err, stdout, stderr) => { - let cmd = 'systemctl restart mysqld'; + let cmd = 'systemctl start mysqld'; json.rm = stdout; exec(cmd, (err, sout, serr) => { json.restart = sout; diff --git a/config.json b/config.json index 54e8345..c87d33b 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", @@ -20,7 +21,8 @@ "db8": "jciocln", "db9": "jcbtwristband", "db10": "jcmblog", - "db11": "jciocserver" + "db11": "jciocserver", + "db12": "jcioccam" }, "permission": { "dio": true, @@ -45,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" } } diff --git a/includes/language/zh.json b/includes/language/zh.json index d6c6bc7..e2bd22c 100644 --- a/includes/language/zh.json +++ b/includes/language/zh.json @@ -66,6 +66,12 @@ "ERR0064": "MAC Addr 輸入錯誤", "ERR0065": "版本輸入錯誤", "ERR0066": "SNMP Level輸入錯誤", + "ERR0067": "型號輸入錯誤", + "ERR0068": "事件數量輸入錯誤", + "ERR0069": "圖片數量輸入錯誤", + "ERR0070": "IPCam 裝置已達上限", + "ERR0071": "無此裝置資料", + "ERR0072": "目錄資訊取得失敗", "ERR7000": "命令執行失敗", diff --git a/route/api/ipcam.js b/route/api/ipcam.js index 7bbe0ab..80d8356 100644 --- a/route/api/ipcam.js +++ b/route/api/ipcam.js @@ -8,27 +8,28 @@ 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) => { res.send({ name: 'WebIO IPCam API' }); }) - .post('*', async(req,res,n) => { - try{ + .post('*', async(req, res, n) => { + try { res.db = await mysql.getConn(); - }catch(e){ + } catch (e) { console.log(`Get DB Connection ERROR ${e}`); return n('ERR8100'); } n(); }) - .post('/getipcamlist', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); + .post('/getipcamlist', (req, res, n) => { + if (!config.permission.ipcam) return n('ERR9000'); - let query = "select * from ??.??"; - let param = [config.db.db1, 'ipcam']; + 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'); + if (err) return rt.err(res, err, n, 'ERR8000'); res.api_res = { record: tool.checkArray(row) @@ -37,68 +38,253 @@ router n(); }); }) - .post('/addipcam', (req,res,n) => { - if(!config.permission.ipcam) return n('ERR9000'); - if(!tool.checkPermission(req)) return n('ERR9000'); + .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'); + if (!arr.data) return n('ERR0000'); + if (!arr.data.ip) return n("ERR0010"); + if (!arr.data.name) return n('ERR0026'); + if (!arr.data.model) return n('ERR0067'); + if (!arr.data.maxevents) return n('ERR0068'); + if (!arr.data.maximg) return n('ERR0069'); + if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); - let query = "select count(*) as c from ??.?? where `ip` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.ip]; - res.db.query(query, param, (err, row) => { - if(err || row.length == 0) return rt.err(res,err,n,'ERR8000'); - if(row[0]['c'] > 0) return n('ERR0027'); + 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; - let query = "insert into ??.?? (`name`, `ip`) values (?,?)"; - let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.ip]; - res.db.query(query, param, (err, row) => { - if(err) return rt.err(res,err,n,'ERR8001'); - res.api_res = { - record: [] - }; - n(); - }); - }); - }) - .post('/delipcam', (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'); + let active = arr.data.active == 1 ? 1 : 0; + + !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 (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'); + } + + 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, \ + `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, u, idx]; + 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'); + } + } - let query = "delete from ??.?? where `uid` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.id]; - res.db.query(query, param, (err, row) => { - if(err) return rt.err(res,err,n,'ERR8003'); res.api_res = { record: [] - }; - n(); - }) - }) - .post('/editipcam', (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'); - if(!arr.data.name) return n('ERR0026'); + } + return n(); + }() + + }) + .post('/editipcam', (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'); + if (!arr.data.ip) return n("ERR0010"); + if (!arr.data.name) return n('ERR0026'); + if (!arr.data.model) return n('ERR0067'); + if (!arr.data.maxevents) return n('ERR0068'); + if (!arr.data.maximg) return n('ERR0069'); + if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025'); + + 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; + + let active = arr.data.active == 1 ? 1 : 0; + + let query = "select count(*) as c from ??.?? where `ip` = ? and `uid` != ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.id]; + + res.db.query(query, param, (err, row) => { + if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000'); + + if (row[0].c >= 1) return n('ERR0027'); + + let query = "update ??.?? set \ + `ip` = ?, \ + `name` = ?, \ + `model` = ?, \ + `maxevents` = ?, \ + `maximg` = ?, \ + `active` = ?, \ + `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]; + res.db.query(query, param, (err, row) => { + if (err) return rt.err(res, err, n, 'ERR8002'); + + res.api_res = { + record: [] + } + n(); + }) + }); + }) + .post('/delipcam', (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'); + + 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 = "update ??.?? set \ + `delete` = 1, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ + where `uid` = ?"; + 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'); - let query = "update ??.?? set `name` = ? where `uid` = ?"; - let param = [config.db.db1, 'ipcam', arr.data.name, arr.data.id]; - res.db.query(query, param, (err,row) => { - if(err) return rt.err(res,err,n,'ERR8002'); res.api_res = { record: [] } n(); }) }) + .post('/swipcamactive', (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'); + + let query = "update ??.?? set \ + `active` = case when `active` = 1 then 0 else 1 end \ + where \ + `uid` = ?"; + let param = [config.db.db1, 'jcioccamset', arr.data.id]; + + res.db.query(query, param, (err, row) => { + if (err) return rt.err(res, err, n, 'ERR8002'); + + res.api_res = { + record: [] + } + 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/IPCamModal.js b/src/components/AdminPage/IPCam/IPCamModal.js new file mode 100644 index 0000000..2ca7357 --- /dev/null +++ b/src/components/AdminPage/IPCam/IPCamModal.js @@ -0,0 +1,68 @@ +import React from 'react'; +import { Modal, Form, Grid, Button, Input, Checkbox } from 'semantic-ui-react'; + +const IPCamModal = ({ i18n, open, type, data, closeModal, submitModal }) => { + + return ( + + + +
{ + e.preventDefault(); + submitModal(type, d.formData); + }} serializer={e=>{ + let json = {}; + + let name = e.querySelector('input[name="name"]'); + if(name && 'value' in name) json.name = name.value; + let ip = e.querySelector('input[name="ip"]'); + if(ip && 'value' in ip) json.ip = ip.value; + let model = e.querySelector('select[name="model"]'); + if(model && 'value' in model) json.model = model.value; + let maxevents = e.querySelector('input[name="maxevents"]'); + if(maxevents && 'value' in maxevents) json.maxevents = maxevents.value; + let maximg = e.querySelector('input[name="maximg"]'); + if(maximg && 'value' in maximg) json.maximg = maximg.value; + let active = e.querySelector('input[type="checkbox"]'); + if(active && 'checked' in active) json.active = active.checked; + if(type == 1) json.id = data.uid; + + return json; + }}> + + + + + + + + + + + + + + + + + + + + +