Skip to content

Commit

Permalink
Modbus/TCP: Add GridMode to Sum (#204)
Browse files Browse the repository at this point in the history
GridMode can now be read via Modbus Address `417`.

Additionally:
- Add new type `enum16`
- Show `text` of Channels in Description
- Improve texts for Channels

Co-authored-by: FENECON\michael.grill <[email protected]>
Co-authored-by: Stefan Feilmeier <[email protected]>
Reviewed-on: https://git.intranet.fenecon.de/FENECON/fems/pulls/204
Reviewed-by: Stefan Feilmeier <[email protected]>
Co-authored-by: Michael Grill <[email protected]>
Co-committed-by: Michael Grill <[email protected]>
  • Loading branch information
Michael Grill and sfeilmeier committed Mar 3, 2022
1 parent 4e1b267 commit ef17b06
Show file tree
Hide file tree
Showing 21 changed files with 566 additions and 543 deletions.
2 changes: 0 additions & 2 deletions io.openems.common/src/io/openems/common/OpenemsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public class OpenemsConstants {
*/
public static final String MANUFACTURER_EMS_SERIAL_NUMBER = "";

public static final String POWER_DOC_TEXT = "Negative values for Consumption; positive for Production";

/*
* Constants for Component properties
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public interface OptionsEnum {
*
* @return the int representation
*/
int getValue();
public int getValue();

/**
* Gets this enums String representation.
*
* @return the String representation
*/
String getName();
public String getName();

/**
* Gets the enum that is used for 'UNDEFINED' values.
*
* @return the UNDEFINED enum
*/
OptionsEnum getUndefined();
public OptionsEnum getUndefined();

/**
* Gets the name in CamelCase format.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.openems.edge.common.channel;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.openems.common.channel.ChannelCategory;
import io.openems.common.exceptions.OpenemsError;
Expand Down Expand Up @@ -143,4 +145,21 @@ public String getOptionString(Integer value) {
}
return option.getName();
}

@Override
public String getText() {
// If Doc has a Text, return it
var docText = super.getText();
if (!docText.isBlank()) {
return docText;
}
// Otherwise generate Text from options without UNDEFINED option in the form
// "<value>:<name>, <value>:<name>".
return Stream.of(this.options) //
.filter(option -> !option.isUndefined()) //
.map(option -> { //
return option.getValue() + ":" + option.getName(); //
}) //
.collect(Collectors.joining(", "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.osgi.framework.Constants;
import org.osgi.service.cm.Configuration;
Expand Down Expand Up @@ -187,6 +189,12 @@ default <T extends Channel<?>> T channel(io.openems.edge.common.channel.ChannelI
public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
// Running State of the component. Keep values in sync with 'Level' enum!
STATE(new StateCollectorChannelDoc() //
// Set Text to "0:Ok, 1:Info, 2:Warning, 3:Fault"
.text(Stream.of(Level.values()) //
.map(option -> { //
return option.getValue() + ":" + option.getName(); //
}) //
.collect(Collectors.joining(", "))) //
.persistencePriority(PersistencePriority.VERY_HIGH));

private final Doc doc;
Expand All @@ -210,7 +218,7 @@ public Doc doc() {
*/
public static ModbusSlaveNatureTable getModbusSlaveNatureTable(AccessMode accessMode) {
return ModbusSlaveNatureTable.of(OpenemsComponent.class, accessMode, 80) //
.channel(0, ChannelId.STATE, ModbusType.UINT16) //
.channel(0, ChannelId.STATE, ModbusType.ENUM16) //
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.openems.edge.common.modbusslave;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -13,8 +11,6 @@
import io.openems.common.types.ChannelAddress;
import io.openems.edge.common.channel.Channel;
import io.openems.edge.common.channel.ChannelId;
import io.openems.edge.common.channel.Doc;
import io.openems.edge.common.channel.EnumDoc;
import io.openems.edge.common.component.OpenemsComponent;

public class ModbusRecordChannel extends ModbusRecord {
Expand Down Expand Up @@ -49,6 +45,7 @@ public ModbusRecordChannel(int offset, ModbusType type, ChannelId channelId, Acc
case STRING16:
byteLength = ModbusRecordString16.BYTE_LENGTH;
break;
case ENUM16:
case UINT16:
byteLength = ModbusRecordUint16.BYTE_LENGTH;
break;
Expand Down Expand Up @@ -141,6 +138,7 @@ public byte[] getValue(OpenemsComponent component) {
case WRITE_ONLY:
return ModbusRecordString16Reserved.UNDEFINED_VALUE;
}
case ENUM16:
case UINT16:
switch (this.accessMode) {
case READ_ONLY:
Expand Down Expand Up @@ -211,6 +209,8 @@ public void writeValue(OpenemsComponent component, int index, byte byte1, byte b
case STRING16:
value = ""; // TODO implement String conversion
break;

case ENUM16:
case UINT16:
value = buff.getShort();
break;
Expand Down Expand Up @@ -239,18 +239,7 @@ public Unit getUnit() {

@Override
public String getValueDescription() {
Doc doc = this.channelId.doc();
if (doc instanceof EnumDoc) {
// List possible Options for this Enum
EnumDoc d = (EnumDoc) doc;
return Arrays.stream(d.getOptions()) //
.map(option -> {
return option.getValue() + ":" + option.getName();
}) //
.collect(Collectors.joining(", "));
}

return ""; // TODO get some meaningful text from Doc(), like 'between 0 and 100 %'
return this.channelId.doc().getText();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static byte[] toByteArray(Object value) {

@Override
public String getValueDescription() {
return this.value != null ? this.value.toString() : "";
return this.value != null ? "\"" + this.value.toString() + "\"" : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static byte[] toByteArray(Object value) {

@Override
public String getValueDescription() {
return this.value != null ? this.value.toString() : "";
return this.value != null ? "\"" + this.value.toString() + "\"" : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static byte[] toByteArray(Object value) {

@Override
public String getValueDescription() {
return this.value != null ? this.value : "";
return this.value != null ? "\"" + this.value.toString() + "\"" : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static byte[] toByteArray(Object value) {

@Override
public String getValueDescription() {
return this.value != null ? Short.toString(this.value) : "";
return this.value != null ? "\"" + Short.toString(this.value) + "\"" : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String toString() {

@Override
public String getValueDescription() {
return "0x" + Integer.toHexString(this.value & 0xffff);
return "\"0x" + Integer.toHexString(this.value & 0xffff) + "\"";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static byte[] toByteArray(Object value) {

@Override
public String getValueDescription() {
return this.value != null ? Integer.toString(this.value) : "";
return this.value != null ? "\"" + Integer.toString(this.value) + "\"" : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Builder channel(int offset, ChannelId channelId, ModbusType type) {
case STRING16:
this.string16Reserved(offset);
break;
case ENUM16:
case UINT16:
this.uint16Reserved(offset);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.edge.common.modbusslave;

public enum ModbusType {
ENUM16(1, "enum16"), //
UINT16(1, "uint16"), //
UINT32(2, "uint32"), //
FLOAT32(2, "float32"), //
Expand Down
Loading

0 comments on commit ef17b06

Please sign in to comment.