# -*- coding: utf-8 -*-
"""
    demeter web page
    name:main.py
    author:rabin
"""
from .__load__ import *

class Core(object):
	@staticmethod
	def check(self):
		pass


# eros注册
class init_path(Load):
	@Web.setting
	def post(self):
		Core.check(self)
		id = self.input('id')
		ip = self.input('ip')
		update = {}
		update['eros_status'] = 1
		update['eros_server'] = ip
		self.service('common').update('farm', id, update)
		self.out('ok')

# 同步数据
class sync_path(Load):
	@Web.setting
	def post(self):
		Core.check(self)
		data = self.input('data')
		
		table = self.input('table')

		if data:
			if '__' in data:
				param = data.split('__')
				data = {}
				data['type'] = param[3]
				data['gateway'] = param[4]
				data['device'] = param[1]
				data['hard'] = param[2]
				data['farm'] = param[0]
				data['source'] = param[5]
				data['value'] = param[6]
				data['time'] = Demeter.time()
				Demeter.model('data', 'tsdb').insert(data)
			else: 
				data = json.loads(data)
				for v in data:
					update = {}
					if 'password' in v:
						Demeter.config['db_md5'] = 1
					info = self.service('common').one(table, id=v['id'])
					if info:
						id = v['id']
						del v['cdate']
						if 'udate' in v:
							del v['udate']
						del v['id']
						for i in v:
							if v[i]:
								update[i] = v[i]
						self.service('common').update(table, id, update, cdate=False)
					else:
						del v['cdate']
						if 'udate' in v:
							del v['udate']
						for i in v:
							if v[i]:
								update[i] = v[i]
						self.service('common').update(table, False, update)
		
		self.out('ok')