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