cron.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 device():
  18. while 1:
  19. model = Demeter.model('device_info')
  20. cur = Demeter.time() - 1800
  21. model.cdate.assgin(cur, '<=')
  22. model.status = True
  23. data = model.select()
  24. for v in data:
  25. model.id = v['id']
  26. if v['hardware_type'] == 5:
  27. model.update(value='0', status=False)
  28. #提醒notice
  29. notice(2, v)
  30. elif v['hardware_type'] != 7 and v['hardware_type'] != 6:
  31. model.update(status=False)
  32. #提醒notice
  33. notice(2, v)
  34. gevent.sleep(60)
  35. def savePic():
  36. while 1:
  37. pic()
  38. gevent.sleep(1800)
  39. # 同步时间,24小时同步一次
  40. def timeSync():
  41. while 1:
  42. pub = Pub()
  43. key = 'time/bh'
  44. value = Demeter.date(Demeter.time())
  45. pub.push(key, value)
  46. gevent.sleep(3600*24)
  47. # 用户一周没有使用的时候提醒
  48. def userNotice():
  49. while 1:
  50. model = Demeter.model('farm_user')
  51. cur = Demeter.time() - 3600*24*7
  52. model.cdate.assgin(cur, '<=')
  53. model.status = True
  54. data = model.select()
  55. for v in data:
  56. notice(1, data)
  57. # 用户提醒
  58. def notice(t, data):
  59. pass
  60. def handle():
  61. gevent.joinall([
  62. gevent.spawn(device),
  63. gevent.spawn(savePic),
  64. gevent.spawn(timeSync),
  65. #gevent.spawn(userNotice),
  66. ])
  67. handle()