From e89011649c0d7dde0d9d463c00afa9b14b2dd4ac Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Sun, 27 Jan 2019 18:41:20 +0800 Subject: [PATCH] optimized remoteIndex validation process --- decode-service.iml | 88 +++++++++++++++++++ .../decoder/businesslogic/DecodeLogic.java | 30 +++---- .../decoder/service/IRDecodeService.java | 45 ++++++++-- .../net/irext/decodesdk/utils/Constants.java | 12 +++ 4 files changed, 150 insertions(+), 25 deletions(-) diff --git a/decode-service.iml b/decode-service.iml index 920ced4..862f201 100644 --- a/decode-service.iml +++ b/decode-service.iml @@ -201,5 +201,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/irext/decoder/businesslogic/DecodeLogic.java b/src/main/java/net/irext/decoder/businesslogic/DecodeLogic.java index a3436e7..a40f9b7 100644 --- a/src/main/java/net/irext/decoder/businesslogic/DecodeLogic.java +++ b/src/main/java/net/irext/decoder/businesslogic/DecodeLogic.java @@ -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; } diff --git a/src/main/java/net/irext/decoder/service/IRDecodeService.java b/src/main/java/net/irext/decoder/service/IRDecodeService.java index 4fdc2f9..6911f30 100644 --- a/src/main/java/net/irext/decoder/service/IRDecodeService.java +++ b/src/main/java/net/irext/decoder/service/IRDecodeService.java @@ -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(); diff --git a/src/main/java/net/irext/decodesdk/utils/Constants.java b/src/main/java/net/irext/decodesdk/utils/Constants.java index 41d79a6..4650c1a 100644 --- a/src/main/java/net/irext/decodesdk/utils/Constants.java +++ b/src/main/java/net/irext/decodesdk/utils/Constants.java @@ -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),