]> git.basschouten.com Git - openhab-addons.git/blob
6a9ec320021a1bcd4977fefcf00d873a8b43e387
[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.dmx.internal.handler;
14
15 import static org.openhab.binding.dmx.internal.DmxBindingConstants.THING_TYPE_SACN_BRIDGE;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.openhab.binding.dmx.internal.config.SacnBridgeHandlerConfiguration;
23 import org.openhab.binding.dmx.internal.dmxoverethernet.DmxOverEthernetHandler;
24 import org.openhab.binding.dmx.internal.dmxoverethernet.IpNode;
25 import org.openhab.binding.dmx.internal.dmxoverethernet.SacnNode;
26 import org.openhab.binding.dmx.internal.dmxoverethernet.SacnPacket;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link SacnBridgeHandler} is responsible for handling the communication
36  * with sACN/E1.31 devices
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 public class SacnBridgeHandler extends DmxOverEthernetHandler {
41     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SACN_BRIDGE);
42     public static final int MIN_UNIVERSE_ID = 1;
43     public static final int MAX_UNIVERSE_ID = 63999;
44
45     private final Logger logger = LoggerFactory.getLogger(SacnBridgeHandler.class);
46     private final UUID senderUUID;
47
48     public SacnBridgeHandler(Bridge sacnBridge) {
49         super(sacnBridge);
50         senderUUID = UUID.randomUUID();
51     }
52
53     @Override
54     protected void updateConfiguration() {
55         SacnBridgeHandlerConfiguration configuration = getConfig().as(SacnBridgeHandlerConfiguration.class);
56
57         setUniverse(configuration.universe, MIN_UNIVERSE_ID, MAX_UNIVERSE_ID);
58         packetTemplate.setUniverse(universe.getUniverseId());
59
60         receiverNodes.clear();
61         if ((configuration.mode.equals("unicast"))) {
62             if (configuration.address.isEmpty()) {
63                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
64                         "Could not initialize unicast sender (address not set)");
65                 return;
66             } else {
67                 try {
68                     receiverNodes = IpNode.fromString(configuration.address, SacnNode.DEFAULT_PORT);
69                     logger.debug("using unicast mode to {} for {}", receiverNodes.toString(), this.thing.getUID());
70                 } catch (IllegalArgumentException e) {
71                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
72                     return;
73                 }
74             }
75         } else {
76             receiverNodes = new ArrayList<>();
77             receiverNodes.add(SacnNode.getBroadcastNode(universe.getUniverseId()));
78             logger.debug("using multicast mode to {} for {}", receiverNodes, this.thing.getUID());
79         }
80
81         if (!configuration.localaddress.isEmpty()) {
82             senderNode = new IpNode(configuration.localaddress);
83         }
84         logger.debug("originating address is {} for {}", senderNode, this.thing.getUID());
85
86         refreshAlways = configuration.refreshmode.equals("always");
87         logger.debug("refresh mode set to always: {}", refreshAlways);
88
89         updateStatus(ThingStatus.UNKNOWN);
90         super.updateConfiguration();
91
92         logger.debug("updated configuration for sACN/E1.31 bridge {}", this.thing.getUID());
93     }
94
95     @Override
96     public void initialize() {
97         logger.debug("initializing sACN/E1.31 bridge {}", this.thing.getUID());
98
99         packetTemplate = new SacnPacket(senderUUID);
100         updateConfiguration();
101     }
102 }