core.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. from demeter.core import *
  3. class Core(object):
  4. def init(self):
  5. # 注册
  6. if 'IOT_FARM' in os.environ:
  7. Demeter.config['setting']['farm'] = int(os.environ['IOT_FARM'])
  8. url = Demeter.config['zeus']['host'] + 'interface/init'
  9. farm = str(Demeter.config['setting']['farm'])
  10. ip = 'f' + farm + '.local.xinnongbaohe.com'
  11. data = Demeter.curl(url, {'id':farm,'ip':ip}, 'post')
  12. # 保存需要同步的数据,定时同步
  13. def save(self):
  14. pass
  15. # 实时同步
  16. def rsync(self, table, id):
  17. command = Demeter.path + 'sync.py -t ' + str(table) + ' -i ' + str(id)
  18. Shell.popen("python3 " + command, bg=True)
  19. #self.sync(table, {'id':id})
  20. # 远程同步
  21. def sync(self, table, search={}):
  22. url = Demeter.config['zeus']['host'] + 'interface/sync'
  23. if 'IOT_FARM' in os.environ:
  24. Demeter.config['setting']['farm'] = int(os.environ['IOT_FARM'])
  25. farm = Demeter.config['setting']['farm']
  26. state = True
  27. id = str(search['id'])
  28. if '__' not in id:
  29. data = Demeter.service('common').list(table, state=state, search=search)
  30. if not data:
  31. state = False
  32. data = Demeter.service('common').list(table, state=state, search=search)
  33. data = json.dumps(data)
  34. else:
  35. data = id
  36. param = {}
  37. param['data'] = data
  38. param['table'] = table
  39. param['farm'] = farm
  40. '''
  41. state = Demeter.curl(url=url, param=param, method='post', timeout=5, max=2)
  42. # 重试3次之后还失败,就要保存起来
  43. if not state:
  44. Demeter.service('common').update('sync_data', False, param)
  45. '''
  46. # 定时同步
  47. def timeSync(self):
  48. data = Demeter.service('common').update('sync_data')
  49. if data:
  50. url = Demeter.config['zeus']['host'] + 'interface/sync'
  51. for key, value in enumerate(data):
  52. param = {}
  53. param['data'] = value['data']
  54. param['table'] = value['table']
  55. param['farm'] = value['farm']
  56. state = Demeter.curl(url=url, param=param, method='post', timeout=5, max=2)
  57. if state:
  58. Demeter.service('common').rDelete('sync_data', value['id'])