cron.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:admin.py
  6. author:rabin
  7. """
  8. import time
  9. from demeter.core import *
  10. from gevent import monkey; monkey.patch_socket()
  11. import gevent
  12. from pic import pic
  13. from demeter.mqtt import *
  14. # 处理定时命令、周期命令、条件控制、消息、设备状态等
  15. timeSleep = 10
  16. # 开关设备
  17. def switch(value):
  18. Demeter.service('device').switchMul(value)
  19. # 更改设备状态(离线)
  20. def device():
  21. while 1:
  22. model = Demeter.model('device_info')
  23. cur = Demeter.time() - 1800
  24. model.cdate.assgin(cur, '<=')
  25. model.status = True
  26. data = model.select()
  27. for v in data:
  28. model.id = v['id']
  29. if v['hardware_type'] == 5:
  30. model.update(value='0', status=False)
  31. elif v['hardware_type'] != 7 and v['hardware_type'] != 6:
  32. model.update(status=False)
  33. gevent.sleep(60)
  34. # 更改控制设备的超时状态
  35. def control():
  36. while 1:
  37. model = Demeter.model('device_info')
  38. cur = Demeter.time() - 10
  39. model.operdate.assgin(cur, '<=')
  40. model.oper = True
  41. model.hardware_type = 3
  42. service = Demeter.service('device')
  43. data = model.select()
  44. if data:
  45. for v in data:
  46. model.id = v['id']
  47. if v['exp'] == '-1':
  48. msg = service.notice('timeout')
  49. service.mul(v, v['name'] + '失败:' + msg, False)
  50. model.update(exp=msg, oper=True, operdate=Demeter.time())
  51. else:
  52. msg = '1'
  53. service.mul(v, '', True)
  54. model.update(exp=msg, oper=False, operdate=Demeter.time())
  55. gevent.sleep(1)
  56. # 批量控制的队列 这块有点乱,但时间太紧,只有两天时间,以后改造优化
  57. def mulQueue():
  58. while 1:
  59. mul_model = Demeter.model('device_mul')
  60. device_model = Demeter.model('device_info')
  61. model = Demeter.model('device_mul_queue')
  62. model.status = 1
  63. data = model.select(type='fetchone', order='inorder asc,udate asc,hardware_id asc')
  64. service = Demeter.service('device')
  65. # 查看当前有没有正在执行的设备,如果有,不能继续执行了
  66. msg = service.notice('timeout')
  67. device_model.oper = True
  68. device_model.exp.nq(msg)
  69. device_model.hardware_type = 3
  70. device = device_model.select(type='fetchone')
  71. if not device and data and not data['operstate']:
  72. service.switch(data['device_id'], switch=data['value'], mul=False, queue=data['id'])
  73. model.id = data['id']
  74. model.update(operstate=True)
  75. #mul_model.id = data['mul_id']
  76. #mul_model.update(oper='')
  77. # 将执行完成的进行清理
  78. mul_model.oper.nq('')
  79. data = mul_model.select()
  80. if data:
  81. for v in data:
  82. devicesNum = len(tuple(eval(v['devices'])))
  83. model.status.ins((2,3))
  84. cur = Demeter.time() - 10
  85. model.operdate.assgin(cur, '<=')
  86. queue = model.select()
  87. num = len(queue)
  88. if num == devicesNum:
  89. errorNum = 0
  90. model.mul_id = v['id']
  91. model.update(status=4)
  92. for qv in queue:
  93. if qv['status'] == 3:
  94. errorNum = errorNum + 1
  95. mul_model.id = v['id']
  96. if errorNum == devicesNum:
  97. mul_model.update(oper='', value=v['old'])
  98. else:
  99. mul_model.update(oper='')
  100. gevent.sleep(1)
  101. # 批量控制的队列清理,10小时清理一次24小时之前完成的数据
  102. def mulQueueDrop():
  103. while 1:
  104. num = 3600*24
  105. model = Demeter.model('device_mul_queue')
  106. model.status = 4
  107. cur = Demeter.time() - num
  108. model.operdate.assgin(cur, '<=')
  109. model.delete()
  110. gevent.sleep(36000)
  111. def timing():
  112. while 1:
  113. model = Demeter.model('device_set_timing')
  114. model.status = 0
  115. cur = Demeter.time()
  116. model.zdate.assgin(cur, '<=')
  117. data = model.select()
  118. if data:
  119. for value in data:
  120. switch(value)
  121. model.id = value['id']
  122. model.update(status=1)
  123. gevent.sleep(timeSleep)
  124. def loop():
  125. while 1:
  126. model = Demeter.model('device_set_loop')
  127. cur = Demeter.time()
  128. date = Demeter.date(cur, '%Y-%m-%d-%w-%H-%M')
  129. date = date.split('-')
  130. week = date[3]
  131. day = date[2]
  132. model.status = True
  133. model.hour = date[4]
  134. model.minute = date[5]
  135. data = model.select()
  136. if data:
  137. for value in data:
  138. state = False
  139. value['loop'] = value['loop'].split(',')
  140. if value['looptype'] == 2:
  141. if week in value['loop']:
  142. state = True
  143. elif value['looptype'] == 3:
  144. if day in value['loop']:
  145. state = True
  146. if state:
  147. switch(value)
  148. gevent.sleep(timeSleep)
  149. def savePic():
  150. while 1:
  151. pic()
  152. gevent.sleep(1800)
  153. # 同步时间,24小时同步一次
  154. def timeSync():
  155. while 1:
  156. pub = Pub()
  157. key = 'time/bh'
  158. value = Demeter.date(Demeter.time())
  159. pub.push(key, value)
  160. gevent.sleep(3600*24)
  161. def handle():
  162. gevent.joinall([
  163. gevent.spawn(timing),
  164. gevent.spawn(loop),
  165. gevent.spawn(device),
  166. gevent.spawn(savePic),
  167. gevent.spawn(control),
  168. gevent.spawn(timeSync),
  169. gevent.spawn(mulQueue),
  170. gevent.spawn(mulQueueDrop),
  171. ])
  172. handle()