device.py 26 KB

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