#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter web
    name:main.py 主界面相关api
    author:rabin
"""
from __load__ import *

class index_path(Load):
	"""
	@api {get} / 获取首页信息
	@apiVersion 1.0.0
	@apiName getIndex
	@apiGroup Common

	@apiSuccess {Object[]} setting 基本信息
	@apiSuccess {String}   setting.name  站点名
	@apiSuccess {Number}   setting.farm  当前的农场ID

	@apiSuccess {Object[]} farm 农场基本信息
	@apiSuccess {String}   farm.name  农场名

	@apiSuccess {Object[]} gateway 网关信息（环境监测）
	@apiSuccess {String}   gateway.name  网关名称
	@apiSuccess {String}   gateway.id    网关ID
	@apiErrorExample 操作成功
	{"status": 1, "msg": "yes", "code": 0, "data": {"farm": {"info": "3123fdf", "master_name": "31232", "tel": "323", "name": "\u9ed8\u8ba4\u56ed\u533a", "pic": "http://192.168.15.10:8087/upload/2017/09/27/5c3d27b95a0375cd5c0eab03c61a494a.jpg", "cdate": 1506496822, "gateway": "", "master_pic": "", "state": true, "location": "", "address": "312", "id": 1}, "gateway": [{"stat": false, "pic": null, "hardware_id": 10086, "farm_id": 1, "cdate": 1506496123, "state": true, "camera": null, "id": "542535db-6c0b-51fc-9f73-cafb3872ce76", "name": "\u9ed8\u8ba4\u7f51\u5173"}], "setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "base": {}, "msg": 0}}
	@apiErrorExample 操作失败
	{"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
	"""
	@auth
	@Web.setting
	def get(self):
		# 获取网关
		self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id': Demeter.config['setting']['farm']})

		# 获取农场信息
		self.data['farm'] = self.service('common').one('farm', id=Demeter.config['setting']['farm'])

		# 获取消息数量
		self.data['msg'] = len(self.service('common').list('msg', search={'status':False, "farm_id":self.data['setting']['farm']}, limit='0,100'))
		if self.data['msg'] > 99:
			self.data['msg'] = str(99) + '+'

		self.out('yes', self.data)