]> git.basschouten.com Git - openhab-addons.git/blob
6c59e623e64f14618ca1f442b8eaba152efe6822
[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.pjlinkdevice.internal.device.command;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.pjlinkdevice.internal.device.PJLinkDevice;
19
20 /**
21  * Common base class for most PJLink commands.
22  *
23  * Takes care of generating the request string, sending it to the device, authentication error checking and response
24  * parsing.
25  *
26  * @author Nils Schnabel - Initial contribution
27  */
28 @NonNullByDefault
29 public abstract class AbstractCommand<RequestType extends Request, ResponseType extends Response<?>>
30         implements Command<ResponseType> {
31     private PJLinkDevice pjLinkDevice;
32
33     public AbstractCommand(PJLinkDevice pjLinkDevice) {
34         this.pjLinkDevice = pjLinkDevice;
35     }
36
37     public PJLinkDevice getDevice() {
38         return this.pjLinkDevice;
39     }
40
41     protected abstract RequestType createRequest();
42
43     protected abstract ResponseType parseResponse(String response) throws ResponseException;
44
45     @Override
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?");
51         }
52         return parseResponse(responseString);
53     }
54 }