<commit_before>package cz.iot.local;

import cz.iot.main.Hub;
import cz.iot.remote.HubClient;
import cz.iot.utils.DataManager;
import jssc.*;

import java.util.Map;

/**
 * Created by Michal on 13. 7. 2015.
 */
public class SerialDataCollector implements DataCollector {

    private SerialPort serialPort;
    private boolean isRunning;
    private DataManager manager;
    private StringBuilder sb = new StringBuilder();

    public SerialDataCollector(DataManager manager) {
        this.manager = manager;
        isRunning = true;
    }

    public void close() {
        isRunning = false;
    }

    public void run() {
        initSerialPort();
        while (isRunning) {
            collectData();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

    public void collectData() {
        try {
            writeLine("AT+REQUEST?");
            System.out.println(readLine());
        } catch (SerialPortException e) {
            e.printStackTrace();
        }
    }

    public String[] getPorts() {
        return SerialPortList.getPortNames();
    }

    public void initSerialPort() {
        try {
            //serialPort = new SerialPort(getPorts()[0]);
            serialPort = new SerialPort("COM4");
            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private void writeLine(String message) throws SerialPortException {
        serialPort.writeString(message+"\r\n");
    }

    public String readLine(){
        StringBuilder sb = new StringBuilder();
        /*while(serialPort.getInputBufferBytesCount()>0){

        }*/
        return null;
    }

}<commit_msg>Serial data collector works now<commit_after>package cz.iot.local;

import cz.iot.utils.DataManager;
import jssc.*;

import java.util.ArrayList;

/**
 * Created by Michal on 13. 7. 2015.
 */
public class SerialDataCollector implements DataCollector {

    private SerialPort serialPort;
    private boolean isRunning;
    private DataManager manager;
    private StringBuilder sb = new StringBuilder();

    public SerialDataCollector(DataManager manager) {
        this.manager = manager;
        isRunning = true;
        initSerialPort();
    }

    public void close() {
        isRunning = false;
    }

    public void run() {

        while (isRunning) {
            collectData();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

    public void collectData() {
        try {
            writeLine("AT+REQUEST?");
            String data = readLine();
            if (data.startsWith("AT+REQUEST START")) {
                ArrayList<Packet> packets = new ArrayList<>();
                int count = Integer.parseInt(data.split(",")[1]);
                for (int i = 0; i < count; i++) {
                    data = readLine();
                    packets.add(new Packet(data.substring(0, 8), data.substring(8)));
                }
                data = readLine();
                if (!data.equalsIgnoreCase("AT+REQUEST END")) {
                    throw new Exception("serial line error - bad end");
                }
                if (packets.size() > 0) {
                    for (Packet p : packets) {
                        System.out.println(p);
                        manager.put(p);
                    }
                }
            } else {
                throw new Exception("serial line error - bad start");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String[] getPorts() {
        return SerialPortList.getPortNames();
    }

    public void initSerialPort() {
        try {
            //serialPort = new SerialPort(getPorts()[0]);
            serialPort = new SerialPort("COM4");
            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);
            serialPort.addEventListener(new SerialPortEventListener() {
                @Override
                public void serialEvent(SerialPortEvent serialPortEvent) {
                    if (serialPortEvent.getEventType() == 1) {
                        try {
                            synchronized (serialPort) {
                                sb.append(serialPort.readString());
                            }
                        } catch (SerialPortException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });

            while (!sb.toString().endsWith("AT+UNKNOWN\r\n")) {
                writeLine("ahoj");
                Thread.sleep(80);
            }
            synchronized (serialPort) {
                sb = new StringBuilder();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private void writeLine(String message) throws SerialPortException {
        serialPort.writeString(message + "\r\n");
    }

    public String readLine() {
        while (sb.indexOf("\r\n") == -1) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String data = "";
        synchronized (serialPort) {
            data = sb.substring(0, sb.indexOf("\r\n"));
            sb = new StringBuilder(sb.substring(sb.indexOf("\r\n") + 2));
        }
        return data;
    }

}
{'fraction_non_alphanumeric': 0.10952380952380952, 'fraction_numerical': 0.009523809523809525, 'mean_word_length': 1.4927472527472527, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4148009', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1410, 'n_words': 327}