]> git.basschouten.com Git - openhab-addons.git/blob
8990d0ec761316655a1fe9c3668af038e577d4f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.ftpupload.internal;
14
15 import static org.openhab.binding.ftpupload.internal.FtpUploadBindingConstants.THING_TYPE_IMAGERECEIVER;
16
17 import java.util.Collections;
18 import java.util.Dictionary;
19 import java.util.Set;
20
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;
39
40 /**
41  * The {@link FtpUploadHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Pauli Anttila - Initial contribution
45  */
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ftpupload")
47 public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
48     private final Logger logger = LoggerFactory.getLogger(FtpUploadHandlerFactory.class);
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_IMAGERECEIVER);
51
52     private final int DEFAULT_PORT = 2121;
53     private final int DEFAULT_IDLE_TIMEOUT = 60;
54
55     private FtpServer ftpServer;
56
57     @Override
58     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
60     }
61
62     @Override
63     protected ThingHandler createHandler(Thing thing) {
64         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
65
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()));
70             }
71             return new FtpUploadHandler(thing, ftpServer);
72         }
73
74         return null;
75     }
76
77     @Override
78     protected synchronized void activate(ComponentContext componentContext) {
79         super.activate(componentContext);
80         ftpServer = new FtpServer();
81         modified(componentContext);
82     }
83
84     @Override
85     protected synchronized void deactivate(ComponentContext componentContext) {
86         stopFtpServer();
87         ftpServer = null;
88         super.deactivate(componentContext);
89     }
90
91     protected synchronized void modified(ComponentContext componentContext) {
92         stopFtpServer();
93         Dictionary<String, Object> properties = componentContext.getProperties();
94         DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
95
96         int port = DEFAULT_PORT;
97         int idleTimeout = DEFAULT_IDLE_TIMEOUT;
98
99         if (properties.get("port") != null) {
100             String strPort = properties.get("port").toString();
101             if (StringUtils.isNotEmpty(strPort)) {
102                 try {
103                     port = Integer.valueOf(strPort);
104                 } catch (NumberFormatException e) {
105                     logger.warn("Invalid port number '{}', using default port {}", strPort, port);
106                 }
107             }
108         }
109
110         if (properties.get("idleTimeout") != null) {
111             String strIdleTimeout = properties.get("idleTimeout").toString();
112             if (StringUtils.isNotEmpty(strIdleTimeout)) {
113                 try {
114                     idleTimeout = Integer.valueOf(strIdleTimeout);
115                 } catch (NumberFormatException e) {
116                     logger.warn("Invalid idle timeout '{}', using default timeout {}", strIdleTimeout, idleTimeout);
117                 }
118             }
119         }
120
121         if (properties.get("passivePorts") != null) {
122             String strPassivePorts = properties.get("passivePorts").toString();
123             if (!strPassivePorts.isEmpty()) {
124                 try {
125                     dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
126                 } catch (IllegalArgumentException e) {
127                     logger.warn("Invalid passive ports '{}' ({})", strPassivePorts, e.getMessage());
128                 }
129             }
130         }
131
132         try {
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());
138         }
139     }
140
141     private void stopFtpServer() {
142         logger.debug("Stopping FTP server");
143         ftpServer.stopServer();
144     }
145 }