]> git.basschouten.com Git - openhab-addons.git/blob
5dcaa130e4ac3fa9e8f88b5b7fcd127ed4b89386
[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.io.homekit.internal;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import io.github.hapjava.accessories.HomekitAccessory;
24 import io.github.hapjava.server.impl.HomekitRoot;
25
26 /**
27  * Stores the created HomekitAccessories. GroupedAccessories are also held here
28  * in a pre-created pending state until all required characteristics are found.
29  *
30  * @author Andy Lintner - Initial contribution
31  */
32 class HomekitAccessoryRegistry {
33     private @Nullable HomekitRoot bridge;
34     private final Map<String, HomekitAccessory> createdAccessories = new HashMap<>();
35     private int configurationRevision = 1;
36     private final Logger logger = LoggerFactory.getLogger(HomekitAccessoryRegistry.class);
37
38     public void setConfigurationRevision(int revision) {
39         configurationRevision = revision;
40     }
41
42     public int getConfigurationRevision() {
43         return configurationRevision;
44     }
45
46     public int makeNewConfigurationRevision() {
47         configurationRevision = (configurationRevision + 1) % 65535;
48         final HomekitRoot bridge = this.bridge;
49         try {
50             if (bridge != null) {
51                 bridge.setConfigurationIndex(configurationRevision);
52             }
53         } catch (IOException e) {
54             logger.warn("Could not update configuration revision number", e);
55         }
56         return configurationRevision;
57     }
58
59     public synchronized void remove(String itemName) {
60         if (createdAccessories.containsKey(itemName)) {
61             HomekitAccessory accessory = createdAccessories.remove(itemName);
62             logger.trace("Removed accessory {} for taggedItem {}", accessory, itemName);
63             final HomekitRoot bridge = this.bridge;
64             if (bridge != null) {
65                 bridge.removeAccessory(accessory);
66             } else {
67                 logger.warn("trying to remove {} but bridge is null", accessory);
68             }
69         }
70     }
71
72     public synchronized void clear() {
73         final HomekitRoot bridge = this.bridge;
74         if (bridge != null) {
75             createdAccessories.values().forEach(bridge::removeAccessory);
76         } else {
77             logger.warn("trying to clear accessories but bridge is null");
78         }
79     }
80
81     public synchronized void setBridge(HomekitRoot bridge) {
82         this.bridge = bridge;
83         createdAccessories.values().forEach(bridge::addAccessory);
84     }
85
86     public synchronized void unsetBridge() {
87         bridge = null;
88     }
89
90     public synchronized HomekitRoot getBridge() {
91         return bridge;
92     }
93
94     public synchronized void addRootAccessory(String itemName, HomekitAccessory accessory) {
95         createdAccessories.put(itemName, accessory);
96         final HomekitRoot bridge = this.bridge;
97         if (bridge != null) {
98             bridge.addAccessory(accessory);
99         }
100     }
101
102     public Map<String, HomekitAccessory> getAllAccessories() {
103         return this.createdAccessories;
104     }
105 }