2 * Copyright (c) 2010-2021 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.powermax.internal.message;
15 import java.util.ArrayList;
16 import java.util.Calendar;
17 import java.util.EventObject;
18 import java.util.GregorianCalendar;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.concurrent.ConcurrentLinkedQueue;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
26 import org.openhab.binding.powermax.internal.connector.PowermaxConnector;
27 import org.openhab.binding.powermax.internal.connector.PowermaxSerialConnector;
28 import org.openhab.binding.powermax.internal.connector.PowermaxTcpConnector;
29 import org.openhab.binding.powermax.internal.state.PowermaxArmMode;
30 import org.openhab.binding.powermax.internal.state.PowermaxPanelSettings;
31 import org.openhab.binding.powermax.internal.state.PowermaxPanelType;
32 import org.openhab.binding.powermax.internal.state.PowermaxState;
33 import org.openhab.binding.powermax.internal.state.PowermaxStateEvent;
34 import org.openhab.binding.powermax.internal.state.PowermaxStateEventListener;
35 import org.openhab.core.common.ThreadPoolManager;
36 import org.openhab.core.i18n.TimeZoneProvider;
37 import org.openhab.core.io.transport.serial.SerialPortManager;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.util.HexUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * A class that manages the communication with the Visonic alarm system
46 * Visonic does not provide a specification of the RS232 protocol and, thus,
47 * the binding uses the available protocol specification given at the ​domoticaforum
48 * http://www.domoticaforum.eu/viewtopic.php?f=68&t=6581
50 * @author Laurent Garnier - Initial contribution
52 public class PowermaxCommManager implements PowermaxMessageEventListener {
54 private final Logger logger = LoggerFactory.getLogger(PowermaxCommManager.class);
56 private static final int DEFAULT_TCP_PORT = 80;
57 private static final int TCP_CONNECTION_TIMEOUT = 5000;
58 private static final int DEFAULT_BAUD_RATE = 9600;
59 private static final int WAITING_DELAY_FOR_RESPONSE = 750;
60 private static final long DELAY_BETWEEN_SETUP_DOWNLOADS = TimeUnit.SECONDS.toMillis(45);
62 private final ScheduledExecutorService scheduler;
64 /** The object to store the current settings of the Powermax alarm system */
65 private PowermaxPanelSettings panelSettings;
67 /** Panel type used when in standard mode */
68 private PowermaxPanelType panelType;
70 private boolean forceStandardMode;
71 private boolean autoSyncTime;
72 private final TimeZoneProvider timeZoneProvider;
74 private List<PowermaxStateEventListener> listeners = new ArrayList<>();
76 /** The serial or TCP connecter used to communicate with the Powermax alarm system */
77 private PowermaxConnector connector;
79 /** The last message sent to the the Powermax alarm system */
80 private PowermaxBaseMessage lastSendMsg;
82 /** The message queue of messages to be sent to the the Powermax alarm system */
83 private ConcurrentLinkedQueue<PowermaxBaseMessage> msgQueue = new ConcurrentLinkedQueue<>();
85 /** The time in milliseconds the last download of the panel setup was requested */
86 private Long lastTimeDownloadRequested;
88 /** The boolean indicating if the download of the panel setup is in progress or not */
89 private boolean downloadRunning;
91 /** The time in milliseconds used to set time and date */
92 private Long syncTimeCheck;
95 * Constructor for Serial Connection
97 * @param sPort the serial port name
98 * @param panelType the panel type to be used when in standard mode
99 * @param forceStandardMode true to force the standard mode rather than trying using the Powerlink mode
100 * @param autoSyncTime true for automatic sync time
101 * @param serialPortManager the serial port manager
102 * @param threadName the prefix name of threads to be created
104 public PowermaxCommManager(String sPort, PowermaxPanelType panelType, boolean forceStandardMode,
105 boolean autoSyncTime, SerialPortManager serialPortManager, String threadName,
106 TimeZoneProvider timeZoneProvider) {
107 this.panelType = panelType;
108 this.forceStandardMode = forceStandardMode;
109 this.autoSyncTime = autoSyncTime;
110 this.timeZoneProvider = timeZoneProvider;
111 this.panelSettings = new PowermaxPanelSettings(panelType);
112 this.scheduler = ThreadPoolManager.getScheduledPool(threadName + "-sender");
113 String serialPort = (sPort != null && !sPort.trim().isEmpty()) ? sPort.trim() : null;
114 if (serialPort != null) {
115 connector = new PowermaxSerialConnector(serialPortManager, serialPort, DEFAULT_BAUD_RATE,
116 threadName + "-reader");
123 * Constructor for TCP connection
125 * @param ip the IP address
126 * @param port TCP port number; default port is used if value <= 0
127 * @param panelType the panel type to be used when in standard mode
128 * @param forceStandardMode true to force the standard mode rather than trying using the Powerlink mode
129 * @param autoSyncTime true for automatic sync time
130 * @param serialPortManager
131 * @param threadName the prefix name of threads to be created
133 public PowermaxCommManager(String ip, int port, PowermaxPanelType panelType, boolean forceStandardMode,
134 boolean autoSyncTime, String threadName, TimeZoneProvider timeZoneProvider) {
135 this.panelType = panelType;
136 this.forceStandardMode = forceStandardMode;
137 this.autoSyncTime = autoSyncTime;
138 this.timeZoneProvider = timeZoneProvider;
139 this.panelSettings = new PowermaxPanelSettings(panelType);
140 this.scheduler = ThreadPoolManager.getScheduledPool(threadName + "-sender");
141 String ipAddress = (ip != null && !ip.trim().isEmpty()) ? ip.trim() : null;
142 int tcpPort = (port > 0) ? port : DEFAULT_TCP_PORT;
143 if (ipAddress != null) {
144 connector = new PowermaxTcpConnector(ipAddress, tcpPort, TCP_CONNECTION_TIMEOUT, threadName + "-reader");
153 * @param listener the listener to be added
155 public synchronized void addEventListener(PowermaxStateEventListener listener) {
156 listeners.add(listener);
157 if (connector != null) {
158 connector.addEventListener(this);
163 * Remove event listener
165 * @param listener the listener to be removed
167 public synchronized void removeEventListener(PowermaxStateEventListener listener) {
168 if (connector != null) {
169 connector.removeEventListener(this);
171 listeners.remove(listener);
175 * Connect to the Powermax alarm system
177 * @return true if connected or false if not
179 public void open() throws Exception {
180 if (connector != null) {
184 msgQueue = new ConcurrentLinkedQueue<>();
188 * Close the connection to the Powermax alarm system.
190 * @return true if connected or false if not
192 public boolean close() {
193 if (connector != null) {
196 lastTimeDownloadRequested = null;
197 downloadRunning = false;
198 return isConnected();
202 * @return true if connected to the Powermax alarm system or false if not
204 public boolean isConnected() {
205 return (connector != null) && connector.isConnected();
209 * @return the current settings of the Powermax alarm system
211 public PowermaxPanelSettings getPanelSettings() {
212 return panelSettings;
216 * Process and store all the panel settings from the raw buffers
218 * @param PowerlinkMode true if in Powerlink mode or false if in standard mode
220 * @return true if no problem encountered to get all the settings; false if not
222 public boolean processPanelSettings(boolean powerlinkMode) {
223 return panelSettings.process(powerlinkMode, panelType, powerlinkMode ? syncTimeCheck : null);
227 * @return a new instance of PowermaxState
229 public PowermaxState createNewState() {
230 return new PowermaxState(panelSettings, timeZoneProvider);
234 * @return the last message sent to the Powermax alarm system
236 public synchronized PowermaxBaseMessage getLastSendMsg() {
241 public void onNewMessageEvent(EventObject event) {
242 PowermaxMessageEvent messageEvent = (PowermaxMessageEvent) event;
243 PowermaxBaseMessage message = messageEvent.getMessage();
245 if (logger.isDebugEnabled()) {
246 logger.debug("onNewMessageReceived(): received message 0x{} ({})",
247 HexUtils.bytesToHex(message.getRawData()),
248 (message.getReceiveType() != null) ? message.getReceiveType()
249 : String.format("%02X", message.getCode()));
252 if (forceStandardMode && message instanceof PowermaxPowerlinkMessage) {
253 message = new PowermaxBaseMessage(message.getRawData());
256 PowermaxState updateState = message.handleMessage(this);
258 if (updateState == null) {
259 updateState = createNewState();
262 updateState.lastMessageTime.setValue(System.currentTimeMillis());
264 if (updateState.getUpdateSettings() != null) {
265 panelSettings.updateRawSettings(updateState.getUpdateSettings());
267 if (!updateState.getUpdatedZoneNames().isEmpty()) {
268 for (Integer zoneIdx : updateState.getUpdatedZoneNames().keySet()) {
269 panelSettings.updateZoneName(zoneIdx, updateState.getUpdatedZoneNames().get(zoneIdx));
272 if (!updateState.getUpdatedZoneInfos().isEmpty()) {
273 for (Integer zoneIdx : updateState.getUpdatedZoneInfos().keySet()) {
274 panelSettings.updateZoneInfo(zoneIdx, updateState.getUpdatedZoneInfos().get(zoneIdx));
278 PowermaxStateEvent newEvent = new PowermaxStateEvent(this, updateState);
280 // send message to event listeners
281 listeners.forEach(listener -> listener.onNewStateEvent(newEvent));
285 public void onCommunicationFailure(String message) {
287 listeners.forEach(listener -> listener.onCommunicationFailure(message));
291 * Compute the CRC of a message
293 * @param data the buffer containing the message
294 * @param len the size of the message in the buffer
296 * @return the computed CRC
298 public static byte computeCRC(byte[] data, int len) {
300 for (int i = 1; i < (len - 2); i++) {
301 checksum = checksum + (data[i] & 0x000000FF);
303 checksum = 0xFF - (checksum % 0xFF);
304 if (checksum == 0xFF) {
307 return (byte) checksum;
311 * Send an ACK for a received message
313 * @param msg the received message object
314 * @param ackType the type of ACK to be sent
316 * @return true if the ACK was sent or false if not
318 public synchronized boolean sendAck(PowermaxBaseMessage msg, byte ackType) {
319 int code = msg.getCode();
320 byte[] rawData = msg.getRawData();
322 if ((code >= 0x80) || ((code < 0x10) && (rawData[rawData.length - 3] == 0x43))) {
323 ackData = new byte[] { 0x0D, ackType, 0x43, 0x00, 0x0A };
325 ackData = new byte[] { 0x0D, ackType, 0x00, 0x0A };
328 if (logger.isDebugEnabled()) {
329 logger.debug("sendAck(): sending message {}", HexUtils.bytesToHex(ackData));
331 boolean done = sendMessage(ackData);
333 logger.debug("sendAck(): failed");
339 * Send a message to the Powermax alarm panel to change arm mode
341 * @param armMode the arm mode
342 * @param pinCode the PIN code. A string of 4 characters is expected
344 * @return true if the message was sent or false if not
346 public boolean requestArmMode(PowermaxArmMode armMode, String pinCode) {
347 logger.debug("requestArmMode(): armMode = {}", armMode.getShortName());
349 boolean done = false;
350 if (!armMode.isAllowedCommand()) {
351 logger.debug("Powermax alarm binding: requested arm mode {} rejected", armMode.getShortName());
352 } else if ((pinCode == null) || (pinCode.length() != 4)) {
353 logger.debug("Powermax alarm binding: requested arm mode {} rejected due to invalid PIN code",
354 armMode.getShortName());
357 byte[] dynPart = new byte[3];
358 dynPart[0] = armMode.getCommandCode();
359 dynPart[1] = (byte) Integer.parseInt(pinCode.substring(0, 2), 16);
360 dynPart[2] = (byte) Integer.parseInt(pinCode.substring(2, 4), 16);
362 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.ARM, dynPart), false, 0, true);
363 } catch (NumberFormatException e) {
364 logger.debug("Powermax alarm binding: requested arm mode {} rejected due to invalid PIN code",
365 armMode.getShortName());
372 * Send a message to the Powermax alarm panel to change PGM or X10 zone state
374 * @param action the requested action. Allowed values are: OFF, ON, DIM, BRIGHT
375 * @param device the X10 device number. null is expected for PGM
377 * @return true if the message was sent or false if not
379 public boolean sendPGMX10(Command action, Byte device) {
380 logger.debug("sendPGMX10(): action = {}, device = {}", action, device);
382 boolean done = false;
384 Map<String, Byte> codes = new HashMap<>();
385 codes.put("OFF", (byte) 0x00);
386 codes.put("ON", (byte) 0x01);
387 codes.put("DIM", (byte) 0x0A);
388 codes.put("BRIGHT", (byte) 0x0B);
390 Byte code = codes.get(action.toString());
392 logger.debug("Powermax alarm binding: invalid PGM/X10 command: {}", action);
393 } else if ((device != null) && ((device < 1) || (device >= panelSettings.getNbPGMX10Devices()))) {
394 logger.debug("Powermax alarm binding: invalid X10 device id: {}", device);
396 int val = (device == null) ? 1 : (1 << device);
397 byte[] dynPart = new byte[3];
399 dynPart[1] = (byte) (val & 0x000000FF);
400 dynPart[2] = (byte) (val >> 8);
402 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.X10PGM, dynPart), false, 0);
408 * Send a message to the Powermax alarm panel to bypass a zone or to not bypass a zone
410 * @param bypass true to bypass the zone; false to not bypass the zone
411 * @param zone the zone number (first zone is number 1)
412 * @param pinCode the PIN code. A string of 4 characters is expected
414 * @return true if the message was sent or false if not
416 public boolean sendZoneBypass(boolean bypass, byte zone, String pinCode) {
417 logger.debug("sendZoneBypass(): bypass = {}, zone = {}", bypass ? "true" : "false", zone);
419 boolean done = false;
421 if ((pinCode == null) || (pinCode.length() != 4)) {
422 logger.debug("Powermax alarm binding: zone bypass rejected due to invalid PIN code");
423 } else if ((zone < 1) || (zone > panelSettings.getNbZones())) {
424 logger.debug("Powermax alarm binding: invalid zone number: {}", zone);
427 int val = (1 << (zone - 1));
429 byte[] dynPart = new byte[10];
430 dynPart[0] = (byte) Integer.parseInt(pinCode.substring(0, 2), 16);
431 dynPart[1] = (byte) Integer.parseInt(pinCode.substring(2, 4), 16);
433 for (i = 2; i < 10; i++) {
437 dynPart[i++] = (byte) (val & 0x000000FF);
438 dynPart[i++] = (byte) ((val >> 8) & 0x000000FF);
439 dynPart[i++] = (byte) ((val >> 16) & 0x000000FF);
440 dynPart[i++] = (byte) ((val >> 24) & 0x000000FF);
442 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.BYPASS, dynPart), false, 0, true);
444 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.BYPASSTAT), false, 0);
446 } catch (NumberFormatException e) {
447 logger.debug("Powermax alarm binding: zone bypass rejected due to invalid PIN code");
454 * Send a message to set the alarm time and date using the system time and date
456 * @return true if the message was sent or false if not
458 public boolean sendSetTime() {
459 logger.debug("sendSetTime()");
461 boolean done = false;
464 GregorianCalendar cal = new GregorianCalendar();
465 if (cal.get(Calendar.YEAR) >= 2000) {
466 logger.debug("sendSetTime(): sync time {}",
467 String.format("%02d/%02d/%04d %02d:%02d:%02d", cal.get(Calendar.DAY_OF_MONTH),
468 cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR), cal.get(Calendar.HOUR_OF_DAY),
469 cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)));
471 byte[] dynPart = new byte[6];
472 dynPart[0] = (byte) cal.get(Calendar.SECOND);
473 dynPart[1] = (byte) cal.get(Calendar.MINUTE);
474 dynPart[2] = (byte) cal.get(Calendar.HOUR_OF_DAY);
475 dynPart[3] = (byte) cal.get(Calendar.DAY_OF_MONTH);
476 dynPart[4] = (byte) (cal.get(Calendar.MONTH) + 1);
477 dynPart[5] = (byte) (cal.get(Calendar.YEAR) - 2000);
479 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.SETTIME, dynPart), false, 0);
481 cal.set(Calendar.MILLISECOND, 0);
482 syncTimeCheck = cal.getTimeInMillis();
485 "Powermax alarm binding: time not synchronized; please correct the date/time of your openHAB server");
486 syncTimeCheck = null;
489 syncTimeCheck = null;
495 * Send a message to the Powermax alarm panel to get all the event logs
497 * @param pinCode the PIN code. A string of 4 characters is expected
499 * @return true if the message was sent or false if not
501 public boolean requestEventLog(String pinCode) {
502 logger.debug("requestEventLog()");
504 boolean done = false;
506 if ((pinCode == null) || (pinCode.length() != 4)) {
507 logger.debug("Powermax alarm binding: requested event log rejected due to invalid PIN code");
510 byte[] dynPart = new byte[3];
511 dynPart[0] = (byte) Integer.parseInt(pinCode.substring(0, 2), 16);
512 dynPart[1] = (byte) Integer.parseInt(pinCode.substring(2, 4), 16);
514 done = sendMessage(new PowermaxBaseMessage(PowermaxSendType.EVENTLOG, dynPart), false, 0, true);
515 } catch (NumberFormatException e) {
516 logger.debug("Powermax alarm binding: requested event log rejected due to invalid PIN code");
523 * Start downloading panel setup
525 * @return true if the message was sent or the sending is delayed; false in other cases
527 public synchronized boolean startDownload() {
528 if (downloadRunning) {
531 lastTimeDownloadRequested = System.currentTimeMillis();
532 downloadRunning = true;
533 return sendMessage(PowermaxSendType.DOWNLOAD);
538 * Act the exit of the panel setup
540 public synchronized void exitDownload() {
541 downloadRunning = false;
544 public void retryDownloadSetup(int remainingAttempts) {
545 long now = System.currentTimeMillis();
546 if ((remainingAttempts > 0) && !isDownloadRunning() && ((lastTimeDownloadRequested == null)
547 || ((now - lastTimeDownloadRequested) >= DELAY_BETWEEN_SETUP_DOWNLOADS))) {
548 // We wait at least 45 seconds before each retry to download the panel setup
549 logger.debug("Powermax alarm binding: try again downloading setup");
554 public void getInfosWhenInStandardMode() {
555 sendMessage(PowermaxSendType.ZONESNAME);
556 sendMessage(PowermaxSendType.ZONESTYPE);
557 sendMessage(PowermaxSendType.STATUS);
560 public void sendRestoreMessage() {
561 sendMessage(PowermaxSendType.RESTORE);
565 * @return true if a download of the panel setup is in progress
567 public boolean isDownloadRunning() {
568 return downloadRunning;
572 * @return the time in milliseconds the last download of the panel setup was requested or null if not yet requested
574 public Long getLastTimeDownloadRequested() {
575 return lastTimeDownloadRequested;
579 * Send a ENROLL message
581 * @return true if the message was sent or the sending is delayed; false in other cases
583 public boolean enrollPowerlink() {
584 return sendMessage(new PowermaxBaseMessage(PowermaxSendType.ENROLL), true, 0);
588 * Send a message or delay the sending if time frame for receiving response is not ended
590 * @param msgType the message type to be sent
592 * @return true if the message was sent or the sending is delayed; false in other cases
594 public boolean sendMessage(PowermaxSendType msgType) {
595 return sendMessage(new PowermaxBaseMessage(msgType), false, 0);
599 * Delay the sending of a message
601 * @param msgType the message type to be sent
602 * @param waitTime the delay in seconds to wait
604 * @return true if the sending is delayed; false in other cases
606 public boolean sendMessageLater(PowermaxSendType msgType, int waitTime) {
607 return sendMessage(new PowermaxBaseMessage(msgType), false, waitTime);
610 private synchronized boolean sendMessage(PowermaxBaseMessage msg, boolean immediate, int waitTime) {
611 return sendMessage(msg, immediate, waitTime, false);
615 * Send a message or delay the sending if time frame for receiving response is not ended
617 * @param msg the message to be sent
618 * @param immediate true if the message has to be send without considering timing
619 * @param waitTime the delay in seconds to wait
620 * @param doNotLog true if the message contains data that must not be logged
622 * @return true if the message was sent or the sending is delayed; false in other cases
624 @SuppressWarnings("PMD.CompareObjectsWithEquals")
625 private synchronized boolean sendMessage(PowermaxBaseMessage msg, boolean immediate, int waitTime,
627 if ((waitTime > 0) && (msg != null)) {
628 logger.debug("sendMessage(): delay ({} s) sending message (type {})", waitTime, msg.getSendType());
629 // Don't queue the message
630 PowermaxBaseMessage msgToSendLater = new PowermaxBaseMessage(msg.getRawData());
631 msgToSendLater.setSendType(msg.getSendType());
632 scheduler.schedule(() -> {
633 sendMessage(msgToSendLater, false, 0);
634 }, waitTime, TimeUnit.SECONDS);
639 msg = msgQueue.peek();
641 logger.debug("sendMessage(): nothing to send");
646 // Delay sending if time frame for receiving response is not ended
647 long delay = WAITING_DELAY_FOR_RESPONSE - (System.currentTimeMillis() - connector.getWaitingForResponse());
649 PowermaxBaseMessage msgToSend = msg;
652 msgToSend = msgQueue.peek();
653 if (msgToSend != msg) {
654 logger.debug("sendMessage(): add message in queue (type {})", msg.getSendType());
656 msgToSend = msgQueue.peek();
658 if ((msgToSend != msg) && (delay > 0)) {
660 } else if ((msgToSend == msg) && (delay > 0)) {
664 logger.debug("sendMessage(): delay ({} ms) sending message (type {})", delay, msgToSend.getSendType());
665 scheduler.schedule(() -> {
666 sendMessage(null, false, 0);
667 }, delay, TimeUnit.MILLISECONDS);
670 msgToSend = msgQueue.poll();
674 if (logger.isDebugEnabled()) {
675 logger.debug("sendMessage(): sending {} message {}", msgToSend.getSendType(),
676 doNotLog ? "***" : HexUtils.bytesToHex(msgToSend.getRawData()));
678 boolean done = sendMessage(msgToSend.getRawData());
680 lastSendMsg = msgToSend;
681 connector.setWaitingForResponse(System.currentTimeMillis());
683 if (!immediate && (msgQueue.peek() != null)) {
684 logger.debug("sendMessage(): delay sending next message (type {})", msgQueue.peek().getSendType());
685 scheduler.schedule(() -> {
686 sendMessage(null, false, 0);
687 }, WAITING_DELAY_FOR_RESPONSE, TimeUnit.MILLISECONDS);
690 logger.debug("sendMessage(): failed");
697 * Send a message to the Powermax alarm panel
699 * @param data the data buffer containing the message to be sent
701 * @return true if the message was sent or false if not
703 private boolean sendMessage(byte[] data) {
704 boolean done = false;
706 data[data.length - 2] = computeCRC(data, data.length);
707 connector.sendMessage(data);
708 done = connector.isConnected();
710 logger.debug("sendMessage(): aborted (not connected)");