re commit

This commit is contained in:
Jay
2017-03-22 13:35:45 +08:00
commit 8947715671
109 changed files with 161369 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
const so = require('./storeObject');
const config = require('../config');
const fs = require('fs');
const checkPermission = (req) => {
let id = req.headers['x-auth-token'];
if (id) {
let obj = so.get(id);
if (config.uni_token.length > 0 && id == config.uni_token) return true;
if (obj != null) {
if ('user' in obj && obj.user.write_privilege == '1') return true;
}
}
return false;
}
const checkArray = (obj) => {
if (Array.isArray(obj)) return obj;
return [];
}
const getLeoneRT = (cb) => {
if (!cb || typeof cb != 'function') return;
fs.exists(config.cmdpath.leonert, exists => {
if (!exists) return cb([]);
fs.readFile(config.cmdpath.leonert, (err, data) => {
if (err) return cb([]);
let str = data.toString();
let tmp = str.split(/\n/);
let rt = [];
for (var i in tmp) {
if (!tmp[i].trim()) continue;
let arr = tmp[i].split(' ');
if (arr.length != 5) continue;
let [ip, ts, hs, mode, mtime] = arr;
rt.push({ ip, ts, hs, mode, mtime });
}
return cb(rt);
});
});
}
const promiseQuery = (res, query, param, key = '') => {
return new Promise((resolve, reject) => {
res.db.query(query, param, (err, row) => {
if (err) return reject(err);
resolve({ data: row, key });
});
});
}
const getMode = (req) => {
let lngs = req.headers['accept-language'].split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
} else {
lng = 'zh';
}
if (!fs.existsSync(`../public/locales/${lng}.json`)) lng = 'zh';
let json = require(`../public/locales/${lng}.json`);
return json[lng].translation.leone_stats;
}
const getCmd = (req) => {
let lngs = req.headers['accept-language'].split(',');
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
} else {
lng = 'zh';
}
if (!fs.existsSync(`../public/locales/${lng}.json`)) lng = 'zh';
let json = require(`../public/locales/${lng}.json`);
return json[lng].translation.action_list;
}
module.exports = {
checkPermission,
checkArray,
getLeoneRT,
promiseQuery,
getCmd,
getMode
}
+9
View File
@@ -0,0 +1,9 @@
const manager = (errCode = '', lang = 'zh') => {
if(typeof lang != 'string' || !lang.trim()) lang = 'zh';
lang = lang.toLowerCase();
let errors = require(`./language/${lang}.json`);
if(errCode in errors) return errors[errCode];
else return 'Error Code not found';
}
module.exports = manager;
+65
View File
@@ -0,0 +1,65 @@
{
"ERR0000": "資料輸入錯誤",
"ERR0001": "PIN輸入錯誤",
"ERR0002": "value輸入錯誤",
"ERR0003": "DI資料查詢錯誤",
"ERR0004": "DO資料查詢錯誤",
"ERR0005": "DI資料輸入錯誤",
"ERR0006": "DO資料輸入錯誤",
"ERR0007": "DO資訊寫入失敗",
"ERR0008": "DI資訊寫入失敗",
"ERR0009": "類型輸入錯誤",
"ERR0010": "IP輸入錯誤",
"ERR0011": "Netmask輸入錯誤",
"ERR0012": "Gateway輸入錯誤",
"ERR0013": "DNS輸入錯誤",
"ERR0014": "網路資訊取得失敗",
"ERR0015": "時間輸入錯誤",
"ERR0016": "帳號輸入錯誤",
"ERR0017": "密碼輸入錯誤",
"ERR0018": "使用者資料取得失敗",
"ERR0019": "帳號或密碼錯誤",
"ERR0020": "刪除使用者失敗",
"ERR0021": "使用者資料更新失敗",
"ERR0022": "使用者資料新增失敗",
"ERR0023": "數量計算錯誤",
"ERR0024": "數量取得失敗",
"ERR0025": "IP格式錯誤",
"ERR0026": "名稱輸入錯誤",
"ERR0027": "此IP裝置已存在",
"ERR0028": "ID輸入錯誤",
"ERR0029": "裝置輸入錯誤",
"ERR0030": "動作輸入錯誤",
"ERR0031": "溫度請介於16-30間",
"ERR0032": "啟用狀態輸入錯誤",
"ERR0033": "分鐘輸入錯誤",
"ERR0034": "小時輸入錯誤",
"ERR0035": "日輸入錯誤",
"ERR0036": "月輸入錯誤",
"ERR0037": "admin 不能被刪除",
"ERR0038": "裝置節點輸入錯誤",
"ERR0039": "裝置Di數量輸入錯誤",
"ERR0040": "裝置Di起始位址輸入錯誤",
"ERR0041": "裝置Do數量輸入錯誤",
"ERR0042": "裝置Do起始位址輸入錯誤",
"ERR0043": "裝置Ai數量輸入錯誤",
"ERR0044": "裝置Ai起始位址輸入錯誤",
"ERR0045": "裝置Ao數量輸入錯誤",
"ERR0046": "裝置Ao起始位址輸入錯誤",
"ERR0047": "裝置節點編號已存在",
"ERR0048": "位址輸入錯誤",
"ERR0049": "數值輸入錯誤",
"ERR0050": "最小值輸入錯誤",
"ERR0051": "最大值輸入錯誤",
"ERR0052": "ScaleMin輸入錯誤",
"ERR0053": "ScaleMax輸入錯誤",
"ERR0054": "此位址設定已存在",
"ERR7000": "命令執行失敗",
"ERR8000": "資料查詢失敗",
"ERR8001": "資料新增失敗",
"ERR8002": "資料更新失敗",
"ERR8003": "資料刪除失敗",
"ERR9000": "操作權限不足"
}
+68
View File
@@ -0,0 +1,68 @@
const StoreObj = (() => {
let memStore = {};
let maxAge = 3600000;
/**
* @param {string} uuid
* @param {object} obj
*/
const set = (uuid, obj) => {
memStore[uuid] = {
obj,
age: Date.now() + maxAge
}
}
/**
* @param {string} uuid
*/
const get = (uuid) => {
if (uuid in memStore) {
let s = memStore[uuid];
if (Date.now() < s.age) {
s.age = Date.now() + maxAge;
return s.obj;
} else {
delete memStore[uuid];
}
}
return null;
}
/**
* @param {string} uuid
*/
const chkKey = (uuid) => {
if (uuid in memStore) return true;
return false;
}
/**
* @param {string} uuid
*/
const del = (uuid) => {
if (uuid in memStore) delete memStore[uuid];
}
const clear = () => {
let t = Date.now();
for (var i in memStore) {
let s = memStore[i];
if (s.age < t) delete memStore[i];
}
}
/**
* @param {string} uuid if not input return all
*/
const show = (uuid) => {
if (uuid && uuid in memStore) return memStore[uuid];
return memStore;
}
return {set, get, chkKey, clear, del, show };
})()
module.exports = StoreObj;