device.py 16 KB

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