device.py 17 KB

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