]> git.basschouten.com Git - openhab-addons.git/blob
8eafdf194245f0fde4eeb45f6fce3fdc6c8f90e2
[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.paradoxalarm.internal.communication;
14
15 import java.io.IOException;
16 import java.net.UnknownHostException;
17 import java.util.Collection;
18 import java.util.concurrent.ScheduledExecutorService;
19
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;
24
25 /**
26  * The {@link GenericCommunicator} Used for the common communication logic for all types of panels.
27  *
28  * @author Konstantin Polihronov - Initial contribution
29  */
30 public class GenericCommunicator extends AbstractCommunicator implements IResponseReceiver {
31
32     private final Logger logger = LoggerFactory.getLogger(GenericCommunicator.class);
33
34     private final byte[] pcPasswordBytes;
35     private byte[] panelInfoBytes;
36     private boolean isEncrypted;
37     private final String password;
38
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);
46     }
47
48     @Override
49     public synchronized void startLoginSequence() {
50         logger.debug("Login sequence started");
51
52         if (isOnline()) {
53             logger.debug("Already logged on. No action needed. Returning.");
54             return;
55         }
56
57         if (socket.isClosed()) {
58             try {
59                 initializeSocket();
60             } catch (IOException e) {
61                 throw new ParadoxRuntimeException(e);
62             }
63         }
64
65         CommunicationState.login(this);
66     }
67
68     @Override
69     public byte[] getPanelInfoBytes() {
70         return panelInfoBytes;
71     }
72
73     @Override
74     public void setPanelInfoBytes(byte[] panelInfoBytes) {
75         this.panelInfoBytes = panelInfoBytes;
76     }
77
78     @Override
79     public byte[] getPcPasswordBytes() {
80         return pcPasswordBytes;
81     }
82
83     @Override
84     protected void receiveEpromResponse(IResponse response) {
85         // Nothing to do here. Override in particular implementation class.
86     }
87
88     @Override
89     protected void receiveRamResponse(IResponse response) {
90         // Nothing to do here. Override in particular implementation class.
91     }
92
93     public void refreshMemoryMap() {
94         // Nothing to do here. Override in particular implementation class.
95     }
96
97     @Override
98     public ScheduledExecutorService getScheduler() {
99         return scheduler;
100     }
101
102     @Override
103     public void setListeners(Collection<IDataUpdateListener> listeners) {
104         this.listeners = listeners;
105     }
106
107     @Override
108     public void updateListeners() {
109         if (listeners != null && !listeners.isEmpty()) {
110             listeners.forEach(IDataUpdateListener::update);
111         }
112     }
113
114     @Override
115     public boolean isEncrypted() {
116         return isEncrypted;
117     }
118
119     @Override
120     public String getPassword() {
121         return password;
122     }
123
124     @Override
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);
134         } else {
135             logger.debug("Probably wrong sender in the request. Request type is not one of the supported methods.");
136         }
137     }
138 }