package com.quyt.iot_demo.esptouch.task;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import com.quyt.iot_demo.esptouch.EsptouchResult;
import com.quyt.iot_demo.esptouch.IEsptouchListener;
import com.quyt.iot_demo.esptouch.IEsptouchResult;
import com.quyt.iot_demo.esptouch.IEsptouchTask;
import com.quyt.iot_demo.esptouch.protocol.EsptouchGenerator;
import com.quyt.iot_demo.esptouch.protocol.TouchData;
import com.quyt.iot_demo.esptouch.security.ITouchEncryptor;
import com.quyt.iot_demo.esptouch.udp.UDPSocketClient;
import com.quyt.iot_demo.esptouch.udp.UDPSocketServer;
import com.quyt.iot_demo.esptouch.util.ByteUtil;
import com.quyt.iot_demo.esptouch.util.TouchNetUtil;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
public class __EsptouchTask implements __IEsptouchTask {
/**
* one indivisible data contain 3 9bits info
*/
private static final int ONE_DATA_LEN = 3;
private static final String TAG = "__EsptouchTask";
private final UDPSocketClient mSocketClient;
private final UDPSocketServer mSocketServer;
private final byte[] mApSsid;
private final byte[] mApPassword;
private final byte[] mApBssid;
private final ITouchEncryptor mEncryptor;
private final Context mContext;
private final List<IEsptouchResult> mEsptouchResultList;
private volatile boolean mIsSuc = false;
private volatile boolean mIsInterrupt = false;
private volatile boolean mIsExecuted = false;
private AtomicBoolean mIsCancelled;
private IEsptouchTaskParameter mParameter;
private volatile Map<String, Integer> mBssidTaskSucCountMap;
private IEsptouchListener mEsptouchListener;
private Thread mTask;
public __EsptouchTask(Context context, TouchData apSsid, TouchData apBssid, TouchData apPassword,
ITouchEncryptor encryptor, IEsptouchTaskParameter parameter) {
Log.i(TAG, "Welcome Esptouch " + IEsptouchTask.ESPTOUCH_VERSION);
mContext = context;
mEncryptor = encryptor;
mApSsid = apSsid.getData();
mApPassword = apPassword.getData();
mApBssid = apBssid.getData();
mIsCancelled = new AtomicBoolean(false);
mSocketClient = new UDPSocketClient();
mParameter = parameter;
mSocketServer = new UDPSocketServer(mParameter.getPortListening(),
mParameter.getWaitUdpTotalMillisecond(), context);
mEsptouchResultList = new ArrayList<>();
mBssidTaskSucCountMap = new HashMap<>();
}
private void __putEsptouchResult(boolean isSuc, String bssid, InetAddress inetAddress) {
synchronized (mEsptouchResultList) {
// check whether the result receive enough UDP response
boolean isTaskSucCountEnough = false;
Integer count = mBssidTaskSucCountMap.get(bssid);
if (count == null) {
count = 0;
}
++count;
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "__putEsptouchResult(): count = " + count);
}
mBssidTaskSucCountMap.put(bssid, count);
isTaskSucCountEnough = count >= mParameter
.getThresholdSucBroadcastCount();
if (!isTaskSucCountEnough) {
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "__putEsptouchResult(): count = " + count
+ ", isn't enough");
}
return;
}
// check whether the result is in the mEsptouchResultList already
boolean isExist = false;
for (IEsptouchResult esptouchResultInList : mEsptouchResultList) {
if (esptouchResultInList.getBssid().equals(bssid)) {
isExist = true;
break;
}
}
// only add the result who isn't in the mEsptouchResultList
if (!isExist) {
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "__putEsptouchResult(): put one more result " +
"bssid = " + bssid + " , address = " + inetAddress);
}
final IEsptouchResult esptouchResult = new EsptouchResult(isSuc,
bssid, inetAddress);
mEsptouchResultList.add(esptouchResult);
if (mEsptouchListener != null) {
mEsptouchListener.onEsptouchResultAdded(esptouchResult);
}
}
}
}
private List<IEsptouchResult> __getEsptouchResultList() {
synchronized (mEsptouchResultList) {
if (mEsptouchResultList.isEmpty()) {
EsptouchResult esptouchResultFail = new EsptouchResult(false,
null, null);
esptouchResultFail.setIsCancelled(mIsCancelled.get());
mEsptouchResultList.add(esptouchResultFail);
}
return mEsptouchResultList;
}
}
private synchronized void __interrupt() {
if (!mIsInterrupt) {
mIsInterrupt = true;
mSocketClient.interrupt();
mSocketServer.interrupt();
// interrupt the current Thread which is used to wait for udp response
if (mTask != null) {
mTask.interrupt();
mTask = null;
}
}
}
@Override
public void interrupt() {
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "interrupt()");
}
mIsCancelled.set(true);
__interrupt();
}
private void __listenAsyn(final int expectDataLen) {
mTask = new Thread() {
public void run() {
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "__listenAsyn() start");
}
long startTimestamp = System.currentTimeMillis();
// byte[] apSsidAndPassword = ByteUtil.getBytesByString(mApSsid
// + mApPassword);
byte expectOneByte = (byte) (mApSsid.length + mApPassword.length + 9);
if (__IEsptouchTask.DEBUG) {
Log.i(TAG, "expectOneByte: " + expectOneByte);
}
byte receiveOneByte = -1;
byte[] receiveBytes = null;
while (mEsptouchResultList.size() < mParameter
.getExpectTaskResultCount() && !mIsInterrupt) {
receiveBytes = mSocketServer
.receiveSpecLenBytes(expectDataLen);
if (receiveBytes != null) {
receiveOneByte = receiveBytes[0];
} else {
receiveOneByte = -1;
}
if (receiveOneByte == expectOneByte) {
if (__IEsptouchTask.DEBUG) {
Log.i(TAG, "receive correct broadcast");
}
// change the socket's timeout
long consume = System.currentTimeMillis()
- startTimestamp;
int timeout = (int) (mParameter
.getWaitUdpTotalMillisecond() - consume);
if (timeout < 0) {
if (__IEsptouchTask.DEBUG) {
Log.i(TAG, "esptouch timeout");
}
break;
} else {
if (__IEsptouchTask.DEBUG) {
Log.i(TAG, "mSocketServer's new timeout is "
+ timeout + " milliseconds");
}
mSocketServer.setSoTimeout(timeout);