# -*- coding: utf-8 -*- """ demeter setting page name:setting.py 服务协议管理 author:rabin """ from .__load__ import * import xlrd class server_path(Load): @Web.auth @Web.setting def get(self): self.set( name = u'服务管理' ,path = '/setting/server' ,width = '600' ,height = '600' ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'服务名称')) ,thead = (u'服务名称', u'服务类型', u'服务地址', u'服务端口', u'更新时间') ,tbody = ('name', 'server_type', 'server_host', 'server_port', 'cdate') ,state = True ) self.list('setting_server') if self.data['list']: server_type = Demeter.service('core', 'setting').server_type() for key, value in enumerate(self.data['list']): self.data['list'][key]['server_type'] = server_type[value['server_type']-1]['name'] self.show('list') class server_update_path(Load): @Web.auth @Web.setting def get(self): self.set( path = '/setting/server' ,label = (u'服务名称',u'服务类型',u'服务地址',u'服务端口',u'请求间隔') ,update = ('name-input-required','server_type-select-required','server_host-input-required','server_port-input-required','server_time-input-required') ,update_server_type = Demeter.service('core', 'setting').server_type() ) self.one('setting_server') self.show('update') @Web.auth @Web.setting def post(self): self.update('setting_server') @Web.auth @Web.setting def delete(self): self.drop('setting_server') class product_path(Load): @Web.auth @Web.setting def get(self): self.set( name = u'产品管理' ,path = '/setting/product' ,width = '600' ,height = '600' ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'产品名称')) ,thead = (u'产品名称',u'产品标识', u'更新时间') ,tbody = ('name', 'key','cdate') ,state = True ) self.list('setting_product') self.show('list') class product_update_path(Load): @Web.auth @Web.setting def get(self): self.set( path = '/setting/product' ,label = (u'产品名称',u'产品标识') ,update = ('name-input-required','key-input-required') ) self.one('setting_product') self.show('update') @Web.auth @Web.setting def post(self): id = self.update('setting_product') @Web.auth @Web.setting def delete(self): self.drop('setting_product') class device_type_path(Load): @Web.auth @Web.setting def get(self): self.set( name = u'设备类型' ,path = '/setting/device_type' ,width = '600' ,height = '400' ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称')) ,thead = (u'类型ID', u'类型名称', u'类型标识', u'类型单位', u'更新时间') ,tbody = ('id', 'name', 'key', 'unit', 'cdate') ,state = True ) self.list('setting_device_type') self.show('list') class device_type_update_path(Load): @Web.auth @Web.setting def get(self): self.set( path = '/setting/device_type' #,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式') #,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据') ,label = (u'类型名称', u'类型标识',u'类型单位',u'操作类型',u'可选项') ,update = ('name-input-required','key-input-required','unit-input-','data_type-select-','data_option-text--多个换行隔开') ,update_data_type = Demeter.service('core', 'setting').data_type() ) self.one('setting_device_type') self.show('update') @Web.auth @Web.setting def post(self): id = self.update('setting_device_type') @Web.auth @Web.setting def delete(self): self.drop('setting_device_type') class modbus_code_path(Load): @Web.auth @Web.setting def get(self): self.set( name = u'设备命令码' ,path = '/setting/modbus_code' ,width = '600' ,height = '600' ,button = ({'name':'批量上传','link':'/setting/upload_update'},) ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike','product_id-select-'), (u'日期范围',u'开始时间',u'截止时间',u'设备名称',u'选择产品')) ,thead = (u'命令码名称', u'所属产品',u'关联设备类型', u'表达式', u'更新时间') ,tbody = ('name', 'product','device_type_key', 'exp', 'cdate') ,state = True ) self.data['common']['search_product_id-select-'] = self.service('common').list('setting_product') self.list('setting_modbus_code') if self.data['list']: for key, value in enumerate(self.data['list']): product = self.service('common').one('setting_product', id=value['product_id']) self.data['list'][key]['product'] = product['name'] self.show('list') class upload_update_path(Load): @Web.auth @Web.setting def get(self): self.set( path = '/setting/upload' ,label = (u'所属产品', u'上传输入点' ,u'上传输出点') ,update = ('product_id-select-required','input-file--','output-file--') ,update_product_id = self.service('common').list('setting_product') ) #self.one('setting_upload') self.data['info'] = {} self.show('update') @Web.auth @Web.setting def post(self): product_id = self.input('update_product_id') inputs = self.input('update_input') output = self.input('update_output') sensor = self.input('update_sensor') #self.inputs(inputs, product_id) self.output(output, product_id) #self.sensor(sensor) self.out('操作成功,请关闭本窗口并刷新页面') def output(self, source, product_id): temp = source.split('/upload/') path = Demeter.path + '/runtime/upload/' + temp[1] data = xlrd.open_workbook(path) table = data.sheets()[0] rows = table.nrows cols = table.ncols result = {} for i in range(rows): if i >= 2: name = str(table.cell_value(i,1)) cn_name = str(table.cell_value(i,2)) if name: temp_slave = str(table.cell_value(i,0)) if temp_slave: slave = temp_slave.replace('.0', '') temp_read = str(table.cell_value(i,5)) if temp_read: read = temp_read control_open = str(table.cell_value(i,3)).replace(slave + ',', '') + '#1' control_close = str(table.cell_value(i,4)).replace(slave + ',', '') + '#2' key = str(name) + '_' + str(slave) if key not in result: result[key] = [] result[key].append(name) result[key].append(cn_name) result[key].append(slave) result[key].append(read) result[key].append(control_open) result[key].append(control_close) #product_id = 1 hardware_type = 3 device_type_key = 'sw' model = Demeter.model('setting_modbus_code') for i in result: name = result[i][1] slave = result[i][2] read = result[i][3] control_open = result[i][4] control_close = result[i][5] name = name + '_' + i model.product_id = product_id model.name = name info = model.select(type='fetchone') if not info: model.product_id = product_id model.name = name if '转' in name: model.device_type_key = 'zhuan' else: model.device_type_key = 'sw' model.exp = '{n}' model.hardware_type = hardware_type model.slave_id = int(slave) model.register_read_address = str(read) model.register_write_address = control_open + '||' + control_close model.insert() def inputs(self, source, product_id): temp = source.split('/upload/') path = Demeter.path + '/runtime/upload/' + temp[1] data = xlrd.open_workbook(path) table = data.sheets()[0] rows = table.nrows cols = table.ncols result = {} for i in range(rows): if i >= 2: name = str(table.cell_value(i,1)) cn_name = str(table.cell_value(i,2)) if name: temp_slave = str(table.cell_value(i,3)) if temp_slave: slave = temp_slave.replace('.0', '') temp_read = str(table.cell_value(i,4)) if temp_read: read = temp_read write = str(table.cell_value(i,5)).replace('.0', '') key = str(name) + '_' + str(slave) if key not in result: result[key] = [] result[key].append(name) result[key].append(cn_name) result[key].append(slave) result[key].append(read) result[key].append(write) #product_id = product_id hardware_type = 2 device_type_key = 'zhuan' model = Demeter.model('setting_modbus_code') for i in result: name = result[i][1] slave = result[i][2] read = result[i][3] write = result[i][4] name = name + '_' + i model.product_id = product_id model.name = name info = model.select(type='fetchone') if not info: model.product_id = product_id model.name = name if '转' in name: model.device_type_key = 'zhuan' else: model.device_type_key = 'sw' model.exp = '{n}' model.hardware_type = hardware_type model.slave_id = int(slave) model.register_read_address = str(read) model.register_write_address = '' model.insert() @Web.auth @Web.setting def delete(self): self.drop('setting_upload') class modbus_code_update_path(Load): @Web.auth @Web.setting def get(self): self.set( path = '/setting/modbus_code' ,label = (u'命令码名称',u'所属产品',u'设备类型',u'表达式',u'硬件类型',u'从机id',u'寄存器读取地址',u'寄存器写入地址') ,update = ('name-input-required','product_id-select-required','device_type_key-text-required-直接输入设备类型的key,多个用换行隔开','exp-text--{n}为当前数据,多个用换行隔开','hardware_type-select-required','slave_id-input-required','register_read_address-text-required-多个用换行隔开,寄存器类型,地址,长度','register_write_address-text--多个用换行隔开,寄存器类型,地址,值#1||寄存器类型,地址,值#2||寄存器类型,地址,值#5&&寄存器类型,地址,值,其中,1是开启或者升起,2是关闭或者下降,5是停止') ,update_product_id = self.service('common').list('setting_product') ,update_device_type = self.service('common').list('setting_device_type') #,update_oper_type = Demeter.service('core', 'setting').oper_type() #,update_register_type = Demeter.service('core', 'setting').register_type() ,update_hardware_type = self.service('common').list('hardware_type') ) self.one('setting_modbus_code') self.show('update') @Web.auth @Web.setting def post(self): self.update('setting_modbus_code') @Web.auth @Web.setting def delete(self): self.drop('setting_modbus_code')