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.io.imperihome.internal.handler;
15 import java.net.URLDecoder;
16 import java.nio.charset.StandardCharsets;
17 import java.util.regex.Matcher;
19 import javax.servlet.http.HttpServletRequest;
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;
27 * Device action request handler.
29 * @author Pepijn de Geus - Initial contribution
31 public class DeviceActionHandler {
33 private final Logger logger = LoggerFactory.getLogger(DeviceActionHandler.class);
35 private final DeviceRegistry deviceRegistry;
37 public DeviceActionHandler(DeviceRegistry deviceRegistry) {
38 this.deviceRegistry = deviceRegistry;
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);
46 if (urlMatcher.group(3) == null) {
49 value = URLDecoder.decode(urlMatcher.group(3), StandardCharsets.UTF_8);
52 logger.debug("Action request for device {}: [{}] [{}]", deviceId, action, value);
54 AbstractDevice device = deviceRegistry.getDevice(deviceId);
56 logger.warn("Received action request for unknown device: {}", urlMatcher.group(0));
58 device.performAction(action, value);