package com.gyee.edge.gateway.config.read;
import java.nio.charset.Charset;
import java.util.ArrayList;
import com.csvreader.CsvReader;
import com.gyee.edge.gateway.config.point.PointMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ReadPointCSVData {
    public static ArrayList<PointMapper> readConfig(String filePath)  {
        //默认路径
        if (filePath.length()>0) {
            return readCsvByCsvReader(filePath);
        }
        else{
            return null;
        }
    }

    public static ArrayList<PointMapper> readCsvByCsvReader(String filePath) {
        ArrayList<PointMapper> strList = null;
        try {
            ArrayList<String[]> arrList = new ArrayList<String[]>();
            strList = new ArrayList<PointMapper>();
            CsvReader reader = new CsvReader(filePath, ',', Charset.forName("UTF-8"));
            while (reader.readRecord()) {
                arrList.add(reader.getValues());
            }
            reader.close();
            for (int row = 0; row < arrList.size(); row++) {
                PointMapper pointMapper = new PointMapper();
                pointMapper.setDataSource(arrList.get(row)[0]);
                pointMapper.setProtocolSource(arrList.get(row)[1]);
                pointMapper.setSourceAddress(Integer.valueOf(arrList.get(row)[2]));
                pointMapper.setRatio(Double.valueOf(arrList.get(row)[3]));
                pointMapper.setMessageKey(Integer.valueOf(arrList.get(row)[4]));
                pointMapper.setMessageId(arrList.get(row)[5]);
                strList.add(pointMapper);
            }
        } catch (Exception e) {
            log.error("读取csv文件发生错误",e);
        }
        return strList;
    }
}
