#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter web page
    name:hardware.py 硬件相关页面
    author:rabin
"""
from __load__ import *

class hardware_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'硬件设备'
			,path = '/hardware/hardware'
			,width = '600'
			,height = '600'
			,add = False
			,edit = False
			,search = (('farm_id-select-','hardware_type-select-','hardware_id-input-mlike'), (u'选择农场',u'硬件类型', u'硬件id'))
			,thead = (u'所属农场', u'硬件类型', u'硬件id',u'网关名',u'设备名', u'接入时间')
			,tbody = ('farm','type', 'hardware_id', 'gateway', 'device', 'cdate')
			,state = False
		)
		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
		self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
		self.commonList('hardware')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				farm = self.service('common').one('farm', id=value['farm_id'])
				self.data['list'][key]['farm'] = farm['name']
				htype = self.service('common').one('hardware_type', id=value['hardware_type'])
				self.data['list'][key]['type'] = htype['name']

				gateway = self.service('common').one('device_gateway', id=value['gateway_id'])
				self.data['list'][key]['gateway'] = gateway['name']

				if value['device_id']:
					device = self.service('common').one('device_info', id=value['device_id'])
					self.data['list'][key]['device'] = device['name']
				else:
					self.data['list'][key]['device'] = ''
		self.commonView('list')

class hardware_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			path = '/hardware/hardware'
			,label = (u'硬件类型',u'硬件id')
			,update = ('hardware_type-select-required','hardware_id-input-required')
			,update_hardware_type = self.service('common').list('hardware_type')
		)
		self.commonOne('hardware')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('hardware')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('hardware')

class type_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			name = u'硬件类型'
			,path = '/hardware/type'
			,width = '600'
			,height = '300'
			,search = (('name-input-mlike',), (u'类型名称',))
			,thead = (u'类型名称',u'类型key', u'更新时间')
			,tbody = ('name', 'key', 'cdate')
			,state = False
		)
		self.commonList('hardware_type')
		self.commonView('list')

class type_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.common(
			path = '/hardware/type'
			,label = (u'类型名称', u'类型key')
			,update = ('name-input-required', 'key-input-required')
		)
		self.commonOne('hardware_type')
		self.commonView('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.commonUpdate('hardware_type')
	@Web.auth
	@Web.setting
	def delete(self):
		self.commonDelete('hardware_type')
