2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.echonetlite.internal;
15 import static org.openhab.binding.echonetlite.internal.EchonetLiteBindingConstants.DEFAULT_POLL_INTERVAL_MS;
16 import static org.openhab.binding.echonetlite.internal.EchonetLiteBindingConstants.DEFAULT_RETRY_TIMEOUT_MS;
18 import java.nio.ByteBuffer;
20 import java.util.function.Consumer;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
25 * @author Michael Barker - Initial contribution
28 public class EchonetProfileNode extends EchonetObject implements EchonetDeviceListener {
30 private final Consumer<EchonetDevice> newDeviceListener;
31 private final EchonetDiscoveryListener echonetDiscoveryListener;
32 private long lastPollMs = 0;
34 public EchonetProfileNode(final InstanceKey instanceKey, Consumer<EchonetDevice> newDeviceListener,
35 EchonetDiscoveryListener echonetDiscoveryListener) {
36 super(instanceKey, Epc.NodeProfile.SELF_NODE_INSTANCE_LIST_S);
37 this.newDeviceListener = newDeviceListener;
38 this.echonetDiscoveryListener = echonetDiscoveryListener;
39 setTimeouts(DEFAULT_POLL_INTERVAL_MS, DEFAULT_RETRY_TIMEOUT_MS);
43 public void applyProperty(InstanceKey sourceInstanceKey, Esv esv, int epcCode, int pdc, ByteBuffer edt) {
44 final Epc epc = Epc.lookup(instanceKey().klass.groupCode(), instanceKey().klass.classCode(), epcCode);
46 if (EchonetClass.NODE_PROFILE == sourceInstanceKey.klass && Epc.NodeProfile.SELF_NODE_INSTANCE_LIST_S == epc) {
47 final int selfNodeInstanceCount = edt.get() & 0xFF;
49 for (int i = 0; i < selfNodeInstanceCount && edt.hasRemaining(); i++) {
50 final byte groupCode = edt.get();
51 final byte classCode = edt.get();
52 final byte instance = edt.get();
53 final EchonetClass itemClass = EchonetClassIndex.INSTANCE.lookup(groupCode, classCode);
55 final InstanceKey newItemKey = new InstanceKey(sourceInstanceKey.address, itemClass, instance);
56 final EchonetDevice discoveredDevice = new EchonetDevice(newItemKey, this);
57 discoveredDevice.setTimeouts(DEFAULT_POLL_INTERVAL_MS, DEFAULT_RETRY_TIMEOUT_MS);
58 newDeviceListener.accept(discoveredDevice);
64 public boolean buildPollMessage(EchonetMessageBuilder messageBuilder, ShortSupplier tidSupplier, long nowMs,
65 InstanceKey managementControllerKey) {
66 boolean result = false;
67 if (lastPollMs + pollIntervalMs <= nowMs) {
68 result = super.buildPollMessage(messageBuilder, tidSupplier, nowMs, managementControllerKey);
79 public void onInitialised(String identifier, InstanceKey instanceKey, Map<String, String> channelIdAndType) {
80 echonetDiscoveryListener.onDeviceFound(identifier, instanceKey);