package com.szpg.plc;

import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.szpg.db.dao.PgAcuDao;
import com.szpg.db.dao.impl.PgAcuDaoImpl;
import com.szpg.db.data.PgAcu;
import com.szpg.plc.server.ACUClient;
import com.szpg.plc.server.ACUClientUtil;
import com.szpg.task.ACUSocketCheckTask;
import com.szpg.task.ReadAssetInSpectionTask;
import com.szpg.task.ReadCH4ValueTask;
import com.szpg.task.ReadCOValueTask;
import com.szpg.task.ReadDSStatusTask;
import com.szpg.task.ReadFjStatTask;
import com.szpg.task.ReadHSValueTask;
import com.szpg.task.ReadJgStatusTask;
import com.szpg.task.ReadMaintanceTask;
import com.szpg.task.ReadO2ValueTask;
import com.szpg.task.ReadSbStatTask;
import com.szpg.task.ReadWSValueTask;
import com.szpg.task.ReadWSYQValueTask;
import com.szpg.task.ReadYWStatusTask;
import com.szpg.task.ReadZmStatTask;
import com.szpg.task.SetCH4ThresholdTask;
import com.szpg.task.SetCOThresholdTask;
import com.szpg.task.SetHSThresholdTask;
import com.szpg.task.SetO2ThresholdTask;
import com.szpg.task.SetWSThresholdTask;
import com.szpg.util.Configure;

public class PGDSCServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4422075957571639803L;
	
	@Override
	public void init() throws ServletException {
		// 1获取数据库中所有ACU
		PgAcuDao acuDao = new PgAcuDaoImpl();
		List<PgAcu> acuList = acuDao.findAllACU();
		
		// 2遍历ACU列表，获取其IP地址与端口号
		for (PgAcu acu : acuList) {
			ACUClient client = new ACUClient(acu.getAcu_host(), Integer.parseInt(acu.getAcu_port()));
			
			client.setAcucode(acu.getAcu_code());
			client.setNet(acu.getAcu_net());
			client.setNode(acu.getAcu_node());
			client.setUnit(acu.getAcu_unit());
			client.setFlag(acu.getAcu_flag());
			
			// 3将ACU的信息加入到map中
			ACUClientUtil.getInstance().addClient(client);
			
			// 4新建线程启动client
			new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(new ACUSocketCheckTask(client), 0, 30, TimeUnit.SECONDS);
		}
		
		// 3启动查询温湿度的定时任务
		sendQueryWSValueCommand();
		sendSetWSThresholdCommand();
		
		// 4启动查询甲烷的定时任务
		sendQueryCH4ValueCommand();
		sendSetCH4ThresholdCommand();
		
		// 5启动查询一氧化碳的定时任务
		sendQueryCOValueCommand();
		sendSetCOThresholdCommand();
		
		// 6氧气
		sendQueryO2ValueCommand();
		sendSetO2ThresholdCommand();
		
		// 7硫化氢
		sendQueryHSValueCommand();
		sendSetHSThresholdCommand();
		
		// 8对射报警
		sendQueryDSStatusCommand();
		
		// 9液位报警
		sendQueryYWStatusCommand();
		
		// 10风机状态
		sendQueryFJStatusCommand();
		
		// 11照明状态
		sendQueryZMStatusCommand();
		
		// 12水泵状态
		sendQuerySBStatusCommand();
		
		// 13井盖状态
		sendQueryJGStatusCommand();
		
		//10巡检数据同步
		synchAssetInSpection();

		//运维数据同步
		synchMaintanceRecord();
		
		// 查询二期温湿度氧气实时数据
//		sendQueryWSYQValueCommand();
	}
	
	/**
	 * 发送查询温湿度监测值命令
	 * 
	 */
	private void sendQueryWSValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_WS_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadWSValueTask(), 20, interval * 60, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询氧气监测值命令
	 *
	 */
	private void sendQueryO2ValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_O2_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadO2ValueTask(), 40, interval * 60, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询硫化氢监测值命令
	 *
	 */
	private void sendQueryHSValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_HS_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadHSValueTask(), 60, interval * 60, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询甲烷监测值命令
	 *
	 */
	private void sendQueryCH4ValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_CH4_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadCH4ValueTask(), 15, interval * 60, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询一氧化碳监测值命令
	 *
	 */
	private void sendQueryCOValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_CO_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadCOValueTask(), 100, interval * 60, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询温湿度报警状态命令
	 *
	 */
	private void sendSetWSThresholdCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new SetWSThresholdTask(), 15, 1440, TimeUnit.MINUTES); // 每日重置所有设备的温湿度阈值
	}
	
	/**
	 * 发送查询甲烷报警状态命令
	 *
	 */
	private void sendSetCH4ThresholdCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new SetCH4ThresholdTask(), 25, 1440, TimeUnit.MINUTES); // 每日重置所有设备的甲烷浓度阈值
	}
	
	/**
	 * 发送查询一氧化碳报警状态命令
	 *
	 */
	private void sendSetCOThresholdCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new SetCOThresholdTask(), 35, 1440, TimeUnit.MINUTES); // 每日重置所有设备的一氧化碳浓度阈值
	}
	
	/**
	 * 发送查询一氧化碳报警状态命令
	 *
	 */
	private void sendSetO2ThresholdCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new SetO2ThresholdTask(), 45, 1440, TimeUnit.MINUTES); // 每日重置所有设备的氧气浓度阈值
	}
	
	/**
	 * 发送查询硫化氢报警状态命令
	 *
	 */
	private void sendSetHSThresholdCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new SetHSThresholdTask(), 55, 1440, TimeUnit.MINUTES); // 每日重置所有设备的硫化氢浓度阈值
	}
	
	/**
	 * 发送查询对射报警状态命令
	 *
	 */
	private void sendQueryDSStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadDSStatusTask(), 120, 600, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询爆管液位报警状态命令
	 *
	 */
	private void sendQueryYWStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadYWStatusTask(), 130, 600, TimeUnit.SECONDS);
	}
	
	
	/**
	 * 发送查询风机状态命令
	 *
	 */
	private void sendQueryFJStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadFjStatTask(), 140, 600, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询照明状态命令
	 *
	 */
	private void sendQueryZMStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadZmStatTask(), 150, 600, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询水泵状态命令
	 *
	 */
	private void sendQuerySBStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadSbStatTask(), 160, 600, TimeUnit.SECONDS);
	}
	
	/**
	 * 发送查询井盖状态命令
	 *
	 */
	private void sendQueryJGStatusCommand() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadJgStatusTask(), 170, 600, TimeUnit.SECONDS);
	}

	/**
	 * 调用巡检接口同步数据
	 *
	 */
	private void synchAssetInSpection() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadAssetInSpectionTask(), 60, 60*60*24, TimeUnit.SECONDS);
	}

	/**
	 * 调用维护接口同步数据
	 *
	 */
	private void synchMaintanceRecord() {
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadMaintanceTask(), 60, 600, TimeUnit.SECONDS);
	}
	
	/**
	 * 查询二期温湿度及氧气实时值定时任务
	 *
	 */
	private void sendQueryWSYQValueCommand() {
		int interval = Integer.parseInt(Configure.getProperty("sys", "READ_WSYQ_VALUE_INTERVAL", "60"));
		ScheduledExecutorService sche = new ScheduledThreadPoolExecutor(1);
		sche.scheduleWithFixedDelay(new ReadWSYQValueTask(), 10, interval * 60, TimeUnit.SECONDS);
	}

}