Skip to content

Commit 2b70411

Browse files
committed
Bridge channels to property
1 parent e16f226 commit 2b70411

2 files changed

Lines changed: 252 additions & 294 deletions

File tree

org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/handler/simaticBridgeHandler.java

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.util.ArrayList;
1717
import java.util.concurrent.TimeUnit;
1818

19-
import org.eclipse.jdt.annotation.NonNull;
2019
import org.eclipse.jdt.annotation.NonNullByDefault;
2120
import org.eclipse.jdt.annotation.Nullable;
2221
import org.openhab.binding.simatic.internal.SimaticBindingConstants;
@@ -27,9 +26,6 @@
2726
import org.openhab.binding.simatic.internal.simatic.SimaticTCP200;
2827
import org.openhab.binding.simatic.internal.simatic.SimaticUpdateMode;
2928
import org.openhab.core.library.types.DecimalType;
30-
import org.openhab.core.library.types.QuantityType;
31-
import org.openhab.core.library.types.StringType;
32-
import org.openhab.core.library.unit.Units;
3329
import org.openhab.core.thing.Bridge;
3430
import org.openhab.core.thing.ChannelUID;
3531
import org.openhab.core.thing.Thing;
@@ -57,7 +53,7 @@ public class SimaticBridgeHandler extends BaseBridgeHandler {
5753
public @Nullable SimaticGenericDevice connection = null;
5854

5955
// bridge channels
60-
private @Nullable ChannelUID chVersion, chPduSize, chAreasCount, chAreas, chTagCount, chRequests, chBytes;
56+
private @Nullable ChannelUID chTagCount, chRequests, chBytes;
6157

6258
private int channelCount = 0;
6359
/** Initial scheduler delay */
@@ -74,15 +70,7 @@ public SimaticBridgeHandler(Bridge bridge) {
7470

7571
// retrieve bridge channels
7672
getThing().getChannels().forEach((channel) -> {
77-
if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_VERSION)) {
78-
chVersion = channel.getUID();
79-
} else if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_PDU_SIZE)) {
80-
chPduSize = channel.getUID();
81-
} else if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_AREAS_COUNT)) {
82-
chAreasCount = channel.getUID();
83-
} else if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_AREAS)) {
84-
chAreas = channel.getUID();
85-
} else if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_TAG_COUNT)) {
73+
if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_TAG_COUNT)) {
8674
chTagCount = channel.getUID();
8775
} else if (channel.getChannelTypeUID().equals(SimaticBindingConstants.CHANNEL_TYPE_REQUESTS)) {
8876
chRequests = channel.getUID();
@@ -95,7 +83,7 @@ public SimaticBridgeHandler(Bridge bridge) {
9583
@SuppressWarnings("null")
9684
@Override
9785
public void initialize() {
98-
updateState(chVersion, new StringType(SimaticBindingConstants.VERSION));
86+
updateProperty(SimaticBindingConstants.PROPERTY_VERSION, SimaticBindingConstants.VERSION);
9987

10088
config = getConfigAs(SimaticBridgeConfiguration.class);
10189

@@ -174,10 +162,11 @@ public void initialize() {
174162
// react on connection changes
175163
connection.onConnectionChanged((connected) -> {
176164
if (connected) {
177-
updateState(chPduSize, new DecimalType((Number) connection.getPduSize()));
178-
updateState(chAreasCount, new DecimalType((Number) connection.getReadAreas().size()));
179-
updateState(chAreas, new StringType(
180-
(connection.getReadAreas().size() == 0) ? "none" : connection.getReadAreas().toString()));
165+
updateProperty(SimaticBindingConstants.PROPERTY_PDU, String.valueOf(connection.getPduSize()));
166+
updateProperty(SimaticBindingConstants.PROPERTY_AREAS_COUNT,
167+
String.valueOf(connection.getReadAreas().size()));
168+
updateProperty(SimaticBindingConstants.PROPERTY_AREAS,
169+
(connection.getReadAreas().size() == 0) ? "none" : connection.getReadAreas().toString());
181170

182171
updateStatus(ThingStatus.ONLINE);
183172
} else {
@@ -186,8 +175,8 @@ public void initialize() {
186175
});
187176

188177
connection.onMetricsUpdated((requests, bytes) -> {
189-
updateState(chRequests, new DecimalType((Number) requests));
190-
updateState(chBytes, new DecimalType((Number) bytes));
178+
updateState(chRequests, new DecimalType(requests));
179+
updateState(chBytes, new DecimalType(bytes));
191180
});
192181

193182
// temporarily status
@@ -221,30 +210,14 @@ public void dispose() {
221210
logger.debug("{} - bridge has been stopped", getThing().getLabel());
222211
}
223212

224-
@SuppressWarnings("null")
225213
@Override
226214
public void handleCommand(ChannelUID channelUID, Command command) {
227215
logger.debug("{} - Command {} for channel {}", thing.getLabel(), command, channelUID);
228216

229217
// get cached values
230218
if (command instanceof RefreshType) {
231-
if (channelUID.equals(chVersion)) {
232-
updateState(channelUID, new StringType(SimaticBindingConstants.VERSION));
233-
} else if (channelUID.equals(chPduSize)) {
234-
if (connection != null && connection.isConnected()) {
235-
updateState(chPduSize, new QuantityType<>(connection.getPduSize(), Units.BYTE));
236-
}
237-
} else if (channelUID.equals(chAreas)) {
238-
if (connection != null && connection.isConnected()) {
239-
updateState(chAreas, new StringType(
240-
(connection.getReadAreas().size() == 0) ? "none" : connection.getReadAreas().toString()));
241-
}
242-
} else if (channelUID.equals(chAreasCount)) {
243-
if (connection != null && connection.isConnected()) {
244-
updateState(chAreasCount, new DecimalType((Number) connection.getReadAreas().size()));
245-
}
246-
} else if (channelUID.equals(chTagCount)) {
247-
updateState(channelUID, new DecimalType((Number) channelCount));
219+
if (channelUID.equals(chTagCount)) {
220+
updateState(channelUID, new DecimalType(channelCount));
248221
}
249222
}
250223
}
@@ -274,7 +247,7 @@ public void updateConfig() {
274247
}
275248
}
276249

277-
var stateItems = new ArrayList<@NonNull SimaticChannel>(stateChannelCount);
250+
var stateItems = new ArrayList<SimaticChannel>(stateChannelCount);
278251

279252
for (Thing th : getThing().getThings()) {
280253
var h = ((SimaticGenericHandler) th.getHandler());
@@ -291,15 +264,9 @@ public void updateConfig() {
291264
if (connection != null) {
292265
var c = connection;
293266
c.setDataAreas(stateItems);
294-
295-
if (c.isConnected()) {
296-
updateState(chAreasCount, new DecimalType((Number) c.getReadAreas().size()));
297-
updateState(chAreas,
298-
new StringType((c.getReadAreas().size() == 0) ? "none" : c.getReadAreas().toString()));
299-
}
300267
}
301268

302-
updateState(chTagCount, new DecimalType((Number) channelCount));
269+
updateState(chTagCount, new DecimalType(channelCount));
303270

304271
logger.debug("{} - updating {} channels({} read)", getThing().getLabel(), channelCount, stateChannelCount);
305272
}

0 commit comments

Comments
 (0)