Skip to content

Commit

Permalink
Fix and refactor Channel Meta-Info (#1597)
Browse files Browse the repository at this point in the history
The Channel Source and Target information introduced in #1595 was not working, because Read-Write-Channel information would always be considered a 'target' information. This Pull-Request refactors the information to a generic MetaInfo and allows explicit definition of different read- and write-registers for one Channel
  • Loading branch information
sfeilmeier committed Aug 23, 2021
1 parent 2e6d628 commit df89482
Show file tree
Hide file tree
Showing 25 changed files with 493 additions and 261 deletions.
Expand Up @@ -301,8 +301,8 @@ protected BatteryProtection(Battery battery, ChargeMaxCurrentHandler chargeMaxCu
this.chargeMaxCurrentHandler = chargeMaxCurrentHandler;
this.dischargeMaxCurrentHandler = dischargeMaxCurrentHandler;

this.battery.getChargeMaxCurrentChannel().setReadSource("Battery-Protection");
this.battery.getDischargeMaxCurrentChannel().setReadSource("Battery-Protection");
this.battery.getChargeMaxCurrentChannel().setMetaInfo("Battery-Protection");
this.battery.getDischargeMaxCurrentChannel().setMetaInfo("Battery-Protection");
}

/**
Expand Down
Expand Up @@ -265,12 +265,22 @@ public ChannelMapper(T element) {
*/
public ChannelMapper<T> m(io.openems.edge.common.channel.ChannelId channelId,
ElementToChannelConverter converter) {
return this.m(channelId, converter, new ChannelMetaInfo(element.getStartAddress()));
}

/**
* Maps the given element 1-to-1 to the Channel identified by channelId.
*
* @param channelId the Channel-ID
* @param converter the {@link ElementToChannelConverter}
* @param channelMetaInfo an object that holds meta information about the
* Channel
* @return the element parameter
*/
public ChannelMapper<T> m(io.openems.edge.common.channel.ChannelId channelId,
ElementToChannelConverter converter, ChannelMetaInfo channelMetaInfo) {
Channel<?> channel = channel(channelId);
if (channel instanceof WriteChannel<?>) {
((WriteChannel<?>) channel).setWriteTarget(new ModbusChannelMetaInfo(element.getStartAddress()));
} else {
channel.setReadSource(new ModbusChannelMetaInfo(element.getStartAddress()));
}
channel.setMetaInfo(channelMetaInfo);
this.channelMaps.put(channel, converter);
return this;
}
Expand Down Expand Up @@ -399,6 +409,21 @@ protected final <T extends AbstractModbusElement<?>> T m(io.openems.edge.common.
return this.m(channelId, element, ElementToChannelConverter.DIRECT_1_TO_1);
}

/**
* Maps the given element 1-to-1 to the Channel identified by channelId.
*
* @param <T> the type of the {@link AbstractModbusElement}d
* @param channelId the Channel-ID
* @param element the ModbusElement
* @param channelMetaInfo an object that holds meta information about the
* Channel
* @return the element parameter
*/
protected final <T extends AbstractModbusElement<?>> T m(io.openems.edge.common.channel.ChannelId channelId,
T element, ChannelMetaInfo channelMetaInfo) {
return this.m(channelId, element, ElementToChannelConverter.DIRECT_1_TO_1, channelMetaInfo);
}

/**
* Maps the given element to the Channel identified by channelId, applying the
* given @link{ElementToChannelConverter}.
Expand All @@ -416,6 +441,25 @@ protected final <T extends AbstractModbusElement<?>> T m(io.openems.edge.common.
.build();
}

/**
* Maps the given element to the Channel identified by channelId, applying the
* given @link{ElementToChannelConverter}.
*
* @param <T> the type of the {@link AbstractModbusElement}d
* @param channelId the Channel-ID
* @param element the ModbusElement
* @param converter the ElementToChannelConverter
* @param channelMetaInfo an object that holds meta information about the
* Channel
* @return the element parameter
*/
protected final <T extends AbstractModbusElement<?>> T m(io.openems.edge.common.channel.ChannelId channelId,
T element, ElementToChannelConverter converter, ChannelMetaInfo channelMetaInfo) {
return new ChannelMapper<T>(element) //
.m(channelId, converter, channelMetaInfo) //
.build();
}

public enum BitConverter {
DIRECT_1_TO_1, INVERT
}
Expand Down
@@ -0,0 +1,47 @@
package io.openems.edge.bridge.modbus.api;

import java.util.Objects;

/**
* Describes a Channel that has a read- or read-and-write-mapping to one Modbus
* Register.
*/
public class ChannelMetaInfo {

/**
* Holds the Address for Modbus Read Register.
*/
protected final int address;

public ChannelMetaInfo(int address) {
this.address = address;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("0x").append(Integer.toHexString(this.address));
return b.toString();
}

@Override
public int hashCode() {
return Objects.hash(this.address);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ChannelMetaInfo other = (ChannelMetaInfo) obj;
return this.address == other.address;
}

}
Expand Up @@ -2,25 +2,18 @@

import java.util.Objects;

public class ModbusChannelMetaInfo {

/**
* Holds the Start-Address of the Modbus Register.
*/
private final int address;
/**
* Describes a Channel that has a read-mapping to a Modbus Coil.
*/
public class ChannelMetaInfoBit extends ChannelMetaInfo {

/**
* Holds the index of the bit inside the Register if applicable.
*/
private final int bit;

public ModbusChannelMetaInfo(int address) {
this.address = address;
this.bit = -1;
}

public ModbusChannelMetaInfo(int address, int bit) {
this.address = address;
public ChannelMetaInfoBit(int address, int bit) {
super(address);
this.bit = bit;
}

Expand Down Expand Up @@ -50,7 +43,7 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass()) {
return false;
}
ModbusChannelMetaInfo other = (ModbusChannelMetaInfo) obj;
ChannelMetaInfoBit other = (ChannelMetaInfoBit) obj;
return this.address == other.address && this.bit == other.bit;
}

Expand Down
@@ -0,0 +1,49 @@
package io.openems.edge.bridge.modbus.api;

import java.util.Objects;

/**
* Describes a Channel that has a read-and-write-mapping to two Modbus
* Registers.
*/
public class ChannelMetaInfoReadAndWrite extends ChannelMetaInfo {

/**
* Holds the Address for Modbus Write Register.
*/
private final int writeAddress;

public ChannelMetaInfoReadAndWrite(int readAddress, int writeAaddress) {
super(readAddress);
this.writeAddress = writeAaddress;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("READ:0x").append(Integer.toHexString(this.address));
b.append(" | WRITE:0x").append(Integer.toHexString(this.writeAddress));
return b.toString();
}

@Override
public int hashCode() {
return Objects.hash(this.address, this.writeAddress);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ChannelMetaInfoReadAndWrite other = (ChannelMetaInfoReadAndWrite) obj;
return this.address == other.address && this.writeAddress == other.writeAddress;
}

}

0 comments on commit df89482

Please sign in to comment.