6 changed files with 168 additions and 16 deletions
-
54src/src/main/java/com/my/graphiteDigesterBg/diframe/DiApiControllerBase.java
-
11src/src/main/java/com/my/graphiteDigesterBg/diframe/DiApiResponse.java
-
10src/src/main/java/com/my/graphiteDigesterBg/diframe/DiDevice.java
-
49src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskExecutor.java
-
24src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskManager.java
-
36src/src/main/java/com/my/graphiteDigesterBg/diframe/api/ApiDevice.java
@ -0,0 +1,54 @@ |
|||||
|
package com.my.graphiteDigesterBg.diframe; |
||||
|
abstract public class DiApiControllerBase { |
||||
|
/** |
||||
|
* success response |
||||
|
* @return ApiResponse |
||||
|
*/ |
||||
|
protected DiApiResponse success() { |
||||
|
DiApiResponse response = new DiApiResponse(); |
||||
|
response.success = true; |
||||
|
response.message = "OK"; |
||||
|
response.code = "OK"; |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* success response with data |
||||
|
* @param data - data to be returned |
||||
|
* @return ApiResponse |
||||
|
*/ |
||||
|
protected DiApiResponse success(Object data) { |
||||
|
DiApiResponse response = new DiApiResponse(); |
||||
|
response.success = true; |
||||
|
response.message = "OK"; |
||||
|
response.code = "OK"; |
||||
|
response.data = data; |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* error response |
||||
|
* @param message - error message |
||||
|
* @return ApiResponse |
||||
|
*/ |
||||
|
protected DiApiResponse error(String message) { |
||||
|
DiApiResponse response = new DiApiResponse(); |
||||
|
response.success = false; |
||||
|
response.message = message; |
||||
|
return response; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* error response with code |
||||
|
* @param message - error message |
||||
|
* @param code - error code |
||||
|
* @return ApiResponse |
||||
|
*/ |
||||
|
protected DiApiResponse error(String message, String code ) { |
||||
|
DiApiResponse response = new DiApiResponse(); |
||||
|
response.success = false; |
||||
|
response.message = message; |
||||
|
response.code = code; |
||||
|
return response; |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.my.graphiteDigesterBg.diframe; |
||||
|
public class DiApiResponse { |
||||
|
// success or not |
||||
|
public boolean success; |
||||
|
// error code |
||||
|
public String code; |
||||
|
// message |
||||
|
public String message; |
||||
|
// data |
||||
|
public Object data; |
||||
|
} |
@ -1,10 +1,55 @@ |
|||||
package com.my.graphiteDigesterBg.diframe; |
package com.my.graphiteDigesterBg.diframe; |
||||
public class DiTaskExecutor { |
|
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
public class DiTaskExecutor implements Runnable { |
||||
|
// logger |
||||
|
public static final Logger LOG = LoggerFactory.getLogger(DiTaskExecutor.class); |
||||
// device instance |
// device instance |
||||
private DiDevice device; |
|
||||
|
private final DiDevice device; |
||||
|
// stop request lock |
||||
|
private final Object stopRequestLock; |
||||
|
// stop request flag |
||||
|
private boolean isStopRequested; |
||||
|
|
||||
// constructor |
// constructor |
||||
public DiTaskExecutor(DiDevice device) { |
public DiTaskExecutor(DiDevice device) { |
||||
this.device = device; |
this.device = device; |
||||
|
this.stopRequestLock = new Object(); |
||||
|
this.isStopRequested = false; |
||||
} |
} |
||||
|
|
||||
|
@Override |
||||
|
public void run() { |
||||
|
LOG.info("Task executor started"); |
||||
|
while ( !this.isStopRequested ) { |
||||
|
LOG.info("Task executor running"); |
||||
|
try { |
||||
|
Thread.sleep(1000); |
||||
|
} catch (InterruptedException e) { |
||||
|
LOG.error("Task executor interrupted"); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
LOG.info("Task executor stopped"); |
||||
|
synchronized (this.stopRequestLock) { |
||||
|
this.stopRequestLock.notifyAll(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// stop executor |
||||
|
public void stop() { |
||||
|
LOG.info("Task executor stop requested"); |
||||
|
synchronized (this.stopRequestLock) { |
||||
|
this.isStopRequested = true; |
||||
|
try { |
||||
|
this.stopRequestLock.wait(); |
||||
|
} catch (InterruptedException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
LOG.info("Task executor stop completed"); |
||||
|
} |
||||
|
|
||||
|
public void pause() {} |
||||
|
public void resume() {} |
||||
} |
} |
@ -1,8 +1,36 @@ |
|||||
package com.my.graphiteDigesterBg.diframe.api; |
package com.my.graphiteDigesterBg.diframe.api; |
||||
public class ApiDevice { |
|
||||
public void init() {} |
|
||||
public void start() {} |
|
||||
public void stop() {} |
|
||||
|
import com.my.graphiteDigesterBg.diframe.DiApiControllerBase; |
||||
|
import com.my.graphiteDigesterBg.diframe.DiApiResponse; |
||||
|
import com.my.graphiteDigesterBg.diframe.DiDevice; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
@Controller |
||||
|
public class ApiDevice extends DiApiControllerBase { |
||||
|
@Resource |
||||
|
private DiDevice device; |
||||
|
|
||||
|
/** |
||||
|
* start device to execute tasks |
||||
|
*/ |
||||
|
@ResponseBody |
||||
|
@PostMapping("/api/device/start") |
||||
|
public DiApiResponse start() { |
||||
|
this.device.getTaskManager().executorStart(); |
||||
|
return this.success(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* stop device to execute tasks |
||||
|
*/ |
||||
|
@ResponseBody |
||||
|
@PostMapping("/api/device/stop") |
||||
|
public DiApiResponse stop() { |
||||
|
this.device.getTaskManager().getExecutor().stop(); |
||||
|
return this.success(); |
||||
|
} |
||||
|
|
||||
public void pause() {} |
public void pause() {} |
||||
public void resume() {} |
public void resume() {} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue