]> git.basschouten.com Git - openhab-addons.git/blob
c007f6b8cf84967852a79c0b88dcbee19be96bc5
[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.authentication;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.pjlinkdevice.internal.device.PJLinkDevice;
19 import org.openhab.binding.pjlinkdevice.internal.device.command.AuthenticationException;
20 import org.openhab.binding.pjlinkdevice.internal.device.command.Command;
21 import org.openhab.binding.pjlinkdevice.internal.device.command.Response;
22 import org.openhab.binding.pjlinkdevice.internal.device.command.ResponseException;
23
24 /**
25  * This command is used to authenticate to the device after the connection established.
26  * As authentication can only be done in conjunction with a real command, a testCommand must be passed to authenticate.
27  *
28  * The authentication procedure is described in
29  * <a href="https://pjlink.jbmia.or.jp/english/data_cl2/PJLink_5-1.pdf">[PJLinkSpec]</a> chapter 5.1. Authentication
30  * procedure
31  *
32  * @author Nils Schnabel - Initial contribution
33  */
34 @NonNullByDefault
35 public class AuthenticationCommand<ResponseType extends Response<?>> implements Command<ResponseType> {
36
37     private String challenge;
38     private Command<ResponseType> testCommand;
39     private PJLinkDevice device;
40
41     public AuthenticationCommand(PJLinkDevice pjLinkDevice, String challenge, Command<ResponseType> testCommand) {
42         this.device = pjLinkDevice;
43         this.challenge = challenge;
44         this.testCommand = testCommand;
45     }
46
47     @Override
48     public ResponseType execute() throws ResponseException, IOException, AuthenticationException {
49         this.device.addPrefixToNextCommand(createRequest().getRequestString());
50         return this.testCommand.execute();
51     }
52
53     protected AuthenticationRequest<ResponseType> createRequest() {
54         return new AuthenticationRequest<>(this);
55     }
56
57     public String getChallenge() {
58         return this.challenge;
59     }
60
61     public PJLinkDevice getDevice() {
62         return this.device;
63     }
64 }