package com.imdroid.beidou.test_device.task;

import com.imdroid.beidou.test_device.service.UDPClient;
import com.imdroid.common.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class BeidouDevice {
    private String host="127.0.0.1";

    private int port=9903;

    UDPClient udpClient;
    public void connectServer(){
        udpClient = new UDPClient();
        udpClient.init(host, port);
    }

    public void run(){
        try{
/*            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_1xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_2xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_3xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_4xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_5xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_6xx.log");

            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_1.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_2.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_1xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_2xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_3xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_4xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_5xx.log");
            execute("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_6xx.log");
*/

            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_1xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_2xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_3xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_4xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_5xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_6xx.log");

            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_1.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_2.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_1xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_2xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_3xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_4xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_5xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412254_0416_6xx.log");

            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_1.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_2.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_1xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_2xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_3xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_4xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2412254_0424_5xx.log");
          //execute("C:\\Users\\wd\\Desktop\\log\\b562_2412270_1.log");
            //executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412270_1xx.log");
            //executeAsD342("C:\\Users\\wd\\Desktop\\log\\b562_2412270_2xx.log");
/*            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_1.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_2.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_1xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_2xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_3xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_4xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2345076_0424_5xx.log");

            execute("C:\\Users\\wd\\Desktop\\log\\2353120_0424_1.log");

            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_1.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_2.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_1xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_2xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_3xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_4xx.log");
            executeAsD342("C:\\Users\\wd\\Desktop\\log\\2353120_0424_5xx.log");
*/        }
        catch (Exception e){

        }
        System.out.println("finish!");
    }

    public void execute(String dataFileName) throws IOException {
        FileReader fr=null;
        BufferedReader br=null;
        try {
            fr = new FileReader(dataFileName);
            br = new BufferedReader(fr);
            String line = "";
            String[] arrs = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

            long lastTime = 0;
            int b562Count = 0;
            int cycleCount = 0;

            while ((line = br.readLine()) != null) {
                arrs = line.split(" ");
                if(arrs.length != 3) continue;
                Date time = sdf.parse(arrs[0] + " " + arrs[1]);

                if (lastTime!=0 && Math.abs(time.getTime() - lastTime) > 60 * 1000) {//超过1分钟为一个周期
                    System.out.println(time+" cycle "+cycleCount+", b562 num "+b562Count);
                    Thread.sleep(30 * 1000);
                    cycleCount++;
                }
                udpClient.sendData(ByteUtil.hexStringTobyte(arrs[2]));
                b562Count++;
                lastTime = time.getTime();
                Thread.sleep(100);
            }
            br.close();
            fr.close();
        }
        catch (Exception e){
            e.printStackTrace();
            if(br!=null) br.close();
            if(fr!=null) fr.close();
        }
    }

    public void executeAsD342(String dataFileName) throws IOException {
        // d3 51 length device_id(4bytes) seq(2bytes) year mon day hour minute
        FileReader fr=null;
        BufferedReader br=null;
        byte[] d342Head = new byte[]{(byte) 0xd3,(byte) 0x42};
        ByteBuf dataBuffer = Unpooled.buffer();
        ByteBuf sendBuffer = Unpooled.buffer();
        try {
            fr = new FileReader(dataFileName);
            br = new BufferedReader(fr);
            String line = "";
            String[] arrs = null;

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

            LocalDateTime time;
            LocalDateTime lastTime=null;
            int b562Count = 0;

            while ((line = br.readLine()) != null) {
                b562Count++;
                arrs = line.split(" ");
                if(arrs.length != 3) continue;
                time = LocalDateTime.parse(arrs[0] + " " + arrs[1],formatter);

                if((b562Count%10)==0 ||
                        (lastTime!=null && time.isAfter(lastTime.plusMinutes(1)))) {
                    sendBuffer.writeBytes(d342Head);
                    sendBuffer.writeShort(dataBuffer.readableBytes()+11);
                    String deviceId = arrs[2].substring(4*2,8*2);
                    sendBuffer.writeBytes(ByteUtil.hexStringTobyte(deviceId));
                    sendBuffer.writeShort(b562Count);
                    byte[] byteTime = new byte[]{(byte) (time.getYear() - 2000), (byte) time.getMonthValue(), (byte) time.getDayOfMonth(), (byte) time.getHour(), (byte) time.getMinute()};
                    sendBuffer.writeBytes(byteTime);
                    sendBuffer.writeBytes(dataBuffer);
                    byte[] data = new byte[sendBuffer.readableBytes()];
                    sendBuffer.getBytes(0,data);
                    udpClient.sendData(data);
                    Thread.sleep(300);
                    sendBuffer.clear();
                    dataBuffer.clear();
                }
                if ((b562Count%100)==0) {
                    System.out.println(time+" b562 num "+b562Count);
                }

                dataBuffer.writeBytes(ByteUtil.hexStringTobyte(arrs[2]));
                lastTime = time;
            }
            br.close();
            fr.close();
        }
        catch (Exception e){
            e.printStackTrace();
            if(br!=null) br.close();
            if(fr!=null) fr.close();
        }
    }
}
