device.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. from demeter.mqtt import *
  10. # 首页
  11. class index_path(Load):
  12. @Web.auth
  13. @Web.setting
  14. def get(self):
  15. # 获取网关列表
  16. self.data['gateway'] = self.service('common').list('device_gateway', state=True, search={'farm_id':self.data['setting']['farm']})
  17. # 获取页面
  18. self.data['page'] = self.service('common').list('device_page', order='inorder asc,cdate desc', search={'farm_id':self.data['setting']['farm']})
  19. self.view("device/index.html")
  20. #批量控制
  21. class mul_path(Load):
  22. @Web.auth
  23. @Web.setting
  24. def get(self):
  25. self.data['list'] = self.service('common').list('device_mul', search={'farm_id':self.data['setting']['farm']})
  26. if self.data['list']:
  27. model = Demeter.model('device_mul_queue')
  28. for key, value in enumerate(self.data['list']):
  29. model.status.ins((2,3))
  30. model.mul_id = value['id']
  31. info = model.select()
  32. #self.data['list'][key]['oper'] = ''
  33. if info:
  34. for v in info:
  35. if v['oper']:
  36. self.data['list'][key]['oper'] = v['oper']
  37. self.view("device/mul.html")
  38. @Web.auth
  39. @Web.setting
  40. def post(self):
  41. id = self.input('id')
  42. value = self.input('value')
  43. if id:
  44. info = self.service('common').one('device_mul', id=id)
  45. if info:
  46. update = {}
  47. update['value'] = value
  48. if info['device_type'] == 1:
  49. if value == '1':
  50. update['oper'] = '批量启动中'
  51. else:
  52. update['oper'] = '批量关闭中'
  53. else:
  54. if value == '1':
  55. update['oper'] = '批量升起中'
  56. elif value == '2':
  57. update['oper'] = '批量下降中'
  58. elif value == '5':
  59. update['oper'] = '批量停止中'
  60. self.service('common').update('device_mul', id, update, cdate=False)
  61. if info['devices']:
  62. devices = tuple(eval(info['devices']))
  63. for device in devices:
  64. Demeter.service('device').switch(device, switch=value, mul=id)
  65. self.out('yes', {'id':id})
  66. else:
  67. self.out('no')
  68. #大屏幕
  69. class screen_path(Load):
  70. @Web.auth
  71. @Web.setting
  72. def get(self):
  73. device = self.input('device')
  74. # 获取摄像头图片
  75. self.data['camera'] = Device.get(self, search={'hardware_type':6,'farm_id':self.data['setting']['farm']})
  76. # 获取当前数据
  77. self.data['device'] = Device.get(self, search={'id--ins':self.data['setting']['farmInfo']['gateway'], 'hardware_type--ins':(2,4,5),'farm_id':self.data['setting']['farm']})
  78. deviceInfo = {}
  79. if self.data['device']:
  80. for key, value in enumerate(self.data['device']):
  81. if value['type_id'] > 0:
  82. type_info = self.service('common').one('device_type', id=value['type_id'])
  83. self.data['device'][key]['unit'] = type_info['unit']
  84. self.data['device'][key]['typeName'] = type_info['name']
  85. if not device and not deviceInfo:
  86. deviceInfo = self.data['device'][key]
  87. self.data['list'] = ({'name':'实时数据监测', 'device':self.data['device']},)
  88. Device.statBase(self, deviceInfo)
  89. log_search = {}
  90. if device:
  91. log_search['device_id'] = device
  92. else:
  93. log_search['device_id--ins'] = self.data['setting']['farmInfo']['gateways']
  94. log_search['device_id--ins'] = self.data['setting']['farmInfo']['gateways']
  95. log_search['type_id'] = 2
  96. log_search['farm_id'] = self.data['setting']['farm']
  97. # 获取设备日志
  98. self.data['logs'] = self.service('common').list('msg', search=log_search, limit='0,1')
  99. if self.data['logs']:
  100. for key, value in enumerate(self.data['logs']):
  101. type_info = self.service('common').one('msg_type', id=value['type_id'])
  102. self.data['logs'][key]['type'] = type_info['name']
  103. self.data['logs'][key]['cdates'] = Demeter.date(value['cdate'])
  104. self.data['info'] = self.data['setting']['farmInfo']
  105. if not self.data['info']['welcome']:
  106. self.data['info']['welcome'] = self.data['info']['name']
  107. self.data['noset'] = True
  108. self.data['stat'] = True
  109. self.view("device/screen.html")
  110. #大屏幕设置
  111. class screen_set_path(Load):
  112. @Web.auth
  113. @Web.setting
  114. def get(self):
  115. self.data['info'] = self.data['setting']['farmInfo']
  116. self.data['info']['devices'] = self.data['info']['gateway']
  117. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  118. self.data['device'] = Device.getByGateway(self, search={'hardware_type--ins' : (2,4,5),'farm_id':self.data['setting']['farm']})
  119. self.view("device/alert/screen.html")
  120. @Web.auth
  121. @Web.setting
  122. def post(self):
  123. id = self.input('id')
  124. update = {}
  125. update['welcome'] = self.input('update_name')
  126. update['gateway'] = self.inputs('update_devices')
  127. if not update['gateway']:
  128. self.out(u'请选择设备')
  129. return
  130. update['gateway'] = str(update['gateway'])
  131. if update['welcome']:
  132. value = update['welcome']
  133. else:
  134. value = self.data['setting']['farmInfo']['name']
  135. key = 'update/w/' + id
  136. pub = Pub()
  137. pub.push(key, value)
  138. state = self.service('common').update('farm', id, update)
  139. self.out('yes', {'id':state})
  140. #特殊的大屏幕
  141. class screens_path(Load):
  142. @Web.setting
  143. def get(self):
  144. farm = self.input('ids', '2,3,4')
  145. self.data['farm'] = farm
  146. farm = farm.split(',')
  147. device = self.input('device')
  148. farmInfo = self.service('common').one('farm', id=farm[0])
  149. # 获取摄像头图片
  150. #self.data['camera'] = Device.get(self, search={'hardware_type':6,'farm_id--ins':farm})
  151. self.data['camera'] = []
  152. # 获取当前数据
  153. deviceList = Device.get(self, search={'hardware_type--ins':(2,4,5),'farm_id--ins':farm})
  154. self.data['device'] = {}
  155. for v in farm:
  156. self.data['device'][v] = []
  157. deviceInfo = {}
  158. devices = []
  159. if deviceList:
  160. for key, value in enumerate(deviceList):
  161. if value['type_id'] > 0:
  162. type_info = self.service('common').one('device_type', id=value['type_id'])
  163. value['unit'] = type_info['unit']
  164. value['typeName'] = type_info['name']
  165. self.data['device'][str(value['farm_id'])].append(value)
  166. devices.append(value['id'])
  167. if not device and not deviceInfo:
  168. deviceInfo = value
  169. else:
  170. self.data['device'][str(value['farm_id'])].append(value)
  171. devices.append(value['id'])
  172. self.data['deviceList'] = {}
  173. for v in farm:
  174. farmData = self.service('common').one('farm', id=int(v))
  175. if farmData:
  176. self.data['deviceList'][v] = ({'name':farmData['name'] + '实时检测', 'device':self.data['device'][v]},)
  177. Device.statBase(self, deviceInfo)
  178. log_search = {}
  179. if device:
  180. log_search['device_id'] = device
  181. else:
  182. log_search['device_id--ins'] = devices
  183. log_search['device_id--ins'] = devices
  184. log_search['type_id'] = 2
  185. log_search['farm_id--ins'] = farm
  186. # 获取设备日志
  187. self.data['logs'] = self.service('common').list('msg', search=log_search, limit='0,1')
  188. if self.data['logs']:
  189. for key, value in enumerate(self.data['logs']):
  190. type_info = self.service('common').one('msg_type', id=value['type_id'])
  191. self.data['logs'][key]['type'] = type_info['name']
  192. self.data['logs'][key]['cdates'] = Demeter.date(value['cdate'])
  193. self.data['info'] = farmInfo
  194. if not self.data['info']['welcome']:
  195. self.data['info']['welcome'] = self.data['info']['name']
  196. self.data['info']['welcome'] = '保定清苑农业局物联网监测系统'
  197. self.data['noset'] = True
  198. self.data['stat'] = True
  199. self.view("device/screens.html")
  200. #获取设备数据和设备控制
  201. class data_path(Load):
  202. @Web.auth
  203. @Web.setting
  204. def get(self):
  205. Device.data(self)
  206. if 'stat' in self.data and self.data['stat']:
  207. Device.statBase(self, self.data['stat'])
  208. self.view("device/data.html")
  209. #获取图像
  210. class pic_path(Load):
  211. @Web.auth
  212. @Web.setting
  213. def get(self):
  214. Device.pic(self)
  215. self.view("device/pic.html")
  216. #数据统计
  217. class stat_path(Load):
  218. @Web.auth
  219. @Web.setting
  220. def get(self):
  221. Device.stat(self)
  222. self.view("device/stat.html")
  223. #数据导出
  224. class out_path(Load):
  225. @Web.auth
  226. @Web.setting
  227. def get(self):
  228. #Device.out(self)
  229. print 22
  230. self.view("device/out.html")
  231. #设备日志
  232. class log_path(Load):
  233. @Web.auth
  234. @Web.setting
  235. def get(self):
  236. Device.log(self)
  237. self.view("device/logs.html")
  238. #设备分组
  239. class group_path(Load):
  240. @Web.auth
  241. @Web.setting
  242. def get(self):
  243. # 获取页面
  244. self.data['page'] = self.service('common').list('device_page', order='inorder asc,cdate desc', search={'farm_id':self.data['setting']['farm']})
  245. if self.data['page']:
  246. for key, value in enumerate(self.data['page']):
  247. self.data['page'][key]['group'] = self.service('common').list('device_group', search={'page_id':value['id'],'farm_id':self.data['setting']['farm']} , order='inorder asc,cdate desc')
  248. if self.data['page'][key]['group']:
  249. for groupk, groupv in enumerate(self.data['page'][key]['group']):
  250. self.data['page'][key]['group'][groupk]['device'] = Device.get(self, search={'id--ins':groupv['devices'],'farm_id':self.data['setting']['farm']})
  251. self.view("device/group.html")
  252. #更新设备的上下限
  253. class update_device_path(Load):
  254. @Web.auth
  255. @Web.setting
  256. def get(self):
  257. id = self.input('device_id')
  258. if id:
  259. Device.data(self)
  260. else:
  261. id = self.input('id')
  262. self.data['set'] = {}
  263. if id:
  264. self.data['set'] = self.service('common').one('device_info', id=id)
  265. if self.data['set'] and self.data['set']['type_id'] > 0:
  266. self.data['type'] = self.service('common').one('device_type', id=self.data['set']['type_id'])
  267. self.view("device/alert/device.html")
  268. @Web.auth
  269. @Web.setting
  270. def post(self):
  271. id = self.input('id')
  272. update = {}
  273. update['max'] = self.input('update_max')
  274. update['min'] = self.input('update_min')
  275. if not update['max'] and not update['min']:
  276. self.out(u'请至少输入一个上限或者下限')
  277. return
  278. if not update['min']:
  279. del update['min']
  280. if not update['max']:
  281. del update['max']
  282. state = self.service('common').update('device_info', id, update)
  283. self.out('yes', {'id':state})
  284. #更新页面
  285. class update_page_path(Load):
  286. @Web.auth
  287. @Web.setting
  288. def get(self):
  289. id = self.input('id')
  290. #self.data['ico'] = ('cj', 'kz', 'syt', 'wg', 'pl', 'dpm', 'fz', 'zn')
  291. self.data['ico'] = ('cj', 'kz', 'syt')
  292. self.data['info'] = {}
  293. if id:
  294. self.data['info'] = self.service('common').one('device_page', id=id)
  295. else:
  296. self.data['info']['ico'] = self.data['ico'][0]
  297. self.view("device/alert/page.html")
  298. @Web.auth
  299. @Web.setting
  300. def post(self):
  301. id = self.input('id')
  302. update = {}
  303. update['name'] = self.input('update_name')
  304. update['ico'] = self.input('update_ico')
  305. update['inorder'] = self.input('update_inorder', 0)
  306. if not update['inorder']:
  307. update['inorder'] = 0
  308. update['inorder'] = int(update['inorder'])
  309. update['stat'] = self.input('update_stat')
  310. if not update['name']:
  311. self.out(u'请输入页面名称')
  312. return
  313. if not update['ico']:
  314. self.out(u'请选择图标')
  315. return
  316. if update['stat']:
  317. update['stat'] = True
  318. else:
  319. update['stat'] = False
  320. state = self.service('common').update('device_page', id, update)
  321. self.out('yes', {'id':state})
  322. #更新分组
  323. class update_group_path(Load):
  324. @Web.auth
  325. @Web.setting
  326. def get(self):
  327. page = self.input('page')
  328. self.data['page'] = self.service('common').one('device_page', id=page)
  329. id = self.input('id')
  330. self.data['info'] = {}
  331. if id:
  332. self.data['info'] = self.service('common').one('device_group', id=id)
  333. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  334. self.data['device'] = Device.getByGateway(self, search={'farm_id':self.data['setting']['farm']})
  335. self.view("device/alert/group.html")
  336. @Web.auth
  337. @Web.setting
  338. def post(self):
  339. id = self.input('id')
  340. update = {}
  341. update['page_id'] = self.input('update_page_id')
  342. update['name'] = self.input('update_name')
  343. update['devices'] = self.inputs('update_devices')
  344. update['inorder'] = self.input('update_inorder', 0)
  345. if not update['inorder']:
  346. update['inorder'] = 0
  347. update['inorder'] = int(update['inorder'])
  348. if not update['name']:
  349. self.out(u'请输入分组名称')
  350. return
  351. if not update['devices']:
  352. self.out(u'请选择设备')
  353. return
  354. update['devices'] = str(update['devices'])
  355. state = self.service('common').update('device_group', id, update)
  356. self.out('yes', {'id':state})
  357. @Web.auth
  358. @Web.setting
  359. def delete(self):
  360. id = self.input('id')
  361. if not id:
  362. self.out('no')
  363. return
  364. state = self.service('common').rDelete('device_group', id)
  365. self.out('yes', {'state':state})
  366. #智能设置-批量设置
  367. class set_mul_path(Load):
  368. @Web.auth
  369. @Web.setting
  370. def get(self):
  371. self.data['name'] = '批量控制'
  372. Device.set(self)
  373. self.data['mul'] = self.service('common').list('device_mul', search={'farm_id':self.data['setting']['farm']})
  374. if self.data['mul']:
  375. for key, value in enumerate(self.data['mul']):
  376. self.data['mul'][key]['device'] = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']})
  377. self.view("device/set/mul.html")
  378. #更新批量控制
  379. class update_set_mul_path(Load):
  380. @Web.auth
  381. @Web.setting
  382. def get(self):
  383. id = self.input('id')
  384. self.data['info'] = {}
  385. if id:
  386. self.data['info'] = self.service('common').one('device_mul', id=id)
  387. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  388. self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']})
  389. self.data['type'] = ({'id':1, 'name':'开关'},{'id':2, 'name':'卷帘'})
  390. self.view("device/alert/mul.html")
  391. @Web.auth
  392. @Web.setting
  393. def post(self):
  394. id = self.input('id')
  395. update = {}
  396. update['name'] = self.input('update_name')
  397. update['device_type'] = self.input('update_type')
  398. update['devices'] = self.inputs('update_devices')
  399. if not update['name']:
  400. self.out(u'请输入名称')
  401. return
  402. if not update['device_type']:
  403. self.out(u'请选择设备类型')
  404. return
  405. if not update['devices']:
  406. self.out(u'请选择设备')
  407. return
  408. update['devices'] = str(update['devices'])
  409. state = self.service('common').update('device_mul', id, update)
  410. self.out('yes', {'id':state})
  411. @Web.auth
  412. @Web.setting
  413. def delete(self):
  414. id = self.input('id')
  415. if not id:
  416. self.out('no')
  417. return
  418. state = self.service('common').rDelete('device_mul', id)
  419. self.out('yes', {'state':state})
  420. #智能设置-定时设置
  421. class set_timing_path(Load):
  422. @Web.auth
  423. @Web.setting
  424. def get(self):
  425. self.data['name'] = '定时设置'
  426. Device.set(self)
  427. Device.setCronOper(self)
  428. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  429. self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']})
  430. self.data['list'] = self.service('common').list('device_set_timing', state=True, page={'num':10}, search={'farm_id':self.data['setting']['farm']})
  431. if self.data['list']:
  432. for key, value in enumerate(self.data['list']):
  433. self.data['list'][key]['device'] = []
  434. if value['devices']:
  435. device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']})
  436. if device:
  437. self.data['list'][key]['device'] = device
  438. self.data['list'][key]['oper'] = self.data['oper'][value['oper']-1]['name']
  439. self.data['list'][key]['cdates'] = Demeter.date(value['cdate'])
  440. self.data['list'][key]['zdates'] = Demeter.date(value['zdate'])
  441. if value['status'] == 1:
  442. self.data['list'][key]['status_text'] = '执行成功'
  443. elif value['status'] == 2:
  444. self.data['list'][key]['status_text'] = '执行失败'
  445. self.view("device/set/timing.html")
  446. class update_set_timing_path(Load):
  447. @Web.auth
  448. @Web.setting
  449. def get(self):
  450. id = self.input('id')
  451. Device.setCronOper(self)
  452. self.data['info'] = {}
  453. if id:
  454. self.data['info'] = self.service('common').one('device_set_timing', id=id)
  455. if self.data['info'] and self.data['info']['zdate']:
  456. self.data['info']['zdate'] = Demeter.date(self.data['info']['zdate'])
  457. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  458. self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']})
  459. self.view("device/alert/timing.html")
  460. @Web.auth
  461. @Web.setting
  462. def post(self):
  463. id = self.input('id')
  464. update = {}
  465. update['devices'] = self.inputs('update_devices')
  466. update['oper'] = self.input('update_oper')
  467. update['zdate'] = self.input('update_zdate')
  468. if not update['devices']:
  469. self.out(u'请选择设备')
  470. return
  471. if not update['oper']:
  472. self.out(u'请选择执行结果')
  473. return
  474. if not update['zdate']:
  475. self.out(u'请选择执行时间')
  476. return
  477. update['devices'] = str(update['devices'])
  478. state = self.service('common').update('device_set_timing', id, update)
  479. self.out('yes', {'id':state})
  480. @Web.auth
  481. @Web.setting
  482. def delete(self):
  483. id = self.input('id')
  484. if not id:
  485. self.out('no')
  486. return
  487. state = self.service('common').rDelete('device_set_timing', id)
  488. self.out('yes', {'state':state})
  489. #智能设置-周期设置
  490. class set_loop_path(Load):
  491. @Web.auth
  492. @Web.setting
  493. def get(self):
  494. self.data['name'] = '周期设置'
  495. Device.set(self)
  496. Device.setCronOper(self)
  497. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  498. self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']})
  499. self.data['list'] = self.service('common').list('device_set_loop', page={'num':10}, search={'farm_id':self.data['setting']['farm']})
  500. if self.data['list']:
  501. for key, value in enumerate(self.data['list']):
  502. self.data['list'][key]['device'] = []
  503. if value['devices']:
  504. device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']})
  505. if device:
  506. self.data['list'][key]['device'] = device
  507. if value['looptype'] == 2:
  508. value['loop'] = value['loop'].split(',')
  509. loop = []
  510. for v in self.data['week']:
  511. if str(v['id']) in value['loop']:
  512. loop.append(v['name'])
  513. loop = ' '.join(loop) + ' '
  514. elif value['looptype'] == 3:
  515. loop = value['loop'].replace(',', ' ') + ' '
  516. else:
  517. loop = ''
  518. self.data['list'][key]['loop'] = loop
  519. self.data['list'][key]['oper'] = self.data['oper'][value['oper']-1]['name']
  520. self.data['list'][key]['looptype'] = self.data['looptype'][value['looptype']-1]['list']
  521. self.data['list'][key]['cdates'] = Demeter.date(value['cdate'])
  522. self.data['list'][key]['hour'] = Demeter.hour(value['hour'])
  523. self.data['list'][key]['minute'] = Demeter.hour(value['minute'])
  524. self.view("device/set/loop.html")
  525. class update_set_loop_path(Load):
  526. @Web.auth
  527. @Web.setting
  528. def get(self):
  529. id = self.input('id')
  530. Device.setCronOper(self)
  531. self.data['info'] = {}
  532. if id:
  533. self.data['info'] = self.service('common').one('device_set_loop', id=id)
  534. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  535. self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']})
  536. self.view("device/alert/loop.html")
  537. @Web.auth
  538. @Web.setting
  539. def post(self):
  540. id = self.input('id')
  541. update = {}
  542. update['devices'] = self.inputs('update_devices')
  543. update['oper'] = self.input('update_oper')
  544. update['loop'] = self.input('update_loop')
  545. update['hour'] = self.input('update_hour')
  546. update['minute'] = self.input('update_minute')
  547. update['looptype'] = self.input('update_looptype')
  548. if not update['devices']:
  549. self.out(u'请选择设备')
  550. return
  551. if not update['oper']:
  552. self.out(u'请选择执行结果')
  553. return
  554. if not update['hour']:
  555. self.out(u'请选择执行时间')
  556. return
  557. if not update['looptype']:
  558. self.out(u'请选择执行时间')
  559. return
  560. if update['looptype'] != '1' and not update['loop']:
  561. self.out(u'请选择执行时间')
  562. return
  563. update['devices'] = str(update['devices'])
  564. state = self.service('common').update('device_set_loop', id, update)
  565. self.out('yes', {'id':state})
  566. @Web.auth
  567. @Web.setting
  568. def delete(self):
  569. id = self.input('id')
  570. if not id:
  571. self.out('no')
  572. return
  573. state = self.input('state', False)
  574. update = {}
  575. update['status'] = state
  576. state = self.service('common').update('device_set_loop', id, update)
  577. self.out('yes', {'state':state})
  578. #智能设置-条件控制
  579. class set_condition_path(Load):
  580. @Web.auth
  581. @Web.setting
  582. def get(self):
  583. self.data['name'] = '条件控制'
  584. Device.set(self)
  585. Device.setCronOper(self)
  586. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  587. self.data['device'] = Device.getByGateway(self, search={'hardware_type': 3,'farm_id':self.data['setting']['farm']})
  588. self.data['list'] = self.service('common').list('device_set_condition', state=True, page={'num':10}, search={'farm_id':self.data['setting']['farm']})
  589. if self.data['list']:
  590. for key, value in enumerate(self.data['list']):
  591. self.data['list'][key]['device'] = []
  592. if value['devices']:
  593. device = Device.get(self, search={'id--ins':value['devices'],'farm_id':self.data['setting']['farm']})
  594. if device:
  595. self.data['list'][key]['device'] = device
  596. self.data['list'][key]['cdates'] = Demeter.date(value['cdate'])
  597. self.data['list'][key]['oper'] = self.data['opers'][value['oper']-1]['name']
  598. self.data['list'][key]['condition'] = self.data['condition'][value['condition']-1]['exp']
  599. gateway = self.service('common').one('device_gateway', id=value['gateway_id'])
  600. self.data['list'][key]['gatewayName'] = gateway['name']
  601. device = self.service('common').one('device_info', id=value['device_id'])
  602. if device:
  603. self.data['list'][key]['deviceName'] = device['name']
  604. type_info = self.service('common').one('device_type', id=device['type_id'])
  605. self.data['list'][key]['unit'] = type_info['unit']
  606. self.view("device/set/condition.html")
  607. class update_set_condition_path(Load):
  608. @Web.auth
  609. @Web.setting
  610. def get(self):
  611. id = self.input('id')
  612. Device.setCronOper(self)
  613. self.data['info'] = {}
  614. if id:
  615. self.data['info'] = self.service('common').one('device_set_condition', id=id)
  616. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  617. self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 3,'farm_id':self.data['setting']['farm']})
  618. self.data['device_list'] = self.service('common').list('device_info', search={'hardware_type--ins' : (2,4),'gateway_id':self.data['gateway'][0]['id']})
  619. self.view("device/alert/condition.html")
  620. @Web.auth
  621. @Web.setting
  622. def post(self):
  623. id = self.input('id')
  624. update = {}
  625. update['devices'] = self.inputs('update_devices')
  626. update['oper'] = self.input('update_oper')
  627. update['gateway_id'] = self.input('update_gateway_id')
  628. update['device_id'] = self.input('update_device_id')
  629. update['condition'] = self.input('update_condition')
  630. update['value'] = self.input('update_value')
  631. update['notice'] = self.input('update_notice')
  632. if not update['devices']:
  633. self.out(u'请选择设备')
  634. return
  635. if not update['oper']:
  636. self.out(u'请选择执行结果')
  637. return
  638. if not update['gateway_id']:
  639. self.out(u'请选择设备条件')
  640. return
  641. if not update['device_id']:
  642. self.out(u'请选择设备条件')
  643. return
  644. if not update['condition']:
  645. self.out(u'请选择判断条件')
  646. return
  647. if not update['value']:
  648. self.out(u'请输入判断条件的值')
  649. return
  650. if update['notice']:
  651. update['notice'] = True
  652. else:
  653. update['notice'] = False
  654. update['devices'] = str(update['devices'])
  655. state = self.service('common').update('device_set_condition', id, update)
  656. self.out('yes', {'id':state})
  657. @Web.auth
  658. @Web.setting
  659. def delete(self):
  660. id = self.input('id')
  661. if not id:
  662. self.out('no')
  663. return
  664. state = self.input('state', False)
  665. update = {}
  666. update['status'] = state
  667. state = self.service('common').update('device_set_condition', id, update)
  668. self.out('yes', {'state':state})
  669. #更新控制设备的状态
  670. class update_path(Load):
  671. @Web.auth
  672. @Web.setting
  673. def post(self):
  674. id = self.input('id')
  675. value = self.input('value')
  676. if id:
  677. Demeter.service('device').switch(id, switch=value)
  678. self.out('yes', {'id':id})
  679. """
  680. yield self.switch(id, value)
  681. Shell.popen(Demeter.path + 'switch.py ' + id + ' ' + value, bg=True)
  682. response = response.replace("\n", "")
  683. if response == 'ok':
  684. self.out('yes', {'id':id})
  685. else:
  686. model = Demeter.model('notice_type')
  687. model.key = response
  688. info = model.select(type='fetchone')
  689. if info:
  690. response = info['name']
  691. self.out(response)
  692. """
  693. else:
  694. self.out('no')
  695. @tornado.gen.coroutine
  696. def switch(self, id, value):
  697. Demeter.service('device').switch(id, switch=value)
  698. class get_path(Load):
  699. @Web.auth
  700. @Web.setting
  701. def get(self):
  702. # 根据网关id获取设备
  703. id = self.input('id')
  704. device = Device.get(self, search={'gateway_id':id, 'hardware_type': 2,'farm_id':self.data['setting']['farm']})
  705. self.view("device/alert/select_device.html")