package com.unibuy.smartdevice.controller;

import com.unibuy.smartdevice.devices.ComPort;
import com.unibuy.smartdevice.devices.Rs232Dev;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsIOException;
import com.unibuy.smartdevice.exception.LogsUnsupportedOperationException;
import com.unibuy.smartdevice.tools.HandlerMain;

public class UseAllComPortSendController extends DevController {
    private Logs logs;

    public UseAllComPortSendController(HandlerMain handlerMain) {
        super( 20, handlerMain);
        this.logs = new Logs(getClass());
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return getSrcHandlerMain();
    }

    @Override
    protected void execute(HandlerMain handlerMain) {
        try {
            Rs232Dev rs232Dev = new Rs232Dev();
            for (ComPort comPort : ComPort.values()) {
                logs.info("connect:" + comPort.getFilepath());
                rs232Dev.connectPort(comPort);
                for(int index=0; index<getCountBySendBuffer(); index++) {
                    rs232Dev.send(getSendBuffer(index));
                    Thread.sleep(100);
                }
                rs232Dev.closePort();
                Thread.sleep(1000);
            }
        } catch (LogsUnsupportedOperationException | LogsIOException e) {
//            getLogs().warning(e);
            getLogs().warning(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }
}
