# -*- coding: utf-8 -*-
from demeter.core import *
import os
#cp -R modbus_tk/* /usr/lib/python3.6/site-packages/modbus_tk/
def manage():
	model = Demeter.model('manage_admin')
	model.id = 1
	info = model.select(type='fetchone')
	if not info:
		model.role_id = 1
		model.username = 'admin'
		model.mobile = '15810090845'
		model.password = '123456'
		model.insert()

	model = Demeter.model('manage_role')
	model.id = 1
	info = model.select(type='fetchone')
	if not info:
		model.name = '管理员'
		model.insert()

def farm():
	if 'IOT_FARM' in os.environ:
		Demeter.config['setting']['farm'] = int(os.environ['IOT_FARM'])
	farm_id = Demeter.config['setting']['farm']
	model = Demeter.model('farm')
	model.id = farm_id
	info = model.select(type='fetchone')
	if not info:
		model.id = farm_id
		model.name = '第' + str(farm_id) + '号本地园区';
		model.insert()

def device():
	# 以下类型只能新增，不能随意更改或删除

	# 初始化默认服务
	'''
	setting_server = (
		{'name':'默认mqtt服务（暂时无效）', 'server_type':1, 'server_host': '192.168.33.10', 'server_port':'8010', 'server_time': '200'}
		,{'name':'默认modbusTcp服务', 'server_type':2, 'server_host': '192.168.33.10', 'server_port':'8010', 'server_time': '200'}
		)
	model = Demeter.model('setting_server')
	for value in setting_server:
		model.server_type = value['server_type']
		model.server_host = value['server_host']
		model.server_port = value['server_port']
		info = model.select(type='fetchone')
		if not info:
			model.name = value['name']
			model.server_type = value['server_type']
			model.server_host = value['server_host']
			model.server_port = value['server_port']
			model.server_time = value['server_time']
			model.insert()
	'''

	# 初始化默认产品
	product = (
		{'name':'默认产品', 'key':'product'},
		)
	model = Demeter.model('setting_product')
	for value in product:
		model.key = value['key']
		info = model.select(type='fetchone')
		if not info:
			model.key = value['key']
			model.name = value['name']
			model.insert()

	# 初始化设备类型
	setting_device_type = (
		{'name':'温度', 'key':'temprature', 'unit':'℃'}
		,{'name':'湿度', 'key':'humidity','unit':'%RH'}
		,{'name':'光照', 'key':'light','unit':'LUX'}
		,{'name':'二氧化碳', 'key':'co2','unit':'PPM'}
		,{'name':'风速', 'key':'speed','unit':'km/h'}
		,{'name':'风向', 'key':'direction','unit':'°'}
		,{'name':'PM2.5', 'key':'pm2.5','unit':'μg/m3'}
		,{'name':'电源', 'key':'power','unit':'V'}
		,{'name':'阀门', 'key':'valve','unit':''}
		,{'name':'放风机', 'key':'blower','unit':''}
		,{'name':'卷帘机', 'key':'curtain','unit':'button'}
		,{'name':'灯光', 'key':'lamp','unit':''}
		,{'name':'除湿器', 'key':'airdryer','unit':''}
		,{'name':'大气压', 'key':'atmos','unit':'hpa'}
		,{'name':'开关', 'key':'sw','unit':'状态'}
		,{'name':'流量', 'key':'flow','unit':'立方米'}
		,{'name':'雨雪', 'key':'yuxue','unit':'button'}
		,{'name':'雨量', 'key':'yuliang','unit':'mm'}
		,{'name':'重量', 'key':'weight','unit':'kg'}
		,{'name':'重量', 'key':'g','unit':'g'}
		,{'name':'上限位', 'key':'uplimit','unit':'状态'}
		,{'name':'下限位', 'key':'downlimit','unit':'状态'}
		,{'name':'空气温度', 'key':'air_temp','unit':'℃'}
		,{'name':'空气湿度', 'key':'air_hume','unit':'%'}
		,{'name':'土壤温度', 'key':'soil_temp','unit':'℃'}
		,{'name':'土壤湿度', 'key':'soil_hume','unit':'%'}
		,{'name':'水泵', 'key':'pump','unit':''}
		)
	model = Demeter.model('setting_device_type')
	for value in setting_device_type:
		model.key = value['key']
		info = model.select(type='fetchone')
		if not info:
			model.key = value['key']
			model.name = value['name']
			model.exp = '{n}'
			model.unit = value['unit']
			model.insert()

	# 初始化硬件类型 1/1/2/3/4/5/6
	hardware_type = (
		{'name':'网关', 'key':'gateway'}
		,{'name':'传感器', 'key':'sensor'}
		,{'name':'控制器', 'key':'control'}
		,{'name':'电源', 'key':'power'}
		,{'name':'状态', 'key':'status'}
		,{'name':'摄像头', 'key':'pic'}
		,{'name':'摄像机', 'key':'camera'}
		)
	model = Demeter.model('hardware_type')
	for value in hardware_type:
		model.key = value['key']
		info = model.select(type='fetchone')
		if not info:
			model.key = value['key']
			model.name = value['name']
			model.insert()

	# 初始化消息类型
	msg_type = (
		{'name':'系统消息', 'key':'system'}
		,{'name':'警告提醒', 'key':'notice'}
		,{'name':'操作提示', 'key':'oper'}
		,{'name':'新闻', 'key':'news'}
		)
	model = Demeter.model('msg_type')
	for value in msg_type:
		model.key = value['key']
		info = model.select(type='fetchone')
		if not info:
			model.key = value['key']
			model.name = value['name']
			model.insert()

	# 初始化错误码类型
	notice_type = (
		{'name':'设备离线', 'key':'offline'}
		,{'name':'请求超时', 'key':'timeout'}
		,{'name':'设备失联', 'key':'outlink'}
		,{'name':'设备响应超时', 'key':'devtimeot'}
		)
	model = Demeter.model('notice_type')
	for value in notice_type:
		model.key = value['key']
		info = model.select(type='fetchone')
		if not info:
			model.key = value['key']
			model.name = value['name']
			model.insert()

manage()
farm()
device()
Demeter.echo('install success!')