package net.northking.cctp.upperComputer.thread;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.upperComputer.constants.UpperComputerConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: WuJinQuan
 * @date:2022-01-21 10:03
 **/
public class IosPerfDataRead{
    private final static Logger logger = LoggerFactory.getLogger(IosPerfDataRead.class);
    private boolean b=false;
    private BufferedReader reader;
    private String redisKey;
    private RabbitTemplate rabbitTemplate;
    private ObjectMapper objectMapper;
    public IosPerfDataRead(){
        this.rabbitTemplate = SpringUtil.getBean(RabbitTemplate.class);
        this.objectMapper = SpringUtil.getBean(ObjectMapper.class);
    }
    public void init(BufferedReader reader,String redisKey){
        b=true;
        this.reader=reader;
        this.redisKey=redisKey;
    }
    public void stopRead(){
        b=false;
    }

    public void run(){
        //启动线程，负责读标准输出流
        JSONObject jsonObject;
        Double d;
        String upFlow;
        String downFlow;
        HashMap<String, Object> dataMap = new HashMap<>();
        Map<String, Object> perfMap = new HashMap<>(8);
        dataMap.put("key", redisKey);
        dataMap.put("hashData", perfMap);
        dataMap.put("expireTime", 100L);
        try {
            String line1 ;
            while (b){
                while ((line1 = reader.readLine()) != null) {
                    logger.info(line1);
                    if (line1 != null ) {
                        if (line1.indexOf(" ") > 0) {
                            String key = line1.substring(0, line1.indexOf(" "));
                            if ("cpu".equals(key) || "memory".equals(key) || "fps".equals(key) || "network".equals(key)) {
                                String value = line1.replace(key + " ", "");
                                try {
                                    jsonObject = JSON.parseObject(value);
                                } catch (Exception e) {
                                    logger.error("Json转换失败",e);
                                    continue;
                                }
                                switch (key) {
                                    case "cpu":
                                        value = jsonObject.getString("value");
                                        if (value == null) {
                                            d = 0.0;
                                        } else {
                                            d = Double.valueOf(value);
                                        }
                                        value = String.format("%.2f", d);
                                        perfMap.put("cpu", value);
                                        break;
                                    case "memory":
                                        value = jsonObject.getString("value");
                                        if (value == null) {
                                            d = 0.0;
                                        } else {
                                            d = Double.valueOf(value);
                                        }
                                        value = String.format("%.2f", d);
                                        perfMap.put("memory", value);
                                        break;
                                    case "fps":
                                        value = jsonObject.getString("fps");
                                        if (value == null) {
                                            value = "0";
                                        }
                                        perfMap.put("fps", value);
                                        break;
                                    case "network":
                                        upFlow = jsonObject.getString("upFlow");
                                        if (upFlow == null) {
                                            d = 0.0;
                                        } else {
                                            d = Double.valueOf(upFlow);
                                        }
                                        upFlow = String.format("%.2f", d);
                                        perfMap.put("upFlow", upFlow);
                                        downFlow = jsonObject.getString("downFlow");
                                        if (downFlow == null) {
                                            d = 0.0;
                                        } else {
                                            d = Double.valueOf(downFlow);
                                        }
                                        downFlow = String.format("%.2f", d);
                                        perfMap.put("downFlow", downFlow);
                                        break;
                                    default: break;
                                }
                                Message message = new Message(objectMapper.writeValueAsBytes(JSON.toJSONString(dataMap)), new MessageProperties());
                                rabbitTemplate.send(UpperComputerConstant.PER_DATA_MQ_EXCHANGE_NAME, "", message);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("获取性能数据异常",e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                logger.error("reader关闭失败，e",e);
            }
        }
    }
}
