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