# -*- 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.set(
			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.list('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'] = '无效'
				if gateway:
					self.data['list'][key]['gateway'] = gateway['name']
				self.data['list'][key]['device'] = '无效'
				if value['device_id']:
					device = self.service('common').one('device_info', id=value['device_id'])
					if device:
						self.data['list'][key]['device'] = device['name']
					
		self.show('list')

class hardware_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			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.one('hardware')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		self.update('hardware')
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('hardware')

class type_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			name = u'硬件类型'
			,path = '/hardware/type'
			,button = ({'name':'重启守护进程','link':'/hardware/restart_cron','ajax':True,'icon':'&#xe631;'},{'name':'重启mqtt订阅服务','link':'/hardware/restart_sub','ajax':True,'icon':'&#xe631;'},{'name':'重启modbus服务','link':'/hardware/restart_modbus','ajax':True,'icon':'&#xe631;'},{'name':'重启园区服务','link':'/hardware/restart_front','ajax':True,'icon':'&#xe631;'},{'name':'重启后台服务','link':'/hardware/restart_admin','ajax':True,'icon':'&#xe631;'})
			,width = '600'
			,height = '300'
			,search = (('name-input-mlike',), (u'类型名称',))
			,thead = (u'类型ID',u'类型名称',u'类型key', u'更新时间')
			,tbody = ('id', 'name', 'key', 'cdate')
			,state = False
			,add = False
			,edit = False
		)
		self.list('hardware_type')
		self.show('list')

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

class restart_cron_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep cron.py | grep -v grep |awk '{print $1}'|xargs kill -9")
		#Shell.popen('/usr/local/demeter/cron.py >/dev/null &')

class restart_sub_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep sub.py | grep -v grep |awk '{print $1}'|xargs kill -9")

class restart_modbus_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep modbus.py | grep -v grep |awk '{print $1}'|xargs kill -9")

class restart_admin_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep admin.py | grep -v grep |awk '{print $1}'|xargs kill -9")

class restart_front_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep front.py | grep -v grep |awk '{print $1}'|xargs kill -9")

class restart_web_path(Load):
	@Web.auth
	@Web.setting
	def post(self):
		Shell.popen("ps -ef | grep web.py | grep -v grep |awk '{print $1}'|xargs kill -9")