device.py 26 KB

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