Reactive changes for samples

- Relates #750
This commit is contained in:
Janne Valkealahti
2019-05-11 09:26:13 +01:00
parent 7932545ea9
commit 64779f4039
22 changed files with 208 additions and 82 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,8 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import reactor.core.publisher.Mono;
@Configuration
public class Application {
@@ -214,7 +216,10 @@ public class Application {
&& context.getEvent() == Events.PLAY
&& context.getTransition().getTarget().getId() == States.CLOSED
&& context.getExtendedState().getVariables().get(Variables.CD) != null) {
context.getStateMachine().sendEvent(Events.PLAY);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PLAY).build()))
.subscribe();
}
}
}
@@ -265,9 +270,11 @@ public class Application {
if (elapsed instanceof Long) {
long e = ((Long)elapsed) + 1000l;
if (e > ((Cd) cd).getTracks()[((Integer) track)].getLength()*1000) {
context.getStateMachine().sendEvent(MessageBuilder
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build());
.setHeader(Headers.TRACKSHIFT.toString(), 1).build()))
.subscribe();
} else {
variables.put(Variables.ELAPSEDTIME, e);
}
@@ -291,7 +298,10 @@ public class Application {
variables.put(Variables.ELAPSEDTIME, 0l);
variables.put(Variables.TRACK, next);
} else if (((Cd)cd).getTracks().length <= next) {
context.getStateMachine().sendEvent(Events.STOP);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.STOP).build()))
.subscribe();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import demo.cdplayer.Application.Headers;
import demo.cdplayer.Application.States;
import demo.cdplayer.Application.StatesOnTransition;
import demo.cdplayer.Application.Variables;
import reactor.core.publisher.Mono;
@WithStateMachine
public class CdPlayer {
@@ -41,37 +42,54 @@ public class CdPlayer {
private String trackStatus = "";
public void load(Cd cd) {
stateMachine.sendEvent(MessageBuilder.withPayload(Events.LOAD).setHeader(Variables.CD.toString(), cd).build());
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.LOAD).setHeader(Variables.CD.toString(), cd).build()))
.subscribe();
}
public void play() {
stateMachine.sendEvent(Events.PLAY);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PLAY).build()))
.subscribe();
}
public void stop() {
stateMachine.sendEvent(Events.STOP);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.STOP).build()))
.subscribe();
}
public void pause() {
stateMachine.sendEvent(Events.PAUSE);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PAUSE).build()))
.subscribe();
}
public void eject() {
stateMachine.sendEvent(Events.EJECT);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.EJECT).build()))
.subscribe();
}
public void forward() {
stateMachine
.sendEvent(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build());
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build()))
.subscribe();
}
public void back() {
stateMachine
.sendEvent(MessageBuilder
.withPayload(Events.BACK)
.setHeader(Headers.TRACKSHIFT.toString(), -1).build());
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.BACK)
.setHeader(Headers.TRACKSHIFT.toString(), -1).build()))
.subscribe();
}
public String getLdcStatus() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.cdplayer;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,14 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.cdplayer.Application.Events;
import demo.cdplayer.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}