cleaned requests and services

This commit is contained in:
strawmanbobi
2018-12-18 21:19:24 +08:00
parent ef5539d2ee
commit f0b3837566
6 changed files with 190 additions and 34 deletions

View File

@@ -15,37 +15,20 @@ import org.springframework.stereotype.Controller;
@Controller
public class DecodeLogic {
private Integer codeCollectState = 0;
private IRCode irCode;
private static DecodeLogic decodeLogic;
public static DecodeLogic getInstance(IRCode irCode) {
public static DecodeLogic getInstance() {
if (null == decodeLogic) {
decodeLogic = new DecodeLogic(irCode);
decodeLogic = new DecodeLogic();
}
return decodeLogic;
}
public DecodeLogic(IRCode irCode) {
this.irCode = irCode;
}
public DecodeLogic() {
}
public boolean collectCodeWorkUnit() {
// step 1. figure out the category and brand for this code
// step 2. go through the FSM for remote in each category
codeCollectState++;
System.out.println("state = " + codeCollectState + " : " + irCode.getKey());
// step 3. record key code
// step 4. temporarily save to file
return true;
public int[] decode() {
return null;
}
}

View File

@@ -0,0 +1,31 @@
package net.irext.decoder.businesslogic;
import net.irext.decoder.model.RemoteIndex;
import org.springframework.stereotype.Controller;
/**
* Filename: CollectCodeLogic
* Revised: Date: 2018-12-08
* Revision: Revision: 1.0
* <p>
* Description: IRext Code Collector Collect Code Logic
* <p>
* Revision log:
* 2018-12-08: created by strawmanbobi
*/
@Controller
public class IndexLogic {
private static IndexLogic indexLogic;
public static IndexLogic getInstance() {
if (null == indexLogic) {
indexLogic = new IndexLogic();
}
return indexLogic;
}
public RemoteIndex getRemoteIndex(int indexId) {
return null;
}
}

View File

@@ -0,0 +1,33 @@
package net.irext.decoder.request;
/**
* Filename: CloseRequest.java
* Revised: Date: 2018-12-18
* Revision: Revision: 1.0
* <p>
* Description: HTTP decode online
* <p>
* Revision log:
* 2018-12-18: created by strawmanbobi
*/
public class CloseRequest {
private int indexId;
public CloseRequest(int indexId) {
this.indexId = indexId;
}
public CloseRequest() {
}
public int getIndexId() {
return indexId;
}
public void setIndexId(int indexId) {
this.indexId = indexId;
}
}

View File

@@ -0,0 +1,33 @@
package net.irext.decoder.request;
/**
* Filename: OpenRequest.java
* Revised: Date: 2018-12-18
* Revision: Revision: 1.0
* <p>
* Description: HTTP decode online
* <p>
* Revision log:
* 2018-12-18: created by strawmanbobi
*/
public class OpenRequest {
private int indexId;
public OpenRequest(int indexId) {
this.indexId = indexId;
}
public OpenRequest() {
}
public int getIndexId() {
return indexId;
}
public void setIndexId(int indexId) {
this.indexId = indexId;
}
}

View File

@@ -1,9 +1,15 @@
package net.irext.decoder.service;
import net.irext.decoder.businesslogic.DecodeLogic;
import net.irext.decoder.businesslogic.IndexLogic;
import net.irext.decoder.model.RemoteIndex;
import net.irext.decoder.request.CloseRequest;
import net.irext.decoder.request.DecodeRequest;
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.service.base.AbstractBaseService;
import net.irext.decodesdk.bean.ACStatus;
import net.irext.decodesdk.utils.Constants;
import org.springframework.web.bind.annotation.*;
@@ -20,13 +26,32 @@ import org.springframework.web.bind.annotation.*;
*/
@RestController
@RequestMapping("/irext")
public class IRDecodeService {
public class IRDecodeService extends AbstractBaseService {
public IRDecodeService() {
}
@GetMapping("/decode")
@PostMapping("/open")
public ServiceResponse irOpen(@RequestBody OpenRequest openRequest) {
try {
int indexId = openRequest.getIndexId();
ServiceResponse response = new ServiceResponse();
RemoteIndex index = IndexLogic.getInstance().getRemoteIndex(indexId);
if (null == index) {
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
return response;
}
return response;
} catch (Exception e) {
e.printStackTrace();
return getExceptionResponse(DecodeResponse.class);
}
}
@PostMapping("/decode")
public DecodeResponse irDecode(@RequestBody DecodeRequest decodeRequest) {
try {
int indexId = decodeRequest.getIndexId();
@@ -34,18 +59,22 @@ public class IRDecodeService {
int keyCode = decodeRequest.getKeyCode();
int changeWindDir = decodeRequest.getChangeWindDir();
RemoteIndex index = indexingLogic.getRemoteIndex(indexId);
if (null == index) {
response.setEntity(null);
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
return response;
}
DecodeResponse response = new DecodeResponse();
int[] irArray = DecodeLogic.getInstance().decode();
byte []binaryContent = operationLogic.prepareBinary(indexId);
System.out.println("binary content fetched : " + binaryContent.length);
int []decoded = operationLogic.decodeIR(index.getCategoryId(), index.getSubCate(),
binaryContent, acstatus, keyCode, changeWindDir);
response.setEntity(decoded);
return response;
} catch (Exception e) {
e.printStackTrace();
return getExceptionResponse(DecodeResponse.class);
}
}
@PostMapping("/close")
public ServiceResponse irClose(@RequestBody CloseRequest closeRequest) {
try {
ServiceResponse response = new ServiceResponse();
DecodeLogic.getInstance().decode();
return response;
} catch (Exception e) {

View File

@@ -0,0 +1,47 @@
package net.irext.decoder.service.base;
import net.irext.decoder.Constants;
import net.irext.decoder.response.ServiceResponse;
import net.irext.decoder.response.Status;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Filename: AbstractBaseService.java
* Revised: Date: 2017-04-27
* Revision: Revision: 1.0
* <p>
* Description: Base service abstract class implemented service interface
* <p>
* Revision log:
* 2017-04-27: created by strawmanbobi
*/
public abstract class AbstractBaseService {
// note : not using ASPECT here but keep the ASPECT sketch
protected static Log log = LogFactory.getLog(AbstractBaseService.class);
protected ServiceResponse getExceptionResponse() {
ServiceResponse r = new ServiceResponse();
Status status = new Status();
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
r.setStatus(status);
return r;
}
protected <T extends ServiceResponse> T getExceptionResponse(Class<T> c) {
T r = null;
Status status = new Status();
try {
r = c.newInstance();
} catch (Exception e) {
log.error("Error when new instance of class: " + c.getName(), e);
}
status.setCode(Constants.ERROR_CODE_AUTH_FAILURE);
if (null != r) {
r.setStatus(status);
}
return r;
}
}