From 105bdecb0299ed924a210e7ac98a3b2586bcf477 Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Sat, 5 Jan 2019 19:18:23 +0800 Subject: [PATCH] implemented openIR with session_id --- .../cache/IDecodeSessionRepository.java | 6 ++-- .../impl/DecodeSessionRepositoryImpl.java | 12 +++---- .../irext/decoder/model/DecodeSession.java | 26 +++++++------- .../irext/decoder/request/DecodeRequest.java | 9 +++++ .../decoder/response/StringResponse.java | 34 +++++++++++++++++++ .../decoder/service/IRDecodeService.java | 22 ++++++++++-- 6 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 src/main/java/net/irext/decoder/response/StringResponse.java diff --git a/src/main/java/net/irext/decoder/cache/IDecodeSessionRepository.java b/src/main/java/net/irext/decoder/cache/IDecodeSessionRepository.java index 120570f..5a6c6c2 100644 --- a/src/main/java/net/irext/decoder/cache/IDecodeSessionRepository.java +++ b/src/main/java/net/irext/decoder/cache/IDecodeSessionRepository.java @@ -18,10 +18,10 @@ public interface IDecodeSessionRepository { Map findAllDecodeSessions(); - void add(DecodeSession decodeSession); + void add(String decodeSessionId, Integer remoteId); - void delete(Integer id); + void delete(String decodeSessionId); - DecodeSession find(Integer id); + Integer find(String decodeSessionId); } \ No newline at end of file diff --git a/src/main/java/net/irext/decoder/cache/impl/DecodeSessionRepositoryImpl.java b/src/main/java/net/irext/decoder/cache/impl/DecodeSessionRepositoryImpl.java index 1895533..b80ca12 100644 --- a/src/main/java/net/irext/decoder/cache/impl/DecodeSessionRepositoryImpl.java +++ b/src/main/java/net/irext/decoder/cache/impl/DecodeSessionRepositoryImpl.java @@ -37,16 +37,16 @@ public class DecodeSessionRepositoryImpl implements IDecodeSessionRepository { hashOperations = redisTemplate.opsForHash(); } - public void add(final DecodeSession decodeSession) { - hashOperations.put(KEY, decodeSession.getId(), decodeSession.getName()); + public void add(final String decodeSessionId, Integer binaryId) { + hashOperations.put(KEY, decodeSessionId, binaryId); } - public void delete(final Integer id) { - hashOperations.delete(KEY, id); + public void delete(String decodeSessionId) { + hashOperations.delete(KEY, decodeSessionId); } - public DecodeSession find(final Integer id) { - return (DecodeSession) hashOperations.get(KEY, id); + public Integer find(String decodeSessionId) { + return (Integer) hashOperations.get(KEY, decodeSessionId); } public Map findAllDecodeSessions() { diff --git a/src/main/java/net/irext/decoder/model/DecodeSession.java b/src/main/java/net/irext/decoder/model/DecodeSession.java index 7851c55..a060038 100644 --- a/src/main/java/net/irext/decoder/model/DecodeSession.java +++ b/src/main/java/net/irext/decoder/model/DecodeSession.java @@ -12,31 +12,31 @@ package net.irext.decoder.model; */ public class DecodeSession { - private Integer id; - private String name; + private String sessionId; + private Integer binaryId; - public DecodeSession(Integer id, String name) { - this.id = id; - this.name = name; + public DecodeSession(String sessionId, Integer binaryId) { + this.sessionId = sessionId; + this.binaryId = binaryId; } public DecodeSession() { } - public Integer getId() { - return id; + public String getSessionId() { + return sessionId; } - public void setId(Integer id) { - this.id = id; + public void setSessionId(String sessionId) { + this.sessionId = sessionId; } - public String getName() { - return name; + public Integer getBinaryId() { + return binaryId; } - public void setName(String name) { - this.name = name; + public void setBinaryId(Integer binaryId) { + this.binaryId = binaryId; } } diff --git a/src/main/java/net/irext/decoder/request/DecodeRequest.java b/src/main/java/net/irext/decoder/request/DecodeRequest.java index 85ff754..9c3eb4f 100644 --- a/src/main/java/net/irext/decoder/request/DecodeRequest.java +++ b/src/main/java/net/irext/decoder/request/DecodeRequest.java @@ -18,6 +18,7 @@ public class DecodeRequest { private ACStatus acStatus; private int keyCode; private int changeWindDir; + private String sessionId; public DecodeRequest(int indexId, ACStatus acStatus, int keyCode, int changeWindDir) { this.indexId = indexId; @@ -61,4 +62,12 @@ public class DecodeRequest { public void setChangeWindDir(int changeWindDir) { this.changeWindDir = changeWindDir; } + + public String getSessionId() { + return sessionId; + } + + public void setSessionId(String sessionId) { + this.sessionId = sessionId; + } } diff --git a/src/main/java/net/irext/decoder/response/StringResponse.java b/src/main/java/net/irext/decoder/response/StringResponse.java new file mode 100644 index 0000000..e9720c0 --- /dev/null +++ b/src/main/java/net/irext/decoder/response/StringResponse.java @@ -0,0 +1,34 @@ +package net.irext.decoder.response; + +/** + * Filename: StringResponse.java + * Revised: Date: 2017-05-16 + * Revision: Revision: 1.0 + *

+ * Description: String response + *

+ * Revision log: + * 2017-05-16: created by strawmanbobi + */ +public class StringResponse extends ServiceResponse { + + private String entity; + + public StringResponse(Status status, String entity) { + super(status); + this.entity = entity; + } + + public StringResponse() { + + } + + public String getEntity() { + return entity; + } + + public void setEntity(String entity) { + this.entity = entity; + } +} + diff --git a/src/main/java/net/irext/decoder/service/IRDecodeService.java b/src/main/java/net/irext/decoder/service/IRDecodeService.java index d48413e..a92c289 100644 --- a/src/main/java/net/irext/decoder/service/IRDecodeService.java +++ b/src/main/java/net/irext/decoder/service/IRDecodeService.java @@ -3,6 +3,7 @@ package net.irext.decoder.service; import net.irext.decoder.businesslogic.DecodeLogic; import net.irext.decoder.businesslogic.IndexLogic; import net.irext.decoder.mapper.RemoteIndexMapper; +import net.irext.decoder.model.DecodeSession; import net.irext.decoder.model.RemoteIndex; import net.irext.decoder.cache.IDecodeSessionRepository; import net.irext.decoder.cache.IIRBinaryRepository; @@ -12,8 +13,10 @@ import net.irext.decoder.request.OpenRequest; import net.irext.decoder.response.DecodeResponse; import net.irext.decoder.response.ServiceResponse; import net.irext.decoder.response.Status; +import net.irext.decoder.response.StringResponse; import net.irext.decoder.service.base.AbstractBaseService; import net.irext.decoder.utils.LoggerUtil; +import net.irext.decoder.utils.MD5Util; import net.irext.decodesdk.bean.ACStatus; import net.irext.decodesdk.utils.Constants; import org.springframework.beans.factory.annotation.Autowired; @@ -21,6 +24,8 @@ import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.*; import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import java.text.SimpleDateFormat; /** * Filename: IRDecodeService.java @@ -55,16 +60,17 @@ public class IRDecodeService extends AbstractBaseService { } @PostMapping("/open") - public ServiceResponse irOpen(@RequestBody OpenRequest openRequest) { + public StringResponse irOpen(HttpServletRequest request, @RequestBody OpenRequest openRequest) { try { int remoteIndexId = openRequest.getRemoteIndexId(); LoggerUtil.getInstance().trace(TAG,"irOpen API called : " + remoteIndexId); - ServiceResponse response = new ServiceResponse(); + StringResponse response = new StringResponse(); RemoteIndex remoteIndex = IndexLogic.getInstance(remoteIndexMapper).getRemoteIndex(remoteIndexId); if (null == remoteIndex) { response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, "")); + response.setEntity(null); return response; } else { LoggerUtil.getInstance().trace(TAG, "remoteIndex get : " + remoteIndex.getId() + ", " + @@ -74,12 +80,21 @@ public class IRDecodeService extends AbstractBaseService { if (null != binaryContent) { LoggerUtil.getInstance().trace(TAG,"binary content fetched : " + binaryContent.length); + // construct a session with this binary + String address = request.getRemoteAddr(); + LoggerUtil.getInstance().trace(TAG, "request Address = " + address); + String timeStamp = + new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date()); + String sessionId = MD5Util.MD5Encode(address + timeStamp, null); + DecodeSession decodeSession = new DecodeSession(sessionId, remoteIndex.getId()); + decodeSessionRepository.add(decodeSession.getSessionId(), decodeSession.getBinaryId()); + response.setEntity(decodeSession.getSessionId()); } response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, "")); return response; } catch (Exception e) { e.printStackTrace(); - return getExceptionResponse(DecodeResponse.class); + return getExceptionResponse(StringResponse.class); } } @@ -90,6 +105,7 @@ public class IRDecodeService extends AbstractBaseService { ACStatus acstatus = decodeRequest.getAcStatus(); int keyCode = decodeRequest.getKeyCode(); int changeWindDir = decodeRequest.getChangeWindDir(); + String sessionId = decodeRequest.getSessionId(); DecodeResponse response = new DecodeResponse(); int[] irArray = DecodeLogic.getInstance().decode();