package com.develop.foodcooking;

import android.serialport.SerialPort;
import android.util.Log;

import com.develop.food.base.utils.ThreadUtils;
import com.kuyuntech.cofarcooking.device.sdk.eventbus.listener.Dev2McuEventListener;
import com.kuyuntech.cofarcooking.device.sdk.eventbus.listener.Mcu2DevEventListener;
import com.kuyuntech.cofarcooking.device.sdk.protocol.core.BaseDecoder;
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK;
import com.kuyuntech.cofarcooking.device.sdk.util.core.Converter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SerialportUtils {


    public static void init(){


        String serialPortPath = "/dev/ttyS1";
        int baudrate = 9600;
        int dataBits = 8;
        int parity = 0;
        int stopBits = 1;
        int flags = 0 ;



        try {

            SerialPort.setSuPath("/system/xbin/su");

            SerialPort serialPort  = new SerialPort(new File(serialPortPath),baudrate,dataBits,parity,stopBits,flags);

            Dev2McuEventListener dev2McuEventListener = new Dev2McuEventListener(event -> {
                Log.i("dev2mcu:",event.getData());
                byte[] data = Converter.hexStringToBytes(event.getData());
                try {
                    serialPort.getOutputStream().write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            CofarSDK.register(dev2McuEventListener);

            CofarSDK.register(new Mcu2DevEventListener());

            ProtocolListener protocolListener = new ProtocolListener(serialPort);
            new Thread(protocolListener).start();


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

        }
    }

}
