package com.jiayue.ipfcst.client.controller;

import com.alibaba.fastjson.JSONObject;
import com.syjy.container.ProtocolDataContainer;
import com.syjy.container.ProtocolTunnelContainer;
import com.syjy.tunnelinfo.TunnelStatus;
import com.syjy.tunnelinfo.TunnelType;
import com.syjy.tunnelworker.BaseProtocolTunnel;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * 协议的controller
 *
 * @author xiuwei
 * @date 2022/04/12
 */
@Controller
@RequestMapping("protocol")
public class ProtocolController {

	@RequestMapping("")
	public String page() {
		return "protocol";
	}

	@ResponseBody
	@GetMapping("getRealTimeData")
	public JSONObject getRealTimeData(){
		JSONObject jsonObject=new JSONObject();
		Map<String,String> tunnels=new HashMap<>();
		for(Map.Entry<String, BaseProtocolTunnel>  e: ProtocolTunnelContainer.getInstance().getAllTunnel().entrySet()){
			if(TunnelType.GENERATEFILE==e.getValue().getTunnelType()){
				if(TunnelStatus.CONNECTEDANDCOMMERROR==e.getValue().getTunnelStatus()){
					tunnels.put(e.getValue().getTunnelInfo().getTunnelName(),"生成文件滞留");
				}else{
					tunnels.put(e.getValue().getTunnelInfo().getTunnelName(),"传输文件正常");
				}
			}else{
				tunnels.put(e.getValue().getTunnelInfo().getTunnelName(),e.getValue().getTunnelStatus().getCnDescribe());
			}
		}
		jsonObject.put("tunnel", tunnels);
		TreeMap<Integer,Object> dataValue=new TreeMap();
		dataValue.putAll(ProtocolDataContainer.getInstance().getAllBooleans());
		dataValue.putAll(ProtocolDataContainer.getInstance().getAllNumbers());
		jsonObject.put("dataValue",dataValue);
		jsonObject.put("time", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
		return jsonObject;
	}


}
