<commit_before>package Controller;

import Model.*;
import com.fazecast.jSerialComm.SerialPort;

import java.io.IOException;

public class ConnectionToSerialDevice implements Runnable, AbstractCrudDao.DataClearedListener{
    private String selectedSerialPortName;
    private DataDaoImpl dataDao = DataDaoImpl.getInstance();
    private static volatile int threadCounter = 0;
    private boolean stopThreadWhenImport = false;

    ConnectionToSerialDevice(String selectedSerialPortName){
        this.selectedSerialPortName = selectedSerialPortName;
    }

    @Override
    public void run() {
        System.out.println("Threads: "+ threadCounter);
        threadCounter++;
        System.out.println("Threads: "+ threadCounter);
        SerialPort[] serialPorts = SerialPort.getCommPorts();
        for(SerialPort serialPort : serialPorts){
            String serialPortName = serialPort.getDescriptivePortName();
            if(serialPortName.equals(selectedSerialPortName)){
                dataDao.clearPatients();
                waitUntilThisThreadIsOnlyThread();
                initializeSerialPort(serialPort);
                collectIncomingData(serialPort);
                serialPort.closePort();
                System.out.println("Close thread");
                break;
            }
        }
        threadCounter--;
        System.out.println("Threads: "+ threadCounter);
    }

    private void waitUntilThisThreadIsOnlyThread(){
        try {
            while (threadCounter != 1) {
                Thread.sleep(200);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    private void initializeSerialPort(SerialPort serialPort){
        serialPort.openPort();
        //serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);
        DataDaoImpl.getInstance().addDataClearedListener(this);
    }

    private void collectIncomingData(SerialPort serialPort){
        while (threadCounter == 1 && !stopThreadWhenImport) {
            String string = "";
            try {
                string = processIncomingDataToStrings(serialPort);
                if(threadCounter == 1 && !stopThreadWhenImport){
                    dataDao.addNewPatientHeartRateData(string);
                    System.out.println("String: " + string);
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    private String processIncomingDataToStrings(SerialPort serialPort) throws IOException{
        //TODO timeout implementeren
        System.out.println("incomingDataProcessing");
        String string = "";
        char c = (char) serialPort.getInputStream().read();
        while (c != '!') {
            string = string + (c);
            c = (char) serialPort.getInputStream().read();
        }
        System.out.println(string);
        return string;
    }

    @Override
    public void stopConnectionThread() {
        stopThreadWhenImport = true;
    }
}<commit_msg>Added timeout for connected device<commit_after>package Controller;

import Model.*;
import com.fazecast.jSerialComm.SerialPort;

import java.io.IOException;
import java.util.concurrent.*;

public class ConnectionToSerialDevice implements Runnable, AbstractCrudDao.DataClearedListener{
    private String selectedSerialPortName;
    private DataDaoImpl dataDao = DataDaoImpl.getInstance();
    private static volatile int threadCounter = 0;
    private boolean stopThreadWhenImport = false;
    private SerialPort connectedSerialPort;

    ConnectionToSerialDevice(String selectedSerialPortName){
        this.selectedSerialPortName = selectedSerialPortName;
    }

    @Override
    public void run() {
        System.out.println("Threads: "+ threadCounter);
        threadCounter++;
        System.out.println("Threads: "+ threadCounter);
        SerialPort[] serialPorts = SerialPort.getCommPorts();
        for(SerialPort serialPort : serialPorts){
            String serialPortName = serialPort.getDescriptivePortName();
            if(serialPortName.equals(selectedSerialPortName)){
                dataDao.clearPatients();
                waitUntilThisThreadIsOnlyThread();
                initializeSerialPort(serialPort);
                collectIncomingData();
                serialPort.closePort();
                System.out.println("Close thread");
                break;
            }
        }
        threadCounter--;
        System.out.println("Threads: "+ threadCounter);
    }

    private void waitUntilThisThreadIsOnlyThread(){
        try {
            while (threadCounter != 1) {
                Thread.sleep(200);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    private void initializeSerialPort(SerialPort serialPort){
        serialPort.openPort();
        //serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);
        DataDaoImpl.getInstance().addDataClearedListener(this);
        connectedSerialPort = serialPort;
    }

    private void collectIncomingData(){
        final Runnable stuffToDo = new Thread(){
            @Override
            public void run(){
                addDataToPatient();
            }
        };
        while (threadCounter == 1 && !stopThreadWhenImport) {
            final ExecutorService executor = Executors.newSingleThreadExecutor();
            final Future future = executor.submit(stuffToDo);
            executor.shutdown();
            try {
                future.get(10, TimeUnit.SECONDS);
            }catch (Exception e){
                e.printStackTrace();
                break;
            }
        }
    }

    private void addDataToPatient(){
        String string = "";
        try {
            string = processIncomingDataToStrings();
            if(threadCounter == 1 && !stopThreadWhenImport){
                dataDao.addNewPatientHeartRateData(string);
                System.out.println("String: " + string);
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    private String processIncomingDataToStrings() throws IOException{
        System.out.println("incomingDataProcessing");
        String string = "";
        char c = (char) connectedSerialPort.getInputStream().read();
        while (c != '!') {
            string = string + (c);
            c = (char) connectedSerialPort.getInputStream().read();
        }
        System.out.println(string);
        return string;
    }

    @Override
    public void stopConnectionThread() {
        stopThreadWhenImport = true;
    }
}
{'fraction_non_alphanumeric': 0.09480030143180106, 'fraction_numerical': 0.003617181612660136, 'mean_word_length': 2.2465753424657535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '123923', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1612, 'n_words': 320}