package com.lianxin.nr510;

import android.content.Context;
import android.content.Intent;

public class Engine {
	
	private NR510Running runEngine = null;
	
	public void start(Context context) {
		if (runEngine == null) {
			runEngine = new NR510Running(context);
			new Thread(runEngine).start();
		}
	}
	
	public void stop(Context context) {
		if (runEngine != null) {
			Intent  intent = new  Intent("com.lachesis.barcode.STOP_BARCODE");
			context.sendBroadcast(intent);  
			runEngine.closeRunning();
			runEngine = null;
		}
	}
	
	private class NR510Running implements Runnable {
		
		private boolean scanRunning = true;
		private Context context;
		
		public NR510Running(Context context) {
			this.context = context;
		}
		
		public void closeRunning() {
			scanRunning = false;
		}

		@Override
		public void run() {
			while (true) {
				if (scanRunning) {
					Intent  intent = new  Intent("com.lachesis.barcode.START_BARCODE");
					context.sendBroadcast(intent); 
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					intent = new  Intent("com.lachesis.barcode.STOP_BARCODE");
					context.sendBroadcast(intent); 
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} else 
					break;
			}
		}
		
	}

}
