]> git.basschouten.com Git - openhab-addons.git/blob
b5fadeec9f692bc7913c2c9e46d9cbbb4aec5671
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.serial;
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.io.transport.serial.SerialPortManager;
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 a serial connection
26  *
27  * @author Laurent Garnier - Initial contribution
28  */
29 @NonNullByDefault
30 public class SonyProjectorSerialSimuConnector extends SonyProjectorSerialConnector {
31
32     private final Logger logger = LoggerFactory.getLogger(SonyProjectorSerialSimuConnector.class);
33
34     /**
35      * Constructor
36      *
37      * @param serialPortManager the serial port manager
38      * @param model the projector model in use
39      */
40     public SonyProjectorSerialSimuConnector(SerialPortManager serialPortManager, SonyProjectorModel model) {
41         super(serialPortManager, "dummyPort", model, true);
42     }
43
44     @Override
45     public synchronized void open() throws ConnectionException {
46         if (!connected) {
47             connected = true;
48             logger.debug("Simulated serial connection opened");
49         }
50     }
51
52     @Override
53     public synchronized void close() {
54         if (connected) {
55             logger.debug("Simulated serial connection closed");
56             connected = false;
57         }
58     }
59
60     @Override
61     protected synchronized byte[] readResponse() throws CommunicationException {
62         byte[] message = new byte[8];
63         message[0] = START_CODE;
64         message[1] = SonyProjectorSerialError.COMPLETE.getDataCode()[0];
65         message[2] = SonyProjectorSerialError.COMPLETE.getDataCode()[1];
66         message[3] = TYPE_ACK;
67         message[4] = 0x00;
68         message[5] = 0x01;
69         message[6] = computeCheckSum(message);
70         message[7] = END_CODE;
71         logger.debug("readResponse: {}", HexUtils.bytesToHex(message));
72         return message;
73     }
74
75     private byte computeCheckSum(byte[] message) {
76         byte result = 0;
77         for (int i = 1; i <= 5; i++) {
78             result |= (message[i] & 0x000000FF);
79         }
80         return result;
81     }
82 }