2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ftpupload.internal;
15 import static org.openhab.binding.ftpupload.internal.FtpUploadBindingConstants.THING_TYPE_IMAGERECEIVER;
17 import java.util.Dictionary;
20 import org.apache.ftpserver.DataConnectionConfigurationFactory;
21 import org.apache.ftpserver.FtpServerConfigurationException;
22 import org.apache.ftpserver.ftplet.FtpException;
23 import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
24 import org.openhab.binding.ftpupload.internal.handler.FtpUploadHandler;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.ThingStatusInfo;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Component;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link FtpUploadHandlerFactory} is responsible for creating things and thing
42 * @author Pauli Anttila - Initial contribution
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ftpupload")
45 public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
46 private final Logger logger = LoggerFactory.getLogger(FtpUploadHandlerFactory.class);
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_IMAGERECEIVER);
50 private final int DEFAULT_PORT = 2121;
51 private final int DEFAULT_IDLE_TIMEOUT = 60;
53 private FtpServer ftpServer;
56 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
61 protected ThingHandler createHandler(Thing thing) {
62 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
64 if (thingTypeUID.equals(THING_TYPE_IMAGERECEIVER)) {
65 if (ftpServer.getStartUpErrorReason() != null) {
66 thing.setStatusInfo(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
67 ftpServer.getStartUpErrorReason()));
69 return new FtpUploadHandler(thing, ftpServer);
76 protected synchronized void activate(ComponentContext componentContext) {
77 super.activate(componentContext);
78 ftpServer = new FtpServer();
79 modified(componentContext);
83 protected synchronized void deactivate(ComponentContext componentContext) {
86 super.deactivate(componentContext);
89 protected synchronized void modified(ComponentContext componentContext) {
91 Dictionary<String, Object> properties = componentContext.getProperties();
92 DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
94 int port = DEFAULT_PORT;
95 int idleTimeout = DEFAULT_IDLE_TIMEOUT;
97 if (properties.get("port") != null) {
98 String strPort = properties.get("port").toString();
99 if (!strPort.isEmpty()) {
101 port = Integer.valueOf(strPort);
102 } catch (NumberFormatException e) {
103 logger.warn("Invalid port number '{}', using default port {}", strPort, port);
108 if (properties.get("idleTimeout") != null) {
109 String strIdleTimeout = properties.get("idleTimeout").toString();
110 if (!strIdleTimeout.isEmpty()) {
112 idleTimeout = Integer.valueOf(strIdleTimeout);
113 } catch (NumberFormatException e) {
114 logger.warn("Invalid idle timeout '{}', using default timeout {}", strIdleTimeout, idleTimeout);
119 if (properties.get("passivePorts") != null) {
120 String strPassivePorts = properties.get("passivePorts").toString();
121 if (!strPassivePorts.isEmpty()) {
123 dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
124 } catch (IllegalArgumentException e) {
125 logger.warn("Invalid passive ports '{}' ({})", strPassivePorts, e.getMessage());
131 logger.debug("Starting FTP server, port={}, idleTimeout={}", port, idleTimeout);
132 ftpServer.startServer(port, idleTimeout,
133 dataConnectionConfigurationFactory.createDataConnectionConfiguration());
134 } catch (FtpException | FtpServerConfigurationException e) {
135 logger.warn("FTP server starting failed, reason: {}", e.getMessage());
139 private void stopFtpServer() {
140 logger.debug("Stopping FTP server");
141 ftpServer.stopServer();