package com.pqs9000.serviceimpl.data;

import cn.hutool.core.util.StrUtil;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.utils.AppConfig;
import com.njcn.utils.SocketClient;
import com.pqs9000.service.data.RealTimeDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * system
 * 实时数据
 * @author cdf
 * @date 2021/8/9
 */
@Service
public class RealTimeDataServiceImpl implements RealTimeDataService {
    @Resource
    private SocketClient socketClient;
    @Resource
    private LineMapper lineMapper;

    @Resource
    private AppConfig appConfig;

    @Override
    public String realTimeData(Integer lineIndex) {
        String host =null;
        host = lineMapper.getNodeIpbylineIndex(lineIndex.longValue());
        if(StrUtil.isBlank(host)){
            return "设备前置机服务器配置异常,请联系管理员";
        }else {
            cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject();
            jsonObject.put("LineId",lineIndex);
            jsonObject.put("type",0);
            cn.hutool.json.JSONObject jsonObject1 = new cn.hutool.json.JSONObject();
            jsonObject1.put("len",0);
            jsonObject1.put("data",jsonObject);
            System.out.println(jsonObject1.toString());
            socketClient.realTimeData(jsonObject1.toString(),host,Integer.parseInt(appConfig.getSocketPort()),lineIndex);
        }
        return "请求成功";
    }

    @Override
    public String heartRealTimeData(Integer lineIndex) {
        cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject();
        jsonObject.put("LineId",lineIndex);
        jsonObject.put("type",1);
        cn.hutool.json.JSONObject jsonObject1 = new cn.hutool.json.JSONObject();
        jsonObject1.put("len",0);
        jsonObject1.put("data",jsonObject);
        String host = lineMapper.getNodeIpbylineIndex(lineIndex.longValue());
        socketClient.heartRealData(jsonObject1.toString(),host,Integer.parseInt(appConfig.getSocketPort()),lineIndex);
        return "实时数据心跳请求成功";
    }

    @Override
    public String closeRealTimeData(Integer lineIndex) {

        socketClient.closeRealData(lineIndex);
        return "关闭实时数据请求成功";
    }
}
