]> git.basschouten.com Git - openhab-addons.git/blob
29431dea7ed95a808d5fd1ee963f974ef089ffd7
[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.handler;
14
15 import static org.openhab.binding.ftpupload.internal.FtpUploadBindingConstants.*;
16
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19
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;
35
36 /**
37  * The {@link FtpUploadHandler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Pauli Anttila - Initial contribution
41  */
42 public class FtpUploadHandler extends BaseThingHandler implements FtpServerEventListener {
43
44     private Logger logger = LoggerFactory.getLogger(FtpUploadHandler.class);
45
46     private FtpUploadConfig configuration;
47     private FtpServer ftpServer;
48
49     public FtpUploadHandler(Thing thing, FtpServer ftpServer) {
50         super(thing);
51         this.ftpServer = ftpServer;
52     }
53
54     @Override
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");
58
59         if (command.equals(RefreshType.REFRESH)) {
60             ftpServer.printStats();
61         }
62     }
63
64     @Override
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());
69
70         ftpServer.addEventListener(this);
71         try {
72             ftpServer.addAuthenticationCredentials(configuration.userName, configuration.password);
73         } catch (IllegalArgumentException e) {
74             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
75         }
76
77         updateStatus(ThingStatus.ONLINE);
78     }
79
80     @Override
81     public void dispose() {
82         ftpServer.removeAuthenticationCredentials(configuration.userName);
83         ftpServer.removeEventListener(this);
84     }
85
86     @Override
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);
92         }
93     }
94
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;
100         }
101         logger.debug("Mime type: {}", mimeType);
102         return mimeType;
103     }
104
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)));
112                     }
113                 }
114             }
115         }
116     }
117
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);
125                     }
126                 }
127             }
128         }
129     }
130
131     private boolean filenameMatch(String filename, String pattern) {
132         try {
133             return Pattern.compile(pattern).matcher(filename).find();
134         } catch (PatternSyntaxException e) {
135             logger.warn("Invalid filename pattern '{}', reason: {}", pattern, e.getMessage());
136         }
137         return false;
138     }
139 }