deleted Logic
This commit is contained in:
parent
223e9baf4a
commit
818145e38b
@ -1,50 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import de.ph87.homeautomation.channel.Channel;
|
||||
import de.ph87.homeautomation.channel.IChannelOwner;
|
||||
import de.ph87.homeautomation.property.Property;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
public class Logic extends Channel {
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
private LogicOperator operator;
|
||||
|
||||
@ManyToMany
|
||||
@ToString.Exclude
|
||||
private Set<Property> properties = new HashSet<>();
|
||||
|
||||
@Column(name = "value_")
|
||||
private Double value;
|
||||
|
||||
private ZonedDateTime timestamp;
|
||||
|
||||
public Logic(final String name, final LogicOperator operator, final HashSet<Property> properties) {
|
||||
this.name = name;
|
||||
this.operator = operator;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends IChannelOwner> getChannelOwnerClass() {
|
||||
return LogicChannelOwner.class;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import de.ph87.homeautomation.channel.Channel;
|
||||
import de.ph87.homeautomation.channel.IChannelOwner;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class LogicChannelOwner implements IChannelOwner {
|
||||
|
||||
private final LogicReader logicReader;
|
||||
|
||||
private final LogicWriter logicWriter;
|
||||
|
||||
@Override
|
||||
public void requestUpdate(final Channel channel) {
|
||||
logicWriter.update((Logic) channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final long id, final double value) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LogicDto toDto(final long id) {
|
||||
return logicReader.toDto(logicReader.getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogicDto> findAllDto() {
|
||||
return logicReader.findAllDto();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogicDto> findAllDtoLikeIgnoreCase(final String like) {
|
||||
return logicReader.findAllDtoLike(like);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import de.ph87.homeautomation.channel.ChannelDto;
|
||||
import de.ph87.homeautomation.property.Property;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class LogicDto extends ChannelDto {
|
||||
|
||||
private LogicOperator operator;
|
||||
|
||||
private Set<Long> propertyIds;
|
||||
|
||||
public LogicDto(final Logic logic) {
|
||||
super(logic);
|
||||
this.operator = logic.getOperator();
|
||||
this.propertyIds = logic.getProperties().stream().map(Property::getId).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
public enum LogicOperator {
|
||||
OR, AND, XOR
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class LogicReader {
|
||||
|
||||
private final LogicRepository logicRepository;
|
||||
|
||||
public List<LogicDto> findAllDto() {
|
||||
return logicRepository.findAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
public LogicDto toDto(final Logic logic) {
|
||||
return new LogicDto(logic);
|
||||
}
|
||||
|
||||
public List<LogicDto> findAllDtoLike(final String like) {
|
||||
return logicRepository.findAllByNameContainsIgnoreCase(like).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
public List<Logic> findAllByPropertyId(final long id) {
|
||||
return logicRepository.findAllByPropertyName(id);
|
||||
}
|
||||
|
||||
public Logic getById(final long id) {
|
||||
return logicRepository.findById(id).orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface LogicRepository extends CrudRepository<Logic, Long> {
|
||||
|
||||
List<Logic> findAll();
|
||||
|
||||
List<Logic> findAllByNameContainsIgnoreCase(String like);
|
||||
|
||||
@Query("select l from Logic l join l.properties p where p.id = :id")
|
||||
List<Logic> findAllByPropertyName(long id);
|
||||
|
||||
Optional<Logic> findByName(String name);
|
||||
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
package de.ph87.homeautomation.logic;
|
||||
|
||||
import de.ph87.homeautomation.channel.ChannelDto;
|
||||
import de.ph87.homeautomation.property.Property;
|
||||
import de.ph87.homeautomation.property.PropertyDto;
|
||||
import de.ph87.homeautomation.web.WebSocketService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.event.TransactionPhase;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
import static de.ph87.homeautomation.shared.Helpers.getCurrentTransactionName;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class LogicWriter {
|
||||
|
||||
private final LogicReader logicReader;
|
||||
|
||||
private final ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
private final WebSocketService webSocketService;
|
||||
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
|
||||
public void onPropertyChanged(final PropertyDto dto) {
|
||||
log.debug("Listen [{}]: {}", getCurrentTransactionName(), dto.getTitle());
|
||||
logicReader.findAllByPropertyId(dto.getId()).forEach(this::update);
|
||||
}
|
||||
|
||||
public void update(final Logic logic) {
|
||||
final Double value = evaluate(logic);
|
||||
if (!Objects.equals(logic.getValue(), value)) {
|
||||
logic.setValue(value);
|
||||
logic.setTimestamp(ZonedDateTime.now());
|
||||
log.info("Logic changed: {}", logic);
|
||||
publish(logic, true);
|
||||
}
|
||||
}
|
||||
|
||||
private Double evaluate(final Logic logic) {
|
||||
if (logic.getProperties().stream().map(Property::getValue).anyMatch(Objects::isNull)) {
|
||||
return null;
|
||||
}
|
||||
switch (logic.getOperator()) {
|
||||
case OR:
|
||||
return logic.getProperties().stream().map(Property::getValue).anyMatch(v -> v > 0) ? 1.0 : 0.0;
|
||||
case AND:
|
||||
return logic.getProperties().stream().map(Property::getValue).allMatch(v -> v > 0) ? 1.0 : 0.0;
|
||||
case XOR:
|
||||
return (double) (logic.getProperties().stream().map(Property::getValue).map(v -> v > 0).count() % 2);
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
private void publish(final Logic logic, final boolean existing) {
|
||||
final LogicDto dto = logicReader.toDto(logic);
|
||||
log.debug("Publish [{}]: {}", getCurrentTransactionName(), dto.getTitle());
|
||||
applicationEventPublisher.publishEvent(dto);
|
||||
webSocketService.send(ChannelDto.class, dto, existing);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user