device.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. id = self.commonUpdate('device_gateway')
  185. self.hardware(id)
  186. @tornado.web.authenticated
  187. def delete(self):
  188. self.commonDelete('device_gateway')
  189. @tornado.web.authenticated
  190. def hardware(self, id):
  191. hardware_id = self.data['update']['hardware_id']
  192. if hardware_id and id > 0:
  193. info = self.service('common').one('hardware', hardware_id=hardware_id)
  194. if not info:
  195. insert = {}
  196. insert['farm_id'] = self.data['update']['farm_id']
  197. insert['hardware_id'] = hardware_id
  198. insert['gateway_id'] = id
  199. insert['hardware_type'] = 1
  200. self.service('common').update('hardware', None, insert)
  201. class info_path(Load):
  202. @tornado.web.authenticated
  203. def get(self):
  204. self.common(
  205. name = u'设备'
  206. ,path = 'info'
  207. ,width = '600'
  208. ,height = '300'
  209. ,add = False
  210. #,edit = False
  211. ,search = (('farm_id-select-','type_id-select-', 'gateway_id-select-', 'hardware_type-select-'), (u'选择农场',u'设备名称',u'设备类型',u'网关',u'硬件类型'))
  212. ,thead = (u'所属农场', u'设备id', u'连接状态', u'数值', u'设备类型', u'网关',u'硬件类型')
  213. ,tbody = ('farm','name', 'hardware_id', 'status', 'show_num', 'type', 'gateway', 'hardware_type')
  214. ,state = False
  215. )
  216. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  217. self.data['common']['search_type_id-select-'] = self.service('common').list('device_type')
  218. self.data['common']['search_gateway_id-select-'] = self.service('common').list('device_gateway')
  219. self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
  220. del self.data['common']['search_hardware_type-select-'][0]
  221. self.commonList('device_info')
  222. if self.data['list']:
  223. for key, value in enumerate(self.data['list']):
  224. farm = self.service('common').one('farm', id=value['farm_id'])
  225. self.data['list'][key]['farm'] = farm['name']
  226. info = self.service('common').one('device_type', id=value['type_id'])
  227. self.data['list'][key]['type'] = info['name']
  228. info = self.service('common').one('device_gateway', id=value['gateway_id'])
  229. self.data['list'][key]['gateway'] = info['name']
  230. if value['status']:
  231. self.data['list'][key]['status'] = '连接'
  232. else:
  233. self.data['list'][key]['status'] = '断开'
  234. if value['show_num']:
  235. # 根据设备类型type_id得到单位和计算公式
  236. self.data['list'][key]['show_num'] = value['show_num'] + '<br />更新时间:' + Demeter.date(value['cdate'])
  237. else:
  238. self.data['list'][key]['show_num'] = '0'
  239. hardware_type = self.service('common').one('hardware_type', id=value['hardware_type'])
  240. self.data['list'][key]['hardware_type'] = hardware_type['name']
  241. self.commonView('list')
  242. class info_update_path(Load):
  243. @tornado.web.authenticated
  244. def get(self):
  245. self.common(
  246. path = 'info'
  247. #,label = (u'网关名称', u'设备id',u'网关连接状态',u'电源数值',u'电源连接状态')
  248. ,label = (u'所属农场',u'设备名称', u'设备id')
  249. ,update = ('farm_id-select-required', 'name-input-required','hardware_id-show-')
  250. #,update = ('name-input-required','hardware_id-input-required','status-switch--开启|关闭','power_num-input-','power_status-switch--连接|断开')
  251. ,update_farm_id = Demeter.config['setting']['farmList']
  252. )
  253. self.commonOne('device_info')
  254. self.commonView('update')
  255. @tornado.web.authenticated
  256. def post(self):
  257. self.commonUpdate('device_info')
  258. @tornado.web.authenticated
  259. def delete(self):
  260. self.commonDelete('device_info')
  261. class type_path(Load):
  262. @tornado.web.authenticated
  263. def get(self):
  264. self.common(
  265. name = u'类型'
  266. ,path = 'type'
  267. ,width = '600'
  268. ,height = '400'
  269. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称'))
  270. ,thead = (u'类型名称', u'类型标识', u'类型单位', u'表达式', u'更新时间')
  271. ,tbody = ('name', 'key', 'unit', 'exp', 'cdate')
  272. ,state = True
  273. )
  274. self.commonList('device_type')
  275. self.commonView('list')
  276. class type_update_path(Load):
  277. @tornado.web.authenticated
  278. def get(self):
  279. self.common(
  280. path = 'type'
  281. ,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式')
  282. ,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据')
  283. )
  284. self.commonOne('device_type')
  285. self.commonView('update')
  286. @tornado.web.authenticated
  287. def post(self):
  288. self.commonUpdate('device_type')
  289. @tornado.web.authenticated
  290. def delete(self):
  291. self.commonDelete('device_type')
  292. class printer_path(Load):
  293. @tornado.web.authenticated
  294. def get(self):
  295. self.common(
  296. name = u'打印机'
  297. ,path = 'printer'
  298. ,width = '600'
  299. ,height = '400'
  300. ,search = (('farm_id-select-','name-input-mlike'), (u'选择农场',u'打印机名称'))
  301. ,thead = (u'所属农场', u'打印机名称', u'打印机地址', u'更新时间')
  302. ,tbody = ('farm','name', 'host', 'cdate')
  303. ,state = True
  304. )
  305. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  306. self.commonList('device_printer')
  307. if self.data['list']:
  308. for key, value in enumerate(self.data['list']):
  309. farm = self.service('common').one('farm', id=value['farm_id'])
  310. self.data['list'][key]['farm'] = farm['name']
  311. self.commonView('list')
  312. class printer_update_path(Load):
  313. @tornado.web.authenticated
  314. def get(self):
  315. self.common(
  316. path = 'printer'
  317. ,label = (u'所属农场', u'打印机名称', u'打印机地址')
  318. ,update = ('farm_id-select-required', 'name-input-required','host-input-required')
  319. ,update_farm_id = Demeter.config['setting']['farmList']
  320. )
  321. self.commonOne('device_printer')
  322. self.commonView('update')
  323. @tornado.web.authenticated
  324. def post(self):
  325. self.commonUpdate('device_printer')
  326. @tornado.web.authenticated
  327. def delete(self):
  328. self.commonDelete('device_printer')