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