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.pjlinkdevice.internal.device.command;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.pjlinkdevice.internal.device.PJLinkDevice;
21 * Common base class for most PJLink commands.
23 * Takes care of generating the request string, sending it to the device, authentication error checking and response
26 * @author Nils Schnabel - Initial contribution
29 public abstract class AbstractCommand<RequestType extends Request, ResponseType extends Response<?>>
30 implements Command<ResponseType> {
31 private PJLinkDevice pjLinkDevice;
33 public AbstractCommand(PJLinkDevice pjLinkDevice) {
34 this.pjLinkDevice = pjLinkDevice;
37 public PJLinkDevice getDevice() {
38 return this.pjLinkDevice;
41 protected abstract RequestType createRequest();
43 protected abstract ResponseType parseResponse(String response) throws ResponseException;
46 public ResponseType execute() throws ResponseException, IOException, AuthenticationException {
47 RequestType request = createRequest();
48 String responseString = this.pjLinkDevice.execute(request.getRequestString() + "\r");
49 if ("PJLINK ERRA".equalsIgnoreCase(responseString)) {
50 throw new AuthenticationException("Authentication error, wrong password provided?");
52 return parseResponse(responseString);