# -*- coding: utf-8 -*- from demeter.core import * class Core(object): def init(self): # 注册 if 'IOT_FARM' in os.environ: Demeter.config['setting']['farm'] = int(os.environ['IOT_FARM']) url = Demeter.config['zeus']['host'] + 'interface/init' farm = str(Demeter.config['setting']['farm']) ip = 'f' + farm + '.local.xinnongbaohe.com' data = Demeter.curl(url, {'id':farm,'ip':ip}, 'post') # 保存需要同步的数据,定时同步 def save(self): pass # 实时同步 def rsync(self, table, id): command = Demeter.path + 'sync.py -t ' + str(table) + ' -i ' + str(id) Shell.popen("python3 " + command, bg=True) #self.sync(table, {'id':id}) # 远程同步 def sync(self, table, search={}): url = Demeter.config['zeus']['host'] + 'interface/sync' if 'IOT_FARM' in os.environ: Demeter.config['setting']['farm'] = int(os.environ['IOT_FARM']) farm = Demeter.config['setting']['farm'] state = True id = str(search['id']) if '__' not in id: data = Demeter.service('common').list(table, state=state, search=search) if not data: state = False data = Demeter.service('common').list(table, state=state, search=search) data = json.dumps(data) else: data = id param = {} param['data'] = data param['table'] = table param['farm'] = farm ''' state = Demeter.curl(url=url, param=param, method='post', timeout=5, max=2) # 重试3次之后还失败,就要保存起来 if not state: Demeter.service('common').update('sync_data', False, param) ''' # 定时同步 def timeSync(self): data = Demeter.service('common').update('sync_data') if data: url = Demeter.config['zeus']['host'] + 'interface/sync' for key, value in enumerate(data): param = {} param['data'] = value['data'] param['table'] = value['table'] param['farm'] = value['farm'] state = Demeter.curl(url=url, param=param, method='post', timeout=5, max=2) if state: Demeter.service('common').rDelete('sync_data', value['id'])