-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[openwebnet] add support for lights general/area/group events and Thi…
…ngs (#15921) * [openwebnet] initial support for general/area light events Signed-off-by: Massimo Valla <mvcode00@gmail.com>
- Loading branch information
Showing
20 changed files
with
731 additions
and
84 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
.../src/main/java/org/openhab/binding/openwebnet/internal/handler/LightAutomHandlersMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.openwebnet.internal.handler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openwebnet4j.message.WhereLightAutom; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A map to store handlers for lights and automations. The map is organised by | ||
* Area. | ||
* | ||
* @author Massimo Valla - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class LightAutomHandlersMap { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(LightAutomHandlersMap.class); | ||
|
||
private Map<Integer, Map<String, OpenWebNetThingHandler>> hndlrsMap; | ||
private @Nullable OpenWebNetThingHandler oneHandler = null; | ||
|
||
protected LightAutomHandlersMap() { | ||
hndlrsMap = new ConcurrentHashMap<>(); | ||
} | ||
|
||
protected void add(int area, OpenWebNetThingHandler handler) { | ||
if (!hndlrsMap.containsKey(area)) { | ||
hndlrsMap.put(area, new ConcurrentHashMap<>()); | ||
} | ||
Map<String, OpenWebNetThingHandler> areaHndlrs = hndlrsMap.get(Integer.valueOf(area)); | ||
final String handlerOwnId = handler.ownId; | ||
if (areaHndlrs != null && handlerOwnId != null) { | ||
areaHndlrs.put(handlerOwnId, handler); | ||
if (oneHandler == null) { | ||
oneHandler = handler; | ||
} | ||
logger.debug("MAP - Added handler {} to Area {}", handlerOwnId, area); | ||
logger.debug("Map: {}", this.toString()); | ||
} | ||
} | ||
|
||
protected void remove(int area, OpenWebNetThingHandler handler) { | ||
if (hndlrsMap.containsKey(area)) { | ||
Map<String, OpenWebNetThingHandler> areaHndlrs = hndlrsMap.get(Integer.valueOf(area)); | ||
if (areaHndlrs != null) { | ||
boolean removed = areaHndlrs.remove(handler.ownId, handler); | ||
OpenWebNetThingHandler oneHandler = this.oneHandler; | ||
// if the removed handler was linked by oneHandler, find another one | ||
if (removed && oneHandler != null && oneHandler.equals(handler)) { | ||
this.oneHandler = getFirst(); | ||
} | ||
logger.debug("MAP - Removed handler {} from Area {}", handler.ownId, area); | ||
logger.debug("Map: {}", this.toString()); | ||
} | ||
} | ||
} | ||
|
||
protected @Nullable List<OpenWebNetThingHandler> getAreaHandlers(int area) { | ||
Map<String, OpenWebNetThingHandler> areaHndlrs = hndlrsMap.get(area); | ||
if (areaHndlrs != null) { | ||
List<OpenWebNetThingHandler> list = new ArrayList<OpenWebNetThingHandler>(areaHndlrs.values()); | ||
return list; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
protected @Nullable List<OpenWebNetThingHandler> getAllHandlers() { | ||
List<OpenWebNetThingHandler> list = new ArrayList<OpenWebNetThingHandler>(); | ||
for (Map.Entry<Integer, Map<String, OpenWebNetThingHandler>> entry : hndlrsMap.entrySet()) { | ||
Map<String, OpenWebNetThingHandler> innerMap = entry.getValue(); | ||
for (Map.Entry<String, OpenWebNetThingHandler> innerEntry : innerMap.entrySet()) { | ||
OpenWebNetThingHandler hndlr = innerEntry.getValue(); | ||
list.add(hndlr); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
protected boolean isEmpty() { | ||
return oneHandler == null; | ||
} | ||
|
||
protected @Nullable OpenWebNetThingHandler getOneHandler() { | ||
if (oneHandler == null) { | ||
oneHandler = getFirst(); | ||
} | ||
return oneHandler; | ||
} | ||
|
||
private @Nullable OpenWebNetThingHandler getFirst() { | ||
for (Map.Entry<Integer, Map<String, OpenWebNetThingHandler>> entry : hndlrsMap.entrySet()) { | ||
Map<String, OpenWebNetThingHandler> innerMap = entry.getValue(); | ||
for (Map.Entry<String, OpenWebNetThingHandler> innerEntry : innerMap.entrySet()) { | ||
OpenWebNetThingHandler thingHandler = innerEntry.getValue(); | ||
WhereLightAutom whereLightAutom = (WhereLightAutom) thingHandler.deviceWhere; | ||
if (whereLightAutom != null && whereLightAutom.isAPL()) { | ||
return thingHandler; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder log = new StringBuilder(); | ||
log.append("\n---- LightAutomHandlersMap ----"); | ||
for (Map.Entry<Integer, Map<String, OpenWebNetThingHandler>> entry : hndlrsMap.entrySet()) { | ||
log.append("\n- Area: " + entry.getKey() + "\n -"); | ||
Map<String, OpenWebNetThingHandler> innerMap = entry.getValue(); | ||
for (Map.Entry<String, OpenWebNetThingHandler> innerEntry : innerMap.entrySet()) { | ||
OpenWebNetThingHandler thingHandler = innerEntry.getValue(); | ||
log.append(" " + thingHandler.ownId); | ||
} | ||
} | ||
log.append("\n# getAllHandlers: "); | ||
List<OpenWebNetThingHandler> allHandlers = getAllHandlers(); | ||
if (allHandlers != null) { | ||
for (OpenWebNetThingHandler singleHandler : allHandlers) { | ||
log.append(" " + singleHandler.ownId); | ||
} | ||
} | ||
OpenWebNetThingHandler oneThingHandler = this.getOneHandler(); | ||
log.append("\n# getOneHandler() = " + (oneThingHandler == null ? "null" : oneThingHandler.ownId)); | ||
log.append("\n-------------------------------"); | ||
|
||
return log.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.