Homeautomation/src/main/java/de/ph87/homeautomation/knx/group/KnxGroup.java

85 lines
1.9 KiB
Java

package de.ph87.homeautomation.knx.group;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import tuwien.auto.calimero.GroupAddress;
import javax.persistence.*;
import java.time.Duration;
import java.time.ZonedDateTime;
@Getter
@Setter
@ToString
@Entity
public class KnxGroup {
@Id
@GeneratedValue
@Setter(AccessLevel.NONE)
private Long id;
@Column(unique = true)
private int addressRaw;
@Column(unique = true)
private String addressStr;
private String dpt;
private byte[] value;
private ZonedDateTime valueTimestamp;
private byte[] sendValue;
private int readInterval;
@Embedded
private ComInfo read = new ComInfo();
@Embedded
private ComInfo send = new ComInfo();
public void setAddress(final int rawAddress) {
setAddress(new GroupAddress(rawAddress));
}
public void setAddress(final int main, final int middle, final int sub) {
setAddress(new GroupAddress(main, middle, sub));
}
public void setAddress(final GroupAddress groupAddress) {
this.addressRaw = groupAddress.getRawAddress();
this.addressStr = groupAddress.toString();
}
public GroupAddress getAddress() {
return new GroupAddress(addressRaw);
}
public void setReadInterval(final int readInterval) {
if (readInterval <= 0) {
this.readInterval = 0;
} else {
this.readInterval = readInterval;
if (read.getNextTimestamp() == null || Duration.between(ZonedDateTime.now(), read.getNextTimestamp()).toSeconds() > this.readInterval) {
updateNextReadTimestamp();
}
}
}
public void updateNextReadTimestamp() {
if (read.getErrorCount() == 0) {
if (this.readInterval <= 0) {
read.setNextTimestamp(null);
} else {
read.setNextTimestamp(ZonedDateTime.now().plusSeconds(read.getNextTimestamp() == null ? 0 : this.readInterval));
}
}
}
}