setting.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter setting page
  4. name:setting.py 服务协议管理
  5. author:rabin
  6. """
  7. from .__load__ import *
  8. import xlrd
  9. class server_path(Load):
  10. @Web.auth
  11. @Web.setting
  12. def get(self):
  13. self.set(
  14. name = u'服务管理'
  15. ,path = '/setting/server'
  16. ,width = '600'
  17. ,height = '600'
  18. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'服务名称'))
  19. ,thead = (u'服务名称', u'服务类型', u'服务地址', u'服务端口', u'更新时间')
  20. ,tbody = ('name', 'server_type', 'server_host', 'server_port', 'cdate')
  21. ,state = True
  22. )
  23. self.list('setting_server')
  24. if self.data['list']:
  25. server_type = Demeter.service('core', 'setting').server_type()
  26. for key, value in enumerate(self.data['list']):
  27. self.data['list'][key]['server_type'] = server_type[value['server_type']-1]['name']
  28. self.show('list')
  29. class server_update_path(Load):
  30. @Web.auth
  31. @Web.setting
  32. def get(self):
  33. self.set(
  34. path = '/setting/server'
  35. ,label = (u'服务名称',u'服务类型',u'服务地址',u'服务端口',u'请求间隔')
  36. ,update = ('name-input-required','server_type-select-required','server_host-input-required','server_port-input-required','server_time-input-required')
  37. ,update_server_type = Demeter.service('core', 'setting').server_type()
  38. )
  39. self.one('setting_server')
  40. self.show('update')
  41. @Web.auth
  42. @Web.setting
  43. def post(self):
  44. self.update('setting_server')
  45. @Web.auth
  46. @Web.setting
  47. def delete(self):
  48. self.drop('setting_server')
  49. class product_path(Load):
  50. @Web.auth
  51. @Web.setting
  52. def get(self):
  53. self.set(
  54. name = u'产品管理'
  55. ,path = '/setting/product'
  56. ,width = '600'
  57. ,height = '600'
  58. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'产品名称'))
  59. ,thead = (u'产品名称',u'产品标识', u'更新时间')
  60. ,tbody = ('name', 'key','cdate')
  61. ,state = True
  62. )
  63. self.list('setting_product')
  64. self.show('list')
  65. class product_update_path(Load):
  66. @Web.auth
  67. @Web.setting
  68. def get(self):
  69. self.set(
  70. path = '/setting/product'
  71. ,label = (u'产品名称',u'产品标识')
  72. ,update = ('name-input-required','key-input-required')
  73. )
  74. self.one('setting_product')
  75. self.show('update')
  76. @Web.auth
  77. @Web.setting
  78. def post(self):
  79. id = self.update('setting_product')
  80. @Web.auth
  81. @Web.setting
  82. def delete(self):
  83. self.drop('setting_product')
  84. class device_type_path(Load):
  85. @Web.auth
  86. @Web.setting
  87. def get(self):
  88. self.set(
  89. name = u'设备类型'
  90. ,path = '/setting/device_type'
  91. ,width = '600'
  92. ,height = '400'
  93. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称'))
  94. ,thead = (u'类型ID', u'类型名称', u'类型标识', u'类型单位', u'更新时间')
  95. ,tbody = ('id', 'name', 'key', 'unit', 'cdate')
  96. ,state = True
  97. )
  98. self.list('setting_device_type')
  99. self.show('list')
  100. class device_type_update_path(Load):
  101. @Web.auth
  102. @Web.setting
  103. def get(self):
  104. self.set(
  105. path = '/setting/device_type'
  106. #,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式')
  107. #,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据')
  108. ,label = (u'类型名称', u'类型标识',u'类型单位',u'操作类型',u'可选项')
  109. ,update = ('name-input-required','key-input-required','unit-input-','data_type-select-','data_option-text--多个换行隔开')
  110. ,update_data_type = Demeter.service('core', 'setting').data_type()
  111. )
  112. self.one('setting_device_type')
  113. self.show('update')
  114. @Web.auth
  115. @Web.setting
  116. def post(self):
  117. id = self.update('setting_device_type')
  118. @Web.auth
  119. @Web.setting
  120. def delete(self):
  121. self.drop('setting_device_type')
  122. class modbus_code_path(Load):
  123. @Web.auth
  124. @Web.setting
  125. def get(self):
  126. self.set(
  127. name = u'设备命令码'
  128. ,path = '/setting/modbus_code'
  129. ,width = '600'
  130. ,height = '600'
  131. ,button = ({'name':'批量上传','link':'/setting/upload_update'},)
  132. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike','product_id-select-'), (u'日期范围',u'开始时间',u'截止时间',u'设备名称',u'选择产品'))
  133. ,thead = (u'命令码名称', u'所属产品',u'关联设备类型', u'表达式', u'更新时间')
  134. ,tbody = ('name', 'product','device_type_key', 'exp', 'cdate')
  135. ,state = True
  136. )
  137. self.data['common']['search_product_id-select-'] = self.service('common').list('setting_product')
  138. self.list('setting_modbus_code')
  139. if self.data['list']:
  140. for key, value in enumerate(self.data['list']):
  141. product = self.service('common').one('setting_product', id=value['product_id'])
  142. self.data['list'][key]['product'] = product['name']
  143. self.show('list')
  144. class upload_update_path(Load):
  145. @Web.auth
  146. @Web.setting
  147. def get(self):
  148. self.set(
  149. path = '/setting/upload'
  150. ,label = (u'所属产品', u'上传输入点' ,u'上传输出点')
  151. ,update = ('product_id-select-required','input-file--','output-file--')
  152. ,update_product_id = self.service('common').list('setting_product')
  153. )
  154. #self.one('setting_upload')
  155. self.data['info'] = {}
  156. self.show('update')
  157. @Web.auth
  158. @Web.setting
  159. def post(self):
  160. product_id = self.input('update_product_id')
  161. inputs = self.input('update_input')
  162. output = self.input('update_output')
  163. sensor = self.input('update_sensor')
  164. #self.inputs(inputs, product_id)
  165. self.output(output, product_id)
  166. #self.sensor(sensor)
  167. self.out('操作成功,请关闭本窗口并刷新页面')
  168. def output(self, source, product_id):
  169. temp = source.split('/upload/')
  170. path = Demeter.path + '/runtime/upload/' + temp[1]
  171. data = xlrd.open_workbook(path)
  172. table = data.sheets()[0]
  173. rows = table.nrows
  174. cols = table.ncols
  175. result = {}
  176. for i in range(rows):
  177. if i >= 2:
  178. name = str(table.cell_value(i,1))
  179. cn_name = str(table.cell_value(i,2))
  180. if name:
  181. temp_slave = str(table.cell_value(i,0))
  182. if temp_slave:
  183. slave = temp_slave.replace('.0', '')
  184. temp_read = str(table.cell_value(i,5))
  185. if temp_read:
  186. read = temp_read
  187. control_open = str(table.cell_value(i,3)).replace(slave + ',', '') + '#1'
  188. control_close = str(table.cell_value(i,4)).replace(slave + ',', '') + '#2'
  189. key = str(name) + '_' + str(slave)
  190. if key not in result:
  191. result[key] = []
  192. result[key].append(name)
  193. result[key].append(cn_name)
  194. result[key].append(slave)
  195. result[key].append(read)
  196. result[key].append(control_open)
  197. result[key].append(control_close)
  198. #product_id = 1
  199. hardware_type = 3
  200. device_type_key = 'sw'
  201. model = Demeter.model('setting_modbus_code')
  202. for i in result:
  203. name = result[i][1]
  204. slave = result[i][2]
  205. read = result[i][3]
  206. control_open = result[i][4]
  207. control_close = result[i][5]
  208. name = name + '_' + i
  209. model.product_id = product_id
  210. model.name = name
  211. info = model.select(type='fetchone')
  212. if not info:
  213. model.product_id = product_id
  214. model.name = name
  215. if '转' in name:
  216. model.device_type_key = 'zhuan'
  217. else:
  218. model.device_type_key = 'sw'
  219. model.exp = '{n}'
  220. model.hardware_type = hardware_type
  221. model.slave_id = int(slave)
  222. model.register_read_address = str(read)
  223. model.register_write_address = control_open + '||' + control_close
  224. model.insert()
  225. def inputs(self, source, product_id):
  226. temp = source.split('/upload/')
  227. path = Demeter.path + '/runtime/upload/' + temp[1]
  228. data = xlrd.open_workbook(path)
  229. table = data.sheets()[0]
  230. rows = table.nrows
  231. cols = table.ncols
  232. result = {}
  233. for i in range(rows):
  234. if i >= 2:
  235. name = str(table.cell_value(i,1))
  236. cn_name = str(table.cell_value(i,2))
  237. if name:
  238. temp_slave = str(table.cell_value(i,3))
  239. if temp_slave:
  240. slave = temp_slave.replace('.0', '')
  241. temp_read = str(table.cell_value(i,4))
  242. if temp_read:
  243. read = temp_read
  244. write = str(table.cell_value(i,5)).replace('.0', '')
  245. key = str(name) + '_' + str(slave)
  246. if key not in result:
  247. result[key] = []
  248. result[key].append(name)
  249. result[key].append(cn_name)
  250. result[key].append(slave)
  251. result[key].append(read)
  252. result[key].append(write)
  253. #product_id = product_id
  254. hardware_type = 2
  255. device_type_key = 'zhuan'
  256. model = Demeter.model('setting_modbus_code')
  257. for i in result:
  258. name = result[i][1]
  259. slave = result[i][2]
  260. read = result[i][3]
  261. write = result[i][4]
  262. name = name + '_' + i
  263. model.product_id = product_id
  264. model.name = name
  265. info = model.select(type='fetchone')
  266. if not info:
  267. model.product_id = product_id
  268. model.name = name
  269. if '转' in name:
  270. model.device_type_key = 'zhuan'
  271. else:
  272. model.device_type_key = 'sw'
  273. model.exp = '{n}'
  274. model.hardware_type = hardware_type
  275. model.slave_id = int(slave)
  276. model.register_read_address = str(read)
  277. model.register_write_address = ''
  278. model.insert()
  279. @Web.auth
  280. @Web.setting
  281. def delete(self):
  282. self.drop('setting_upload')
  283. class modbus_code_update_path(Load):
  284. @Web.auth
  285. @Web.setting
  286. def get(self):
  287. self.set(
  288. path = '/setting/modbus_code'
  289. ,label = (u'命令码名称',u'所属产品',u'设备类型',u'表达式',u'硬件类型',u'从机id',u'寄存器读取地址',u'寄存器写入地址')
  290. ,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是停止')
  291. ,update_product_id = self.service('common').list('setting_product')
  292. ,update_device_type = self.service('common').list('setting_device_type')
  293. #,update_oper_type = Demeter.service('core', 'setting').oper_type()
  294. #,update_register_type = Demeter.service('core', 'setting').register_type()
  295. ,update_hardware_type = self.service('common').list('hardware_type')
  296. )
  297. self.one('setting_modbus_code')
  298. self.show('update')
  299. @Web.auth
  300. @Web.setting
  301. def post(self):
  302. self.update('setting_modbus_code')
  303. @Web.auth
  304. @Web.setting
  305. def delete(self):
  306. self.drop('setting_modbus_code')