#!/usr/bin/env python
# -*- coding: utf-8 -*-
from demeter.core import *

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 web():
	model = Demeter.model('web_set')
	model.id = 1
	info = model.select(type='fetchone')
	if not info:
		model.name = '农小盒'
		model.subname = '农作物生长监测与产品溯源'
		model.keywords = '农小盒,智慧云平台,智能硬件,大数据,农业,农业互联网'
		model.description = '农小盒'
		model.icp = '京ICP备12045252号-2'
		model.tel = '18701029660'
		model.passtel = '18701029660'
		model.mobile = ''
		model.buy = ''
		model.insert()

	model = Demeter.model('web_article_category')
	data = ('站点介绍', '日志', '产品功能', '客户案例')
	for value in data:
		model.name = value
		info = model.select(type='fetchone')
		if not info:
			model.name = value
			model.insert()

	model = Demeter.model('web_article')
	data = ('联系我们',)
	for value in data:
		model.name = value
		info = model.select(type='fetchone')
		if not info:
			model.category_id = 1
			model.name = value
			model.insert()

	model = Demeter.model('web_menu')
	data = (
		{'name':'首页','link':'/main'}
		,{'name':'农小盒日志','link':'/article'}
		,{'name':'联系我们','link':'/us'}
		)
	for value in data:
		model.link = value['link']
		info = model.select(type='fetchone')
		if not info:
			model.name = value['name']
			model.link = value['link']
			model.insert()

def farm():
	farm_id = 1
	model = Demeter.model('farm')
	model.id = farm_id
	info = model.select(type='fetchone')
	if not info:
		model.name = '默认园区'
		model.insert()

	# 初始化网关，测试用
	hardware_id = 10086
	model = Demeter.model('device_gateway')
	model.hardware_id = hardware_id
	info = model.select(type='fetchone')
	if not info:
		model.farm_id = farm_id
		model.hardware_id = hardware_id
		model.name = '默认网关'
		id = model.insert()
		Demeter.service('device').upGatewayStatus(farm_id, id, hardware_id)

def device():
	# 以下类型只能新增，不能随意更改或删除
	# 初始化设备类型
	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'}
		)
	model = Demeter.model('device_type')
	for value in 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()

	# 初始化农事分类
	msg_type = (
		{'name':'施肥'}
		,{'name':'劳作'}
		,{'name':'除虫'}
		)
	model = Demeter.model('farm_work_category')
	for value in msg_type:
		model.name = value['name']
		info = model.select(type='fetchone')
		if not info:
			model.name = value['name']
			model.insert()

manage()
web()
farm()
device()
print 'install success!'