]> git.basschouten.com Git - openhab-addons.git/blob
9adbf518e08e68967d4a16f10f85b41ace7441d9
[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.io.imperihome.internal.handler;
14
15 import java.net.URLDecoder;
16 import java.nio.charset.StandardCharsets;
17 import java.util.regex.Matcher;
18
19 import javax.servlet.http.HttpServletRequest;
20
21 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
22 import org.openhab.io.imperihome.internal.processor.DeviceRegistry;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Device action request handler.
28  *
29  * @author Pepijn de Geus - Initial contribution
30  */
31 public class DeviceActionHandler {
32
33     private final Logger logger = LoggerFactory.getLogger(DeviceActionHandler.class);
34
35     private final DeviceRegistry deviceRegistry;
36
37     public DeviceActionHandler(DeviceRegistry deviceRegistry) {
38         this.deviceRegistry = deviceRegistry;
39     }
40
41     public void handle(HttpServletRequest req, Matcher urlMatcher) {
42         String deviceId, action, value;
43         deviceId = URLDecoder.decode(urlMatcher.group(1), StandardCharsets.UTF_8);
44         action = URLDecoder.decode(urlMatcher.group(2), StandardCharsets.UTF_8);
45
46         if (urlMatcher.group(3) == null) {
47             value = null;
48         } else {
49             value = URLDecoder.decode(urlMatcher.group(3), StandardCharsets.UTF_8);
50         }
51
52         logger.debug("Action request for device {}: [{}] [{}]", deviceId, action, value);
53
54         AbstractDevice device = deviceRegistry.getDevice(deviceId);
55         if (device == null) {
56             logger.warn("Received action request for unknown device: {}", urlMatcher.group(0));
57         } else {
58             device.performAction(action, value);
59         }
60     }
61 }