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.Collections;
18 import java.util.Dictionary;
21 import org.apache.ftpserver.DataConnectionConfigurationFactory;
22 import org.apache.ftpserver.FtpServerConfigurationException;
23 import org.apache.ftpserver.ftplet.FtpException;
24 import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
25 import org.openhab.binding.ftpupload.internal.handler.FtpUploadHandler;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.ThingStatusInfo;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link FtpUploadHandlerFactory} is responsible for creating things and thing
43 * @author Pauli Anttila - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ftpupload")
46 public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(FtpUploadHandlerFactory.class);
49 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_IMAGERECEIVER);
51 private final int DEFAULT_PORT = 2121;
52 private final int DEFAULT_IDLE_TIMEOUT = 60;
54 private FtpServer ftpServer;
57 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
58 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62 protected ThingHandler createHandler(Thing thing) {
63 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
65 if (thingTypeUID.equals(THING_TYPE_IMAGERECEIVER)) {
66 if (ftpServer.getStartUpErrorReason() != null) {
67 thing.setStatusInfo(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
68 ftpServer.getStartUpErrorReason()));
70 return new FtpUploadHandler(thing, ftpServer);
77 protected synchronized void activate(ComponentContext componentContext) {
78 super.activate(componentContext);
79 ftpServer = new FtpServer();
80 modified(componentContext);
84 protected synchronized void deactivate(ComponentContext componentContext) {
87 super.deactivate(componentContext);
90 protected synchronized void modified(ComponentContext componentContext) {
92 Dictionary<String, Object> properties = componentContext.getProperties();
93 DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
95 int port = DEFAULT_PORT;
96 int idleTimeout = DEFAULT_IDLE_TIMEOUT;
98 if (properties.get("port") != null) {
99 String strPort = properties.get("port").toString();
100 if (!strPort.isEmpty()) {
102 port = Integer.valueOf(strPort);
103 } catch (NumberFormatException e) {
104 logger.warn("Invalid port number '{}', using default port {}", strPort, port);
109 if (properties.get("idleTimeout") != null) {
110 String strIdleTimeout = properties.get("idleTimeout").toString();
111 if (!strIdleTimeout.isEmpty()) {
113 idleTimeout = Integer.valueOf(strIdleTimeout);
114 } catch (NumberFormatException e) {
115 logger.warn("Invalid idle timeout '{}', using default timeout {}", strIdleTimeout, idleTimeout);
120 if (properties.get("passivePorts") != null) {
121 String strPassivePorts = properties.get("passivePorts").toString();
122 if (!strPassivePorts.isEmpty()) {
124 dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
125 } catch (IllegalArgumentException e) {
126 logger.warn("Invalid passive ports '{}' ({})", strPassivePorts, e.getMessage());
132 logger.debug("Starting FTP server, port={}, idleTimeout={}", port, idleTimeout);
133 ftpServer.startServer(port, idleTimeout,
134 dataConnectionConfigurationFactory.createDataConnectionConfiguration());
135 } catch (FtpException | FtpServerConfigurationException e) {
136 logger.warn("FTP server starting failed, reason: {}", e.getMessage());
140 private void stopFtpServer() {
141 logger.debug("Stopping FTP server");
142 ftpServer.stopServer();