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

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.caoyao.cpzshandset.service.bll.CertificateBll;
import cn.net.caoyao.cpzshandset.service.bll.LandBll;
import cn.net.caoyao.cpzshandset.service.bll.ProductBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;

/**
 * @author 作者 李扬:
 * @date 创建时间：2017-3-28 上午10:02:41
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class SeedsUpdateProductListThread implements Runnable {
	private long deviceId;

	private ProductBll pBll;
	private LandBll lBll;
	private CertificateBll cBll;

	private Handler mHandler;

	public SeedsUpdateProductListThread(Handler mHandler, long deviceId,
			ProductBll pBll, LandBll lBll, CertificateBll cBll) {
		
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.pBll = pBll;
		this.lBll = lBll;
		this.cBll = cBll;

	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				requestProductList(create);
			} else {
				Message msg = new Message();
				msg.what = 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);
		}
	}
	private void requestProductList(SocketCreate create) {
		mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS);
	}

}
