|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 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; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.openhab.binding.paradoxalarm.internal.model.ParadoxPanel; |
| 20 | + |
| 21 | +/** |
| 22 | + * Verifies that {@link ParadoxPanel#parseTroubleFlags} extracts the correct bits from RAM block 1. |
| 23 | + * |
| 24 | + * Byte/bit offsets are derived from the PAI EVO RAMDataParserMap[1] BitStruct |
| 25 | + * (paradox/hardware/evo/parsers.py) and validated against live EVO192 RAM captures. |
| 26 | + * |
| 27 | + * Trouble flags are in RAMDataParserMap[1] (RAM block number 2 in PAI), but stored at |
| 28 | + * memoryMap.getElement(0) (RAM block number 1 = first 64-byte page, 0-indexed). |
| 29 | + * The troubles BitStruct starts at byte offset 13 within that page. |
| 30 | + * |
| 31 | + * RAM block 1 layout (relevant bytes, construct BitStruct MSB-first ordering): |
| 32 | + * byte 13: module_supervision_trouble = bit 3 from LSB (mask 0x08) |
| 33 | + * byte 14: battery_failure_trouble = bit 1 from LSB (mask 0x02) |
| 34 | + * ac_trouble = bit 0 from LSB (mask 0x01) |
| 35 | + * byte 15: com_pc_trouble = bit 5 from LSB (mask 0x20) |
| 36 | + */ |
| 37 | +@NonNullByDefault |
| 38 | +public class TestParadoxPanelTroubleFlags { |
| 39 | + |
| 40 | + private ParadoxPanel panel = new ParadoxPanel(); |
| 41 | + |
| 42 | + private byte[] emptyRamBlock() { |
| 43 | + return new byte[64]; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testNoTroublesWhenAllBytesZero() { |
| 48 | + panel.parseTroubleFlags(emptyRamBlock()); |
| 49 | + assertFalse(panel.isAcTrouble()); |
| 50 | + assertFalse(panel.isBatteryTrouble()); |
| 51 | + assertFalse(panel.isModuleSupervisionTrouble()); |
| 52 | + assertFalse(panel.isCommunicationTrouble()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testAcTrouble() { |
| 57 | + byte[] ram = emptyRamBlock(); |
| 58 | + ram[14] |= 0x01; // ac_trouble = bit 0 of byte 14 |
| 59 | + panel.parseTroubleFlags(ram); |
| 60 | + assertTrue(panel.isAcTrouble()); |
| 61 | + assertFalse(panel.isBatteryTrouble()); |
| 62 | + assertFalse(panel.isModuleSupervisionTrouble()); |
| 63 | + assertFalse(panel.isCommunicationTrouble()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testBatteryTrouble() { |
| 68 | + byte[] ram = emptyRamBlock(); |
| 69 | + ram[14] |= 0x02; // battery_failure_trouble = bit 1 of byte 14 |
| 70 | + panel.parseTroubleFlags(ram); |
| 71 | + assertFalse(panel.isAcTrouble()); |
| 72 | + assertTrue(panel.isBatteryTrouble()); |
| 73 | + assertFalse(panel.isModuleSupervisionTrouble()); |
| 74 | + assertFalse(panel.isCommunicationTrouble()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testModuleSupervisionTrouble() { |
| 79 | + byte[] ram = emptyRamBlock(); |
| 80 | + ram[13] |= 0x08; // module_supervision_trouble = bit 3 of byte 13 |
| 81 | + panel.parseTroubleFlags(ram); |
| 82 | + assertFalse(panel.isAcTrouble()); |
| 83 | + assertFalse(panel.isBatteryTrouble()); |
| 84 | + assertTrue(panel.isModuleSupervisionTrouble()); |
| 85 | + assertFalse(panel.isCommunicationTrouble()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testCommunicationTrouble() { |
| 90 | + byte[] ram = emptyRamBlock(); |
| 91 | + ram[15] |= 0x20; // com_pc_trouble = bit 5 of byte 15 |
| 92 | + panel.parseTroubleFlags(ram); |
| 93 | + assertFalse(panel.isAcTrouble()); |
| 94 | + assertFalse(panel.isBatteryTrouble()); |
| 95 | + assertFalse(panel.isModuleSupervisionTrouble()); |
| 96 | + assertTrue(panel.isCommunicationTrouble()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testAdjacentBitsDoNotTriggerFalsePositives() { |
| 101 | + byte[] ram = emptyRamBlock(); |
| 102 | + // Set every bit around the target bits to ensure no mask overlap |
| 103 | + ram[13] = (byte) 0xF7; // all bits set except bit 3 (module_supervision) |
| 104 | + ram[14] = (byte) 0xFC; // all bits set except bits 0 and 1 (ac + battery) |
| 105 | + ram[15] = (byte) 0xDF; // all bits set except bit 5 (com_pc) |
| 106 | + panel.parseTroubleFlags(ram); |
| 107 | + assertFalse(panel.isAcTrouble()); |
| 108 | + assertFalse(panel.isBatteryTrouble()); |
| 109 | + assertFalse(panel.isModuleSupervisionTrouble()); |
| 110 | + assertFalse(panel.isCommunicationTrouble()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testAllTroublesSimultaneously() { |
| 115 | + byte[] ram = emptyRamBlock(); |
| 116 | + ram[13] |= 0x08; |
| 117 | + ram[14] |= 0x03; // both ac (0x01) and battery (0x02) |
| 118 | + ram[15] |= 0x20; |
| 119 | + panel.parseTroubleFlags(ram); |
| 120 | + assertTrue(panel.isAcTrouble()); |
| 121 | + assertTrue(panel.isBatteryTrouble()); |
| 122 | + assertTrue(panel.isModuleSupervisionTrouble()); |
| 123 | + assertTrue(panel.isCommunicationTrouble()); |
| 124 | + } |
| 125 | +} |
0 commit comments