# -*- coding: utf-8 -*- """ demeter web page name:device.py author:rabin """ from .__load__ import * # 首页 class index_path(Load): @Web.auth @Web.setting def get(self): if 'url' in self.data: self.redirect(self.data['url']) # 获取网关列表 self.data['gateway'] = self.service('common').list('device_gateway', state=True, search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') # 获取页面 self.data['page'] = self.service('common').list('device_page', order='inorder asc,cdate desc', search={'farm_id':self.data['setting']['farm']}) self.view("device/index.html") #批量控制 class mul_path(Load): @Web.auth @Web.setting def get(self): self.data['list'] = self.service('common').list('device_mul', search={'farm_id':self.data['setting']['farm']}) if self.data['list']: model = Demeter.model('device_mul_queue') for key, value in enumerate(self.data['list']): model.status.ins((2,3)) model.mul_id = value['id'] info = model.select() #self.data['list'][key]['oper'] = '' if info: for v in info: if v['oper']: self.data['list'][key]['oper'] = v['oper'] self.view("device/mul.html") @Web.auth @Web.setting def post(self): id = self.input('id') value = self.input('value') if id: info = self.service('common').one('device_mul', id=id) if info: update = {} update['value'] = value if info['device_type'] == 1: if value == '1': update['oper'] = '批量启动中' else: update['oper'] = '批量关闭中' else: if value == '1': update['oper'] = '批量升起中' elif value == '2': update['oper'] = '批量下降中' elif value == '5': update['oper'] = '批量停止中' self.service('common').update('device_mul', id, update, cdate=False) if info['devices']: devices = tuple(eval(info['devices'])) for device in devices: Demeter.service('device').switch(device, switch=value, mul=id) self.out('yes', {'id':id}) else: self.out('no') #大屏幕 class screen_path(Load): @Web.auth @Web.setting def get(self): device = self.input('device') # 获取摄像头图片 self.data['camera'] = Device.get(self, search={'hardware_type':6,'farm_id':self.data['setting']['farm']}) # 获取当前数据 self.data['device'] = Device.get(self, search={'id--ins':self.data['setting']['farmInfo']['gateway'], 'hardware_type--ins':(2,4,5),'farm_id':self.data['setting']['farm']}) deviceInfo = {} if self.data['device']: for key, value in enumerate(self.data['device']): if value['type_id'] > 0: type_info = self.service('common').one('setting_device_type', id=value['type_id']) self.data['device'][key]['unit'] = type_info['unit'] self.data['device'][key]['typeName'] = type_info['name'] if not device and not deviceInfo: deviceInfo = self.data['device'][key] self.data['list'] = ({'name':'实时数据监测', 'device':self.data['device']},) Device.statBase(self, deviceInfo) log_search = {} if device: log_search['device_id'] = device else: log_search['device_id--ins'] = self.data['setting']['farmInfo']['gateways'] log_search['device_id--ins'] = self.data['setting']['farmInfo']['gateways'] log_search['type_id'] = 2 log_search['farm_id'] = self.data['setting']['farm'] # 获取设备日志 self.data['logs'] = self.service('common').list('msg', search=log_search, limit='0,1') if self.data['logs']: for key, value in enumerate(self.data['logs']): type_info = self.service('common').one('msg_type', id=value['type_id']) self.data['logs'][key]['type'] = type_info['name'] self.data['logs'][key]['cdates'] = Demeter.date(value['cdate']) self.data['info'] = self.data['setting']['farmInfo'] if not self.data['info']['welcome']: self.data['info']['welcome'] = self.data['info']['name'] self.data['noset'] = True self.data['stat'] = True self.view("device/screen.html") #大屏幕设置 class screen_set_path(Load): @Web.auth @Web.setting def get(self): self.data['info'] = self.data['setting']['farmInfo'] self.data['info']['devices'] = self.data['info']['gateway'] self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type--ins' : (2,4,5),'farm_id':self.data['setting']['farm']}) self.view("device/alert/screen.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['welcome'] = self.input('update_name') update['gateway'] = self.inputs('update_devices') if not update['gateway']: self.out(u'请选择设备') return update['gateway'] = str(update['gateway']) if update['welcome']: value = update['welcome'] else: value = self.data['setting']['farmInfo']['name'] key = 'update/w/' + id from demeter.mqtt import Pub pub = Pub() pub.push(key, value) state = self.service('common').update('farm', id, update) self.out('yes', {'id':state}) #特殊的大屏幕 class screens_path(Load): @Web.setting def get(self): farm = self.input('ids', '2,3,4') self.data['farm'] = farm farm = farm.split(',') device = self.input('device') farmInfo = self.service('common').one('farm', id=farm[0]) # 获取摄像头图片 #self.data['camera'] = Device.get(self, search={'hardware_type':6,'farm_id--ins':farm}) self.data['camera'] = [] # 获取当前数据 deviceList = Device.get(self, search={'hardware_type--ins':(2,4,5),'farm_id--ins':farm}) self.data['device'] = {} for v in farm: self.data['device'][v] = [] deviceInfo = {} devices = [] if deviceList: for key, value in enumerate(deviceList): if value['type_id'] > 0: type_info = self.service('common').one('setting_device_type', id=value['type_id']) value['unit'] = type_info['unit'] value['typeName'] = type_info['name'] self.data['device'][str(value['farm_id'])].append(value) devices.append(value['id']) if not device and not deviceInfo: deviceInfo = value else: self.data['device'][str(value['farm_id'])].append(value) devices.append(value['id']) self.data['deviceList'] = {} for v in farm: farmData = self.service('common').one('farm', id=int(v)) if farmData: self.data['deviceList'][v] = ({'name':farmData['name'] + '实时检测', 'device':self.data['device'][v]},) Device.statBase(self, deviceInfo) log_search = {} if device: log_search['device_id'] = device else: log_search['device_id--ins'] = devices log_search['device_id--ins'] = devices log_search['type_id'] = 2 log_search['farm_id--ins'] = farm # 获取设备日志 self.data['logs'] = self.service('common').list('msg', search=log_search, limit='0,1') if self.data['logs']: for key, value in enumerate(self.data['logs']): type_info = self.service('common').one('msg_type', id=value['type_id']) self.data['logs'][key]['type'] = type_info['name'] self.data['logs'][key]['cdates'] = Demeter.date(value['cdate']) self.data['info'] = farmInfo if not self.data['info']['welcome']: self.data['info']['welcome'] = self.data['info']['name'] self.data['info']['welcome'] = '保定清苑农业局物联网监测系统' self.data['noset'] = True self.data['stat'] = True self.view("device/screens.html") #获取设备数据和设备控制 class data_path(Load): @Web.auth @Web.setting def get(self): Device.data(self) if 'stat' in self.data and self.data['stat']: Device.statBase(self, self.data['stat']) self.view("device/data.html") #获取图像 class pic_path(Load): @Web.auth @Web.setting def get(self): Device.pic(self) self.view("device/pic.html") #数据统计 class stat_path(Load): @Web.auth @Web.setting def get(self): Device.stat(self) self.view("device/stat.html") #数据导出 class out_path(Load): @Web.auth @Web.setting def get(self): Device.out(self) self.data['input'] = {} self.data['input']['type'] = self.input('type') self.data['input']['id'] = self.input('id') self.view("device/out.html") #设备日志 class log_path(Load): @Web.auth @Web.setting def get(self): Device.log(self) self.view("device/logs.html") #设备分组 class group_path(Load): @Web.auth @Web.setting def get(self): # 获取页面 self.data['page'] = self.service('common').list('device_page', order='inorder asc,cdate desc', search={'farm_id':self.data['setting']['farm']}) if self.data['page']: for key, value in enumerate(self.data['page']): self.data['page'][key]['group'] = self.service('common').list('device_group', search={'page_id':value['id'],'farm_id':self.data['setting']['farm']} , order='inorder asc,cdate desc') if self.data['page'][key]['group']: for groupk, groupv in enumerate(self.data['page'][key]['group']): self.data['page'][key]['group'][groupk]['device'] = Device.get(self, search={'id--ins':groupv['devices'],'farm_id':self.data['setting']['farm']}) self.view("device/group.html") #更新设备的上下限 class update_device_path(Load): @Web.auth @Web.setting def get(self): id = self.input('device_id') if id: Device.data(self) else: id = self.input('id') self.data['set'] = {} if id: self.data['set'] = self.service('common').one('device_info', id=id) if self.data['set'] and self.data['set']['type_id'] > 0: self.data['type'] = self.service('common').one('setting_device_type', id=self.data['set']['type_id']) self.view("device/alert/device.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['max'] = self.input('update_max') update['min'] = self.input('update_min') if not update['max'] and not update['min']: self.out(u'请至少输入一个上限或者下限') return if not update['min']: del update['min'] if not update['max']: del update['max'] state = self.service('common').update('device_info', id, update) self.out('yes', {'id':state}) #更新页面 class update_page_path(Load): @Web.auth @Web.setting def get(self): id = self.input('id') #self.data['ico'] = ('cj', 'kz', 'syt', 'wg', 'pl', 'dpm', 'fz', 'zn') self.data['ico'] = ('cj', 'kz', 'syt') self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_page', id=id) else: self.data['info']['ico'] = self.data['ico'][0] self.view("device/alert/page.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['name'] = self.input('update_name') update['ico'] = self.input('update_ico') update['inorder'] = self.input('update_inorder', 0) if not update['inorder']: update['inorder'] = 0 update['inorder'] = int(update['inorder']) update['stat'] = self.input('update_stat') if not update['name']: self.out(u'请输入页面名称') return if not update['ico']: self.out(u'请选择图标') return if update['stat']: update['stat'] = True else: update['stat'] = False state = self.service('common').update('device_page', id, update) self.out('yes', {'id':state}) #更新分组 class update_group_path(Load): @Web.auth @Web.setting def get(self): page = self.input('page') self.data['page'] = self.service('common').one('device_page', id=page) id = self.input('id') self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_group', id=id) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'farm_id':self.data['setting']['farm']}) self.view("device/alert/group.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['page_id'] = self.input('update_page_id') update['name'] = self.input('update_name') update['devices'] = self.inputs('update_devices') update['inorder'] = self.input('update_inorder', 0) if not update['inorder']: update['inorder'] = 0 update['inorder'] = int(update['inorder']) if not update['name']: self.out(u'请输入分组名称') return if not update['devices']: self.out(u'请选择设备') return update['devices'] = str(update['devices']) state = self.service('common').update('device_group', id, update) self.out('yes', {'id':state}) @Web.auth @Web.setting def delete(self): id = self.input('id') if not id: self.out('no') return state = self.service('common').rDelete('device_group', id) self.out('yes', {'state':state}) #智能设置-批量设置 class set_mul_path(Load): @Web.auth @Web.setting def get(self): self.data['name'] = '批量控制' Device.set(self) self.data['mul'] = self.service('common').list('device_mul', search={'farm_id':self.data['setting']['farm']}) if self.data['mul']: for key, value in enumerate(self.data['mul']): self.data['mul'][key]['device'] = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']}) self.view("device/set/mul.html") #更新批量控制 class update_set_mul_path(Load): @Web.auth @Web.setting def get(self): id = self.input('id') self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_mul', id=id) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']}) self.data['type'] = ({'id':1, 'name':'开关'},{'id':2, 'name':'卷帘'}) self.view("device/alert/mul.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['name'] = self.input('update_name') update['device_type'] = self.input('update_type') update['devices'] = self.inputs('update_devices') if not update['name']: self.out(u'请输入名称') return if not update['device_type']: self.out(u'请选择设备类型') return if not update['devices']: self.out(u'请选择设备') return update['devices'] = str(update['devices']) state = self.service('common').update('device_mul', id, update) self.out('yes', {'id':state}) @Web.auth @Web.setting def delete(self): id = self.input('id') if not id: self.out('no') return state = self.service('common').rDelete('device_mul', id) self.out('yes', {'state':state}) #智能设置-定时设置 class set_timing_path(Load): @Web.auth @Web.setting def get(self): self.data['name'] = '定时设置' Device.set(self) Device.setCronOper(self) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']}) self.data['list'] = self.service('common').list('device_set_timing', state=True, page={'num':10}, search={'farm_id':self.data['setting']['farm']}) if self.data['list']: for key, value in enumerate(self.data['list']): self.data['list'][key]['device'] = [] if value['devices']: device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']}) if device: self.data['list'][key]['device'] = device self.data['list'][key]['oper'] = self.data['oper'][value['oper']-1]['name'] self.data['list'][key]['cdates'] = Demeter.date(value['cdate']) self.data['list'][key]['zdates'] = Demeter.date(value['zdate']) if value['status'] == 1: self.data['list'][key]['status_text'] = '执行成功' elif value['status'] == 2: self.data['list'][key]['status_text'] = '执行失败' self.view("device/set/timing.html") class update_set_timing_path(Load): @Web.auth @Web.setting def get(self): id = self.input('id') Device.setCronOper(self) self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_set_timing', id=id) if self.data['info'] and self.data['info']['zdate']: self.data['info']['zdate'] = Demeter.date(self.data['info']['zdate']) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']}) self.view("device/alert/timing.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['devices'] = self.inputs('update_devices') update['oper'] = self.input('update_oper') update['zdate'] = self.input('update_zdate') if not update['devices']: self.out(u'请选择设备') return if not update['oper']: self.out(u'请选择执行结果') return if not update['zdate']: self.out(u'请选择执行时间') return update['devices'] = str(update['devices']) state = self.service('common').update('device_set_timing', id, update) self.out('yes', {'id':state}) @Web.auth @Web.setting def delete(self): id = self.input('id') if not id: self.out('no') return state = self.service('common').rDelete('device_set_timing', id) self.out('yes', {'state':state}) #智能设置-周期设置 class set_loop_path(Load): @Web.auth @Web.setting def get(self): self.data['name'] = '周期设置' Device.set(self) Device.setCronOper(self) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']}) self.data['list'] = self.service('common').list('device_set_loop', page={'num':10}, search={'farm_id':self.data['setting']['farm']}) if self.data['list']: for key, value in enumerate(self.data['list']): self.data['list'][key]['device'] = [] if value['devices']: device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']}) if device: self.data['list'][key]['device'] = device if value['looptype'] == 2: value['loop'] = value['loop'].split(',') loop = [] for v in self.data['week']: if str(v['id']) in value['loop']: loop.append(v['name']) loop = ' '.join(loop) + ' ' elif value['looptype'] == 3: loop = value['loop'].replace(',', ' ') + ' ' else: loop = '' self.data['list'][key]['loop'] = loop self.data['list'][key]['oper'] = self.data['oper'][value['oper']-1]['name'] self.data['list'][key]['looptype'] = self.data['looptype'][value['looptype']-1]['list'] self.data['list'][key]['cdates'] = Demeter.date(value['cdate']) self.data['list'][key]['hour'] = Demeter.hour(value['hour']) self.data['list'][key]['minute'] = Demeter.hour(value['minute']) self.view("device/set/loop.html") class update_set_loop_path(Load): @Web.auth @Web.setting def get(self): id = self.input('id') Device.setCronOper(self) self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_set_loop', id=id) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']}) self.view("device/alert/loop.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['devices'] = self.inputs('update_devices') update['oper'] = self.input('update_oper') update['loop'] = self.input('update_loop') update['hour'] = self.input('update_hour') update['minute'] = self.input('update_minute') update['looptype'] = self.input('update_looptype') if not update['devices']: self.out(u'请选择设备') return if not update['oper']: self.out(u'请选择执行结果') return if not update['hour']: self.out(u'请选择执行时间') return if not update['looptype']: self.out(u'请选择执行时间') return if update['looptype'] != '1' and not update['loop']: self.out(u'请选择执行时间') return update['devices'] = str(update['devices']) state = self.service('common').update('device_set_loop', id, update) self.out('yes', {'id':state}) @Web.auth @Web.setting def delete(self): id = self.input('id') if not id: self.out('no') return state = self.input('state', False) update = {} update['status'] = state state = self.service('common').update('device_set_loop', id, update) self.out('yes', {'state':state}) #智能设置-条件控制 class set_condition_path(Load): @Web.auth @Web.setting def get(self): self.data['name'] = '条件控制' Device.set(self) Device.setCronOper(self) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']}) self.data['list'] = self.service('common').list('device_set_condition', state=True, page={'num':10}, search={'farm_id':self.data['setting']['farm']}) if self.data['list']: for key, value in enumerate(self.data['list']): self.data['list'][key]['device'] = [] if value['devices']: device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']}) if device: self.data['list'][key]['device'] = device self.data['list'][key]['cdates'] = Demeter.date(value['cdate']) self.data['list'][key]['oper'] = self.data['opers'][value['oper']-1]['name'] self.data['list'][key]['condition'] = self.data['condition'][value['condition']-1]['exp'] gateway = self.service('common').one('device_gateway', id=value['gateway_id']) self.data['list'][key]['gatewayName'] = gateway['name'] device = self.service('common').one('device_info', id=value['device_id']) if device: self.data['list'][key]['deviceName'] = device['name'] type_info = self.service('common').one('setting_device_type', id=device['type_id']) self.data['list'][key]['unit'] = type_info['unit'] self.view("device/set/condition.html") class update_set_condition_path(Load): @Web.auth @Web.setting def get(self): id = self.input('id') Device.setCronOper(self) self.data['info'] = {} if id: self.data['info'] = self.service('common').one('device_set_condition', id=id) self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']}, order='hardware_id asc') self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']}) self.data['device_list'] = self.service('common').list('device_info', search={'hardware_type--ins' : (2,4),'gateway_id':self.data['gateway'][0]['id']}) self.view("device/alert/condition.html") @Web.auth @Web.setting def post(self): id = self.input('id') update = {} update['devices'] = self.inputs('update_devices') update['oper'] = self.input('update_oper') update['gateway_id'] = self.input('update_gateway_id') update['device_id'] = self.input('update_device_id') update['condition'] = self.input('update_condition') update['value'] = self.input('update_value') update['notice'] = self.input('update_notice') if not update['devices']: self.out(u'请选择设备') return if not update['oper']: self.out(u'请选择执行结果') return if not update['gateway_id']: self.out(u'请选择设备条件') return if not update['device_id']: self.out(u'请选择设备条件') return if not update['condition']: self.out(u'请选择判断条件') return if not update['value']: self.out(u'请输入判断条件的值') return if update['notice']: update['notice'] = True else: update['notice'] = False update['devices'] = str(update['devices']) state = self.service('common').update('device_set_condition', id, update) self.out('yes', {'id':state}) @Web.auth @Web.setting def delete(self): id = self.input('id') if not id: self.out('no') return state = self.input('state', False) update = {} update['status'] = state state = self.service('common').update('device_set_condition', id, update) self.out('yes', {'state':state}) #更新控制设备的状态 class update_path(Load): @Web.auth @Web.setting def post(self): id = self.input('id') value = self.input('value') if id: Demeter.service('device').switch(id, switch=value) self.out('yes', {'id':id}) """ yield self.switch(id, value) Shell.popen(Demeter.path + 'switch.py ' + id + ' ' + value, bg=True) response = response.replace("\n", "") if response == 'ok': self.out('yes', {'id':id}) else: model = Demeter.model('notice_type') model.key = response info = model.select(type='fetchone') if info: response = info['name'] self.out(response) """ else: self.out('no') @tornado.gen.coroutine def switch(self, id, value): Demeter.service('device').switch(id, switch=value) class get_path(Load): @Web.auth @Web.setting def get(self): # 根据网关id获取设备 id = self.input('id') device = Device.get(self, search={'gateway_id':id, 'hardware_type': 2,'farm_id':self.data['setting']['farm']}) self.view("device/alert/select_device.html")