]> git.basschouten.com Git - openhab-addons.git/blob
3e8ce652251cf58564721fa97680b222811c587b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.echonetlite.internal;
14
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;
17
18 import java.nio.ByteBuffer;
19 import java.util.Map;
20 import java.util.function.Consumer;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23
24 /**
25  * @author Michael Barker - Initial contribution
26  */
27 @NonNullByDefault
28 public class EchonetProfileNode extends EchonetObject implements EchonetDeviceListener {
29
30     private final Consumer<EchonetDevice> newDeviceListener;
31     private final EchonetDiscoveryListener echonetDiscoveryListener;
32     private long lastPollMs = 0;
33
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);
40     }
41
42     @Override
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);
45
46         if (EchonetClass.NODE_PROFILE == sourceInstanceKey.klass && Epc.NodeProfile.SELF_NODE_INSTANCE_LIST_S == epc) {
47             final int selfNodeInstanceCount = edt.get() & 0xFF;
48
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);
54
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);
59             }
60         }
61     }
62
63     @Override
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);
69
70             if (result) {
71                 lastPollMs = nowMs;
72             }
73         }
74
75         return result;
76     }
77
78     @Override
79     public void onInitialised(String identifier, InstanceKey instanceKey, Map<String, String> channelIdAndType) {
80         echonetDiscoveryListener.onDeviceFound(identifier, instanceKey);
81     }
82 }