11package io .openems .edge .bridge .modbus ;
22
3+ import java .io .IOException ;
34import java .net .InetAddress ;
45import java .net .UnknownHostException ;
56import java .time .LocalDateTime ;
910import io .openems .edge .bridge .modbus .api .LogVerbosity ;
1011import io .openems .edge .bridge .modbus .api .task .AbstractTask ;
1112import io .openems .edge .common .cycle .Cycle ;
13+ import org .osgi .service .cm .ConfigurationAdmin ;
1214import 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 ;
1421import org .osgi .service .event .Event ;
1522import org .osgi .service .event .EventHandler ;
1623import 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
0 commit comments