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