updating (and deleting) KnxGroup from ETS files

This commit is contained in:
Patrick Haßel 2024-11-25 14:25:18 +01:00
parent bad017e1dd
commit 2986238dc8
4 changed files with 31 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.knx.group; package de.ph87.homeautomation.knx.group;
import de.ph87.homeautomation.property.PropertyReader;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
@ -23,6 +24,8 @@ public class KnxGroupImportService {
private final KnxGroupRepository knxGroupRepository; private final KnxGroupRepository knxGroupRepository;
private final PropertyReader propertyReader;
public void importGroups() { public void importGroups() {
try { try {
execute("/usr/bin/git", "fetch", "--all"); execute("/usr/bin/git", "fetch", "--all");
@ -34,6 +37,18 @@ public class KnxGroupImportService {
knxGroupRepository.findAll().forEach(knxGroup -> knxGroup.setEts(false)); knxGroupRepository.findAll().forEach(knxGroup -> knxGroup.setEts(false));
try { try {
Jsoup.parse(new File(ETS_HOME, "G"), "UTF-8").select("GA").forEach(this::importGroup); Jsoup.parse(new File(ETS_HOME, "G"), "UTF-8").select("GA").forEach(this::importGroup);
knxGroupRepository.findAllByEtsFalse().forEach(group -> {
log.warn("Removing obsolete group: {}", group);
propertyReader.findAllByReadChannel_Id(group.getId()).forEach(property -> {
property.setReadChannel(null);
log.warn(" - removed group as Property readChannel: {}", property);
});
propertyReader.findAllByWriteChannel_Id(group.getId()).forEach(property -> {
property.setWriteChannel(null);
log.warn(" - removed group as Property writeChannel: {}", property);
});
knxGroupRepository.delete(group);
});
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to import KnxGroups: {}", e.toString()); log.error("Failed to import KnxGroups: {}", e.toString());
} }
@ -73,7 +88,13 @@ public class KnxGroupImportService {
private void setDpt(final KnxGroup knxGroup, final String dptString) { private void setDpt(final KnxGroup knxGroup, final String dptString) {
final Matcher mainSub = Pattern.compile("^DPST-(?<main>\\d+)-(?<sub>\\d+)$").matcher(dptString); final Matcher mainSub = Pattern.compile("^DPST-(?<main>\\d+)-(?<sub>\\d+)$").matcher(dptString);
if (mainSub.matches()) { if (mainSub.matches()) {
knxGroup.setDptMain(Integer.parseInt(mainSub.group("main"))); final int main = Integer.parseInt(mainSub.group("main"));
if (knxGroup.getDptMain() != main) {
knxGroup.setLastTelegram(null);
knxGroup.setValue(null);
knxGroup.setTimestamp(null);
}
knxGroup.setDptMain(main);
knxGroup.setDptSub(Integer.parseInt(mainSub.group("sub"))); knxGroup.setDptSub(Integer.parseInt(mainSub.group("sub")));
return; return;
} }

View File

@ -1,18 +1,16 @@
package de.ph87.homeautomation.knx.group; package de.ph87.homeautomation.knx.group;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.ListCrudRepository;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface KnxGroupRepository extends CrudRepository<KnxGroup, Long>, JpaSpecificationExecutor<KnxGroup> { public interface KnxGroupRepository extends ListCrudRepository<KnxGroup, Long>, JpaSpecificationExecutor<KnxGroup> {
Optional<KnxGroup> findByAddressRaw(int rawAddress); Optional<KnxGroup> findByAddressRaw(int rawAddress);
List<KnxGroup> findAll();
Optional<KnxGroup> findFirstBySend_NextTimestampNotNullOrderBySend_NextTimestampAsc(); Optional<KnxGroup> findFirstBySend_NextTimestampNotNullOrderBySend_NextTimestampAsc();
Optional<KnxGroup> findFirstByRead_NextTimestampLessThanEqualOrderByRead_NextTimestampAsc(ZonedDateTime timestamp); Optional<KnxGroup> findFirstByRead_NextTimestampLessThanEqualOrderByRead_NextTimestampAsc(ZonedDateTime timestamp);
@ -21,4 +19,6 @@ public interface KnxGroupRepository extends CrudRepository<KnxGroup, Long>, JpaS
List<KnxGroup> findAllByNameContainsIgnoreCaseOrAddressStrContainsIgnoreCase(String name, String addressStr); List<KnxGroup> findAllByNameContainsIgnoreCaseOrAddressStrContainsIgnoreCase(String name, String addressStr);
List<KnxGroup> findAllByEtsFalse();
} }

View File

@ -23,6 +23,10 @@ public class PropertyReader {
return propertyRepository.findAllByReadChannel_Id(readChannelId); return propertyRepository.findAllByReadChannel_Id(readChannelId);
} }
public List<Property> findAllByWriteChannel_Id(final long writeChannelId) {
return propertyRepository.findAllByWriteChannel_Id(writeChannelId);
}
public List<PropertyDto> search(final String term) { public List<PropertyDto> search(final String term) {
return RepositorySearchHelper.search(term, propertyRepository, "title", propertyMapper::toDto); return RepositorySearchHelper.search(term, propertyRepository, "title", propertyMapper::toDto);
} }

View File

@ -12,7 +12,7 @@ public interface PropertyRepository extends JpaRepository<Property, Long>, JpaSp
List<Property> findAllByReadChannel_Id(long readChannelId); List<Property> findAllByReadChannel_Id(long readChannelId);
List<Property> findAllByTitleLikeIgnoreCase(final String like); List<Property> findAllByWriteChannel_Id(final long writeChannelId);
boolean existsByTitle(String title); boolean existsByTitle(String title);