2 * Copyright (c) 2010-2020 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.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();
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 (StringUtils.isNotEmpty(strPort)) {
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 (StringUtils.isNotEmpty(strIdleTimeout)) {
112 idleTimeout = Integer.valueOf(strIdleTimeout);
113 } catch (NumberFormatException e) {
114 logger.warn("Invalid idle timeout '{}', using default timeout {}", strIdleTimeout, idleTimeout);
120 logger.debug("Starting FTP server, port={}, idleTimeout={}", port, idleTimeout);
121 ftpServer.startServer(port, idleTimeout);
122 } catch (FtpException | FtpServerConfigurationException e) {
123 logger.warn("FTP server starting failed, reason: {}", e.getMessage());
127 private void stopFtpServer() {
128 logger.debug("Stopping FTP server");
129 ftpServer.stopServer();