package com.miya.weighing;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.hisense.optionalservice.aidl.OnDataReceiveListener;
import com.hisense.optionalservice.aidl.OptionalApi;
import com.hisense.optionalservice.aidl.UartScale;
import com.miya.weighing.utils.D;

/**
 * 海信称重实现子类
 *
 * @author pupu
 * @time 2018/10/19
 */
public class HisenseWeighing extends BaseWeighing {

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

    /**
     * 服务名称
     */
    final static String SERVICE_PACKAGE = "com.hisense.optionalapi";

    /**
     * 服务action
     */
    final static String SERVICE_ACTION = "com.hisense.optionalapi";

    /**
     * aidl接口
     */
    OptionalApi optionApi;

    /**
     * 重量操作者
     */
    UartScale uartScale;

    @Override
    public boolean init(Context context) {
        this.context = context;
        Intent intent = new Intent();
        intent.setPackage(SERVICE_PACKAGE);
        return this.context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public int getWeight() {
        try {
            uartScale.readWeight();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return curWeight;
    }

    @Override
    public void setWeighingCallback(WeighingCallback weighingCallback) {
        if (weighingCallback != null) {
            this.weighingCallback = weighingCallback;
        }
    }

    @Override
    public boolean resetZero() {
        try {
            uartScale.resetZero();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean setTare(int tare) {
        if (tare < 0) {
            return false;
        }
        this.tare = tare;
        return true;
    }

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

    @Override
    public boolean release() {
        if (context != null && serviceConnection != null) {
            this.context.unbindService(serviceConnection);
        }
        try {
            uartScale.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        context = null;
        serviceConnection = null;
        curWeight = 0;
        tare = 0;
        weighingCallback = null;
        return true;
    }

    /**
     * 服务连接器
     */
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            optionApi = OptionalApi.Stub.asInterface(service);
            if (optionApi != null) {
                try {
                    uartScale = optionApi.getUartScale();
                    if (uartScale != null) {
                        //初始化
                        uartScale.open("/dev/ttySAC3", 9600, 0);
                        //设置重量监听回调
                        try {
                            uartScale.readWeight();
                            uartScale.setOnDataReceiveListener(new OnDataReceiveListener.Stub() {
                                @Override
                                public void onDataReceive(float weight, int status) throws RemoteException {
                                    D.i("pupu", "weight==>" + weight + ";" + "status==>" + status);
                                    if (status > 0) {
                                        //读取正常
                                        curWeight = parseKg2g(weight + "");
                                        if (weighingCallback != null) {
                                            weighingCallback.returnWeight(curWeight, true);
                                        }
                                    } else if (status == -1) {
                                        //读取超时，失败。考虑是否串口接触不良或者秤故障
                                        curWeight = -1;
                                    } else if (status == -2) {
                                        //读取的重量为负值，需要重置零点
                                        curWeight = -2;
                                        uartScale.resetZero();
                                    }
                                }
                            });
                            D.i(TAG, "称重连接成功!");
                        } catch (Exception ex) {
                            //设置监听失败
                            ex.printStackTrace();
                        }
                    } else {
                        //连接失败
                        D.i(TAG, "防损称连接失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //连接失败
                    D.i(TAG, "防损称连接失败");
                }
            } else {
                //连接服务失败
                D.i(TAG, "防损称连接失败");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //连接断开
        }
    };
}
