|
@ -34,7 +34,7 @@ public class CraftsContext implements Runnable { |
|
|
this.ws = ws; |
|
|
this.ws = ws; |
|
|
|
|
|
|
|
|
this.sm = factory.getStateMachine(heatId); |
|
|
this.sm = factory.getStateMachine(heatId); |
|
|
Mono.from(sm.startReactively()).block(); // READY -> RUNNING |
|
|
|
|
|
|
|
|
Mono.from(sm.startReactively()).block(); |
|
|
Message<CraftEvents> startMsg = MessageBuilder.withPayload(CraftEvents.START).build(); |
|
|
Message<CraftEvents> startMsg = MessageBuilder.withPayload(CraftEvents.START).build(); |
|
|
Mono.from(sm.sendEvent(Mono.just(startMsg))).block(); |
|
|
Mono.from(sm.sendEvent(Mono.just(startMsg))).block(); |
|
|
sm.addStateListener(new StateMachineListenerAdapter<>() { |
|
|
sm.addStateListener(new StateMachineListenerAdapter<>() { |
|
@ -43,6 +43,7 @@ public class CraftsContext implements Runnable { |
|
|
Map<String, Object> dataMap = new HashMap<>(); |
|
|
Map<String, Object> dataMap = new HashMap<>(); |
|
|
dataMap.put("heatId", heatId); |
|
|
dataMap.put("heatId", heatId); |
|
|
dataMap.put("event", to.getId()); |
|
|
dataMap.put("event", to.getId()); |
|
|
|
|
|
dataMap.put("index", currentIndex); |
|
|
ws.push(WebSocketMessageType.CRAFTS_STATE, dataMap); |
|
|
ws.push(WebSocketMessageType.CRAFTS_STATE, dataMap); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
@ -52,16 +53,18 @@ public class CraftsContext implements Runnable { |
|
|
public void run() { |
|
|
public void run() { |
|
|
try { |
|
|
try { |
|
|
for (; currentIndex < craftsStepList.size(); currentIndex++) { |
|
|
for (; currentIndex < craftsStepList.size(); currentIndex++) { |
|
|
if (sm.getState().getId() == CraftStates.STOPPED) break; // 如果收到 STOP 事件,直接退出 |
|
|
|
|
|
|
|
|
if (sm.getState().getId() == CraftStates.STOPPED) break; |
|
|
CraftsStep step = craftsStepList.get(currentIndex); |
|
|
CraftsStep step = craftsStepList.get(currentIndex); |
|
|
boolean ok = executeStep(step); |
|
|
boolean ok = executeStep(step); |
|
|
if (!ok) { |
|
|
if (!ok) { |
|
|
Message<CraftEvents> errorMsg = MessageBuilder.withPayload(CraftEvents.ERROR_OCCUR).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(errorMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> errMsg = MessageBuilder.withPayload(CraftEvents.ERROR_OCCUR).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(errMsg))).block(); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
Message<CraftEvents> completeMsg = MessageBuilder.withPayload(CraftEvents.STEP_COMPLETE).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(completeMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> compMsg = MessageBuilder.withPayload(CraftEvents.STEP_COMPLETE).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(compMsg))).block(); |
|
|
|
|
|
|
|
|
|
|
|
// 如果被 PAUSE,阻塞等待 |
|
|
synchronized (this) { |
|
|
synchronized (this) { |
|
|
while (sm.getState().getId() == CraftStates.PAUSED) { |
|
|
while (sm.getState().getId() == CraftStates.PAUSED) { |
|
|
this.wait(); |
|
|
this.wait(); |
|
@ -69,8 +72,9 @@ public class CraftsContext implements Runnable { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (sm.getState().getId() == CraftStates.RUNNING) { |
|
|
if (sm.getState().getId() == CraftStates.RUNNING) { |
|
|
Message<CraftEvents> stopMsg = MessageBuilder.withPayload(CraftEvents.STOP).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(stopMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> finishMsg = MessageBuilder |
|
|
|
|
|
.withPayload(CraftEvents.FINISH).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(finishMsg))).block(); |
|
|
} |
|
|
} |
|
|
} catch (InterruptedException e) { |
|
|
} catch (InterruptedException e) { |
|
|
Message<CraftEvents> stopMsg = MessageBuilder.withPayload(CraftEvents.STOP).build(); |
|
|
Message<CraftEvents> stopMsg = MessageBuilder.withPayload(CraftEvents.STOP).build(); |
|
@ -79,31 +83,35 @@ public class CraftsContext implements Runnable { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private boolean executeStep(CraftsStep step) throws InterruptedException { |
|
|
private boolean executeStep(CraftsStep step) throws InterruptedException { |
|
|
|
|
|
// TODO: 推送单步信息并实际调用设备服务 |
|
|
Map<String, Object> dataMap = new HashMap<>(); |
|
|
Map<String, Object> dataMap = new HashMap<>(); |
|
|
dataMap.put("heatId", heatId); |
|
|
dataMap.put("heatId", heatId); |
|
|
dataMap.put("currentStep", step.getMethod()); |
|
|
dataMap.put("currentStep", step.getMethod()); |
|
|
ws.push(WebSocketMessageType.CRAFTS_STEP, dataMap); |
|
|
ws.push(WebSocketMessageType.CRAFTS_STEP, dataMap); |
|
|
|
|
|
|
|
|
Thread.sleep(3000); |
|
|
Thread.sleep(3000); |
|
|
// TODO: 调用 craftsStepService 或 device 服务执行具体命令 |
|
|
|
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 暂停 |
|
|
public void pause() { |
|
|
public void pause() { |
|
|
Message<CraftEvents> pauseMsg = MessageBuilder.withPayload(CraftEvents.PAUSE).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(pauseMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> msg = MessageBuilder.withPayload(CraftEvents.PAUSE).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(msg))).block(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 恢复 |
|
|
public void resume() { |
|
|
public void resume() { |
|
|
Message<CraftEvents> resumeMsg = MessageBuilder.withPayload(CraftEvents.RESUME).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(resumeMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> msg = MessageBuilder.withPayload(CraftEvents.RESUME).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(msg))).block(); |
|
|
synchronized (this) { |
|
|
synchronized (this) { |
|
|
this.notify(); |
|
|
this.notify(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 用户手动停止 |
|
|
public void stop() { |
|
|
public void stop() { |
|
|
Message<CraftEvents> stopMsg = MessageBuilder.withPayload(CraftEvents.STOP).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(stopMsg))).block(); |
|
|
|
|
|
|
|
|
Message<CraftEvents> msg = MessageBuilder.withPayload(CraftEvents.STOP).build(); |
|
|
|
|
|
Mono.from(sm.sendEvent(Mono.just(msg))).block(); |
|
|
synchronized (this) { |
|
|
synchronized (this) { |
|
|
this.notify(); |
|
|
this.notify(); |
|
|
} |
|
|
} |
|
|