package cn.net.caoyao.cpzshandset.service.thread.impl.medicineXY;

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20042ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.ProtCheckLabelStatusResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20042CheckLabelStatus;

public class MedicineXYCheckLabelStatusThread implements Runnable{
	private Handler mHandler;
	private long deviceId;
	private Prot20042ReqBean reqBean;
	private String consume_code;
	public MedicineXYCheckLabelStatusThread(Handler mHandler,long deviceId,Prot20042ReqBean reqBean,String consume_code) {
		this.deviceId = deviceId;
		this.mHandler = mHandler;
		this.reqBean = reqBean;
		this.consume_code = consume_code;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
//			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				Prot20042CheckLabelStatus checkLabelStatus = new Prot20042CheckLabelStatus();
				checkLabelStatus.checkLabelStatus(create, reqBean,consume_code);
				ProtCheckLabelStatusResBean resBean = checkLabelStatus.getResBean();
				Message message = mHandler.obtainMessage(RequestConstant.PROT_CHECK_LABEL_SUCCESS);
				message.obj = resBean;
				mHandler.sendMessage(message);
				
			} else {
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_FAIL);
				msg.arg1 = create.getiErrCode();
				msg.obj = create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (ConnectException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}
}
