setting.py 10 KB

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