package com.siwei.recyclebox.deviceUtils;

import android.hardware.usb.UsbManager;
import android.util.Log;

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

import java.io.IOException;

public class ManyDevices extends BaseDeviceEntity{

    private static final String TAG = "ManyDevices";

    private Object lock = new Object();
    public ManyDevices(UsbManager usbManager, UsbSerialDriver driver) {
        super(usbManager , driver);
    }

    /**
     * 控制推杆
     */
    public synchronized void controlPushOpen(){
        try {
            Log.i(TAG,"openpush");
            //伸杆 66 后面 01代表杆 01伸 05代表时间1-60s  02 1C表示电流报警阈值 默认540
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x01, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);
//            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x01, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);//
//            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x02, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);//
//            Thread.sleep(100);

            for (int i=1;i>0;i++)
            {
//                Thread.sleep(50);
                //发送红外线和门磁
                port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A,0x01,0x55,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
                byte[] response = new byte[12];
                int len = port.read(response,10);
                Log.i(TAG,ByteUtil.bytesToString(response)+"len hongwaix:"+len);
                if (ByteUtil.bytesToString(response)=="AA BB 0A 01 55 03 01 FF FF FF CC DD "){//红外线有障碍 03红外线  01 有障碍
                    //    AA,BB,0A,01,55,03,01,FF,FF,FF,CC,DD 有障碍
//                    controlPushOff();
                    //杆停止
                    Log.i(TAG,"伸杆停止:1秒");
                    port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x03, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);
                    Thread.sleep(1000);
            }
//                else if(ByteUtil.bytesToString(response)=="AA BB 0A 01 55 03 02 FF FF FF CC DD"){//红外线  02无障碍
//                    //缩杆
//                    port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x01, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);
//                }
            }
//            byte[] response = new byte[12];
//            port.read(response,200);
//            Log.i(TAG,ByteUtil.bytesToString(response)+"  //response");
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    public synchronized void controlPushOff(){
        try {
            Log.i(TAG,"offpush");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01,0x02,0x05,0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);//
            for (int i=1;i>0;i++)
            {
                //发送红外线和门磁
                port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A,0x01,0x55,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
                byte[] response = new byte[12];
                int len = port.read(response,10);
                Log.i(TAG,ByteUtil.bytesToString(response)+"len hongwaix:"+len);
                if (ByteUtil.bytesToString(response)=="AA BB 0A 01 55 03 01 FF FF FF CC DD "){//红外线有障碍 03红外线  01 有障碍
                    //杆停止
                    Log.i(TAG,"伸杆停止:1秒");
                    port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A,0x01,0x66,0x01, 0x03, 0x05, 0x02,0x1C, (byte) 0xCC, (byte) 0xDD},150);
                    Thread.sleep(1000);
                }
            }

        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    public synchronized void controlLockOpen(){//开锁
        try {
            //电控锁   开锁
            Log.i(TAG,"openlock");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A,0x01,0x66,0x02,0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//
            Thread.sleep(100);
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    public synchronized void controlLockOff(){
        try {
            //电控锁   关锁
            Log.i(TAG,"offlock");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0A,0x01,0x66,0x02,0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//
            Thread.sleep(100);
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    //红外线和磁传感
//    port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C,0x01,0x55,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
//    byte[] response = new byte[12];
    //门磁关
//    AA BB 0C 01 55 04 01 FF FF FF CC DD
    //门磁开
//    A,BB,0C,01,55,04,02,FF,FF,FF,CC,DD
//

    //去皮清零
//    port.write(new byte[]{0xAA,0xBB,0x0B,0x01,0x66,0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
    //称重
//    port.write(new byte[]{0xAA,0xBB,0x0B,0x01,0x55,0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
//    AA,BB,0B,01,55,00,00,C9,FF,FF,CC,DD;//00 00 C9 是称重数据 0x0000C9=201  10进制

    //距离查询
//     port.write(new byte[]{0xAA,0xBB,0x0C,0x01,0x55,0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
//     AA,BB,0B,01,55,01,00,DB,FF,FF,CC,DD;//距离返回参数 01 是距离 00DB 是十进制219 mm单位

    //温度查询
//    port.write(new byte[]{0xAA,0xBB,0x0C,0x01,0x55,0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
//    AA,BB,0B,01,55,02,0A,FF,FF,FF,CC,DD;//02是温度  0A是十进制 10
//

    //温度查询
//    port.write(new byte[]{0xAA,0xBB,0x0D,0x01,0x55,0xFF,0xFF,0xFF,0xFF,0xFF,0xCC,0xDD},150);
//      AA BB 0D 01 55 01 05 FF FF FF CC DD    //01 05是数据
//返回的温度数据长度为两字节，高位在前低位在后，将这两字节转换成10进制数再除以10即为当前温度值；当最高位为1时表示负值，此时需将此值取补加1，也可将此值直接减去65536，即为当前温度值，下面举例说明：
//返回： AA BB 0E 01 55 00 DB FF FF FF CC DD
//00DB即为温度值，最高位为0，所以温度为正，将其转换为10进制=219，在将其除以10：21.9 即为当前温度值；
//返回：AA BB 0E 01 55 FF 90 FF FF FF CC DD
//FF 90为温度值，最高位为1，所以温度为负，将其转换为10进制=65424，再减去65536=  -11.2 即为当前温度值。

    public synchronized void controlDoorMag(){//门磁
        try {
            Log.i(TAG,"doormag");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C,0x01,0x66,0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//
            Thread.sleep(100);
            byte[] response = new byte[12];
            port.read(response,10);
            Log.i(TAG,ByteUtil.bytesToString(response)+"doormag:");
            //门磁关
//    AA,BB,0C,01,55,04,01,FF,FF,FF,CC,DD
            //门磁开
//    A,BB,0C,01,55,04,02,FF,FF,FF,CC,DD
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }



    public synchronized void controlWeightClear(){
        try {
            //去皮清零
            Log.i(TAG,"clearweight");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B,0x01,0x66,0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
            Thread.sleep(100);
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    public synchronized void controlWeightOpen(){
        try {
            //称重
            Log.i(TAG,"openweight");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B,0x01,0x55, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
            Thread.sleep(100);
            //    AA BB 0B 01 55 00 00 C9 FF FF CC DD;//00 00 C9 是称重数据 0x0000C9=201  10进制
            byte[] response = new byte[12];
            int len = port.read(response,10);
            Log.i(TAG,ByteUtil.bytesToString(response)+"称重:"+response[5]+response[6]+response[7]);

        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }
    public synchronized void distanceQuuery(){
        try {
            //距离查询
            Log.i(TAG,"querydistance");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C,0x01,0x55,0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
            Thread.sleep(100);
            byte[] response = new byte[12];
            port.read(response,10);
            Log.i(TAG,ByteUtil.bytesToString(response)+"querydistance:");
//     AA,BB,0B,01,55,01,00,DB,FF,FF,CC,DD;//距离返回参数 01 是距离 00DB 是十进制219 mm单位
            
            //AA BB 0C 01 66 02 18 FF FF FF CC DD   返回
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }

    public synchronized void temperatureQuery(){
        try {
            //温度查询
            Log.i(TAG,"querytemperature");
            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C,0x01,0x55,0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);
            Thread.sleep(100);
            byte[] response = new byte[12];
            port.read(response,10);
            Log.i(TAG,ByteUtil.bytesToString(response)+"querytemperature:");
            //    AA,BB,0B,01,55,02,0A,FF,FF,FF,CC,DD;//02是温度  0A是十进制 10
        }catch ( Exception e ){
            e.printStackTrace();
            Log.e(TAG,"",e);
        }
    }




    @Override
    public void connect(UsbManager usbManager) throws IOException {
        super.connect(usbManager);
        System.out.println("connect  readWeight:");

    }

    @Override
    void onDataReceived(byte[] response) {
        byte[] weightBytes = new byte[2];
//        weightBytes[0] = response[3];
//        weightBytes[1] = response[4];
//        latestWeight = ByteUtil.byte2short(weightBytes);
    }

    java.util.Observer observer = new java.util.Observer() {

        @Override
        public void update(java.util.Observable observable, Object arg) {

        }
    };


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

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

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

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

    @Override
    int getVendorId() {
        return 6790;
    }

}
