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