Skip to content

Commit 9d9b269

Browse files
committed
Reference ConfigurationAdmin instead of Cycle for cycle time reading
1 parent 7c5c34b commit 9d9b269

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

io.openems.edge.bridge.modbus/src/io/openems/edge/bridge/modbus/BridgeModbusTcpImpl.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.openems.edge.bridge.modbus;
22

3+
import java.io.IOException;
34
import java.net.InetAddress;
45
import java.net.UnknownHostException;
56
import java.time.LocalDateTime;
@@ -9,8 +10,14 @@
910
import io.openems.edge.bridge.modbus.api.LogVerbosity;
1011
import io.openems.edge.bridge.modbus.api.task.AbstractTask;
1112
import io.openems.edge.common.cycle.Cycle;
13+
import org.osgi.service.cm.ConfigurationAdmin;
1214
import org.osgi.service.component.ComponentContext;
13-
import org.osgi.service.component.annotations.*;
15+
import org.osgi.service.component.annotations.Activate;
16+
import org.osgi.service.component.annotations.Component;
17+
import org.osgi.service.component.annotations.ConfigurationPolicy;
18+
import org.osgi.service.component.annotations.Deactivate;
19+
import org.osgi.service.component.annotations.Modified;
20+
import org.osgi.service.component.annotations.Reference;
1421
import org.osgi.service.event.Event;
1522
import org.osgi.service.event.EventHandler;
1623
import org.osgi.service.event.propertytypes.EventTopics;
@@ -55,8 +62,10 @@ public class BridgeModbusTcpImpl extends AbstractModbusBridge
5562
private int noSkipIdx = 0;
5663
private long cycleIdx = 0;
5764

65+
private int coreCycleTime = 1000;
66+
5867
@Reference
59-
private Cycle cycle;
68+
protected ConfigurationAdmin cm;
6069

6170
public BridgeModbusTcpImpl() {
6271
super(//
@@ -67,26 +76,27 @@ public BridgeModbusTcpImpl() {
6776
}
6877

6978
@Activate
70-
private void activate(ComponentContext context, ConfigTcp config) throws UnknownHostException {
79+
private void activate(ComponentContext context, ConfigTcp config) throws IOException {
7180
super.activate(context, config.id(), config.alias(), config.enabled(), config.logVerbosity(),
7281
config.invalidateElementsAfterReadErrors());
7382
this.applyConfig(config);
7483
}
7584

7685
@Modified
77-
private void modified(ComponentContext context, ConfigTcp config) throws UnknownHostException {
86+
private void modified(ComponentContext context, ConfigTcp config) throws IOException {
7887
super.modified(context, config.id(), config.alias(), config.enabled(), config.logVerbosity(),
7988
config.invalidateElementsAfterReadErrors());
8089
this.applyConfig(config);
8190
this.closeModbusConnection();
8291
}
8392

84-
private void applyConfig(ConfigTcp config) {
93+
private void applyConfig(ConfigTcp config) throws IOException {
8594
this.setIpAddress(InetAddressUtils.parseOrNull(config.ip()));
8695
this.port = config.port();
87-
this.noSkipIdx = (int)Math.ceil(config.intervalBetweenAccesses() * 1.0 / cycle.getCycleTime());
96+
this.coreCycleTime = (int) (Integer) this.cm.getConfiguration("Core.Cycle").getProperties().get("cycleTime");
97+
this.noSkipIdx = (int)Math.ceil(config.intervalBetweenAccesses() * 1.0 / this.coreCycleTime);
8898
this.noSkipIdx = Math.max(this.noSkipIdx, 1);
89-
this.logCycle("applyConfig: cycleTime=" + cycle.getCycleTime()
99+
this.logCycle("applyConfig: cycleTime=" + this.coreCycleTime
90100
+ ", interval=" + config.intervalBetweenAccesses()
91101
+ ", noSkipIdx=" + this.noSkipIdx);
92102
}
@@ -162,11 +172,11 @@ public void handleEvent(Event event) {
162172
}
163173

164174
if (this.isNewCycle(event)) {
165-
shouldSkip = this.cycleIdx % this.noSkipIdx != 0;
166-
this.logCycle("handleEvent: Cycle " + this.cycleIdx + (shouldSkip ? " will" : " won't") + " be skipped");
175+
this.shouldSkip = this.cycleIdx % this.noSkipIdx != 0;
176+
this.logCycle("handleEvent: Cycle " + this.cycleIdx + (this.shouldSkip ? " will" : " won't") + " be skipped");
167177
this.cycleIdx++;
168178
}
169-
if (shouldSkip) {
179+
if (this.shouldSkip) {
170180
return;
171181
}
172182

io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.openems.edge.bridge.modbus;
22

3+
import io.openems.edge.common.test.DummyConfigurationAdmin;
34
import org.junit.Ignore;
45
import org.junit.Test;
56

@@ -25,6 +26,9 @@
2526
import io.openems.edge.common.test.DummyCycle;
2627
import io.openems.edge.common.test.TestUtils;
2728

29+
import java.util.Dictionary;
30+
import java.util.Hashtable;
31+
2832
public class BridgeModbusTcpImplTest {
2933

3034
private static final String MODBUS_ID = "modbus0";
@@ -128,16 +132,20 @@ public void testSkipInterval() throws Exception {
128132
slave.addProcessImage(UNIT_ID, processImage);
129133
slave.open();
130134

131-
int NUM_TESTS, MAX_INTERVAL;
135+
var cm = new DummyConfigurationAdmin();
136+
var cfg = cm.createFactoryConfiguration("Core.Cycle", null);
137+
Dictionary<String, Object> properties = new Hashtable<>();
138+
properties.put("cycleTime", 100);
139+
cfg.update(properties);
132140

133141
// interval = 0, should not change original modbus behavior
134-
NUM_TESTS = 1;
135-
for (int i = 0; i < NUM_TESTS; i++) {
142+
int numTests = 1;
143+
for (int i = 0; i < numTests; i++) {
136144
var sut = new BridgeModbusTcpImpl();
137145
var device = new MyModbusComponent(DEVICE_ID, sut, UNIT_ID);
138146
var test = new ComponentTest(sut) //
139147
.addComponent(device) //
140-
.addReference("cycle", new DummyCycle(CYCLE_TIME));
148+
.addReference("cm", cm);
141149

142150
test.activate(MyConfigTcp.create() //
143151
.setId(MODBUS_ID) //
@@ -167,17 +175,17 @@ public void testSkipInterval() throws Exception {
167175
// Important! Otherwise, new sut cannot connect to the slave (only 1 slave thread)
168176
sut.deactivate();
169177
}
170-
// 0 <= interval < MAX_INTERVAL
171-
NUM_TESTS = 7;
172-
MAX_INTERVAL = CYCLE_TIME * 3;
173-
for (int i = 0; i < NUM_TESTS; i++) {
178+
// 0 <= interval < maxInterval
179+
numTests = 7;
180+
int maxInterval = CYCLE_TIME * 3;
181+
for (int i = 0; i < numTests; i++) {
174182
var sut = new BridgeModbusTcpImpl();
175183
var device = new MyModbusComponent(DEVICE_ID, sut, UNIT_ID);
176184
var test = new ComponentTest(sut) //
177-
.addComponent(device) //
178-
.addReference("cycle", new DummyCycle(CYCLE_TIME));
185+
.addComponent(device)
186+
.addReference("cm", cm);
179187

180-
int interval = MAX_INTERVAL * i / NUM_TESTS;
188+
int interval = maxInterval * i / numTests;
181189
int skips = (int)Math.ceil(interval * 1.0 / CYCLE_TIME) - 1;
182190

183191
System.out.println("Interval=" + interval + ", skips=" + skips);

0 commit comments

Comments
 (0)