package cn.net.communal.cpzshandset.service.thread.impl.checkAvaiable;

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import cn.net.communal.cpzshandset.common.config.Config;
import cn.net.communal.cpzshandset.common.config.RequestConstant;
import cn.net.communal.cpzshandset.common.config.TConfig;
import cn.net.communal.cpzshandset.common.util.LogUtil;
import cn.net.communal.cpzshandset.service.bll.ProductBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;



/**
 * 链接服务器测试线程
 * @author zwj
 *
 * 2015-5-28
 */
public class Test8990Thread implements Runnable{
	private long deviceId;
	private ProductBll bll;
	private Handler mHandler;

	public Test8990Thread(Handler mHandler,long deviceId) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		//
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, 8765, "");
		try {
			LogUtil.d(TConfig.Echo, "登录"+deviceId);
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
				msg.obj = "108 - 8765 连接成功";
				mHandler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = "108 - 8765连接成功:"+create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			mHandler.sendMessage(msg);
		} catch (ConnectException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			mHandler.sendMessage(msg);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
			msg.obj = "108 - 8765 请求超时";
			mHandler.sendMessage(msg);
		} catch (UnknownHostException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765连接失败";
			mHandler.sendMessage(msg);
		} catch (IOException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			mHandler.sendMessage(msg);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}



}
