device.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 gateway_path(Load):
  10. @Web.auth
  11. @Web.setting
  12. def get(self):
  13. self.common(
  14. name = u'网关'
  15. ,path = 'gateway'
  16. ,width = '600'
  17. ,height = '500'
  18. #,add = False
  19. #,edit = False
  20. ,search = (('label-1','cdate-time-start','cdate-time-end','farm_id-select-','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'选择农场',u'网关名称'))
  21. ,thead = (u'所属农场',u'网关名称', u'设备id', u'网关连接状态', u'电源数值', u'电源连接状态', u'更新时间')
  22. ,tbody = ('farm','name', 'hardware_id', 'status', 'power_num', 'power_status', 'cdate')
  23. ,state = False
  24. )
  25. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  26. self.commonList('device_gateway')
  27. if self.data['list']:
  28. for key, value in enumerate(self.data['list']):
  29. farm = self.service('common').one('farm', id=value['farm_id'])
  30. self.data['list'][key]['farm'] = farm['name']
  31. device = self.service('common').list('device_info', search={'hardware_id':value['hardware_id']})
  32. self.data['list'][key]['status'] = '断开'
  33. self.data['list'][key]['power_status'] = '断开'
  34. self.data['list'][key]['power_num'] = 0
  35. if device:
  36. for v in device:
  37. if v['value']:
  38. v['value'] = float(v['value'])
  39. if v['hardware_type'] == 5 and v['value'] == 1:
  40. self.data['list'][key]['status'] = '连接'
  41. elif v['hardware_type'] == 4 and v['value']:
  42. self.data['list'][key]['power_status'] = '连接'
  43. self.data['list'][key]['power_num'] = v['value']
  44. self.commonView('list')
  45. class gateway_update_path(Load):
  46. @Web.auth
  47. @Web.setting
  48. def get(self):
  49. id = self.input('id')
  50. if id:
  51. #hardware_id = 'hardware_id-show-required'
  52. hardware_id = 'hardware_id-input-required'
  53. else:
  54. hardware_id = 'hardware_id-input-required'
  55. self.common(
  56. path = 'gateway'
  57. ,label = (u'所属农场', u'网关名称', u'设备id', u'设备分布图纸')
  58. ,update = ('farm_id-select-required', 'name-input-required',hardware_id, 'pic-pic-')
  59. ,update_farm_id = Demeter.config['setting']['farmList']
  60. )
  61. self.commonOne('device_gateway')
  62. self.commonView('update')
  63. @Web.auth
  64. @Web.setting
  65. def post(self):
  66. id = self.commonUpdate('device_gateway')
  67. self.device(id)
  68. @Web.auth
  69. @Web.setting
  70. def delete(self):
  71. self.commonDelete('device_gateway')
  72. @Web.auth
  73. @Web.setting
  74. def device(self, id):
  75. info = self.service('common').one('device_gateway', id=id)
  76. hardware_id = info['hardware_id']
  77. self.service('device').upGatewayStatus(self.data['update']['farm_id'], id, hardware_id)
  78. class info_path(Load):
  79. @Web.auth
  80. @Web.setting
  81. def get(self):
  82. self.common(
  83. name = u'设备'
  84. ,path = 'info'
  85. ,width = '600'
  86. ,height = '400'
  87. ,button = ({'name':'添加摄像机','link':'/device/camera_update'},{'name':'添加摄像头','link':'/device/camera_pic_update'})
  88. ,add = False
  89. #,edit = False
  90. ,search = (('farm_id-select--gateway_id', 'hardware_type-select-','type_id-select-','name-input-mlike', 'status-hidden-'), (u'选择农场',u'硬件类型',u'设备类型',u'设备名称', '设备状态'))
  91. ,thead = (u'所属农场', u'设备名称', u'设备id', u'当前值', u'设备类型', u'网关',u'硬件类型')
  92. ,tbody = ('farm','name', 'hardware_id', 'value', 'type', 'gateway', 'hardware_type')
  93. ,state = True
  94. )
  95. self.data['common']['search_farm_id-select--gateway_id'] = Demeter.config['setting']['farmList']
  96. self.data['common']['search_type_id-select-'] = self.service('common').list('device_type')
  97. self.data['common']['search_gateway_id-select---farm_id'] = self.service('common').list('device_gateway')
  98. self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
  99. """
  100. if self.data['common']['search_hardware_type-select-']:
  101. del self.data['common']['search_hardware_type-select-'][0]
  102. """
  103. status = self.input('search_status-hidden-', False)
  104. if status:
  105. self.data['search']['status'] = False
  106. self.commonList('device_info', order='inorder asc, type_id asc,udate asc,hardware_id asc')
  107. if self.data['list']:
  108. for key, value in enumerate(self.data['list']):
  109. farm = self.service('common').one('farm', id=value['farm_id'])
  110. self.data['list'][key]['farm'] = farm['name']
  111. self.data['list'][key]['type'] = '无'
  112. if value['type_id'] > 0:
  113. type_info = self.service('common').one('device_type', id=value['type_id'])
  114. if type_info:
  115. self.data['list'][key]['type'] = type_info['name']
  116. info = self.service('common').one('device_gateway', id=value['gateway_id'])
  117. self.data['list'][key]['gateway'] = info['name']
  118. if value['status']:
  119. self.data['list'][key]['status'] = '已连接'
  120. else:
  121. self.data['list'][key]['status'] = '已断开'
  122. if not value['value']:
  123. value['value'] = ''
  124. if value['hardware_type'] == 2 or value['hardware_type'] == 4:
  125. if value['type_id'] > 0 and type_info:
  126. value['value'] = value['value'] + ' ' + type_info['unit']
  127. self.data['list'][key]['value'] = '<a href="/device/stat?id='+value['id']+'&search_date-select-=1&method=list">'+value['value'] + '</a><br />更新时间:' + Demeter.date(value['cdate']) + '<br /><a href="/device/stat?id='+value['id']+'&search_date-select-=1&method=avg">[平均值统计]</a> <a href="/device/stat?id='+value['id']+'&search_date-select-=1&method=maxmin">[高低值统计]</a>'
  128. elif value['hardware_type'] == 6:
  129. self.data['list'][key]['value'] = '<a href="/device/pic?id='+value['id']+'"><img src="'+value['value']+'" width="200px"/></a><br />更新时间:' + Demeter.date(value['cdate'])
  130. elif value['hardware_type'] == 3:
  131. if value['status']:
  132. status = ''
  133. else:
  134. status = 'disabled'
  135. url = '/device/info'
  136. if value['value'] == '1':
  137. #disabled
  138. self.data['list'][key]['value'] = '<input type="checkbox" name="switch" lay-filter="switch" lay-skin="switch" checked '+status+' value="1" data-value="0" data-id="'+value['id']+'" data-url="'+url+'">'
  139. else:
  140. self.data['list'][key]['value'] = '<input type="checkbox" lay-filter="switch" name="switch" lay-skin="switch" '+status+' value="0" data-value="1" data-id="'+value['id']+'" data-url="'+url+'">'
  141. self.data['list'][key]['value'] = ''
  142. elif value['hardware_type'] == 5:
  143. if value['value'] == '1':
  144. self.data['list'][key]['value'] = '已连接'
  145. else:
  146. self.data['list'][key]['value'] = '已断开'
  147. hardware_type = self.service('common').one('hardware_type', id=value['hardware_type'])
  148. self.data['list'][key]['hardware_type'] = hardware_type['name']
  149. self.commonView('list')
  150. @Web.auth
  151. @Web.setting
  152. def post(self):
  153. id = self.input('id')
  154. value = self.input('value')
  155. if id:
  156. Demeter.service('device').switch(id, value)
  157. self.out('yes', {'state':id})
  158. else:
  159. self.out('no')
  160. class info_update_path(Load):
  161. @Web.auth
  162. @Web.setting
  163. def get(self):
  164. self.commonOne('device_info')
  165. if self.data['info']['hardware_type'] == 7:
  166. self.common(
  167. path = 'camera'
  168. #,label = (u'摄像机名称', u'摄像机id', u'所属农场', u'选择网关', u'选择硬件类型', u'摄像机地址')
  169. #,update = ('name-input-required','hardware_id-show-','farm_id-select-required','gateway_id-select-required','hardware_type-select-required','value-text-required')
  170. ,label = (u'摄像机名称', u'摄像机id', u'所属农场', u'选择网关', u'摄像机地址', u'排序')
  171. ,update = ('name-input-required','hardware_id-show-','farm_id-select-required-gateway_id','gateway_id-select-required--farm_id','value-text-required','inorder-text-')
  172. ,update_farm_id = Demeter.config['setting']['farmList']
  173. ,update_gateway_id = self.service('common').list('device_gateway')
  174. #,update_hardware_type = self.service('common').list('hardware_type')
  175. )
  176. elif self.data['info']['hardware_type'] == 6:
  177. self.common(
  178. path = 'camera_pic'
  179. ,label = (u'摄像头名称', u'摄像头id', u'所属农场', u'选择网关', u'摄像头地址', u'排序')
  180. ,update = ('name-input-required','hardware_id-show-','farm_id-select-required-gateway_id','gateway_id-select-required--farm_id','value-text-required','inorder-text-')
  181. ,update_farm_id = Demeter.config['setting']['farmList']
  182. ,update_gateway_id = self.service('common').list('device_gateway')
  183. )
  184. elif self.data['info']['hardware_type'] in (2,4):
  185. self.common(
  186. path = 'info'
  187. #,label = (u'设备名称', u'设备id', u'计算公式', u'实时计算公式')
  188. #,update = ('name-input-required','hardware_id-show-',u'exp-text--{n}为当前数据,只处理最新数据',u'out_exp-text--处理所有数据,比较消耗服务器资源')
  189. ,label = (u'设备名称', u'设备id', u'计算公式', u'排序')
  190. ,update = ('name-input-required','hardware_id-show-',u'exp-text--{n}为当前数据,只处理最新数据','inorder-text-')
  191. )
  192. else:
  193. self.common(
  194. path = 'info'
  195. ,label = (u'设备名称', u'设备id', u'排序')
  196. ,update = ('name-input-required','hardware_id-show-','inorder-text-')
  197. )
  198. self.commonView('update')
  199. @Web.auth
  200. @Web.setting
  201. def post(self):
  202. self.commonUpdate('device_info')
  203. @Web.auth
  204. @Web.setting
  205. def delete(self):
  206. self.commonDelete('device_info')
  207. """
  208. self.commonOne('device_info')
  209. if self.input('state') == 'True' or (self.data['info'] and 'hardware_type' in self.data['info'] and self.data['info']['hardware_type'] == 7):
  210. self.commonDelete('device_info')
  211. else:
  212. #除摄像机之外,其余设备不允许删除
  213. self.out('no')
  214. """
  215. class camera_path(Load):
  216. @Web.auth
  217. @Web.setting
  218. def get(self):
  219. self.redirect('/device/info?search_hardware_type=7')
  220. class camera_update_path(Load):
  221. @Web.auth
  222. @Web.setting
  223. def get(self):
  224. self.common(
  225. path = 'camera'
  226. ,label = (u'摄像机名称', u'摄像机id', u'所属农场', u'选择网关', u'摄像机地址',u'排序')
  227. ,update = ('name-input-required','hardware_id-input-required','farm_id-select-required-gateway_id','gateway_id-select-required--farm_id','value-text-required','inorder-text-')
  228. ,update_farm_id = Demeter.config['setting']['farmList']
  229. ,update_gateway_id = self.service('common').list('device_gateway')
  230. )
  231. self.commonOne('device_info')
  232. self.commonView('update')
  233. @Web.auth
  234. @Web.setting
  235. def post(self):
  236. self.data['update']['hardware_type'] = 7
  237. self.data['update']['type_id'] = 0
  238. self.commonUpdate('device_info')
  239. @Web.auth
  240. @Web.setting
  241. def delete(self):
  242. self.commonDelete('device_info')
  243. class camera_pic_path(Load):
  244. @Web.auth
  245. @Web.setting
  246. def get(self):
  247. self.redirect('/device/info?search_hardware_type=6')
  248. class camera_pic_update_path(Load):
  249. @Web.auth
  250. @Web.setting
  251. def get(self):
  252. self.common(
  253. path = 'camera_pic'
  254. ,label = (u'摄像头名称', u'摄像头id', u'所属农场', u'选择网关', u'摄像头地址',u'排序')
  255. ,update = ('name-input-required','hardware_id-input-required','farm_id-select-required-gateway_id','gateway_id-select-required--farm_id','value-text-required','inorder-text-')
  256. ,update_farm_id = Demeter.config['setting']['farmList']
  257. ,update_gateway_id = self.service('common').list('device_gateway')
  258. )
  259. self.commonOne('device_info')
  260. self.commonView('update')
  261. @Web.auth
  262. @Web.setting
  263. def post(self):
  264. self.data['update']['hardware_type'] = 6
  265. self.data['update']['type_id'] = 0
  266. self.commonUpdate('device_info')
  267. @Web.auth
  268. @Web.setting
  269. def delete(self):
  270. self.commonDelete('device_info')
  271. class pic_path(Load):
  272. @Web.auth
  273. @Web.setting
  274. def get(self):
  275. self.common(
  276. name = u'摄像头图片'
  277. ,path = 'pic'
  278. ,width = '600'
  279. ,height = '300'
  280. ,add = False
  281. ,edit = False
  282. ,search = (('label-1','sdate-time-start','sdate-time-end'), (u'日期范围',u'开始时间',u'截止时间'))
  283. ,thead = (u'图片地址', u'更新时间')
  284. ,tbody = ('pic', 'sdate')
  285. ,state = False
  286. )
  287. self.commonList('device_pic', order='sdate desc')
  288. if self.data['list']:
  289. for key, value in enumerate(self.data['list']):
  290. self.data['list'][key]['pic'] = '<img src="'+value['pic']+'" width="200px" />'
  291. self.commonView('list')
  292. class stat_path(Load):
  293. @Web.auth
  294. @Web.setting
  295. def get(self):
  296. method = self.input('method', 'avg')
  297. id = self.input('id')
  298. self.common(
  299. name = u'设备统计'
  300. ,path = 'stat'
  301. ,search = (('label-1','cdate-time-start','cdate-time-end', 'date-select-'), (u'日期范围',u'开始时间',u'截止时间',u'周期选择'))
  302. )
  303. data = self.service('common').one('device_info', id=id)
  304. typeInfo = self.service('common').one('device_type', id=data['type_id'])
  305. farm = self.service('common').one('farm', id=data['farm_id'])
  306. self.data['info'] = data
  307. self.data['type'] = typeInfo
  308. self.data['farm'] = farm
  309. self.data['method'] = method
  310. model = Demeter.model('data', 'tsdb')
  311. self.data['common']['search_date-select-'] = model.dateConfig()
  312. search = {}
  313. if 'cdate-time-start' in self.data['search']:
  314. search['start'] = self.data['search']['cdate-time-start']
  315. if 'cdate-time-end' in self.data['search']:
  316. search['end'] = self.data['search']['cdate-time-end']
  317. if 'date-select-' in self.data['search']:
  318. search['group'] = self.data['search']['date-select-']
  319. self.data['list'] = model.getData(method, search, data)
  320. self.commonView('stat')
  321. class type_path(Load):
  322. @Web.auth
  323. @Web.setting
  324. def get(self):
  325. self.common(
  326. name = u'类型'
  327. ,path = 'type'
  328. ,width = '600'
  329. ,height = '400'
  330. ,search = (('label-1','cdate-time-start','cdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'类型名称'))
  331. ,thead = (u'类型ID', u'类型名称', u'类型标识', u'类型单位', u'更新时间')
  332. ,tbody = ('id', 'name', 'key', 'unit', 'cdate')
  333. ,state = True
  334. )
  335. self.commonList('device_type')
  336. self.commonView('list')
  337. class type_update_path(Load):
  338. @Web.auth
  339. @Web.setting
  340. def get(self):
  341. self.common(
  342. path = 'type'
  343. #,label = (u'类型名称', u'类型标识',u'类型单位',u'表达式')
  344. #,update = ('name-input-required','key-input-required','unit-input-',u'exp-text--请输入表达式,{n}代表当前数据')
  345. ,label = (u'类型名称', u'类型标识',u'类型单位')
  346. ,update = ('name-input-required','key-input-required','unit-input-')
  347. )
  348. self.commonOne('device_type')
  349. self.commonView('update')
  350. @Web.auth
  351. @Web.setting
  352. def post(self):
  353. self.commonUpdate('device_type')
  354. @Web.auth
  355. @Web.setting
  356. def delete(self):
  357. self.commonDelete('device_type')
  358. class printer_path(Load):
  359. @Web.auth
  360. @Web.setting
  361. def get(self):
  362. self.common(
  363. name = u'打印机'
  364. ,path = 'printer'
  365. ,width = '600'
  366. ,height = '400'
  367. ,search = (('farm_id-select-','name-input-mlike'), (u'选择农场',u'打印机名称'))
  368. ,thead = (u'所属农场', u'打印机名称', u'打印机地址', u'更新时间')
  369. ,tbody = ('farm','name', 'host', 'cdate')
  370. ,state = True
  371. )
  372. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  373. self.commonList('device_printer')
  374. if self.data['list']:
  375. for key, value in enumerate(self.data['list']):
  376. farm = self.service('common').one('farm', id=value['farm_id'])
  377. self.data['list'][key]['farm'] = farm['name']
  378. self.commonView('list')
  379. class printer_update_path(Load):
  380. @Web.auth
  381. @Web.setting
  382. def get(self):
  383. self.common(
  384. path = 'printer'
  385. ,label = (u'所属农场', u'打印机名称', u'打印机地址')
  386. ,update = ('farm_id-select-required', 'name-input-required','host-input-required')
  387. ,update_farm_id = Demeter.config['setting']['farmList']
  388. )
  389. self.commonOne('device_printer')
  390. self.commonView('update')
  391. @Web.auth
  392. @Web.setting
  393. def post(self):
  394. self.commonUpdate('device_printer')
  395. @Web.auth
  396. @Web.setting
  397. def delete(self):
  398. self.commonDelete('device_printer')
  399. class notice_type_path(Load):
  400. @Web.auth
  401. @Web.setting
  402. def get(self):
  403. self.common(
  404. name = u'错误码设置'
  405. ,path = '/device/notice_type'
  406. ,width = '600'
  407. ,height = '300'
  408. ,search = (('name-input-mlike','key-input-mlike'), (u'错误码描述',u'错误码key'))
  409. ,thead = (u'ID', u'错误码描述',u'错误码key', u'更新时间')
  410. ,tbody = ('id', 'name', 'key', 'cdate')
  411. ,state = False
  412. )
  413. self.commonList('notice_type')
  414. self.commonView('list')
  415. class notice_type_update_path(Load):
  416. @Web.auth
  417. @Web.setting
  418. def get(self):
  419. self.common(
  420. path = '/device/notice_type'
  421. ,label = (u'错误码描述', u'错误码key')
  422. ,update = ('name-input-required', 'key-input-required')
  423. )
  424. self.commonOne('notice_type')
  425. self.commonView('update')
  426. @Web.auth
  427. @Web.setting
  428. def post(self):
  429. self.commonUpdate('notice_type')
  430. @Web.auth
  431. @Web.setting
  432. def delete(self):
  433. self.commonDelete('notice_type')
  434. class log_path(Load):
  435. @Web.auth
  436. @Web.setting
  437. def get(self):
  438. self.common(
  439. name = u'网关日志'
  440. ,path = '/device/log'
  441. ,width = '600'
  442. ,height = '600'
  443. ,add = False
  444. ,edit = False
  445. ,search = (('farm_id-select-','content-input-mlike'), (u'选择农场', u'内容'))
  446. ,thead = (u'所属农场', u'所属网关', u'日志内容',u'发送时间')
  447. ,tbody = ('farm','gateway', 'content', 'cdate')
  448. ,state = False
  449. )
  450. self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
  451. self.commonList('log')
  452. if self.data['list']:
  453. for key, value in enumerate(self.data['list']):
  454. farm = self.service('common').one('farm', id=value['farm_id'])
  455. self.data['list'][key]['farm'] = farm['name']
  456. gateway = self.service('common').one('device_gateway', id=value['gateway_id'])
  457. self.data['list'][key]['gateway'] = gateway['name']
  458. self.commonView('list')