16 changed files with 320 additions and 24 deletions
-
1src/src/main/java/com/my/graphiteDigesterBg/diframe/DiApplicationRunner.java
-
5src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTask.java
-
36src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskBase.java
-
10src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskExecutor.java
-
22src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskManager.java
-
15src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskStep.java
-
40src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskStepBase.java
-
80src/src/main/java/com/my/graphiteDigesterBg/diframe/DiTaskStepTaskBase.java
-
9src/src/main/java/com/my/graphiteDigesterBg/diframe/TaskStep.java
-
18src/src/main/java/com/my/graphiteDigesterBg/model/MdbDigestionTask.java
-
19src/src/main/java/com/my/graphiteDigesterBg/resource/ResHeatingTubeRackSlot.java
-
41src/src/main/java/com/my/graphiteDigesterBg/step/StepHeating.java
-
17src/src/main/java/com/my/graphiteDigesterBg/step/StepPump.java
-
21src/src/main/java/com/my/graphiteDigesterBg/task/TaskDigestion.java
-
8src/src/main/resources/application.yml
-
2src/src/main/resources/device.yml
@ -1,4 +1,17 @@ |
|||
package com.my.graphiteDigesterBg.diframe; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
public interface DiTaskStep { |
|||
void run(); |
|||
|
|||
String getAction(); |
|||
|
|||
void setStepNode(JsonNode stepNode); |
|||
|
|||
JsonNode getStepNode(); |
|||
|
|||
void setTask(DiTask task); |
|||
|
|||
DiTask getTask(); |
|||
|
|||
<T extends DiTask> T getTask( Class<T> clazz ); |
|||
} |
@ -0,0 +1,40 @@ |
|||
package com.my.graphiteDigesterBg.diframe; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
abstract public class DiTaskStepBase implements DiTaskStep { |
|||
// name of step action |
|||
public String action; |
|||
// step node |
|||
private JsonNode stepNode; |
|||
// task |
|||
private DiTask task; |
|||
|
|||
@Override |
|||
public void setTask(DiTask task) { |
|||
this.task = task; |
|||
} |
|||
|
|||
@Override |
|||
public DiTask getTask() { |
|||
return this.task; |
|||
} |
|||
|
|||
@Override |
|||
public <T extends DiTask> T getTask( Class<T> clazz ) { |
|||
return (T)this.task; |
|||
} |
|||
|
|||
@Override |
|||
public void setStepNode(JsonNode stepNode) { |
|||
this.stepNode = stepNode; |
|||
} |
|||
|
|||
@Override |
|||
public JsonNode getStepNode() { |
|||
return this.stepNode; |
|||
} |
|||
|
|||
@Override |
|||
public String getAction() { |
|||
return action; |
|||
} |
|||
} |
@ -0,0 +1,80 @@ |
|||
package com.my.graphiteDigesterBg.diframe; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
abstract public class DiTaskStepTaskBase extends DiTaskBase { |
|||
// logger |
|||
public static final Logger LOG = LoggerFactory.getLogger(DiTask.class); |
|||
// steps |
|||
public List<DiTaskStep> steps; |
|||
// step index |
|||
public Integer stepIndex; |
|||
|
|||
// setup task |
|||
abstract protected void setup(); |
|||
|
|||
@Override |
|||
public void run() { |
|||
if ( null == this.stepIndex ) { |
|||
this.setup(); |
|||
this.stepIndex = 0; |
|||
} |
|||
|
|||
this.setStatus(TaskStatus.RUNNING); |
|||
|
|||
// execute steps |
|||
while ( this.stepIndex < this.steps.size() ) { |
|||
if ( this.getIsRequestTaskStop() ) { |
|||
this.setStatus(TaskStatus.CANCELLED); |
|||
break ; |
|||
} |
|||
|
|||
DiTaskStep step = this.steps.get(this.stepIndex); |
|||
this.executeStep(step); |
|||
this.stepIndex++; |
|||
if ( !TaskStatus.RUNNING.equals(this.getStatus()) ) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
// finish task |
|||
if ( TaskStatus.RUNNING.equals(this.getStatus()) ) { |
|||
this.setStatus(TaskStatus.FINISHED); |
|||
} |
|||
} |
|||
|
|||
// execute step |
|||
private void executeStep( DiTaskStep step ) { |
|||
LOG.info("[Task #{}] - Step {} => {}", this.getUUID(), step.getAction(), step.getStepNode().toString()); |
|||
step.run(); |
|||
} |
|||
|
|||
// load steps by json |
|||
protected void loadStepsByJson( String json ) { |
|||
ObjectMapper jsonMapper = new ObjectMapper(); |
|||
JsonNode stepJsonTree = null; |
|||
try { |
|||
stepJsonTree = jsonMapper.readTree(json); |
|||
} catch (JsonProcessingException e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
this.steps = new ArrayList<>(); |
|||
for ( JsonNode stepNode : stepJsonTree ) { |
|||
String name = stepNode.get("action").asText(); |
|||
|
|||
var stepClass = (Class<? extends DiTaskStep>)this.getDevice().getTaskManager().getTaskStepClassByName(name); |
|||
if ( stepClass == null ) { |
|||
throw new RuntimeException("step class not found: " + name); |
|||
} |
|||
DiTaskStep step = jsonMapper.convertValue(stepNode, stepClass); |
|||
step.setStepNode(stepNode); |
|||
step.setTask(this); |
|||
this.steps.add(step); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
package com.my.graphiteDigesterBg.diframe; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target({java.lang.annotation.ElementType.TYPE}) |
|||
public @interface TaskStep { |
|||
String name(); |
|||
} |
@ -0,0 +1,18 @@ |
|||
package com.my.graphiteDigesterBg.model; |
|||
import com.my.graphiteDigesterBg.diframe.ActiveRecordField; |
|||
import com.my.graphiteDigesterBg.diframe.DiActiveRecord; |
|||
public class MdbDigestionTask extends DiActiveRecord { |
|||
@ActiveRecordField |
|||
public Integer id; |
|||
|
|||
@ActiveRecordField |
|||
public String name; |
|||
|
|||
@ActiveRecordField |
|||
public String steps; |
|||
|
|||
// get table name |
|||
public static String getTableName() { |
|||
return "app_digestion_tasks"; |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
package com.my.graphiteDigesterBg.step; |
|||
import com.my.graphiteDigesterBg.diframe.DiTask; |
|||
import com.my.graphiteDigesterBg.diframe.DiTaskStepBase; |
|||
import com.my.graphiteDigesterBg.diframe.TaskStep; |
|||
import com.my.graphiteDigesterBg.resource.ResHeatingTubeRackSlotManager; |
|||
import com.my.graphiteDigesterBg.task.TaskDigestion; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import java.util.Timer; |
|||
import java.util.TimerTask; |
|||
@TaskStep(name="Heating") |
|||
public class StepHeating extends DiTaskStepBase { |
|||
// logger |
|||
public static final Logger LOG = LoggerFactory.getLogger(StepHeating.class); |
|||
// temperature |
|||
public Integer temperature; |
|||
// duration in seconds |
|||
public Integer duration; |
|||
// timer |
|||
private Timer timer; |
|||
|
|||
@Override |
|||
public void run() { |
|||
var task = this.getTask(TaskDigestion.class); |
|||
var slotMan = task.getDevice().getResource().getManager(ResHeatingTubeRackSlotManager.class); |
|||
var slot = slotMan.getSlotByIndex(task.slotIndex); |
|||
|
|||
slot.heatingOn(); |
|||
task.setStatus(DiTask.TaskStatus.WAITING); |
|||
TimerTask timerTask = new TimerTask() { |
|||
@Override |
|||
public void run() { |
|||
slot.heatingOff(); |
|||
task.setStatus(DiTask.TaskStatus.READY); |
|||
} |
|||
}; |
|||
|
|||
this.timer = new Timer(); |
|||
this.timer.schedule(timerTask, this.duration * 1000); |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
package com.my.graphiteDigesterBg.step; |
|||
import com.my.graphiteDigesterBg.diframe.DiTaskStepBase; |
|||
import com.my.graphiteDigesterBg.diframe.TaskStep; |
|||
@TaskStep(name="Pump") |
|||
public class StepPump extends DiTaskStepBase { |
|||
// type |
|||
public String type; |
|||
// amount |
|||
public Integer amount; |
|||
// shake |
|||
public Integer shake; |
|||
|
|||
@Override |
|||
public void run() { |
|||
System.out.println("StepPump.run()"); |
|||
} |
|||
} |
@ -1,11 +1,22 @@ |
|||
package com.my.graphiteDigesterBg.task; |
|||
import com.my.graphiteDigesterBg.diframe.DiTaskBase; |
|||
import com.my.graphiteDigesterBg.diframe.DiActiveRecord; |
|||
import com.my.graphiteDigesterBg.diframe.DiTaskStepTaskBase; |
|||
import com.my.graphiteDigesterBg.diframe.Task; |
|||
import com.my.graphiteDigesterBg.model.MdbDigestionTask; |
|||
import java.util.Map; |
|||
@Task(name="Digestion") |
|||
public class TaskDigestion extends DiTaskBase { |
|||
public class TaskDigestion extends DiTaskStepTaskBase { |
|||
// name of digesting task |
|||
public String name; |
|||
// slot index |
|||
public Integer slotIndex; |
|||
|
|||
@Override |
|||
public void run() { |
|||
System.out.println("TaskDigestion.run()"); |
|||
this.setStatus(TaskStatus.FINISHED); |
|||
protected void setup() { |
|||
var taskModel = DiActiveRecord.findOne(MdbDigestionTask.class, Map.of("name", this.name)); |
|||
if ( taskModel == null ) { |
|||
throw new RuntimeException("task [" + this.name + "] not found"); |
|||
} |
|||
this.loadStepsByJson(taskModel.steps); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue