/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yfd.platform.component.nettyclient;

import com.yfd.platform.component.WebSocketServer;
import com.yfd.platform.config.HttpServerConfig;
import com.yfd.platform.config.ParentConfig;
import com.yfd.platform.constant.SystemCode;
import com.yfd.platform.utils.SpringContextHolder;
import io.netty.util.CharsetUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @date 2023-05-23
 * 动态执行巡视主机心跳
 */
@Slf4j
public class HeartBeatJob implements Runnable {

    //  使用定时器发送心跳
    @Resource
    private HttpServerConfig httpServerConfig;
    private ParentConfig parentConfig;

    public HeartBeatJob(ParentConfig parentConfig) {
        this.parentConfig = parentConfig;
        this.httpServerConfig = SpringContextHolder.getBean(HttpServerConfig.class);
    }

    @SneakyThrows
    @Override
    public void run() {
        String xml = MyXmlUtil.getXml(httpServerConfig.getPatrolServerid(), parentConfig.getParentId(), SystemCode.TYPE_SYSTEM_MESSAGE_CODE.getCode()
                , SystemCode.COMMAND_HEARTBEAT_CODE.getCode(), "", null);
        WebSocketServer.sendInfo("robotinfo", xml);
        MyMessageProtocol messageProtocol = new MyMessageProtocol();
        messageProtocol.setSenderSerialNo(BootSessionCache.getRequestSessionID());
        messageProtocol.setReceiverSerialNo(0L);
        messageProtocol.setSessionflag(0);
        messageProtocol.setXmllen(xml.getBytes(CharsetUtil.UTF_8).length);
        messageProtocol.setXmlcontent(xml.getBytes(CharsetUtil.UTF_8));
        if (BootNettyClientChannelCache.channelMapCache.size() > 0) {
            for (Map.Entry<String, BootNettyClientChannel> entry :
                    BootNettyClientChannelCache.channelMapCache.entrySet()) {
                BootNettyClientChannel bootNettyChannel = entry.getValue();
                if (bootNettyChannel != null && bootNettyChannel.getChannel().isOpen()) {
                    bootNettyChannel.getChannel().writeAndFlush(messageProtocol);
                }
                else {
                    BootNettyClientThread thread = new BootNettyClientThread(parentConfig.getParentIp(),
                            Integer.parseInt(parentConfig.getTcpPort()), parentConfig);
                    thread.start();
                }
            }
        }
        else {
            BootNettyClientThread thread = new BootNettyClientThread(parentConfig.getParentIp(),
                    Integer.parseInt(parentConfig.getTcpPort()), parentConfig);
            thread.start();
        }

    }
}
