2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.paradoxalarm.internal.communication;
15 import java.io.IOException;
16 import java.net.UnknownHostException;
17 import java.util.Collection;
18 import java.util.concurrent.ScheduledExecutorService;
20 import org.openhab.binding.paradoxalarm.internal.exceptions.ParadoxRuntimeException;
21 import org.openhab.binding.paradoxalarm.internal.util.ParadoxUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The {@link GenericCommunicator} Used for the common communication logic for all types of panels.
28 * @author Konstantin Polihronov - Initial contribution
30 public class GenericCommunicator extends AbstractCommunicator implements IResponseReceiver {
32 private final Logger logger = LoggerFactory.getLogger(GenericCommunicator.class);
34 private final byte[] pcPasswordBytes;
35 private byte[] panelInfoBytes;
36 private boolean isEncrypted;
37 private final String password;
39 public GenericCommunicator(String ipAddress, int tcpPort, String ip150Password, String pcPassword,
40 ScheduledExecutorService scheduler, boolean useEncryption) throws UnknownHostException, IOException {
41 super(ipAddress, tcpPort, scheduler);
42 this.isEncrypted = useEncryption;
43 logger.debug("Use encryption={}", isEncrypted);
44 this.password = ip150Password;
45 this.pcPasswordBytes = ParadoxUtil.stringToBCD(pcPassword);
49 public synchronized void startLoginSequence() {
50 logger.debug("Login sequence started");
53 logger.debug("Already logged on. No action needed. Returning.");
57 if (socket.isClosed()) {
60 } catch (IOException e) {
61 throw new ParadoxRuntimeException(e);
65 CommunicationState.login(this);
69 public byte[] getPanelInfoBytes() {
70 return panelInfoBytes;
74 public void setPanelInfoBytes(byte[] panelInfoBytes) {
75 this.panelInfoBytes = panelInfoBytes;
79 public byte[] getPcPasswordBytes() {
80 return pcPasswordBytes;
84 protected void receiveEpromResponse(IResponse response) {
85 // Nothing to do here. Override in particular implementation class.
89 protected void receiveRamResponse(IResponse response) {
90 // Nothing to do here. Override in particular implementation class.
93 public void refreshMemoryMap() {
94 // Nothing to do here. Override in particular implementation class.
98 public ScheduledExecutorService getScheduler() {
103 public void setListeners(Collection<IDataUpdateListener> listeners) {
104 this.listeners = listeners;
108 public void updateListeners() {
109 if (listeners != null && !listeners.isEmpty()) {
110 listeners.forEach(IDataUpdateListener::update);
115 public boolean isEncrypted() {
120 public String getPassword() {
125 public void receiveResponse(IResponse response, IParadoxInitialLoginCommunicator communicator) {
126 IRequest request = response.getRequest();
127 logger.trace("Handling response for request={}", request);
128 ParadoxUtil.printPacket("Full packet", response.getPacketBytes());
129 RequestType type = request.getType();
130 if (type == RequestType.RAM) {
131 receiveRamResponse(response);
132 } else if (type == RequestType.EPROM) {
133 receiveEpromResponse(response);
135 logger.debug("Probably wrong sender in the request. Request type is not one of the supported methods.");