webio-node/route/api/ipcam.js

95 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-03-27 06:53:34 +00:00
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
2017-03-27 08:27:35 +00:00
const mysql = require('../../libs/mysql_pool');
2017-03-27 06:53:34 +00:00
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res, n) => {
res.send({ name: 'WebIO IPCam API' });
})
2017-03-27 10:00:21 +00:00
.post('/getipcamlist', (req,res,n) => {
2017-03-27 08:27:35 +00:00
if(!config.permission.ipcam) return n('ERR9000');
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();
});
2017-03-27 08:27:35 +00:00
})
.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');
2017-03-27 10:00:21 +00:00
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 n('ERR8000');
if(row[0]['c'] > 0) return n('ERR0027');
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 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 query = "delete from ??.?? where `uid` = ?";
let param = [config.db.db1, 'ipcam', arr.data.id];
res.db.query(query, param, (err, row) => {
if(err) return 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');
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 n('ERR8002');
res.api_res = {
record: []
}
n();
})
2017-03-27 08:27:35 +00:00
})
2017-03-27 06:53:34 +00:00
.all('*', rt.send);
module.exports = router;