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