add db
This commit is contained in:
parent
5dd568f64a
commit
323c662cbc
@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'
|
|||||||
|
|
||||||
java {
|
java {
|
||||||
toolchain {
|
toolchain {
|
||||||
languageVersion = JavaLanguageVersion.of(21)
|
languageVersion = JavaLanguageVersion.of(22)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +31,8 @@ dependencies {
|
|||||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
// https://mvnrepository.com/artifact/org.springframework.statemachine/spring-statemachine-core
|
// https://mvnrepository.com/artifact/org.springframework.statemachine/spring-statemachine-core
|
||||||
implementation 'org.springframework.statemachine:spring-statemachine-core:4.0.0'
|
implementation 'org.springframework.statemachine:spring-statemachine-core:4.0.0'
|
||||||
|
implementation 'org.jdbi:jdbi3-core:3.49.3'
|
||||||
|
implementation 'com.mysql:mysql-connector-j:9.3.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
package de.w665.pr.statemachine.trafficlight;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
import org.jdbi.v3.core.mapper.EnumMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class StateRepository {
|
||||||
|
|
||||||
|
@Value("${db.address}")
|
||||||
|
private String dbAddress;
|
||||||
|
|
||||||
|
@Value("${db.username}")
|
||||||
|
private String dbUsername;
|
||||||
|
|
||||||
|
@Value("${db.password}")
|
||||||
|
private String dbPassword;
|
||||||
|
|
||||||
|
Jdbi jdbi;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
jdbi = Jdbi.create(dbAddress, dbUsername, dbPassword);
|
||||||
|
|
||||||
|
// Register enum mapper for TrafficLightStates
|
||||||
|
jdbi.registerColumnMapper(TrafficLightStates.class,
|
||||||
|
EnumMapper.byName(TrafficLightStates.class));
|
||||||
|
|
||||||
|
// Create the table if it doesn't exist
|
||||||
|
jdbi.useHandle(handle -> {
|
||||||
|
handle.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS trafficlightstate (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
state VARCHAR(10) NOT NULL,
|
||||||
|
timestamp TIMESTAMP NOT NULL
|
||||||
|
)
|
||||||
|
""");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveTrafficLightState(TrafficLightStates trafficLightState) {
|
||||||
|
jdbi.useHandle(handle -> {
|
||||||
|
handle.createUpdate("INSERT INTO trafficlightstate (state, timestamp) VALUES (:state, :timestamp)")
|
||||||
|
.bind("state", trafficLightState.name())
|
||||||
|
.bind("timestamp", LocalDateTime.now())
|
||||||
|
.execute();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package de.w665.pr.statemachine.trafficlight;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TrafficLightPersistanceService {
|
||||||
|
|
||||||
|
private StateRepository stateRepository;
|
||||||
|
|
||||||
|
public TrafficLightPersistanceService(StateRepository stateRepository) {
|
||||||
|
this.stateRepository = stateRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void persistTrafficLightState(TrafficLightStates trafficLightState) {
|
||||||
|
stateRepository.saveTrafficLightState(trafficLightState);
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ public class TrafficLightStateMachineConfig extends StateMachineConfigurerAdapte
|
|||||||
|
|
||||||
private final MinimumTimePassedGuard minimumTimePassedGuard;
|
private final MinimumTimePassedGuard minimumTimePassedGuard;
|
||||||
private final TimingContext timingContext;
|
private final TimingContext timingContext;
|
||||||
|
private final TrafficLightPersistanceService trafficLightPersistanceService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(StateMachineStateConfigurer<TrafficLightStates, TrafficLightEvents> states) throws Exception{
|
public void configure(StateMachineStateConfigurer<TrafficLightStates, TrafficLightEvents> states) throws Exception{
|
||||||
@ -59,7 +60,11 @@ public class TrafficLightStateMachineConfig extends StateMachineConfigurerAdapte
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Action<TrafficLightStates, TrafficLightEvents> recordTransitionAction() {
|
public Action<TrafficLightStates, TrafficLightEvents> recordTransitionAction() {
|
||||||
return context -> timingContext.recordTransition();
|
return context -> {
|
||||||
|
TrafficLightStates targetState = context.getTarget().getId();
|
||||||
|
trafficLightPersistanceService.persistTrafficLightState(targetState);
|
||||||
|
timingContext.recordTransition();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
spring.application.name=statemachine
|
spring.application.name=statemachine
|
||||||
server.port=80
|
server.port=80
|
||||||
|
|
||||||
|
db.address=jdbc:mysql://hymax.online:3306/jdbitest
|
||||||
|
db.username=rememcloud
|
||||||
|
db.password=WdtIhXPtNawGgq0gt340
|
Loading…
x
Reference in New Issue
Block a user