package com.siwei.recyclebox.deviceUtils;

import android.hardware.usb.UsbManager;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.netty.util.concurrent.SingleThreadEventExecutor;

public class PushRodDevice extends BaseDeviceEntity {

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    public PushRodDevice(UsbManager usbManager, UsbSerialDriver driver) {
        super(usbManager, driver);
    }

    /**
     * 串口的连接参数：
     * baudRate: 波特率
     * dataBits: 数据位 一般为8
     * stopBits: 停止位 一般为1
     * parity: 奇偶校验 UsbSerialPort.PARITY_ODD、UsbSerialPort.PARITY_EVEN、UsbSerialPort.PARITY_NONE
     */
    @Override
    int getBaudRate() {
        return 115200;
    }

    @Override
    int getDataBits() {
        return UsbSerialPort.DATABITS_8;
    }

    @Override
    int getStopBits() {
        return UsbSerialPort.STOPBITS_1;
    }

    @Override
    int getParity() {
       return UsbSerialPort.PARITY_NONE;

    }

    @Override
    int getVendorId() {
        return 6790;
    }

    @Override
    void onDataReceived(byte[] response) {

    }

    /**
     * 推杆命令
     */
    public void pushRod(){
        executorService.submit(() -> {

            byte[] cleanOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A ,0x01 ,0x66 ,0x01 ,0x02 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
            System.out.println("推杆！！！！！");
            try {
                for(int i = 0;i<3;i++) {
                    port.write(cleanOrderBytes, 150);
                    Thread.sleep(10000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    /**
     * 收杆命令
     */
    public void pullRod(){
        executorService.submit(()->{
            byte[] cleanOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A ,0x01 ,0x66 ,0x01 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
            System.out.println("收杆！！！！！");
            try {
                for(int i = 0;i<3;i++) {
                    port.write(cleanOrderBytes, 150);
                    Thread.sleep(10000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }




}
