package com.miya.weighing;

import android.content.Context;

import com.miya.weighing.utils.CpuUtils;
import com.miya.weighing.utils.D;

import java.util.Timer;
import java.util.TimerTask;

import cn.wintec.wtandroidjar.SCL;

/**
 * 中科英泰称重驱动
 *
 * @author pupu
 * @time 2018/10/19
 */
public class YingtaiWeighing extends BaseWeighing {

    final static String TAG = YingtaiWeighing.class.getSimpleName();

    /**
     * 指令操作标志
     */
    final static int OP_NONE = 0x000;

    /**
     * 重置零点
     */
    final static int OP_ZERO = 0x001;

    /**
     * 最后一次指令操作记录
     */
    int operate = OP_NONE;

    /**
     * 中科英泰称重
     */
    SCL scl;

    /**
     * 用于源源不断提供重量数据
     */
    Timer timer;

    /**
     * 重量数据数组
     */
    byte[] buf;

    @Override
    public boolean init(Context context) {
        if (CpuUtils.isArm()) {
            this.context = context;
            scl = new SCL("/dev/ttyS1");
            buf = new byte[20];
            return scl.isConnected();
        } else {
            return false;
        }
    }

    @Override
    public int getWeight() {
        if (scl != null) {
            scl.read_standard(buf);
        }
        return super.getWeight();
    }

    @Override
    public int getWeightWithOutTare() {
        return curWeight - tare;
    }

    @Override
    public boolean markWeight(int weight) {
        if (scl != null) {
            return scl.markWeight(parseg2kg(weight));
        }
        return false;
    }

    @Override
    public boolean setGravity(double gravity) {
        if (scl != null) {
            return scl.setGravity(gravity);
        }
        return false;
    }

    @Override
    public boolean setTare(int tare) {
        this.tare = tare;
        return true;
        //暂不用英泰提供的去皮方法，建议交给业务层维护皮重计算，可以调用getWeightWithoutTare获取
//        if(scl!=null){
//            return scl.setTare(parseg2kg(tare));
//        }
//        return false;
    }

    @Override
    public boolean resetZero() {
        if (scl != null) {
            return scl.resetZero();
        }
        return false;
    }

    @Override
    public void setWeighingCallback(WeighingCallback callback) {
        if (scl == null) {
            return;
        }
        this.weighingCallback = callback;
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //不断读取重量数据,每10ms一次
                    try {
                        int ret = scl.read_standard(buf);
                        if (ret == 0) {
                            //读取正常
                            D.i(TAG, "当前重量为(kg)：" + scl.ASCII2HexString(buf, 20).substring(1, 7));
                            curWeight = parseKg2g(scl.ASCII2HexString(buf, 20).substring(1, 7).trim());
                            //获取标志位
                            byte stateByte = buf[0];
                            //是否稳定标志
                            boolean isStable = Integer.toBinaryString((int) stateByte).substring(5, 6).equals("1");
                            //是否在零点
                            boolean isZero = Integer.toBinaryString((int) stateByte).substring(4, 5).equals("1");
                            if (isZero == false && curWeight < 0) {
                                //重置零点一次
                                resetZero();
                            }
                            //是否去皮
                            boolean isTared = Integer.toBinaryString((int) stateByte).substring(3, 4).equals("1");
                            D.i(TAG, "当前称状态：" + Integer.toBinaryString((int) stateByte) + "\n" + "去皮:" + isTared + ";零点:" + isZero + ";稳定：" + isStable);
                            if (weighingCallback != null) {
                                weighingCallback.returnWeight(curWeight, isStable);
                            }
                        } else if (ret == 1) {
                            //发送指令成功,暂不关心
                            D.i(TAG, "指令操作结果:" + scl.ASCII2HexString(buf, 20) + ";时间：" + System.currentTimeMillis());
                        } else if (ret == 2) {
                            //读取异常
                            curWeight = -2;
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }, 0, 10);
        }
    }

    @Override
    public boolean release() {
        //调用该方法时会抛native异常，不建议调用
//        if (scl != null) {
//            scl.SCL_close();
//        }
        scl = null;
        buf = null;
        if (timer != null) {
            timer.cancel();
        }
        return true;
    }
}
