2 * Copyright (c) 2010-2022 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.lcn.internal.common;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.lcn.internal.LcnBindingConstants;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Helpers to generate LCN-PCK commands.
23 * LCN-PCK is the command-syntax used by LCN-PCHK to send and receive LCN commands.
25 * @author Tobias Jüttner - Initial Contribution
26 * @author Fabian Wolter - Migration to OH2
29 public final class PckGenerator {
30 private static final Logger LOGGER = LoggerFactory.getLogger(PckGenerator.class);
31 /** Termination character after a PCK message */
32 public static final String TERMINATION = "\n";
35 * Generates a keep-alive.
36 * LCN-PCHK will close the connection if it does not receive any commands from
37 * an open {@link Connection} for a specific period (10 minutes by default).
39 * @param counter the current ping's id (optional, but "best practice"). Should start with 1
40 * @return the PCK command as text
42 public static String ping(int counter) {
43 return String.format("^ping%d", counter);
47 * Generates a PCK command that will set the LCN-PCHK connection's operation mode.
48 * This influences how output-port commands and status are interpreted and must be
49 * in sync with the LCN bus.
51 * @param dimMode see {@link LcnDefs.OutputPortDimMode}
52 * @param statusMode see {@link LcnDefs.OutputPortStatusMode}
53 * @return the PCK command as text
55 public static String setOperationMode(LcnDefs.OutputPortDimMode dimMode, LcnDefs.OutputPortStatusMode statusMode) {
56 return "!OM" + (dimMode == LcnDefs.OutputPortDimMode.NATIVE200 ? "1" : "0")
57 + (statusMode == LcnDefs.OutputPortStatusMode.PERCENT ? "P" : "N");
61 * Generates a PCK address header.
62 * Used for commands to LCN modules and groups.
64 * @param addr the target's address (module or group)
65 * @param localSegId the local segment id where the physical bus connection is located
66 * @param wantsAck true to claim an acknowledge / receipt from the target
67 * @return the PCK address header as text
69 public static String generateAddressHeader(LcnAddr addr, int localSegId, boolean wantsAck) {
70 return String.format(">%s%03d%03d%s", addr.isGroup() ? "G" : "M", addr.getPhysicalSegmentId(localSegId),
71 addr.getId(), wantsAck ? "!" : ".");
75 * Generates a scan-command for LCN segment-couplers.
76 * Used to detect the local segment (where the physical bus connection is located).
78 * @return the PCK command (without address header) as text
80 public static String segmentCouplerScan() {
85 * Generates a firmware/serial-number request.
87 * @return the PCK command (without address header) as text
89 public static String requestSn() {
94 * Generates a command to request a part of a name of a module.
96 * @param partNumber 0..1
97 * @return the PCK command (without address header) as text
99 public static String requestModuleName(int partNumber) {
100 return "NMN" + (partNumber + 1);
104 * Generates an output-port status request.
106 * @param outputId 0..3
107 * @return the PCK command (without address header) as text
108 * @throws LcnException if out of range
110 public static String requestOutputStatus(int outputId) throws LcnException {
111 if (outputId < 0 || outputId > 3) {
112 throw new LcnException();
114 return String.format("SMA%d", outputId + 1);
118 * Generates a dim command for a single output-port.
120 * @param outputId 0..3
121 * @param percent 0..100
122 * @param rampMs ramp in milliseconds
123 * @return the PCK command (without address header) as text
124 * @throws LcnException if out of range
126 public static String dimOutput(int outputId, double percent, int rampMs) throws LcnException {
127 if (outputId < 0 || outputId > 3) {
128 throw new LcnException();
130 int rampNative = PckGenerator.timeToRampValue(rampMs);
131 int n = (int) Math.round(percent * 2);
132 if ((n % 2) == 0) { // Use the percent command (supported by all LCN-PCHK versions)
133 return String.format("A%dDI%03d%03d", outputId + 1, n / 2, rampNative);
134 } else { // We have a ".5" value. Use the native command (supported since LCN-PCHK 2.3)
135 return String.format("O%dDI%03d%03d", outputId + 1, n, rampNative);
140 * Generates a dim command for all output-ports.
142 * Attention: This command is supported since module firmware version 180501 AND LCN-PCHK 2.61
144 * @param firstPercent dimmer value of the first output 0..100
145 * @param secondPercent dimmer value of the first output 0..100
146 * @param thirdPercent dimmer value of the first output 0..100
147 * @param fourthPercent dimmer value of the first output 0..100
148 * @param rampMs ramp in milliseconds
149 * @return the PCK command (without address header) as text
151 public static String dimAllOutputs(double firstPercent, double secondPercent, double thirdPercent,
152 double fourthPercent, int rampMs) {
153 long n1 = Math.round(firstPercent * 2);
154 long n2 = Math.round(secondPercent * 2);
155 long n3 = Math.round(thirdPercent * 2);
156 long n4 = Math.round(fourthPercent * 2);
158 return String.format("OY%03d%03d%03d%03d%03d", n1, n2, n3, n4, timeToRampValue(rampMs));
162 * Generates a control command for switching all outputs ON or OFF with a fixed ramp of 0.5s.
164 * @param percent 0..100
165 * @returnthe PCK command (without address header) as text
167 public static String controlAllOutputs(double percent) {
168 return String.format("AH%03d", Math.round(percent));
172 * Generates a control command for switching dimmer output 1 and 2 both ON or OFF with a fixed ramp of 0.5s or
175 * @param on true, if outputs shall be switched on
176 * @param ramp true, if the ramp shall be 0.5s, else 0s
177 * @return the PCK command (without address header) as text
179 public static String controlOutputs12(boolean on, boolean ramp) {
182 commandByte = ramp ? 0xC8 : 0xFD;
184 commandByte = ramp ? 0x00 : 0xFC;
186 return String.format("X2%03d%03d%03d", 1, commandByte, commandByte);
190 * Generates a dim command for setting the brightness of dimmer output 1 and 2 with a fixed ramp of 0.5s.
192 * @param percent brightness of both outputs 0..100
193 * @return the PCK command (without address header) as text
195 public static String dimOutputs12(double percent) {
196 long localPercent = Math.round(percent);
197 return String.format("AY%03d%03d", localPercent, localPercent);
201 * Let an output flicker.
203 * @param outputId output id 0..3
204 * @param depth flicker depth, the higher the deeper 0..2
205 * @param ramp the flicker speed 0..2
206 * @param count number of flashes 1..15
207 * @return the PCK command (without address header) as text
208 * @throws LcnException when the input values are out of range
210 public static String flickerOutput(int outputId, int depth, int ramp, int count) throws LcnException {
211 if (outputId < 0 || outputId > 3) {
212 throw new LcnException("Output number out of range");
214 if (count < 1 || count > 15) {
215 throw new LcnException("Number of flashes out of range");
229 throw new LcnException("Depth out of range");
243 throw new LcnException("Ramp out of range");
245 return String.format("A%dFL%s%s%02d", outputId + 1, depthString, rampString, count);
249 * Generates a command to change the value of an output-port.
251 * @param outputId 0..3
252 * @param percent -100..100
253 * @return the PCK command (without address header) as text
254 * @throws LcnException if out of range
256 public static String relOutput(int outputId, double percent) throws LcnException {
257 if (outputId < 0 || outputId > 3) {
258 throw new LcnException();
260 int n = (int) Math.round(percent * 2);
261 if ((n % 2) == 0) { // Use the percent command (supported by all LCN-PCHK versions)
262 return String.format("A%d%s%03d", outputId + 1, percent >= 0 ? "AD" : "SB", Math.abs(n / 2));
263 } else { // We have a ".5" value. Use the native command (supported since LCN-PCHK 2.3)
264 return String.format("O%d%s%03d", outputId + 1, percent >= 0 ? "AD" : "SB", Math.abs(n));
269 * Generates a command that toggles a single output-port (on->off, off->on).
271 * @param outputId 0..3
272 * @param ramp see {@link PckGenerator#timeToRampValue(int)}
273 * @return the PCK command (without address header) as text
274 * @throws LcnException if out of range
276 public static String toggleOutput(int outputId, int ramp) throws LcnException {
277 if (outputId < 0 || outputId > 3) {
278 throw new LcnException();
280 return String.format("A%dTA%03d", outputId + 1, ramp);
284 * Generates a command that toggles all output-ports (on->off, off->on).
286 * @param ramp see {@link PckGenerator#timeToRampValue(int)}
287 * @return the PCK command (without address header) as text
289 public static String toggleAllOutputs(int ramp) {
290 return String.format("AU%03d", ramp);
294 * Generates a relays-status request.
296 * @return the PCK command (without address header) as text
298 public static String requestRelaysStatus() {
303 * Generates a command to control relays.
305 * @param states the 8 modifiers for the relay states
306 * @return the PCK command (without address header) as text
307 * @throws LcnException if out of range
309 public static String controlRelays(LcnDefs.RelayStateModifier[] states) throws LcnException {
310 if (states.length != 8) {
311 throw new LcnException();
313 StringBuilder ret = new StringBuilder("R8");
314 for (int i = 0; i < 8; ++i) {
329 throw new LcnException();
332 return ret.toString();
336 * Generates a binary-sensors status request.
338 * @return the PCK command (without address header) as text
340 public static String requestBinSensorsStatus() {
345 * Generates a command that sets a variable absolute.
347 * @param number regulator number 0..1
348 * @param value the absolute value to set
349 * @return the PCK command (without address header) as text
350 * @throws LcnException
352 public static String setSetpointAbsolute(int number, int value) {
353 int internalValue = value;
354 // Set absolute (not in PCK yet)
355 int b1 = number << 6; // 01000000
356 b1 |= 0x20; // xx10xxxx (set absolute)
358 internalValue = 1000 - internalValue;
361 internalValue -= 1000;
363 b1 |= (internalValue >> 8) & 0x0f; // xxxx1111
364 int b2 = internalValue & 0xff;
365 return String.format("X2%03d%03d%03d", 30, b1, b2);
369 * Generates a command to change the value of a variable.
371 * @param variable the target variable to change
372 * @param type the reference-point
373 * @param value the native LCN value to add/subtract (can be negative)
374 * @return the PCK command (without address header) as text
375 * @throws LcnException if command is not supported
377 public static String setVariableRelative(Variable variable, LcnDefs.RelVarRef type, int value) {
378 if (variable.getNumber() == 0) {
379 // Old command for variable 1 / T-var (compatible with all modules)
380 return String.format("Z%s%d", value >= 0 ? "A" : "S", Math.abs(value));
381 } else { // New command for variable 1-12 (compatible with all modules, since LCN-PCHK 2.8)
382 return String.format("Z%s%03d%d", value >= 0 ? "+" : "-", variable.getNumber() + 1, Math.abs(value));
387 * Generates a command the change the value of a regulator setpoint relative.
390 * @param type relative to the current or to the programmed value
391 * @param value the relative value -4000..+4000
392 * @return the PCK command (without address header) as text
394 public static String setSetpointRelative(int number, LcnDefs.RelVarRef type, int value) {
395 return String.format("RE%sS%s%s%d", number == 0 ? "A" : "B", type == LcnDefs.RelVarRef.CURRENT ? "A" : "P",
396 value >= 0 ? "+" : "-", Math.abs(value));
400 * Generates a command the change the value of a threshold relative.
402 * @param variable the threshold to change
403 * @param type relative to the current or to the programmed value
404 * @param value the relative value -4000..+4000
405 * @param is2013 true, if the LCN module's firmware is equal to or newer than 2013
406 * @return the PCK command (without address header) as text
408 public static String setThresholdRelative(Variable variable, LcnDefs.RelVarRef type, int value, boolean is2013)
409 throws LcnException {
410 if (is2013) { // New command for registers 1-4 (since 170206, LCN-PCHK 2.8)
411 return String.format("SS%s%04d%sR%d%d", type == LcnDefs.RelVarRef.CURRENT ? "R" : "E", Math.abs(value),
412 value >= 0 ? "A" : "S", variable.getNumber() + 1, variable.getThresholdNumber().get() + 1);
413 } else if (variable.getNumber() == 0) { // Old command for register 1 (before 170206)
414 return String.format("SS%s%04d%s%s%s%s%s%s", type == LcnDefs.RelVarRef.CURRENT ? "R" : "E", Math.abs(value),
415 value >= 0 ? "A" : "S", variable.getThresholdNumber().get() == 0 ? "1" : "0",
416 variable.getThresholdNumber().get() == 1 ? "1" : "0",
417 variable.getThresholdNumber().get() == 2 ? "1" : "0",
418 variable.getThresholdNumber().get() == 3 ? "1" : "0",
419 variable.getThresholdNumber().get() == 4 ? "1" : "0");
421 throw new LcnException(
422 "Module does not have threshold register " + (variable.getThresholdNumber().get() + 1));
427 * Generates a variable value request.
429 * @param variable the variable to request
430 * @param firmwareVersion the target module's firmware version
431 * @return the PCK command (without address header) as text
432 * @throws LcnException if command is not supported
434 public static String requestVarStatus(Variable variable, int firmwareVersion) throws LcnException {
435 if (firmwareVersion >= LcnBindingConstants.FIRMWARE_2013) {
436 int id = variable.getNumber();
437 switch (variable.getType()) {
439 throw new LcnException("Variable unknown");
441 return String.format("MWT%03d", id + 1);
443 return String.format("MWS%03d", id + 1);
445 return String.format("SE%03d", id + 1); // Whole register
447 return String.format("MWC%03d", id + 1);
449 throw new LcnException("Unsupported variable type: " + variable);
462 case THRESHOLDREGISTER11:
463 case THRESHOLDREGISTER12:
464 case THRESHOLDREGISTER13:
465 case THRESHOLDREGISTER14:
466 case THRESHOLDREGISTER15:
467 return "SL1"; // Whole register
469 throw new LcnException("Unsupported variable type: " + variable);
475 * Generates a request for LED and logic-operations states.
477 * @return the PCK command (without address header) as text
479 public static String requestLedsAndLogicOpsStatus() {
484 * Generates a command to the set the state of a single LED.
487 * @param state the state to set
488 * @return the PCK command (without address header) as text
489 * @throws LcnException if out of range
491 public static String controlLed(int ledId, LcnDefs.LedStatus state) throws LcnException {
492 if (ledId < 0 || ledId > 11) {
493 throw new LcnException();
495 return String.format("LA%03d%s", ledId + 1, state == LcnDefs.LedStatus.OFF ? "A"
496 : state == LcnDefs.LedStatus.ON ? "E" : state == LcnDefs.LedStatus.BLINK ? "B" : "F");
500 * Generates a command to send LCN keys.
502 * @param cmds the 4 concrete commands to send for the tables (A-D)
503 * @param keys the tables' 8 key-states (true means "send")
504 * @return the PCK command (without address header) as text
505 * @throws LcnException if out of range
507 public static String sendKeys(LcnDefs.SendKeyCommand[] cmds, boolean[] keys) throws LcnException {
508 if (cmds.length != 4 || keys.length != 8) {
509 throw new LcnException();
511 StringBuilder ret = new StringBuilder("TS");
512 for (int i = 0; i < 4; ++i) {
524 // By skipping table D (if it is not used), we use the old command
525 // for table A-C which is compatible with older LCN modules
531 throw new LcnException();
534 for (int i = 0; i < 8; ++i) {
535 ret.append(keys[i] ? "1" : "0");
537 return ret.toString();
541 * Generates a command to send LCN keys deferred / delayed.
543 * @param tableId 0(A)..3(D)
544 * @param time the delay time
545 * @param timeUnit the time unit
546 * @param keys the key-states (true means "send")
547 * @return the PCK command (without address header) as text
548 * @throws LcnException if out of range
550 public static String sendKeysHitDefered(int tableId, int time, LcnDefs.TimeUnit timeUnit, boolean[] keys)
551 throws LcnException {
552 if (tableId < 0 || tableId > 3 || keys.length != 8) {
553 throw new LcnException();
555 StringBuilder ret = new StringBuilder("TV");
570 throw new LcnException();
572 ret.append(String.format("%03d", time));
575 if (time < 1 || time > 60) {
576 throw new LcnException();
581 if (time < 1 || time > 90) {
582 throw new LcnException();
587 if (time < 1 || time > 50) {
588 throw new LcnException();
593 if (time < 1 || time > 45) {
594 throw new LcnException();
599 throw new LcnException();
601 for (int i = 0; i < 8; ++i) {
602 ret.append(keys[i] ? "1" : "0");
604 return ret.toString();
608 * Generates a request for key-lock states.
609 * Always requests table A-D. Supported since LCN-PCHK 2.8.
611 * @return the PCK command (without address header) as text
613 public static String requestKeyLocksStatus() {
618 * Generates a command to lock keys.
620 * @param tableId 0(A)..3(D)
621 * @param states the 8 key-lock modifiers
622 * @return the PCK command (without address header) as text
623 * @throws LcnException if out of range
625 public static String lockKeys(int tableId, LcnDefs.KeyLockStateModifier[] states) throws LcnException {
626 if (tableId < 0 || tableId > 3 || states.length != 8) {
627 throw new LcnException();
629 StringBuilder ret = new StringBuilder(
630 String.format("TX%s", tableId == 0 ? "A" : tableId == 1 ? "B" : tableId == 2 ? "C" : "D"));
631 for (int i = 0; i < 8; ++i) {
646 throw new LcnException();
649 return ret.toString();
653 * Generates a command to lock keys for table A temporary.
654 * There is no hardware-support for locking tables B-D.
656 * @param time the lock time
657 * @param timeUnit the time unit
658 * @param keys the 8 key-lock states (true means lock)
659 * @return the PCK command (without address header) as text
660 * @throws LcnException if out of range
662 public static String lockKeyTabATemporary(int time, LcnDefs.TimeUnit timeUnit, boolean[] keys) throws LcnException {
663 if (keys.length != 8) {
664 throw new LcnException();
666 StringBuilder ret = new StringBuilder(String.format("TXZA%03d", time));
669 if (time < 1 || time > 60) {
670 throw new LcnException();
675 if (time < 1 || time > 90) {
676 throw new LcnException();
681 if (time < 1 || time > 50) {
682 throw new LcnException();
687 if (time < 1 || time > 45) {
688 throw new LcnException();
693 throw new LcnException();
695 for (int i = 0; i < 8; ++i) {
696 ret.append(keys[i] ? "1" : "0");
698 return ret.toString();
702 * Generates the command header / start for sending dynamic texts.
703 * Used by LCN-GTxD periphery (supports 4 text rows).
704 * To complete the command, the text to send must be appended (UTF-8 encoding).
705 * Texts are split up into up to 5 parts with 12 "UTF-8 bytes" each.
709 * @return the PCK command (without address header) as text
710 * @throws LcnException if out of range
712 public static String dynTextHeader(int row, int part) throws LcnException {
713 if (row < 0 || row > 3 || part < 0 || part > 4) {
714 throw new LcnException("Row number is out of range: " + (row + 1));
716 return String.format("GTDT%d%d", row + 1, part + 1);
720 * Generates a command to lock a regulator.
723 * @param state the lock state
724 * @return the PCK command (without address header) as text
725 * @throws LcnException if out of range
727 public static String lockRegulator(int regId, boolean state) throws LcnException {
728 if (regId < 0 || regId > 1) {
729 throw new LcnException();
731 return String.format("RE%sX%s", regId == 0 ? "A" : "B", state ? "S" : "A");
735 * Generates a command to start a relay timer
737 * @param relayNumber number of relay (1..8)
738 * @param duration duration in milliseconds
739 * @return the PCK command (without address header) as text
740 * @throws LcnException if out of range
742 public static String startRelayTimer(int relayNumber, double duration) throws LcnException {
743 if (relayNumber < 1 || relayNumber > 8 || duration < 30 || duration > 240960) {
744 throw new LcnException();
746 StringBuilder command = new StringBuilder("R8T");
747 command.append(String.format("%03d", convertMsecToLCNTimer(duration)));
748 StringBuilder data = new StringBuilder("--------");
749 data.setCharAt(relayNumber - 1, '1');
750 command.append(data);
751 return command.toString();
755 * Generates a null command, used for broadcast messages.
757 * @return the PCK command (without address header) as text
759 public static String nullCommand() {
764 * Converts the given time into an LCN ramp value.
766 * @param timeMSec the time in milliseconds
767 * @return the (LCN-internal) ramp value (0..250)
769 private static int timeToRampValue(int timeMSec) {
771 if (timeMSec < 250) {
773 } else if (timeMSec < 500) {
775 } else if (timeMSec < 660) {
777 } else if (timeMSec < 1000) {
779 } else if (timeMSec < 1400) {
781 } else if (timeMSec < 2000) {
783 } else if (timeMSec < 3000) {
785 } else if (timeMSec < 4000) {
787 } else if (timeMSec < 5000) {
789 } else if (timeMSec < 6000) {
792 ret = (timeMSec / 1000 - 6) / 2 + 10;
795 LOGGER.warn("Ramp value is too high. Limiting value to 486s.");
802 * Converts duration in milliseconds to lcntimer value
803 * Source: https://www.symcon.de/forum/threads/38603-LCN-Relais-Kurzzeit-Timer-umrechnen
805 * @param ms time in milliseconds
806 * @return lcn timer value
808 private static int convertMsecToLCNTimer(double ms) {
809 Integer lcntimer = -1;
810 if (ms >= 0 && ms <= 240960) {
811 double a = ms / 1000 / 0.03;
812 double b = (a / 32.0) + 1.0;
813 double c = Math.log(b) / Math.log(2);
814 double mod = Math.floor(c);
815 double faktor = 32 * (Math.pow(2, mod) - 1);
816 double offset = (a - faktor) / Math.pow(2, mod);
817 lcntimer = (int) (offset + mod * 32);
819 LOGGER.warn("Timer not in [0,240960] ms");