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

import java.io.IOException;
import java.net.SocketTimeoutException;

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.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot10061ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot10061ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20009CheckPackageId;

public class CheckPackageIdRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private Prot10061ReqBean reqBean;
	public CheckPackageIdRunable(Handler mHandler, long deveicedId,Prot10061ReqBean reqBean) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.reqBean = reqBean;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				Prot20009CheckPackageId checkReq = new Prot20009CheckPackageId();
				checkReq.dealCheckPackageId(create, reqBean);
				Prot10061ResBean resBean = checkReq.getRes();
				if (resBean != null) {
					//result = 0;//0：请求成功     1：遇到错误信息，返回错误码
					//flag;//1表示package_id是空的，0表示package_id非空
					LogUtil.d(TConfig.Echo, "校验返回：" + resBean.toString());
					Message msg = mHandler.obtainMessage(RequestConstant.PROT_CHECK_PACKAGEID_10061_SUCCESS);
					msg.obj = resBean;
					mHandler.sendMessage(msg);
				} else {
					mHandler.sendEmptyMessage(RequestConstant.PROT_CHECK_PACKAGEID_10061_FAIL);
				}
				
				
			} else {
//				mHandler.sendEmptyMessage(ThreadConstant.)
				Message loginFailMsg = mHandler.obtainMessage();
				loginFailMsg.obj = create.getErrMsg();
				loginFailMsg.arg1 = create.getiErrCode();
				loginFailMsg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				mHandler.sendMessage(loginFailMsg);
			}
		}catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} finally{
			new Prot02Logout().dealLogout(create);
		}
	}

}
