/*
 *  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.iec104.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.iec104.config.Iec104Config;
import com.yfd.platform.component.iec104.server.Iec104Master;
import com.yfd.platform.component.iec104.server.Iec104MasterFactory;
import com.yfd.platform.component.utils.OptimizedThreadPool;
import com.yfd.platform.modules.auxcontrol.domain.GatewayDevice;
import com.yfd.platform.modules.auxcontrol.mapper.GatewayDeviceMapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author
 * @date 2019-01-07
 */
@Component
@RequiredArgsConstructor
public class IEC104ClientRunner implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(IEC104ClientRunner.class);
    private final GatewayDeviceMapper gatewayDeviceMapper;

    /**
     * 项目启动时激活启用的IEC104 与各子站的连接任务
     *
     * @param applicationArguments /
     */
    @Override
    @Async
    public void run(ApplicationArguments applicationArguments) throws Exception {

        LambdaQueryWrapper<GatewayDevice> queryWrapper = new LambdaQueryWrapper<>();
        //没有停用
        queryWrapper.ne(GatewayDevice::getStatus, "00");
        queryWrapper.eq(GatewayDevice::getDeviceType, "30");
        queryWrapper.eq(GatewayDevice::getProtocol, "IEC104");
        queryWrapper.select(GatewayDevice::getId, GatewayDevice::getDeviceCode, GatewayDevice::getDeviceName,
                GatewayDevice::getIpAddr, GatewayDevice::getIecAddr);
        queryWrapper.orderByAsc(GatewayDevice::getDeviceCode);
        List<GatewayDevice> list = gatewayDeviceMapper.selectList(queryWrapper);
//        if (list.size() > 0) {
//            for (GatewayDevice gatewayDevice : list) {
//                Iec104Config iec104Config = new Iec104Config();
//                iec104Config.setFrameAmountMax((short) 10);
//                short terminnalAddress = gatewayDevice.getIecAddr().shortValue();
//                //通讯网关机地址
//                iec104Config.setTerminnalAddress(terminnalAddress);
//                iec104Config.setSlaveCode(gatewayDevice.getDeviceCode());
//                iec104Config.setSlaveIP(gatewayDevice.getIpAddr());
//                OptimizedThreadPool threadPool = OptimizedThreadPool.getInstance();
//                threadPool.execute(() -> {
//                    Iec104Master iec104server = Iec104MasterFactory.createTcpClientMaster(iec104Config.getSlaveIP(),
//                            2404);
//                    try {
//                        iec104server.setDataHandler(new MasterSysDataHandler()).setConfig(iec104Config).run();
//
//                    } catch (Exception e) {
//                        log.error(String.format("%s-对应的终端连接失败！", iec104Config.getSlaveIP()));
//                    }
//                });
//                threadPool.shutdown();
//            }
//
//        }

    }
}
