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