2 * Copyright (c) 2010-2023 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.handler;
15 import static org.openhab.binding.ftpupload.internal.FtpUploadBindingConstants.*;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
20 import org.openhab.binding.ftpupload.internal.config.FtpUploadConfig;
21 import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
22 import org.openhab.binding.ftpupload.internal.ftp.FtpServerEventListener;
23 import org.openhab.core.io.net.http.HttpUtil;
24 import org.openhab.core.library.types.RawType;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.ChannelUID;
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.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link FtpUploadHandler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Pauli Anttila - Initial contribution
42 public class FtpUploadHandler extends BaseThingHandler implements FtpServerEventListener {
44 private Logger logger = LoggerFactory.getLogger(FtpUploadHandler.class);
46 private FtpUploadConfig configuration;
47 private FtpServer ftpServer;
49 public FtpUploadHandler(Thing thing, FtpServer ftpServer) {
51 this.ftpServer = ftpServer;
55 public void handleCommand(ChannelUID channelUID, Command command) {
56 logger.debug("handleCommand for channel {}: {}", channelUID.getId(), command.toString());
57 logger.debug("Command sending not supported by this binding");
59 if (command.equals(RefreshType.REFRESH)) {
60 ftpServer.printStats();
65 public void initialize() {
66 logger.debug("Initializing handler for FTP Upload Binding");
67 configuration = getConfigAs(FtpUploadConfig.class);
68 logger.debug("Using configuration: {}", configuration.toString());
70 ftpServer.addEventListener(this);
72 ftpServer.addAuthenticationCredentials(configuration.userName, configuration.password);
73 } catch (IllegalArgumentException e) {
74 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
77 updateStatus(ThingStatus.ONLINE);
81 public void dispose() {
82 ftpServer.removeAuthenticationCredentials(configuration.userName);
83 ftpServer.removeEventListener(this);
87 public void fileReceived(String userName, String filename, byte[] data) {
88 if (configuration.userName.equals(userName)) {
89 updateStatus(ThingStatus.ONLINE);
90 updateChannels(filename, data);
91 updateTriggers(filename);
95 private String guessMimeTypeFromData(byte[] data) {
96 String mimeType = HttpUtil.guessContentTypeFromData(data);
97 logger.debug("Mime type guess from content: {}", mimeType);
98 if (mimeType == null) {
99 mimeType = RawType.DEFAULT_MIME_TYPE;
101 logger.debug("Mime type: {}", mimeType);
105 private void updateChannels(String filename, byte[] data) {
106 for (Channel channel : thing.getChannels()) {
107 String channelConf = (String) channel.getConfiguration().get(PARAM_FILENAME_PATTERN);
108 if (channelConf != null) {
109 if (filenameMatch(filename, channelConf)) {
110 if ("Image".equals(channel.getAcceptedItemType())) {
111 updateState(channel.getUID().getId(), new RawType(data, guessMimeTypeFromData(data)));
118 private void updateTriggers(String filename) {
119 for (Channel channel : thing.getChannels()) {
120 String channelConf = (String) channel.getConfiguration().get(PARAM_FILENAME_PATTERN);
121 if (channelConf != null) {
122 if (filenameMatch(filename, channelConf)) {
123 if ("TRIGGER".equals(channel.getKind().toString())) {
124 triggerChannel(channel.getUID().getId(), EVENT_IMAGE_RECEIVED);
131 private boolean filenameMatch(String filename, String pattern) {
133 return Pattern.compile(pattern).matcher(filename).find();
134 } catch (PatternSyntaxException e) {
135 logger.warn("Invalid filename pattern '{}', reason: {}", pattern, e.getMessage());