package com.shining.cloud.serviceImpl.RealTimeInfo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.njcn.mapper.configuration.DeviceMapper;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.pojo.configuration.Device;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.terminal.ContUpdateDevValueVO;
import com.njcn.pojo.terminal.ContUpdateValueVO;
import com.njcn.pojo.terminal.ContValueVO;
import com.njcn.pojo.terminal.UpDevVO;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.AppConfig;
import com.njcn.utils.SocketClient;
import com.shining.cloud.mapper.RealTimeInfo.RealTimeInfoMapper;
import com.shining.cloud.pojo.RealTimeInfo.RealTimeRVO;
import com.shining.cloud.pojo.RealTimeInfo.RealTimeVO;
import com.shining.cloud.service.RealTimeInfo.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

import static java.util.Objects.isNull;

/**
 * @Description 历史数据
 * @Author cdf
 * @Date 2020/8/6 13:35
 **/
@Service
public class VersionServiceImpl implements VersionService {
    //@Autowired
    //private VersionMapper versionMapper;

    @Autowired
    private SocketClient socketClient;
    @Autowired
    private AppConfig appConfig;

    @Autowired
    private LineMapper lineMapper;

    @Resource
    private RealTimeInfoMapper realTimeInfoMapper;

    @Override
    public String sentLine(int index) {
        String jsonStr = "";
        try {
            String host = lineMapper.getNodeIpbylineIndex(Long.valueOf(index));
            Integer port = Integer.valueOf(appConfig.getSocketPort());
            RealTimeRVO realTimeRVO = new RealTimeRVO();
            String devType = realTimeInfoMapper.devType(index);
            if (devType.indexOf("V_300") != -1) {
                realTimeRVO.setType(211);
            } else {
                realTimeRVO.setType(210);
            }
            realTimeRVO.setLineid(index);
            JSONObject jsonstr = new JSONObject(realTimeRVO);
            Integer len = jsonstr.toString().length();  
            RealTimeVO realTimeVO = new RealTimeVO();
            realTimeVO.setLen(len.toString());
            realTimeVO.setData(realTimeRVO);
            JSONObject jsonObject = new JSONObject(realTimeVO);
            String str = jsonObject.toString();
            jsonStr = socketClient.sentLineRealTime(str, host, port);
        } catch (Exception e) {
            jsonStr = "";
        }
        return jsonStr;
    }

    @Override
    public String getInfoBySocket(int index) {
        String jsonStr = "";
        try {
            String host = lineMapper.getNodeIpbylineIndex(Long.valueOf(index));
            Integer port = Integer.valueOf(appConfig.getSocketPort());
            RealTimeRVO realTimeRVO = new RealTimeRVO();
            String devType = realTimeInfoMapper.devType(index);
            if (devType.indexOf("V_300") != -1) {
                realTimeRVO.setType(211);
            } else {
                realTimeRVO.setType(210);
            }
            realTimeRVO.setLineid(index);
            JSONObject jsonstr = new JSONObject(realTimeRVO);
            Integer len = jsonstr.toString().length();
            RealTimeVO realTimeVO = new RealTimeVO();
            realTimeVO.setLen(len.toString());
            realTimeVO.setData(realTimeRVO);
            JSONObject jsonObject = new JSONObject(realTimeVO);
            String str = jsonObject.toString();
            jsonStr = socketClient.sentLineRealTimeTwo(str, host, port);
        } catch (Exception e) {
            jsonStr = "";
        }
        return jsonStr;
    }

}
