Implemented server shutdown to use in deploy.sh

This commit is contained in:
Patrick Haßel 2021-10-04 14:02:13 +02:00
parent 4d894f2fb4
commit 06099e291f
5 changed files with 94 additions and 2 deletions

View File

@ -3,4 +3,5 @@
cd "$(dirname "$0")" || exit 1
mvn clean package spring-boot:repackage && \
scp target/Homeautomation.jar media@10.0.0.50:/home/media/Homeautomation/Homeautomation.jar
scp target/Homeautomation.jar media@10.0.0.50:/home/media/Homeautomation/Homeautomation.jar && \
curl -m 2 -s http://10.0.0.50:8080/server/shutdown && echo "Server restarting..." || echo "Failed to restart server!"

View File

@ -35,15 +35,28 @@ public class DemoDataService {
@PostConstruct
public void postConstruct() {
final KnxGroupDto eg_flur_licht_schalten = createKnxGroupIfNotExists("EG Flur Licht Schalten", 1294, "1.001", false);
final KnxGroupDto eg_ambiente_schalten = createKnxGroupIfNotExists("EG Ambiente Schalten", 848, "1.001", false);
final KnxGroupDto og_ambiente_schalten = createKnxGroupIfNotExists("OG Ambiente Schalten", 1539, "1.001", false);
final KnxGroupDto wohnzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Wohnzimmer Rollladen Position Anfahren", new GroupAddress(0, 4, 24), "5.001", false);
final KnxGroupDto schlafzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Schlafzimmer Rollladen Position Anfahren", new GroupAddress(0, 3, 3), "5.001", false);
final KnxGroupDto flur_rollladen_position_anfahren = createKnxGroupIfNotExists("Flur Rollladen Position Anfahren", new GroupAddress(0, 5, 13), "5.001", false);
final KnxGroupDto badewanne_schalten = createKnxGroupIfNotExists("Badewanne Schalten", 781, "1.001", false);
final KnxGroupDto badewanne_status = createKnxGroupIfNotExists("Badewanne Status", 782, "1.001", true);
final KnxGroupDto bad_licht_mitteschalten = createKnxGroupIfNotExists("Bad Licht Mitte Schalten", 797, "1.001", false);
final KnxGroupDto helligkeit = createKnxGroupIfNotExists("Helligkeit", 1286, "9.004", false);
final Schedule scheduleEgFlurLicht = new Schedule();
scheduleEgFlurLicht.setName("EG Flur Licht");
createTime(scheduleEgFlurLicht, 11, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "true"));
createTime(scheduleEgFlurLicht, 12, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "false"));
createTime(scheduleEgFlurLicht, 16, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "true"));
createTime(scheduleEgFlurLicht, 17, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "false"));
createTime(scheduleEgFlurLicht, 22, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "true"));
createTime(scheduleEgFlurLicht, 23, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "false"));
createTime(scheduleEgFlurLicht, 1, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "true"));
createTime(scheduleEgFlurLicht, 2, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_flur_licht_schalten.getAddress(), "false"));
scheduleRepository.save(scheduleEgFlurLicht);
final Schedule scheduleEgAmbiente = new Schedule();
scheduleEgAmbiente.setName("EG Ambiente");
createTime(scheduleEgAmbiente, 7, 15, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(eg_ambiente_schalten.getAddress(), "true"));
@ -88,6 +101,18 @@ public class DemoDataService {
createRelative(scheduleBadewanneBlinken, seconds += interval, fuzzy, fuzzy, new PropertyEntry(badewanne_schalten.getAddress(), "true"));
createRelative(scheduleBadewanneBlinken, seconds += interval, fuzzy, fuzzy, new PropertyEntry(badewanne_schalten.getAddress(), "false"));
scheduleRepository.save(scheduleBadewanneBlinken);
final Schedule scheduleBadLichtMitte = new Schedule();
scheduleBadLichtMitte.setName("Bad Licht Mitte");
createTime(scheduleBadLichtMitte, 10, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "true"));
createTime(scheduleBadLichtMitte, 11, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "false"));
createTime(scheduleBadLichtMitte, 15, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "true"));
createTime(scheduleBadLichtMitte, 16, 30, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "false"));
createTime(scheduleBadLichtMitte, 21, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "true"));
createTime(scheduleBadLichtMitte, 22, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "false"));
createTime(scheduleBadLichtMitte, 0, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "true"));
createTime(scheduleBadLichtMitte, 1, 0, 0, MIN30_SEC, MIN30_SEC, new PropertyEntry(bad_licht_mitteschalten.getAddress(), "false"));
scheduleRepository.save(scheduleBadLichtMitte);
}
private KnxGroupDto createKnxGroupIfNotExists(final String name, final int address, final String dpt, final boolean readable) {

View File

@ -0,0 +1,20 @@
package de.ph87.homeautomation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("server")
@RequiredArgsConstructor
public class ServerController {
private final ServerService serverService;
@GetMapping("shutdown")
public void shutdown() {
serverService.shutdown();
}
}

View File

@ -0,0 +1,43 @@
package de.ph87.homeautomation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@EnableAsync
@RequiredArgsConstructor
public class ServerService implements ApplicationContextAware {
private static final int DELAY_SECONDS = 1;
private ConfigurableApplicationContext applicationContext;
@Async
public void shutdown() {
try {
for (int delay = DELAY_SECONDS; delay > 0; delay--) {
log.info("Shutdown in {} second.", delay);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
log.warn("Shutdown interrupted.");
return;
}
applicationContext.close();
System.exit(0);
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) ctx;
}
}

View File

@ -1,6 +1,7 @@
package de.ph87.homeautomation.schedule;
import de.ph87.homeautomation.schedule.entry.ScheduleEntry;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -32,9 +33,11 @@ public class ScheduleController {
.map(schedule -> ScheduleEntryNextDto.create(schedule, now))
.filter(Optional::isPresent)
.map(Optional::get)
.sorted(Comparator.comparing(ScheduleEntryNextDto::getNextTimestamp))
.collect(Collectors.toList());
}
@Getter
static class ScheduleEntryNextDto {
public final String name;