2 * Copyright (c) 2010-2021 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.commons.lang.StringUtils;
22 import org.apache.ftpserver.DataConnectionConfigurationFactory;
23 import org.apache.ftpserver.FtpServerConfigurationException;
24 import org.apache.ftpserver.ftplet.FtpException;
25 import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
26 import org.openhab.binding.ftpupload.internal.handler.FtpUploadHandler;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.ComponentContext;
36 import org.osgi.service.component.annotations.Component;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link FtpUploadHandlerFactory} is responsible for creating things and thing
44 * @author Pauli Anttila - Initial contribution
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ftpupload")
47 public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
48 private final Logger logger = LoggerFactory.getLogger(FtpUploadHandlerFactory.class);
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_IMAGERECEIVER);
52 private final int DEFAULT_PORT = 2121;
53 private final int DEFAULT_IDLE_TIMEOUT = 60;
55 private FtpServer ftpServer;
58 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63 protected ThingHandler createHandler(Thing thing) {
64 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66 if (thingTypeUID.equals(THING_TYPE_IMAGERECEIVER)) {
67 if (ftpServer.getStartUpErrorReason() != null) {
68 thing.setStatusInfo(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
69 ftpServer.getStartUpErrorReason()));
71 return new FtpUploadHandler(thing, ftpServer);
78 protected synchronized void activate(ComponentContext componentContext) {
79 super.activate(componentContext);
80 ftpServer = new FtpServer();
81 modified(componentContext);
85 protected synchronized void deactivate(ComponentContext componentContext) {
88 super.deactivate(componentContext);
91 protected synchronized void modified(ComponentContext componentContext) {
93 Dictionary<String, Object> properties = componentContext.getProperties();
94 DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
96 int port = DEFAULT_PORT;
97 int idleTimeout = DEFAULT_IDLE_TIMEOUT;
99 if (properties.get("port") != null) {
100 String strPort = properties.get("port").toString();
101 if (StringUtils.isNotEmpty(strPort)) {
103 port = Integer.valueOf(strPort);
104 } catch (NumberFormatException e) {
105 logger.warn("Invalid port number '{}', using default port {}", strPort, port);
110 if (properties.get("idleTimeout") != null) {
111 String strIdleTimeout = properties.get("idleTimeout").toString();
112 if (StringUtils.isNotEmpty(strIdleTimeout)) {
114 idleTimeout = Integer.valueOf(strIdleTimeout);
115 } catch (NumberFormatException e) {
116 logger.warn("Invalid idle timeout '{}', using default timeout {}", strIdleTimeout, idleTimeout);
121 if (properties.get("passivePorts") != null) {
122 String strPassivePorts = properties.get("passivePorts").toString();
123 if (!strPassivePorts.isEmpty()) {
125 dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
126 } catch (IllegalArgumentException e) {
127 logger.warn("Invalid passive ports '{}' ({})", strPassivePorts, e.getMessage());
133 logger.debug("Starting FTP server, port={}, idleTimeout={}", port, idleTimeout);
134 ftpServer.startServer(port, idleTimeout,
135 dataConnectionConfigurationFactory.createDataConnectionConfiguration());
136 } catch (FtpException | FtpServerConfigurationException e) {
137 logger.warn("FTP server starting failed, reason: {}", e.getMessage());
141 private void stopFtpServer() {
142 logger.debug("Stopping FTP server");
143 ftpServer.stopServer();