Spaces:
Sleeping
Sleeping
| /** | |
| * Copyright (c) 2017~2020, OBCon Inc. | |
| * All rights reserved. | |
| */ | |
| /** | |
| * @file | |
| * @copyright 2017~2020, OBCon Inc. | |
| * @author gye hyun james kim [pnuskgh@gmail.com] | |
| */ | |
| let moment = require('moment'); | |
| let Sequelize = require('sequelize'); //--- http://docs.sequelizejs.com/ | |
| let View = utils.getClass('include', 'view.js'); | |
| class Devicedata2sView extends View { | |
| constructor() { | |
| super(); | |
| this._name = 'devicedata2s'; | |
| this._title = '장비데이터 Type 2'; | |
| this._model = modules.devicedata2s.model; | |
| this._req = null; | |
| this._hasSite = false; | |
| } | |
| //--- 권한 설정 반환 | |
| getRights() { | |
| let rights = super.getRights(); | |
| rights.get = { | |
| list : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| detail : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| create : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| update : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| }; | |
| rights.ajaxPost = { | |
| create : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| update : { isLogin: true, isAdmin: true, isGlobalAdmin: true }, | |
| delete : { isLogin: true, isAdmin: true, isGlobalAdmin: true } | |
| } | |
| rights.ajaxGet.list = { isLogin: true, isAdmin: false, isGlobalAdmin: false }; | |
| rights.ajaxGet.list2 = { isLogin: true, isAdmin: false, isGlobalAdmin: false }; | |
| return rights; | |
| } | |
| getMenus() { | |
| return { | |
| menu: { | |
| name: this.name, //--- 메뉴 선택 여부를 확인하기 위한 이름 | |
| title: this.title, //--- 메뉴 이름 | |
| path: this.name, //--- 메뉴 URL path, | |
| isLogin: true, //--- 로그인한 사용자에게만 표시 | |
| isAdmin: true, //--- 관리자에게만 표시 | |
| isGlobalAdmin: false //--- 글로벌 관리자에게만 표시 | |
| }, | |
| submenus: [ | |
| // { name:'create', title: '생성', action: 'create', isLogin: true, isAdmin: true, isGlobalAdmin: false, target: null }, | |
| { name:'list', title: '목록', action: 'list', isLogin: true, isAdmin: true, isGlobalAdmin: false, target: null } | |
| ] | |
| }; | |
| } | |
| getRouters() { | |
| let routers = super.getRouters(); | |
| routers.ajaxGet.list2 = this.ajaxList2.bind(this); | |
| return routers; | |
| } | |
| getSearchData(req, res, next, action) { | |
| let item = { | |
| site: req.query.site, | |
| type: req.query.type, | |
| deviceKey: req.query.deviceKey, | |
| statusDatetimeFr: req.query.statusDatetimeFr, | |
| statusDatetimeTo: req.query.statusDatetimeTo, | |
| uuid: (req.query.uuid) ? req.query.uuid:null | |
| }; | |
| this._req = req; | |
| return item; | |
| } | |
| getSearchQuery(search, query, action) { | |
| query = { | |
| attributes: ['statusDatetime', 'preventPotential', 'batteryVoltage', 'ac', 'sensitivity', 'temperature', 'humidity'], | |
| where: { | |
| siteKey: this._req.session.site.siteKey, | |
| // type: search.type, | |
| deviceKey: search.deviceKey, | |
| statusDatetime: { | |
| [Sequelize.Op.gte]: search.statusDatetimeFr, | |
| [Sequelize.Op.lte]: search.statusDatetimeTo | |
| // '$gte': search.statusDatetimeFr, | |
| // '$lte': search.statusDatetimeTo | |
| } | |
| }, | |
| order: [ | |
| ['statusDatetime', 'ASC'] | |
| ] | |
| }; | |
| if (search.uuid != null) { | |
| query.where.statusDatetime = { | |
| [Sequelize.Op.gte]: search.statusDatetimeFr, | |
| [Sequelize.Op.lt]: search.statusDatetimeTo | |
| }; | |
| } | |
| return query; | |
| } | |
| // ajaxList2(req, res, next) { | |
| // let search = this.getSearchData(req, res, next, 'ajaxList'); | |
| // let query = { where: { site: req.session.site.id, deleted: false } }; | |
| // query = this.getSearchQuery(search, query, 'ajaxList'); | |
| // this.model.table.findAll(query) | |
| // .then(function(results) { | |
| // if (results == null) { | |
| // utils.obj.resJson(res, 2, 'Data not found.', {}, {}); | |
| // return; | |
| // } | |
| // let data = { | |
| // search: search, | |
| // data: results | |
| // }; | |
| // utils.obj.resJson(res, 0, 'ok', data, {}); | |
| // }).catch(function(err) { | |
| // utils.obj.resJson(res, 1, utils.obj.getErrorMessage(err), {}, err); | |
| // utils.obj.loggerError(err); | |
| // }); | |
| // } | |
| getSearchDataList(req, res, next, action) { | |
| let item = { | |
| siteKey: req.session.site.siteKey, | |
| record: req.query.record || 'none', | |
| statusDatetimeFr: req.query.statusDatetimeFr || moment().format('YYYY-MM-DD'), | |
| statusDatetimeTo: req.query.statusDatetimeTo || moment().format('YYYY-MM-DD') | |
| }; | |
| return item; | |
| } | |
| getSearchQueryList(search, query, action) { | |
| query = { | |
| // attributes: ['statusDatetime', 'dcVoltage', 'dcElectric', 'preventPotential', 'acVoltage', 'acElectric'], | |
| where: { | |
| siteKey: search.siteKey, | |
| deviceKey: search.deviceKey, | |
| statusDatetime: { | |
| [Sequelize.Op.gte]: search.statusDatetimeFr.replace(/-/g, ''), | |
| [Sequelize.Op.lte]: search.statusDatetimeTo.replace(/-/g, '') + '999999' | |
| } | |
| }, | |
| order: [ | |
| ['statusDatetime', 'ASC'] | |
| ] | |
| }; | |
| return query; | |
| } | |
| //--- /scada/devicedata2s?action=list&record=1 | |
| async list(req, res, next) { | |
| let theme = this.getTheme('list', req, 'list', 'list.ejs'); | |
| theme.javascriptFiles.push('/js/datatables.min.js'); //--- dataTables | |
| theme.cssFiles.push('/css/datatables.min.css'); //--- dataTables | |
| let search = this.getSearchDataList(req, res, next, 'list'); | |
| let query = { where: { id: search.record, deleted: false } }; | |
| let device = await modules.devices.model.table.findOne(query); | |
| if (device != null) { | |
| search.deviceKey = device.deviceKey; | |
| } | |
| query = this.getSearchQueryList(search, query, 'list'); | |
| let results = await this.model.table.findAll(query); | |
| let data = { | |
| search: search, | |
| data: results | |
| }; | |
| res.render(utils.getTemplateFullPath(theme.serviceInfo.site.id, theme.layout), { theme: theme, baseUrl: req.baseUrl, session: req.session, data: data }); | |
| } | |
| updateView(req, res, next) { | |
| let theme = this.getTheme('updateView', req, 'updateView', 'updateView.ejs'); | |
| let record = req.query.record; | |
| let query = { where: { id: record } }; | |
| this.model.table.findOne(query) | |
| .then(function(results) { | |
| //--- To-Do : 읽은 데이터가 없는 경우 (results == null), 오류 페이지를 표시 한다. | |
| let data = { | |
| item: results, | |
| device: req.query.device | |
| }; | |
| res.render(utils.getTemplateFullPath(theme.serviceInfo.site.id, theme.layout), { theme: theme, baseUrl: req.baseUrl, session: req.session, data: data }); | |
| }).catch(utils.obj.loggerError); | |
| } | |
| //--- /scada/devicedata2s?action=update&record=1702&device=1 | |
| ajaxUpdate(req, res, next) { | |
| let record = req.body.record; | |
| let item = this.getBrowserData(req, res, next, 'update'); | |
| let checkErrors = this.checkData('update', item); | |
| if (0 < checkErrors.count) { | |
| //--- To-Do : 입력 데이터에 대한 오류 처리를 한다. | |
| utils.obj.resJson(res, 2, '입력 데이터 오류', {}, {}); | |
| return; | |
| } | |
| let myPromise = new Promise(function(resolve, reject) { | |
| let query = { where: { id: record } }; | |
| this.model.table.findOne(query) | |
| .then(function(itemOld) { | |
| let params = { itemOld: itemOld }; | |
| if (itemOld == null) { | |
| logger.error(`include/view.js :: Error : 수정할 데이터가 없습니다.`); | |
| reject({}); | |
| } else { | |
| resolve(params); | |
| } | |
| }.bind(this)).catch(); | |
| }.bind(this)); | |
| myPromise = myPromise.then(function(params) { | |
| return new Promise(function(resolve, reject) { | |
| this.model.table.update(item, { where: { id: record } }) | |
| .then(function(results) { | |
| utils.obj.resJson(res, 0, 'ok', results, {}); | |
| }.bind(this)).catch(reject); | |
| }.bind(this)); | |
| }.bind(this)); | |
| myPromise = myPromise.catch(function(err) { //--- reject() 함수에서 에러 처리를 위해 호출 한다. | |
| utils.obj.loggerError(err); | |
| utils.obj.resJson(res, 3, '수정시 오류 발생', {}, {}); | |
| }.bind(this)); | |
| } | |
| detail(req, res, next) { | |
| let theme = this.getTheme('detail', req, 'detail', 'detail.ejs'); | |
| let record = req.query.record; | |
| let query = { where: { id: record } }; | |
| this.model.table.findOne(query) | |
| .then(function(results) { | |
| //--- To-Do : 읽은 데이터가 없는 경우 (results == null), 오류 페이지를 표시 한다. | |
| let data = { | |
| item: results, | |
| device: req.query.device | |
| }; | |
| res.render(utils.getTemplateFullPath(theme.serviceInfo.site.id, theme.layout), { theme: theme, baseUrl: req.baseUrl, session: req.session, data: data }); | |
| }).catch(utils.obj.loggerError); | |
| } | |
| } | |
| module.exports = Devicedata2sView; | |