optimized remoteIndex validation process
This commit is contained in:
@@ -93,27 +93,19 @@ public class DecodeLogic {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int[] decode(IIRBinaryRepository irBinaryRepository, IDecodeSessionRepository decodeSessionRepository,
|
||||
String sessionId, int remoteIndexId, ACStatus acStatus, int keyCode, int changeWindDirection) {
|
||||
// since the binary is already opened and probably cached to redis, we just need to load it
|
||||
Integer cachedRemoteIndexId = decodeSessionRepository.find(sessionId);
|
||||
public int[] decode(RemoteIndex remoteIndex, ACStatus acStatus, int keyCode, int changeWindDirection) {
|
||||
int[] decoded = null;
|
||||
if (null != cachedRemoteIndexId) {
|
||||
RemoteIndex cachedRemoteIndex = irBinaryRepository.find(cachedRemoteIndexId);
|
||||
if (null != cachedRemoteIndex) {
|
||||
int categoryId = cachedRemoteIndex.getCategoryId();
|
||||
int subCate = cachedRemoteIndex.getSubCate();
|
||||
byte[] binaryContent = cachedRemoteIndex.getBinaries();
|
||||
IRDecode irDecode = IRDecode.getInstance();
|
||||
int ret = irDecode.openBinary(categoryId, subCate, binaryContent, binaryContent.length);
|
||||
if (0 == ret) {
|
||||
decoded = irDecode.decodeBinary(keyCode, acStatus, changeWindDirection);
|
||||
}
|
||||
irDecode.closeBinary();
|
||||
return decoded;
|
||||
if (null != remoteIndex) {
|
||||
int categoryId = remoteIndex.getCategoryId();
|
||||
int subCate = remoteIndex.getSubCate();
|
||||
byte[] binaryContent = remoteIndex.getBinaries();
|
||||
IRDecode irDecode = IRDecode.getInstance();
|
||||
int ret = irDecode.openBinary(categoryId, subCate, binaryContent, binaryContent.length);
|
||||
if (0 == ret) {
|
||||
decoded = irDecode.decodeBinary(keyCode, acStatus, changeWindDirection);
|
||||
}
|
||||
} else {
|
||||
LoggerUtil.getInstance().trace(TAG, "session cache missed, need to re-open binary");
|
||||
irDecode.closeBinary();
|
||||
return decoded;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ public class IRDecodeService extends AbstractBaseService {
|
||||
StringResponse response = new StringResponse();
|
||||
RemoteIndex remoteIndex = IndexLogic.getInstance(remoteIndexMapper).getRemoteIndex(remoteIndexId);
|
||||
if (null == remoteIndex) {
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR,
|
||||
Constants.ERROR_CODE_NETWORK_ERROR_TEXT));
|
||||
response.setEntity(null);
|
||||
return response;
|
||||
} else {
|
||||
@@ -96,7 +97,7 @@ public class IRDecodeService extends AbstractBaseService {
|
||||
decodeSessionRepository.add(decodeSession.getSessionId(), decodeSession.getBinaryId());
|
||||
response.setEntity(decodeSession.getSessionId());
|
||||
}
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, ""));
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, Constants.ERROR_CODE_SUCESS_TEXT));
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -113,16 +114,48 @@ public class IRDecodeService extends AbstractBaseService {
|
||||
int changeWindDir = decodeRequest.getChangeWindDir();
|
||||
String sessionId = decodeRequest.getSessionId();
|
||||
|
||||
RemoteIndex cachedRemoteIndex = null;
|
||||
DecodeResponse response = new DecodeResponse();
|
||||
|
||||
if (null == sessionId) {
|
||||
LoggerUtil.getInstance().trace(TAG, "sessionId is not given, abort");
|
||||
response.setEntity(null);
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_INVALID_SESSION,
|
||||
Constants.ERROR_CODE_INVALID_SESSION_TEXT));
|
||||
} else {
|
||||
Integer cachedRemoteIndexId = decodeSessionRepository.find(sessionId);
|
||||
if (null == cachedRemoteIndexId) {
|
||||
response.setEntity(null);
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_INVALID_SESSION,
|
||||
Constants.ERROR_CODE_INVALID_SESSION_TEXT));
|
||||
} else {
|
||||
cachedRemoteIndex = irBinaryRepository.find(cachedRemoteIndexId);
|
||||
if (null == cachedRemoteIndex) {
|
||||
response.setEntity(null);
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_INVALID_SESSION,
|
||||
Constants.ERROR_CODE_INVALID_SESSION_TEXT));
|
||||
} else {
|
||||
if (indexId != cachedRemoteIndex.getId()) {
|
||||
response.setEntity(null);
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_INVALID_SESSION,
|
||||
Constants.ERROR_CODE_INVALID_SESSION_TEXT));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.getStatus().getCode() != Constants.ERROR_CODE_SUCCESS) {
|
||||
return response;
|
||||
}
|
||||
|
||||
int[] irArray = DecodeLogic.getInstance().decode(
|
||||
irBinaryRepository,
|
||||
decodeSessionRepository,
|
||||
sessionId,
|
||||
indexId,
|
||||
cachedRemoteIndex,
|
||||
acStatus,
|
||||
keyCode,
|
||||
changeWindDir);
|
||||
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, Constants.ERROR_CODE_SUCESS_TEXT));
|
||||
response.setEntity(irArray);
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -14,11 +14,23 @@ public class Constants {
|
||||
|
||||
public static final int ERROR_CODE_SUCCESS = 0;
|
||||
public static final int ERROR_CODE_NETWORK_ERROR = -1;
|
||||
|
||||
public static final int ERROR_CODE_AUTH_FAILURE = 1;
|
||||
public static final int ERROR_CODE_INVALID_CATEGORY = 2;
|
||||
public static final int ERROR_CODE_INVALID_BRAND = 3;
|
||||
public static final int ERROR_CODE_INVALID_PARAMETER = 4;
|
||||
|
||||
public static final int ERROR_CODE_INVALID_SESSION = 20;
|
||||
|
||||
public static final String ERROR_CODE_SUCESS_TEXT = "success";
|
||||
public static final String ERROR_CODE_NETWORK_ERROR_TEXT = "network error";
|
||||
public static final String ERROR_CODE_AUTH_FAILUTRE_TEXT = "auth failure";
|
||||
public static final String ERROR_CODE_INVALID_CATEGORY_TEXT = "invalid category";
|
||||
public static final String ERROR_CODE_INVALID_BRAND_TEXT = "invalid brand";
|
||||
public static final String ERROR_CODE_INVALID_PARAMETER_TEXT = "invalid parameter";
|
||||
|
||||
public static final String ERROR_CODE_INVALID_SESSION_TEXT = "invalid decode session";
|
||||
|
||||
public enum CategoryID {
|
||||
AIR_CONDITIONER(1),
|
||||
TV(2),
|
||||
|
||||
Reference in New Issue
Block a user