device.py 18 KB

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