package chaoran.business.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import chaoran.business.activity.ResultListener;
import chaoran.business.strategy.Strategy;

/**
 * 23-6-13 霍尼韦尔EDA56使用
 * 25-1-14 兰陵县人民医院 测试可以使用（唐圆圆）
 */
public class HoneywellEda56Adapter implements Adapter {
    private Context context;
    private ResultListener resultListener;
    private Strategy strategy;


    public HoneywellEda56Adapter(Context context, ResultListener resultListener) {
        this.context = context;
        this.resultListener = resultListener;
        strategy = new HoneywellEda56Adapter.Receiver();
    }

    @Override
    public void start() {
        strategy.executeStrategy(resultListener);
    }

    @Override
    public void stop() {
        strategy.exclusiveStrategy();
    }

    public class Receiver extends BroadcastReceiver implements Strategy {

        @Override
        public void onReceive(Context context, Intent intent) {
            resultListener.result(intent.getStringExtra("data"));
        }

        @Override
        public void executeStrategy(ResultListener resultListener) {
            IntentFilter filter = new IntentFilter();
            filter.addAction("chaoran");
            filter.setPriority(2);
            context.registerReceiver(this, filter);
        }

        @Override
        public void exclusiveStrategy() {
            context.unregisterReceiver(this);
        }
    }
}
