package io.wdd.agent.agent;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AgentOperationInfoService {

    @Resource
    AgentServerInfo agentServerInfo;

    @Resource
    OMessageToServerSender oMessageToServerSender;

    @Resource
    ObjectMapper objectMapper;

    public void AgentOpInfo(OctopusMessage octopusMessage){

        // 构造结果OM
        octopusMessage.setAc_time(TimeUtils.currentTime());

        // 需要此部分写为String, 否则无法解析
        String agentServerInfoString;
        try {
            agentServerInfoString = objectMapper.writeValueAsString(agentServerInfo);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        octopusMessage.setResult(agentServerInfoString);

        // 发送相应的OM至 OctopusToServer 中
        oMessageToServerSender.send(octopusMessage);

    }

    public void AgentOpVersion(OctopusMessage octopusMessage){

        // 收集版本信息
        String agentVersion = agentServerInfo.getAgentVersion();

        // 构造结果OM
        octopusMessage.setAc_time(TimeUtils.currentTime());
        octopusMessage.setResult(agentVersion);

        // 发送相应的OM至 OctopusToServer 中
        oMessageToServerSender.send(octopusMessage);

    }




}
