#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
demeter web page
name:device.py 设备相关页面
author:rabin
"""
from __load__ import *
class page_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'页面' #中文名
,path = 'page' #路径
,width = '600' # 新增页面的宽度
,height = '200' # 新增页面的高度
,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'页面名称')) #搜索
,thead = (u'页面名称', '子页面', u'更新时间') #表头
,tbody = ('name', 'group', 'cdate') #表内容
,state = True #启用回收站
)
self.commonList('device_page')
if self.data['list']:
for key, value in enumerate(self.data['list']):
self.data['list'][key]['group'] = '分组'
self.commonView('list')
class page_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'page'
,label = (u'标题',)
,update = ('name-input-required',)
)
self.commonOne('device_page')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_page')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_page')
class group_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'分组'
,path = 'group'
,width = '600'
,height = '250'
,full = True
,search = (('label-1','cdate-time-start','cdate-time-end','page_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择页面',u'分组名称'))
,thead = (u'页面名称', u'分组名称', '设备', u'更新时间')
,tbody = ('page', 'name', 'device', 'cdate')
,state = True
)
self.data['common']['search_page_id-select-'] = self.service('common').list('device_page')
self.commonList('device_group')
if self.data['list']:
for key, value in enumerate(self.data['list']):
page = self.service('common').one('device_page', id=value['page_id'])
self.data['list'][key]['page'] = page['name']
if value['devices']:
#self.data['list'][key]['device'] = '查看设备'
self.data['list'][key]['device'] = '查看设备'
else:
self.data['list'][key]['device'] = '暂无设备'
self.commonView('list')
class group_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'group'
,label = (u'所属页面', u'标题', u'选择设备')
,update = ('page_id-select-required','name-input-required', 'devices-checkboxs-')
,update_page_id = self.service('common').list('device_page')
,update_devices = self.getDevice()
)
self.commonOne('device_group')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_group')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_group')
def getDevice(self):
gateway = self.service('common').list('device_gateway')
if gateway:
for key, value in enumerate(gateway):
gateway[key]['child'] = self.service('common').list('device_info', True, {'gateway_id-input-':value['id']})
return gateway
class mul_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'批量控制'
,path = 'mul'
,width = '600'
,height = '250'
,full = True
,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'分组名称'))
,thead = (u'分组名称', '设备', u'更新时间')
,tbody = ('name', 'device', 'cdate')
,state = True
)
self.commonList('device_mul')
if self.data['list']:
for key, value in enumerate(self.data['list']):
if value['devices']:
#self.data['list'][key]['device'] = '设备'
self.data['list'][key]['device'] = '查看设备'
else:
self.data['list'][key]['device'] = '暂无设备'
self.commonView('list')
class mul_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'mul'
,label = (u'标题', u'选择设备')
,update = ('name-input-required', 'devices-checkboxs-')
,update_devices = self.getDevice()
)
self.commonOne('device_mul')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_mul')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_mul')
def getDevice(self):
gateway = self.service('common').list('device_gateway')
if gateway:
for key, value in enumerate(gateway):
gateway[key]['child'] = self.service('common').list('device_info', True, {'gateway_id-input-':value['id']})
return gateway
class gateway_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'网关'
,path = 'gateway'
,width = '600'
,height = '300'
#,add = False
#,edit = False
,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'网关名称'))
,thead = (u'所属农场',u'网关名称', u'设备id', u'网关连接状态', u'电源数值', u'电源连接状态', u'更新时间')
,tbody = ('farm','name', 'hardware_id', 'status', 'power_num', 'power_status', 'cdate')
,state = False
)
self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
self.commonList('device_gateway')
if self.data['list']:
for key, value in enumerate(self.data['list']):
farm = self.service('common').one('farm', id=value['farm_id'])
self.data['list'][key]['farm'] = farm['name']
if value['status']:
self.data['list'][key]['status'] = '开启'
else:
self.data['list'][key]['status'] = '关闭'
if value['power_status']:
self.data['list'][key]['power_status'] = '连接'
else:
self.data['list'][key]['power_status'] = '断开'
self.commonView('list')
class gateway_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'gateway'
#,label = (u'网关名称', u'设备id',u'网关连接状态',u'电源数值',u'电源连接状态')
,label = (u'所属农场', u'网关名称', u'设备id')
,update = ('farm_id-select-required', 'name-input-required','hardware_id-input-required')
#,update = ('name-input-required','hardware_id-input-required','status-switch--开启|关闭','power_num-input-','power_status-switch--连接|断开')
,update_farm_id = Demeter.config['setting']['farmList']
)
self.commonOne('device_gateway')
self.commonView('update')
@tornado.web.authenticated
def post(self):
id = self.commonUpdate('device_gateway')
self.hardware(id)
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_gateway')
@tornado.web.authenticated
def hardware(self, id):
hardware_id = self.data['update']['hardware_id']
if hardware_id and id > 0:
info = self.service('common').one('hardware', hardware_id=hardware_id)
if not info:
insert = {}
insert['farm_id'] = self.data['update']['farm_id']
insert['hardware_id'] = hardware_id
insert['gateway_id'] = id
insert['hardware_type'] = 1
self.service('common').update('hardware', None, insert)
class info_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'设备'
,path = 'info'
,width = '600'
,height = '300'
,add = False
#,edit = False
,search = (('farm_id-select-','type_id-select-', 'gateway_id-select-', 'hardware_type-select-'), (u'选择农场',u'设备名称',u'设备类型',u'网关',u'硬件类型'))
,thead = (u'所属农场', u'设备id', u'连接状态', u'数值', u'设备类型', u'网关',u'硬件类型')
,tbody = ('farm','name', 'hardware_id', 'status', 'show_num', 'type', 'gateway', 'hardware_type')
,state = False
)
self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
self.data['common']['search_type_id-select-'] = self.service('common').list('device_type')
self.data['common']['search_gateway_id-select-'] = self.service('common').list('device_gateway')
self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
del self.data['common']['search_hardware_type-select-'][0]
self.commonList('device_info')
if self.data['list']:
for key, value in enumerate(self.data['list']):
farm = self.service('common').one('farm', id=value['farm_id'])
self.data['list'][key]['farm'] = farm['name']
info = self.service('common').one('device_type', id=value['type_id'])
self.data['list'][key]['type'] = info['name']
info = self.service('common').one('device_gateway', id=value['gateway_id'])
self.data['list'][key]['gateway'] = info['name']
if value['status']:
self.data['list'][key]['status'] = '连接'
else:
self.data['list'][key]['status'] = '断开'
if value['show_num']:
# 根据设备类型type_id得到单位和计算公式
self.data['list'][key]['show_num'] = value['show_num'] + '
更新时间:' + Demeter.date(value['cdate'])
else:
self.data['list'][key]['show_num'] = '0'
hardware_type = self.service('common').one('hardware_type', id=value['hardware_type'])
self.data['list'][key]['hardware_type'] = hardware_type['name']
self.commonView('list')
class info_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'info'
#,label = (u'网关名称', u'设备id',u'网关连接状态',u'电源数值',u'电源连接状态')
,label = (u'所属农场',u'设备名称', u'设备id')
,update = ('farm_id-select-required', 'name-input-required','hardware_id-show-')
#,update = ('name-input-required','hardware_id-input-required','status-switch--开启|关闭','power_num-input-','power_status-switch--连接|断开')
,update_farm_id = Demeter.config['setting']['farmList']
)
self.commonOne('device_info')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_info')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_info')
class type_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'类型'
,path = 'type'
,width = '600'
,height = '400'
,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称'))
,thead = (u'类型名称', u'类型标识', u'类型单位', u'表达式', u'更新时间')
,tbody = ('name', 'key', 'unit', 'exp', 'cdate')
,state = True
)
self.commonList('device_type')
self.commonView('list')
class type_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'type'
,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式')
,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据')
)
self.commonOne('device_type')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_type')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_type')
class printer_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
name = u'打印机'
,path = 'printer'
,width = '600'
,height = '400'
,search = (('farm_id-select-','name-input-mlike'), (u'选择农场',u'打印机名称'))
,thead = (u'所属农场', u'打印机名称', u'打印机地址', u'更新时间')
,tbody = ('farm','name', 'host', 'cdate')
,state = True
)
self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
self.commonList('device_printer')
if self.data['list']:
for key, value in enumerate(self.data['list']):
farm = self.service('common').one('farm', id=value['farm_id'])
self.data['list'][key]['farm'] = farm['name']
self.commonView('list')
class printer_update_path(Load):
@tornado.web.authenticated
def get(self):
self.common(
path = 'printer'
,label = (u'所属农场', u'打印机名称', u'打印机地址')
,update = ('farm_id-select-required', 'name-input-required','host-input-required')
,update_farm_id = Demeter.config['setting']['farmList']
)
self.commonOne('device_printer')
self.commonView('update')
@tornado.web.authenticated
def post(self):
self.commonUpdate('device_printer')
@tornado.web.authenticated
def delete(self):
self.commonDelete('device_printer')