device.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web page
  5. name:device.py 设备相关页面
  6. author:rabin
  7. """
  8. from __load__ import *
  9. class page_path(Load):
  10. @tornado.web.authenticated
  11. def get(self):
  12. self.common(
  13. name = u'页面' #中文名
  14. ,path = 'page' #路径
  15. ,width = '600' # 新增页面的宽度
  16. ,height = '200' # 新增页面的高度
  17. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'页面名称')) #搜索
  18. ,thead = (u'页面名称', '子页面', u'更新时间') #表头
  19. ,tbody = ('name', 'group', 'cdate') #表内容
  20. ,state = True #启用回收站
  21. )
  22. self.commonList('device_page')
  23. if self.data['list']:
  24. for key, value in enumerate(self.data['list']):
  25. self.data['list'][key]['group'] = '<a href="/device/group?search_page_id-select-='+value['id']+'">分组</a>'
  26. self.commonView('list')
  27. class page_update_path(Load):
  28. @tornado.web.authenticated
  29. def get(self):
  30. self.common(
  31. path = 'page'
  32. ,label = (u'标题',)
  33. ,update = ('name-input-required',)
  34. )
  35. self.commonOne('device_page')
  36. self.commonView('update')
  37. @tornado.web.authenticated
  38. def post(self):
  39. self.commonUpdate('device_page')
  40. @tornado.web.authenticated
  41. def delete(self):
  42. self.commonDelete('device_page')
  43. class group_path(Load):
  44. @tornado.web.authenticated
  45. def get(self):
  46. self.common(
  47. name = u'分组'
  48. ,path = 'group'
  49. ,width = '600'
  50. ,height = '250'
  51. ,full = True
  52. ,search = (('label-1','cdate-time-start','cdate-time-end','page_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择页面',u'分组名称'))
  53. ,thead = (u'页面名称', u'分组名称', '设备', u'更新时间')
  54. ,tbody = ('page', 'name', 'device', 'cdate')
  55. ,state = True
  56. )
  57. self.data['common']['search_page_id-select-'] = self.service('common').list('device_page')
  58. self.commonList('device_group')
  59. if self.data['list']:
  60. for key, value in enumerate(self.data['list']):
  61. page = self.service('common').one('device_page', id=value['page_id'])
  62. self.data['list'][key]['page'] = page['name']
  63. if value['devices']:
  64. #self.data['list'][key]['device'] = '<a href="/device/info?search_id-input-ins='+value['devices']+'">查看设备</a>'
  65. self.data['list'][key]['device'] = '<a>查看设备</a>'
  66. else:
  67. self.data['list'][key]['device'] = '暂无设备'
  68. self.commonView('list')
  69. class group_update_path(Load):
  70. @tornado.web.authenticated
  71. def get(self):
  72. self.common(
  73. path = 'group'
  74. ,label = (u'所属页面', u'标题', u'选择设备')
  75. ,update = ('page_id-select-required','name-input-required', 'devices-checkboxs-')
  76. ,update_page_id = self.service('common').list('device_page')
  77. ,update_devices = self.getDevice()
  78. )
  79. self.commonOne('device_group')
  80. self.commonView('update')
  81. @tornado.web.authenticated
  82. def post(self):
  83. self.commonUpdate('device_group')
  84. @tornado.web.authenticated
  85. def delete(self):
  86. self.commonDelete('device_group')
  87. def getDevice(self):
  88. gateway = self.service('common').list('device_gateway')
  89. if gateway:
  90. for key, value in enumerate(gateway):
  91. gateway[key]['child'] = self.service('common').list('device_info', True, {'gateway_id-input-':value['id']})
  92. return gateway
  93. class mul_path(Load):
  94. @tornado.web.authenticated
  95. def get(self):
  96. self.common(
  97. name = u'批量控制'
  98. ,path = 'mul'
  99. ,width = '600'
  100. ,height = '250'
  101. ,full = True
  102. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'分组名称'))
  103. ,thead = (u'分组名称', '设备', u'更新时间')
  104. ,tbody = ('name', 'device', 'cdate')
  105. ,state = True
  106. )
  107. self.commonList('device_mul')
  108. if self.data['list']:
  109. for key, value in enumerate(self.data['list']):
  110. if value['devices']:
  111. #self.data['list'][key]['device'] = '<a href="/device/info?search_id-input-ins='+value['devices']+'">设备</a>'
  112. self.data['list'][key]['device'] = '<a>查看设备</a>'
  113. else:
  114. self.data['list'][key]['device'] = '暂无设备'
  115. self.commonView('list')
  116. class mul_update_path(Load):
  117. @tornado.web.authenticated
  118. def get(self):
  119. self.common(
  120. path = 'mul'
  121. ,label = (u'标题', u'选择设备')
  122. ,update = ('name-input-required', 'devices-checkboxs-')
  123. ,update_devices = self.getDevice()
  124. )
  125. self.commonOne('device_mul')
  126. self.commonView('update')
  127. @tornado.web.authenticated
  128. def post(self):
  129. self.commonUpdate('device_mul')
  130. @tornado.web.authenticated
  131. def delete(self):
  132. self.commonDelete('device_mul')
  133. def getDevice(self):
  134. gateway = self.service('common').list('device_gateway')
  135. if gateway:
  136. for key, value in enumerate(gateway):
  137. gateway[key]['child'] = self.service('common').list('device_info', True, {'gateway_id-input-':value['id']})
  138. return gateway
  139. class gateway_path(Load):
  140. @tornado.web.authenticated
  141. def get(self):
  142. self.common(
  143. name = u'网关'
  144. ,path = 'gateway'
  145. ,width = '600'
  146. ,height = '300'
  147. #,add = False
  148. #,edit = False
  149. ,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'网关名称'))
  150. ,thead = (u'所属农场',u'网关名称', u'设备id', u'网关连接状态', u'电源数值', u'电源连接状态', u'更新时间')
  151. ,tbody = ('farm','name', 'hardware_id', 'status', 'power_num', 'power_status', 'cdate')
  152. ,state = False
  153. )
  154. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  155. self.commonList('device_gateway')
  156. if self.data['list']:
  157. for key, value in enumerate(self.data['list']):
  158. farm = self.service('common').one('farm', id=value['farm_id'])
  159. self.data['list'][key]['farm'] = farm['name']
  160. if value['status']:
  161. self.data['list'][key]['status'] = '开启'
  162. else:
  163. self.data['list'][key]['status'] = '关闭'
  164. if value['power_status']:
  165. self.data['list'][key]['power_status'] = '连接'
  166. else:
  167. self.data['list'][key]['power_status'] = '断开'
  168. self.commonView('list')
  169. class gateway_update_path(Load):
  170. @tornado.web.authenticated
  171. def get(self):
  172. self.common(
  173. path = 'gateway'
  174. #,label = (u'网关名称', u'设备id',u'网关连接状态',u'电源数值',u'电源连接状态')
  175. ,label = (u'所属农场', u'网关名称', u'设备id')
  176. ,update = ('farm_id-select-required', 'name-input-required','hardware_id-input-required')
  177. #,update = ('name-input-required','hardware_id-input-required','status-switch--开启|关闭','power_num-input-','power_status-switch--连接|断开')
  178. ,update_farm_id = Demeter.config['setting']['farmList']
  179. )
  180. self.commonOne('device_gateway')
  181. self.commonView('update')
  182. @tornado.web.authenticated
  183. def post(self):
  184. self.commonUpdate('device_gateway')
  185. @tornado.web.authenticated
  186. def delete(self):
  187. self.commonDelete('device_gateway')
  188. class info_path(Load):
  189. @tornado.web.authenticated
  190. def get(self):
  191. self.common(
  192. name = u'设备'
  193. ,path = 'info'
  194. ,width = '600'
  195. ,height = '300'
  196. ,add = False
  197. #,edit = False
  198. ,search = (('farm_id-select-','type_id-select-', 'gateway_id-select-', 'hardware_type-select-'), (u'选择农场',u'设备名称',u'设备类型',u'网关',u'硬件类型'))
  199. ,thead = (u'所属农场', u'设备id', u'连接状态', u'数值', u'设备类型', u'网关',u'硬件类型')
  200. ,tbody = ('farm','name', 'hardware_id', 'status', 'show_num', 'type', 'gateway', 'hardware_type')
  201. ,state = False
  202. )
  203. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  204. hardware = json.loads(Demeter.config['hardware']['type'])
  205. hardware_type = []
  206. hardware_value = {}
  207. for key,value in hardware.items():
  208. send = {}
  209. send['id'] = value
  210. send['name'] = key
  211. hardware_type.append(send)
  212. hardware_value[value] = key
  213. self.data['common']['search_type_id-select-'] = self.service('common').list('device_type')
  214. self.data['common']['search_gateway_id-select-'] = self.service('common').list('device_gateway')
  215. self.data['common']['search_hardware_type-select-'] = hardware_type
  216. self.commonList('device_info')
  217. if self.data['list']:
  218. for key, value in enumerate(self.data['list']):
  219. farm = self.service('common').one('farm', id=value['farm_id'])
  220. self.data['list'][key]['farm'] = farm['name']
  221. info = self.service('common').one('device_type', id=value['type_id'])
  222. self.data['list'][key]['type'] = info['name']
  223. info = self.service('common').one('device_gateway', id=value['gateway_id'])
  224. self.data['list'][key]['gateway'] = info['name']
  225. if value['status']:
  226. self.data['list'][key]['status'] = '连接'
  227. else:
  228. self.data['list'][key]['status'] = '断开'
  229. if value['show_num']:
  230. # 根据设备类型type_id得到单位和计算公式
  231. self.data['list'][key]['show_num'] = value['show_num'] + '<br />更新时间:' + Demeter.date(value['cdate'])
  232. else:
  233. self.data['list'][key]['show_num'] = '0'
  234. self.data['list'][key]['hardware_type'] = hardware_value[self.data['list'][key]['hardware_type']]
  235. self.commonView('list')
  236. class info_update_path(Load):
  237. @tornado.web.authenticated
  238. def get(self):
  239. self.common(
  240. path = 'info'
  241. #,label = (u'网关名称', u'设备id',u'网关连接状态',u'电源数值',u'电源连接状态')
  242. ,label = (u'所属农场',u'设备名称', u'设备id')
  243. ,update = ('farm_id-select-required', 'name-input-required','hardware_id-show-')
  244. #,update = ('name-input-required','hardware_id-input-required','status-switch--开启|关闭','power_num-input-','power_status-switch--连接|断开')
  245. ,update_farm_id = Demeter.config['setting']['farmList']
  246. )
  247. self.commonOne('device_info')
  248. self.commonView('update')
  249. @tornado.web.authenticated
  250. def post(self):
  251. self.commonUpdate('device_info')
  252. @tornado.web.authenticated
  253. def delete(self):
  254. self.commonDelete('device_info')
  255. class type_path(Load):
  256. @tornado.web.authenticated
  257. def get(self):
  258. self.common(
  259. name = u'类型'
  260. ,path = 'type'
  261. ,width = '600'
  262. ,height = '400'
  263. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称'))
  264. ,thead = (u'类型名称', u'类型标识', u'类型单位', u'表达式', u'更新时间')
  265. ,tbody = ('name', 'key', 'unit', 'exp', 'cdate')
  266. ,state = True
  267. )
  268. self.commonList('device_type')
  269. self.commonView('list')
  270. class type_update_path(Load):
  271. @tornado.web.authenticated
  272. def get(self):
  273. self.common(
  274. path = 'type'
  275. ,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式')
  276. ,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据')
  277. )
  278. self.commonOne('device_type')
  279. self.commonView('update')
  280. @tornado.web.authenticated
  281. def post(self):
  282. self.commonUpdate('device_type')
  283. @tornado.web.authenticated
  284. def delete(self):
  285. self.commonDelete('device_type')
  286. class printer_path(Load):
  287. @tornado.web.authenticated
  288. def get(self):
  289. self.common(
  290. name = u'打印机'
  291. ,path = 'printer'
  292. ,width = '600'
  293. ,height = '400'
  294. ,search = (('farm_id-select-','name-input-mlike'), (u'选择农场',u'打印机名称'))
  295. ,thead = (u'所属农场', u'打印机名称', u'打印机地址', u'更新时间')
  296. ,tbody = ('farm','name', 'host', 'cdate')
  297. ,state = True
  298. )
  299. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  300. self.commonList('device_printer')
  301. if self.data['list']:
  302. for key, value in enumerate(self.data['list']):
  303. farm = self.service('common').one('farm', id=value['farm_id'])
  304. self.data['list'][key]['farm'] = farm['name']
  305. self.commonView('list')
  306. class printer_update_path(Load):
  307. @tornado.web.authenticated
  308. def get(self):
  309. self.common(
  310. path = 'printer'
  311. ,label = (u'所属农场', u'打印机名称', u'打印机地址')
  312. ,update = ('farm_id-select-required', 'name-input-required','host-input-required')
  313. ,update_farm_id = Demeter.config['setting']['farmList']
  314. )
  315. self.commonOne('device_printer')
  316. self.commonView('update')
  317. @tornado.web.authenticated
  318. def post(self):
  319. self.commonUpdate('device_printer')
  320. @tornado.web.authenticated
  321. def delete(self):
  322. self.commonDelete('device_printer')