]> git.basschouten.com Git - openhab-addons.git/blob
39dd1d27671e46db0b511160ad20a69d6f37851f
[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.sonyprojector.internal.communication.sdcp;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.sonyprojector.internal.SonyProjectorModel;
17 import org.openhab.core.i18n.CommunicationException;
18 import org.openhab.core.i18n.ConnectionException;
19 import org.openhab.core.util.HexUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Class for communicating with Sony Projectors through an IP connection
25  * using Pj Talk service (SDCP protocol)
26  *
27  * @author Markus Wehrle - Initial contribution
28  * @author Laurent Garnier - Refactoring to consider SonyProjectorConnector and add a full check of responses
29  */
30 @NonNullByDefault
31 public class SonyProjectorSdcpSimuConnector extends SonyProjectorSdcpConnector {
32
33     private final Logger logger = LoggerFactory.getLogger(SonyProjectorSdcpSimuConnector.class);
34
35     private byte[] lastItemCode = new byte[] { 0x00, 0x00 };
36
37     /**
38      * Constructor
39      *
40      * @param model the projector model in use
41      */
42     public SonyProjectorSdcpSimuConnector(SonyProjectorModel model) {
43         super("127.0.0.1", null, null, model, true);
44     }
45
46     @Override
47     public synchronized void open() throws ConnectionException {
48         if (!connected) {
49             connected = true;
50             logger.debug("Simulated SDCP connection opened");
51         }
52     }
53
54     @Override
55     public synchronized void close() {
56         if (connected) {
57             logger.debug("Simulated SDCP connection closed");
58             connected = false;
59         }
60     }
61
62     @Override
63     protected byte[] buildMessage(byte[] itemCode, boolean getCommand, byte[] data) {
64         lastItemCode = itemCode;
65         return super.buildMessage(itemCode, getCommand, data);
66     }
67
68     @Override
69     protected synchronized byte[] readResponse() throws CommunicationException {
70         byte[] message = new byte[34];
71         byte[] communityData = getCommunity().getBytes();
72         message[0] = HEADER[0];
73         message[1] = HEADER[1];
74         message[2] = communityData[0];
75         message[3] = communityData[1];
76         message[4] = communityData[2];
77         message[5] = communityData[3];
78         message[6] = OK;
79         message[7] = lastItemCode[0];
80         message[8] = lastItemCode[1];
81         message[9] = 2;
82         message[10] = 0;
83         message[11] = 1;
84         logger.debug("readResponse: {}", HexUtils.bytesToHex(message));
85         return message;
86     }
87 }