-
Notifications
You must be signed in to change notification settings - Fork 563
Implement OpenDTU meter #3562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sthhs
wants to merge
18
commits into
OpenEMS:develop
Choose a base branch
from
sthhs:OpenDTUMeter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+551
−0
Open
Implement OpenDTU meter #3562
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
44dff64
first
c4d943c
Implement ModbusSlave
90adcbc
gitignore
24e64d3
Merge branch 'develop' of https://github.com/sthhs/openems into develop
c16dff6
Merge branch 'develop' of https://github.com/sthhs/openems into develop
78a2d86
Merge branch 'develop' into OpenDTUMeter
cef9d84
Merge branch 'develop' of https://github.com/sthhs/openems into develop
b2bc2df
Merge branch 'develop' into OpenDTUMeter
8bbbc50
Merge branch 'develop' into OpenDTUMeter
56266e9
update usage of HTTP Bridge
753cf63
fix comment
66464bb
Merge branch 'develop' into OpenDTUMeter
cc76215
gitignore
26c6e84
Remove comment
2964649
gitignore
a32e964
config typos, fix meter type
ad13bd7
comment removed, fixed meter type
778355c
Unit test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <classpath> | ||
| <classpathentry kind="con" path="aQute.bnd.classpath.container"/> | ||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/> | ||
| <classpathentry kind="src" output="bin" path="src"/> | ||
| <classpathentry kind="src" output="bin_test" path="test"> | ||
| <attributes> | ||
| <attribute name="test" value="true"/> | ||
| </attributes> | ||
| </classpathentry> | ||
| <classpathentry kind="output" path="bin"/> | ||
| </classpath> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <projectDescription> | ||
| <name>io.openems.edge.meter.opendtu</name> | ||
| <comment></comment> | ||
| <projects> | ||
| </projects> | ||
| <buildSpec> | ||
| <buildCommand> | ||
| <name>org.eclipse.jdt.core.javabuilder</name> | ||
| <arguments> | ||
| </arguments> | ||
| </buildCommand> | ||
| <buildCommand> | ||
| <name>bndtools.core.bndbuilder</name> | ||
| <arguments> | ||
| </arguments> | ||
| </buildCommand> | ||
| </buildSpec> | ||
| <natures> | ||
| <nature>org.eclipse.jdt.core.javanature</nature> | ||
| <nature>bndtools.core.bndnature</nature> | ||
| </natures> | ||
| </projectDescription> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Bundle-Name: OpenEMS Edge Meter OpenDTU | ||
| Bundle-Vendor: | ||
| Bundle-License: https://opensource.org/licenses/EPL-2.0 | ||
| Bundle-Version: 1.0.0.${tstamp} | ||
|
|
||
| -buildpath: \ | ||
| ${buildpath},\ | ||
| io.openems.common,\ | ||
| io.openems.edge.bridge.http,\ | ||
| io.openems.edge.common,\ | ||
| io.openems.edge.meter.api,\ | ||
| io.openems.edge.timedata.api,\ | ||
| io.openems.common.bridge.http | ||
| -testpath: \ | ||
| ${testpath} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| = OpenDTU Meter | ||
|
|
||
| This bundle implements requesting power, voltage and current from an OpenDTU. |
36 changes: 36 additions & 0 deletions
36
io.openems.edge.meter.opendtu/src/io/openems/edge/meter/opendtu/Config.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package io.openems.edge.meter.opendtu; | ||
|
|
||
| import org.osgi.service.metatype.annotations.AttributeDefinition; | ||
| import org.osgi.service.metatype.annotations.ObjectClassDefinition; | ||
|
|
||
| import io.openems.common.types.MeterType; | ||
| import io.openems.edge.common.type.Phase.SinglePhase; | ||
|
|
||
| @ObjectClassDefinition(name = "Meter OpenDTU", // | ||
| description = "Implements the metering component for OpenDTU via HTTP API") | ||
| @interface Config { | ||
|
|
||
| @AttributeDefinition(name = "Component-ID", description = "Unique ID of this Component") | ||
| String id() default "meterOpenDTU0"; | ||
|
|
||
| @AttributeDefinition(name = "Alias", description = "Human-readable name of this Component; defaults to Component-ID") | ||
| String alias() default ""; | ||
|
|
||
| @AttributeDefinition(name = "Is enabled?", description = "Is this Component enabled?") | ||
| boolean enabled() default true; | ||
|
|
||
| @AttributeDefinition(name = "Phase", description = "Which Phase is measured by this Meter?") | ||
| SinglePhase phase() default SinglePhase.L1; | ||
|
|
||
| @AttributeDefinition(name = "Meter-Type", description = "Grid, Production (=default), Consumption") | ||
sthhs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MeterType type() default MeterType.PRODUCTION; | ||
|
|
||
| @AttributeDefinition(name = "IP-Address", description = "The IP address of the OpenWB.") | ||
sthhs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String ipAddress(); | ||
|
|
||
| @AttributeDefinition(name = "Inverter Serial Number", description = "Serial Number of the Inverter") | ||
| String serialNumber() default ""; | ||
|
|
||
| String webconsole_configurationFactory_nameHint() default "Meter OpenDTU[{id}]"; | ||
|
|
||
| } | ||
72 changes: 72 additions & 0 deletions
72
io.openems.edge.meter.opendtu/src/io/openems/edge/meter/opendtu/MeterOpenDtu.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package io.openems.edge.meter.opendtu; | ||
|
|
||
| import io.openems.common.channel.Level; | ||
| import io.openems.edge.common.channel.Doc; | ||
| import io.openems.edge.common.channel.StateChannel; | ||
| import io.openems.edge.common.channel.value.Value; | ||
| import io.openems.edge.common.component.OpenemsComponent; | ||
| import io.openems.edge.meter.api.ElectricityMeter; | ||
| import io.openems.edge.meter.api.SinglePhaseMeter; | ||
|
|
||
| public interface MeterOpenDtu extends ElectricityMeter, SinglePhaseMeter, OpenemsComponent { | ||
|
|
||
| public enum ChannelId implements io.openems.edge.common.channel.ChannelId { | ||
|
|
||
| /** | ||
| * Slave Communication Failed Fault. | ||
| * | ||
| * <p> | ||
| * Indicates a failure in communication with a slave device, which might affect | ||
| * system operations. | ||
| * | ||
| * <ul> | ||
| * <li>Interface: MeterOpenDtu | ||
| * <li>Type: State | ||
| * </ul> | ||
| */ | ||
| SLAVE_COMMUNICATION_FAILED(Doc.of(Level.FAULT) // | ||
| .text("Communication with slave device failed.")); | ||
|
|
||
| private final Doc doc; | ||
|
|
||
| private ChannelId(Doc doc) { | ||
| this.doc = doc; | ||
| } | ||
|
|
||
| @Override | ||
| public Doc doc() { | ||
| return this.doc; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the Channel for {@link ChannelId#SLAVE_COMMUNICATION_FAILED}. | ||
| * | ||
| * @return the StateChannel representing communication failure with a slave | ||
| * device. | ||
| */ | ||
| public default StateChannel getSlaveCommunicationFailedChannel() { | ||
| return this.channel(ChannelId.SLAVE_COMMUNICATION_FAILED); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current state of the Slave Communication Failed channel. | ||
| * | ||
| * @return the Channel {@link Value} indicating whether communication has | ||
| * failed. | ||
| */ | ||
| public default Value<Boolean> getSlaveCommunicationFailed() { | ||
| return this.getSlaveCommunicationFailedChannel().value(); | ||
| } | ||
|
|
||
| /** | ||
| * Internal method to set the 'nextValue' on | ||
| * {@link ChannelId#SLAVE_COMMUNICATION_FAILED} Channel. | ||
| * | ||
| * @param value the next value indicating communication failure state. | ||
| */ | ||
| public default void _setSlaveCommunicationFailed(boolean value) { | ||
| this.getSlaveCommunicationFailedChannel().setNextValue(value); | ||
| } | ||
|
|
||
| } |
222 changes: 222 additions & 0 deletions
222
io.openems.edge.meter.opendtu/src/io/openems/edge/meter/opendtu/MeterOpenDtuImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| package io.openems.edge.meter.opendtu; | ||
|
|
||
| import static io.openems.common.utils.JsonUtils.getAsJsonObject; | ||
| import static io.openems.edge.common.channel.ChannelUtils.setValue; | ||
| import static org.osgi.service.component.annotations.ReferenceCardinality.MANDATORY; | ||
|
|
||
| import java.security.KeyManagementException; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| import org.osgi.service.component.ComponentContext; | ||
| import org.osgi.service.component.annotations.Activate; | ||
| import org.osgi.service.component.annotations.Component; | ||
| import org.osgi.service.component.annotations.ConfigurationPolicy; | ||
| import org.osgi.service.component.annotations.Deactivate; | ||
| import org.osgi.service.component.annotations.Reference; | ||
| import org.osgi.service.component.annotations.ReferenceCardinality; | ||
| import org.osgi.service.component.annotations.ReferencePolicy; | ||
| import org.osgi.service.component.annotations.ReferencePolicyOption; | ||
| import org.osgi.service.event.Event; | ||
| import org.osgi.service.event.EventHandler; | ||
| import org.osgi.service.event.propertytypes.EventTopics; | ||
| import org.osgi.service.metatype.annotations.Designate; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
|
|
||
| import io.openems.common.channel.AccessMode; | ||
| import io.openems.common.exceptions.InvalidValueException; | ||
| import io.openems.common.exceptions.OpenemsException; | ||
| import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; | ||
| import io.openems.common.types.MeterType; | ||
| import io.openems.common.utils.JsonUtils; | ||
| import io.openems.edge.bridge.http.cycle.HttpBridgeCycleServiceDefinition; | ||
|
|
||
| import io.openems.common.bridge.http.api.BridgeHttp; | ||
| import io.openems.common.bridge.http.api.BridgeHttpFactory; | ||
| import io.openems.common.bridge.http.api.HttpResponse; | ||
|
|
||
| import io.openems.edge.common.component.AbstractOpenemsComponent; | ||
| import io.openems.edge.common.component.OpenemsComponent; | ||
| import io.openems.edge.common.event.EdgeEventConstants; | ||
| import io.openems.edge.common.modbusslave.ModbusSlave; | ||
| import io.openems.edge.common.modbusslave.ModbusSlaveNatureTable; | ||
| import io.openems.edge.common.modbusslave.ModbusSlaveTable; | ||
| import io.openems.edge.common.type.Phase.SinglePhase; | ||
| import io.openems.edge.meter.api.ElectricityMeter; | ||
| import io.openems.edge.meter.api.SinglePhaseMeter; | ||
| import io.openems.edge.timedata.api.Timedata; | ||
| import io.openems.edge.timedata.api.TimedataProvider; | ||
| import io.openems.edge.timedata.api.utils.CalculateEnergyFromPower; | ||
|
|
||
| import static io.openems.common.utils.JsonUtils.getAsFloat; | ||
| import static java.lang.Math.round; | ||
|
|
||
| @Designate(ocd = Config.class, factory = true) | ||
| @Component(// | ||
| name = "Meter.OpenDTU", // | ||
| immediate = true, // | ||
| configurationPolicy = ConfigurationPolicy.REQUIRE // | ||
| ) | ||
| @EventTopics({ // | ||
| EdgeEventConstants.TOPIC_CYCLE_AFTER_PROCESS_IMAGE, // | ||
| EdgeEventConstants.TOPIC_CYCLE_EXECUTE_WRITE // | ||
| }) | ||
| public class MeterOpenDtuImpl extends AbstractOpenemsComponent | ||
| implements MeterOpenDtu, ElectricityMeter, SinglePhaseMeter, OpenemsComponent, TimedataProvider, EventHandler, ModbusSlave { | ||
|
|
||
| private final CalculateEnergyFromPower calculateProductionEnergy = new CalculateEnergyFromPower(this, | ||
| ElectricityMeter.ChannelId.ACTIVE_PRODUCTION_ENERGY); | ||
| private final CalculateEnergyFromPower calculateConsumptionEnergy = new CalculateEnergyFromPower(this, | ||
| ElectricityMeter.ChannelId.ACTIVE_CONSUMPTION_ENERGY); | ||
|
|
||
|
|
||
| private final Logger log = LoggerFactory.getLogger(MeterOpenDtuImpl.class); | ||
|
|
||
| private String baseUrl; | ||
| private Config config; | ||
|
|
||
| @Reference(cardinality = MANDATORY) | ||
| private BridgeHttpFactory httpBridgeFactory; | ||
| private BridgeHttp httpBridge; | ||
| @Reference | ||
| private HttpBridgeCycleServiceDefinition httpBridgeCycleServiceDefinition; | ||
|
|
||
| @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.OPTIONAL) | ||
| private volatile Timedata timedata; | ||
|
|
||
| public MeterOpenDtuImpl() { | ||
| super(// | ||
| OpenemsComponent.ChannelId.values(), // | ||
| ElectricityMeter.ChannelId.values(), // | ||
| MeterOpenDtu.ChannelId.values() // | ||
| ); | ||
|
|
||
| SinglePhaseMeter.calculateSinglePhaseFromActivePower(this); | ||
| SinglePhaseMeter.calculateSinglePhaseFromCurrent(this); | ||
| SinglePhaseMeter.calculateSinglePhaseFromVoltage(this); | ||
|
|
||
| } | ||
|
|
||
| @Activate | ||
| protected void activate(ComponentContext context, Config config) throws InvalidValueException, KeyManagementException, NoSuchAlgorithmException, OpenemsException { | ||
| super.activate(context, config.id(), config.alias(), config.enabled()); | ||
| this.config = config; | ||
|
|
||
| this.baseUrl = "http://" + config.ipAddress(); | ||
| this.httpBridge = this.httpBridgeFactory.get(); | ||
|
|
||
| if (!this.isEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| final var cycleService = this.httpBridge.createService(this.httpBridgeCycleServiceDefinition); | ||
| cycleService.subscribeJsonEveryCycle(this.baseUrl + "/api/livedata/status?inv=" + config.serialNumber(), this::processHttpResult); | ||
| } | ||
|
|
||
| @Deactivate | ||
| protected void deactivate() { | ||
| if (this.httpBridge != null) { | ||
| this.httpBridgeFactory.unget(this.httpBridge); | ||
| this.httpBridge = null; | ||
| } | ||
| super.deactivate(); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String debugLog() { | ||
| return this.getPhase() + ":" + this.getActivePower().asString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleEvent(Event event) { | ||
| if (!this.isEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| switch (event.getTopic()) { | ||
| case EdgeEventConstants.TOPIC_CYCLE_AFTER_PROCESS_IMAGE: // | ||
| this.calculateEnergy(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private void processHttpResult(HttpResponse<JsonElement> result, Throwable error) { | ||
| setValue(this, MeterOpenDtu.ChannelId.SLAVE_COMMUNICATION_FAILED, result == null); | ||
|
|
||
| Integer power = null; | ||
| Integer voltage = null; | ||
| Integer current = null; | ||
| //boolean restartRequired = false; | ||
sthhs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (error != null) { | ||
| this.logDebug(this.log, error.getMessage()); | ||
|
|
||
| } else { | ||
| try { | ||
| var jsonResponse = getAsJsonObject(result.data()); | ||
| var inverters = JsonUtils.getAsJsonArray(jsonResponse, "inverters"); | ||
| var inverter = inverters.get(0).getAsJsonObject(); | ||
| var ac = inverter.getAsJsonObject("AC"); | ||
| var ac0 = ac.getAsJsonObject("0"); | ||
|
|
||
| power = round(getAsFloat(ac0.getAsJsonObject("Power"), "v")); | ||
| voltage = round(getAsFloat(ac0.getAsJsonObject("Voltage"), "v") * 1000); | ||
| current = round(getAsFloat(ac0.getAsJsonObject("Current"), "v") * 1000); | ||
|
|
||
| } catch (OpenemsNamedException e) { | ||
| this.logDebug(this.log, e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| this._setActivePower(power); | ||
| this._setCurrent(current); | ||
| this._setVoltage(voltage); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Calculate the Energy values from ActivePower. | ||
| */ | ||
| private void calculateEnergy() { | ||
| // Calculate Energy | ||
| final var activePower = this.getActivePower().get(); | ||
| if (activePower == null) { | ||
| this.calculateProductionEnergy.update(null); | ||
| this.calculateConsumptionEnergy.update(null); | ||
| } else if (activePower >= 0) { | ||
| this.calculateProductionEnergy.update(activePower); | ||
| this.calculateConsumptionEnergy.update(0); | ||
| } else { | ||
| this.calculateProductionEnergy.update(0); | ||
| this.calculateConsumptionEnergy.update(-activePower); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Timedata getTimedata() { | ||
| return this.timedata; | ||
| } | ||
|
|
||
| @Override | ||
| public MeterType getMeterType() { | ||
| return this.config.type(); | ||
sthhs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public SinglePhase getPhase() { | ||
| return this.config.phase(); | ||
| } | ||
|
|
||
| @Override | ||
| public ModbusSlaveTable getModbusSlaveTable(AccessMode accessMode) { | ||
| return new ModbusSlaveTable(// | ||
| OpenemsComponent.getModbusSlaveNatureTable(accessMode), // | ||
| ElectricityMeter.getModbusSlaveNatureTable(accessMode), // | ||
| ModbusSlaveNatureTable.of(MeterOpenDtu.class, accessMode, 100) // | ||
| .build()); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.