device.py 17 KB

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