queue.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter web
  4. name:admin.py
  5. author:rabin
  6. """
  7. import time
  8. from demeter.core import *
  9. timeSleep = 10
  10. # 批量控制的队列 这块有点乱,但时间太紧,只有两天时间,以后改造优化
  11. class Queue(object):
  12. def run(self):
  13. self.handle()
  14. time.sleep(timeSleep)
  15. def handle(self):
  16. mul_model = Demeter.model('device_mul')
  17. device_model = Demeter.model('device_info')
  18. model = Demeter.model('device_mul_queue')
  19. model.status = 1
  20. data = model.select(type='fetchone', order='inorder asc,udate asc,hardware_id asc')
  21. service = Demeter.service('device')
  22. # 查看当前有没有正在执行的设备,如果有,不能继续执行了
  23. msg = service.notice('timeout')
  24. device_model.oper = True
  25. device_model.exp.nq(msg)
  26. device_model.hardware_type = 3
  27. device = device_model.select(type='fetchone')
  28. if not device and data and not data['operstate']:
  29. service.switch(data['device_id'], switch=data['value'], mul=False, queue=data['id'])
  30. model.id = data['id']
  31. model.update(operstate=True)
  32. #mul_model.id = data['mul_id']
  33. #mul_model.update(oper='')
  34. # 将执行完成的进行清理
  35. mul_model.oper.nq('')
  36. data = mul_model.select()
  37. if data:
  38. for v in data:
  39. devicesNum = len(tuple(eval(v['devices'])))
  40. model.status.ins((2,3))
  41. cur = Demeter.time() - 10
  42. model.operdate.assgin(cur, '<=')
  43. queue = model.select()
  44. num = len(queue)
  45. if num == devicesNum:
  46. errorNum = 0
  47. model.mul_id = v['id']
  48. model.update(status=4)
  49. for qv in queue:
  50. if qv['status'] == 3:
  51. errorNum = errorNum + 1
  52. mul_model.id = v['id']
  53. if errorNum == devicesNum:
  54. mul_model.update(oper='', value=v['old'])
  55. else:
  56. mul_model.update(oper='')