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.rotel.internal.handler;
15 import static org.openhab.binding.rotel.internal.RotelBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.EventObject;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.rotel.internal.RotelBindingConstants;
29 import org.openhab.binding.rotel.internal.RotelCommandDescriptionOptionProvider;
30 import org.openhab.binding.rotel.internal.RotelException;
31 import org.openhab.binding.rotel.internal.RotelModel;
32 import org.openhab.binding.rotel.internal.RotelPlayStatus;
33 import org.openhab.binding.rotel.internal.RotelRepeatMode;
34 import org.openhab.binding.rotel.internal.RotelStateDescriptionOptionProvider;
35 import org.openhab.binding.rotel.internal.communication.RotelCommand;
36 import org.openhab.binding.rotel.internal.communication.RotelConnector;
37 import org.openhab.binding.rotel.internal.communication.RotelDsp;
38 import org.openhab.binding.rotel.internal.communication.RotelIpConnector;
39 import org.openhab.binding.rotel.internal.communication.RotelSerialConnector;
40 import org.openhab.binding.rotel.internal.communication.RotelSimuConnector;
41 import org.openhab.binding.rotel.internal.communication.RotelSource;
42 import org.openhab.binding.rotel.internal.configuration.RotelThingConfiguration;
43 import org.openhab.binding.rotel.internal.protocol.RotelAbstractProtocolHandler;
44 import org.openhab.binding.rotel.internal.protocol.RotelMessageEvent;
45 import org.openhab.binding.rotel.internal.protocol.RotelMessageEventListener;
46 import org.openhab.binding.rotel.internal.protocol.RotelProtocol;
47 import org.openhab.binding.rotel.internal.protocol.ascii.RotelAsciiV1ProtocolHandler;
48 import org.openhab.binding.rotel.internal.protocol.ascii.RotelAsciiV2ProtocolHandler;
49 import org.openhab.binding.rotel.internal.protocol.hex.RotelHexProtocolHandler;
50 import org.openhab.core.io.transport.serial.SerialPortManager;
51 import org.openhab.core.library.types.DecimalType;
52 import org.openhab.core.library.types.IncreaseDecreaseType;
53 import org.openhab.core.library.types.NextPreviousType;
54 import org.openhab.core.library.types.OnOffType;
55 import org.openhab.core.library.types.PercentType;
56 import org.openhab.core.library.types.PlayPauseType;
57 import org.openhab.core.library.types.StringType;
58 import org.openhab.core.thing.ChannelUID;
59 import org.openhab.core.thing.Thing;
60 import org.openhab.core.thing.ThingStatus;
61 import org.openhab.core.thing.ThingStatusDetail;
62 import org.openhab.core.thing.binding.BaseThingHandler;
63 import org.openhab.core.types.Command;
64 import org.openhab.core.types.CommandOption;
65 import org.openhab.core.types.RefreshType;
66 import org.openhab.core.types.State;
67 import org.openhab.core.types.StateOption;
68 import org.openhab.core.types.UnDefType;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
73 * The {@link RotelHandler} is responsible for handling commands, which are sent to one of the channels.
75 * @author Laurent Garnier - Initial contribution
78 public class RotelHandler extends BaseThingHandler implements RotelMessageEventListener {
80 private final Logger logger = LoggerFactory.getLogger(RotelHandler.class);
82 private static final RotelModel DEFAULT_MODEL = RotelModel.RSP1066;
83 private static final long POLLING_INTERVAL = TimeUnit.SECONDS.toSeconds(60);
84 private static final boolean USE_SIMULATED_DEVICE = false;
85 private static final int SLEEP_INTV = 30;
87 private final RotelStateDescriptionOptionProvider stateDescriptionProvider;
88 private final RotelCommandDescriptionOptionProvider commandDescriptionProvider;
89 private final SerialPortManager serialPortManager;
91 private @Nullable ScheduledFuture<?> reconnectJob;
92 private @Nullable ScheduledFuture<?> powerOffJob;
93 private @Nullable ScheduledFuture<?>[] powerOnZoneJobs = { null, null, null, null, null };
95 private RotelModel model;
96 private RotelProtocol protocol;
97 private RotelAbstractProtocolHandler protocolHandler;
98 private RotelConnector connector;
100 private int minVolume;
101 private int maxVolume;
102 private int minToneLevel;
103 private int maxToneLevel;
105 private int currentZone = 1;
106 private boolean selectingRecord;
107 private @Nullable Boolean[] powers = { null, false, false, false, false };
108 private boolean powerControlPerZone;
109 private @Nullable RotelSource recordSource;
110 private @Nullable RotelSource[] sources = { RotelSource.CAT0_CD, null, null, null, null };
111 private RotelDsp dsp = RotelDsp.CAT1_NONE;
112 private boolean[] fixedVolumeZones = { false, false, false, false, false };
113 private int[] volumes = { 0, 0, 0, 0, 0 };
114 private boolean[] mutes = { false, false, false, false, false };
115 private int[] basses = { 0, 0, 0, 0, 0 };
116 private int[] trebles = { 0, 0, 0, 0, 0 };
117 private RotelPlayStatus playStatus = RotelPlayStatus.STOPPED;
119 private boolean randomMode;
120 private RotelRepeatMode repeatMode = RotelRepeatMode.OFF;
121 private int radioPreset;
122 private double[] frequencies = { 0.0, 0.0, 0.0, 0.0, 0.0 };
123 private String frontPanelLine1 = "";
124 private String frontPanelLine2 = "";
125 private int brightness;
126 private boolean tcbypass;
127 private int[] balances = { 0, 0, 0, 0, 0 };
128 private int minBalanceLevel;
129 private int maxBalanceLevel;
130 private boolean speakera;
131 private boolean speakerb;
133 private Object sequenceLock = new Object();
138 public RotelHandler(Thing thing, RotelStateDescriptionOptionProvider stateDescriptionProvider,
139 RotelCommandDescriptionOptionProvider commandDescriptionProvider, SerialPortManager serialPortManager) {
141 this.stateDescriptionProvider = stateDescriptionProvider;
142 this.commandDescriptionProvider = commandDescriptionProvider;
143 this.serialPortManager = serialPortManager;
144 this.model = DEFAULT_MODEL;
145 this.protocolHandler = new RotelHexProtocolHandler(model, Map.of());
146 this.protocol = protocolHandler.getProtocol();
147 this.connector = new RotelSimuConnector(model, protocolHandler, new HashMap<>(), "OH-binding-rotel");
151 public void initialize() {
152 logger.debug("Start initializing handler for thing {}", getThing().getUID());
154 RotelThingConfiguration config = getConfigAs(RotelThingConfiguration.class);
156 protocol = RotelProtocol.HEX;
157 if (config.protocol != null && !config.protocol.isEmpty()) {
159 protocol = RotelProtocol.getFromName(config.protocol);
160 } catch (RotelException e) {
161 // Invalid protocol name in configuration, HEX will be considered by default
164 Map<String, String> properties = editProperties();
165 String property = properties.get(RotelBindingConstants.PROPERTY_PROTOCOL);
166 if (property != null && !property.isEmpty()) {
168 protocol = RotelProtocol.getFromName(property);
169 } catch (RotelException e) {
170 // Invalid protocol name in thing property, HEX will be considered by default
174 logger.debug("rotelProtocol {}", protocol.getName());
176 switch (getThing().getThingTypeUID().getId()) {
177 case THING_TYPE_ID_RSP1066:
178 model = RotelModel.RSP1066;
180 case THING_TYPE_ID_RSP1068:
181 model = RotelModel.RSP1068;
183 case THING_TYPE_ID_RSP1069:
184 model = RotelModel.RSP1069;
186 case THING_TYPE_ID_RSP1098:
187 model = RotelModel.RSP1098;
189 case THING_TYPE_ID_RSP1570:
190 model = RotelModel.RSP1570;
192 case THING_TYPE_ID_RSP1572:
193 model = RotelModel.RSP1572;
195 case THING_TYPE_ID_RSX1055:
196 model = RotelModel.RSX1055;
198 case THING_TYPE_ID_RSX1056:
199 model = RotelModel.RSX1056;
201 case THING_TYPE_ID_RSX1057:
202 model = RotelModel.RSX1057;
204 case THING_TYPE_ID_RSX1058:
205 model = RotelModel.RSX1058;
207 case THING_TYPE_ID_RSX1065:
208 model = RotelModel.RSX1065;
210 case THING_TYPE_ID_RSX1067:
211 model = RotelModel.RSX1067;
213 case THING_TYPE_ID_RSX1550:
214 model = RotelModel.RSX1550;
216 case THING_TYPE_ID_RSX1560:
217 model = RotelModel.RSX1560;
219 case THING_TYPE_ID_RSX1562:
220 model = RotelModel.RSX1562;
222 case THING_TYPE_ID_A11:
223 model = RotelModel.A11;
225 case THING_TYPE_ID_A12:
226 model = RotelModel.A12;
228 case THING_TYPE_ID_A14:
229 model = RotelModel.A14;
231 case THING_TYPE_ID_CD11:
232 model = RotelModel.CD11;
234 case THING_TYPE_ID_CD14:
235 model = RotelModel.CD14;
237 case THING_TYPE_ID_RA11:
238 model = RotelModel.RA11;
240 case THING_TYPE_ID_RA12:
241 model = RotelModel.RA12;
243 case THING_TYPE_ID_RA1570:
244 model = RotelModel.RA1570;
246 case THING_TYPE_ID_RA1572:
247 model = RotelModel.RA1572;
249 case THING_TYPE_ID_RA1592:
250 if (protocol == RotelProtocol.ASCII_V1) {
251 model = RotelModel.RA1592_V1;
253 model = RotelModel.RA1592_V2;
256 case THING_TYPE_ID_RAP1580:
257 model = RotelModel.RAP1580;
259 case THING_TYPE_ID_RC1570:
260 model = RotelModel.RC1570;
262 case THING_TYPE_ID_RC1572:
263 model = RotelModel.RC1572;
265 case THING_TYPE_ID_RC1590:
266 if (protocol == RotelProtocol.ASCII_V1) {
267 model = RotelModel.RC1590_V1;
269 model = RotelModel.RC1590_V2;
272 case THING_TYPE_ID_RCD1570:
273 model = RotelModel.RCD1570;
275 case THING_TYPE_ID_RCD1572:
276 model = RotelModel.RCD1572;
278 case THING_TYPE_ID_RCX1500:
279 model = RotelModel.RCX1500;
281 case THING_TYPE_ID_RDD1580:
282 model = RotelModel.RDD1580;
284 case THING_TYPE_ID_RDG1520:
285 case THING_TYPE_ID_RT09:
286 model = RotelModel.RDG1520;
288 case THING_TYPE_ID_RSP1576:
289 model = RotelModel.RSP1576;
291 case THING_TYPE_ID_RSP1582:
292 model = RotelModel.RSP1582;
294 case THING_TYPE_ID_RT11:
295 model = RotelModel.RT11;
297 case THING_TYPE_ID_RT1570:
298 model = RotelModel.RT1570;
300 case THING_TYPE_ID_T11:
301 model = RotelModel.T11;
303 case THING_TYPE_ID_T14:
304 model = RotelModel.T14;
306 case THING_TYPE_ID_C8:
307 model = RotelModel.C8;
309 case THING_TYPE_ID_M8:
310 model = RotelModel.M8;
312 case THING_TYPE_ID_P5:
313 model = RotelModel.P5;
315 case THING_TYPE_ID_S5:
316 model = RotelModel.S5;
318 case THING_TYPE_ID_X3:
319 model = RotelModel.X3;
321 case THING_TYPE_ID_X5:
322 model = RotelModel.X5;
325 model = DEFAULT_MODEL;
329 Map<RotelSource, String> sourcesCustomLabels = new HashMap<>();
330 Map<RotelSource, String> sourcesLabels = new HashMap<>();
332 String readerThreadName = "OH-binding-" + getThing().getUID().getAsString();
334 if (model.hasVolumeControl()) {
335 maxVolume = model.getVolumeMax();
336 if (!model.hasDirectVolumeControl()) {
338 "Set minValue to {} and maxValue to {} for your sitemap widget attached to your volume item.",
339 minVolume, maxVolume);
342 if (model.hasToneControl()) {
343 maxToneLevel = model.getToneLevelMax();
344 minToneLevel = -maxToneLevel;
346 "Set minValue to {} and maxValue to {} for your sitemap widget attached to your bass or treble item.",
347 minToneLevel, maxToneLevel);
349 if (model.hasBalanceControl()) {
350 maxBalanceLevel = model.getBalanceLevelMax();
351 minBalanceLevel = -maxBalanceLevel;
352 logger.info("Set minValue to {} and maxValue to {} for your sitemap widget attached to your balance item.",
353 minBalanceLevel, maxBalanceLevel);
356 powerControlPerZone = model.hasPowerControlPerZone();
358 // Check configuration settings
359 String configError = null;
360 if ((config.serialPort == null || config.serialPort.isEmpty())
361 && (config.host == null || config.host.isEmpty())) {
362 configError = "@text/offline.config-error-unknown-serialport-and-host";
363 } else if (config.host == null || config.host.isEmpty()) {
364 if (config.serialPort.toLowerCase().startsWith("rfc2217")) {
365 configError = "@text/offline.config-error-invalid-serial-over-ip";
368 if (config.port == null) {
369 configError = "@text/offline.config-error-unknown-port";
370 } else if (config.port <= 0) {
371 configError = "@text/offline.config-error-invalid-port";
375 if (configError != null) {
376 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, configError);
378 for (RotelSource src : model.getSources()) {
379 // Consider custom input labels
381 switch (src.getName()) {
383 label = config.inputLabelCd;
386 label = config.inputLabelTuner;
389 label = config.inputLabelTape;
392 label = config.inputLabelPhono;
395 label = config.inputLabelVideo1;
398 label = config.inputLabelVideo2;
401 label = config.inputLabelVideo3;
404 label = config.inputLabelVideo4;
407 label = config.inputLabelVideo5;
410 label = config.inputLabelVideo6;
413 label = config.inputLabelUsb;
416 label = config.inputLabelMulti;
421 if (label != null && !label.isEmpty()) {
422 sourcesCustomLabels.put(src, label);
424 sourcesLabels.put(src, (label == null || label.isEmpty()) ? src.getLabel() : label);
427 if (protocol == RotelProtocol.HEX) {
428 protocolHandler = new RotelHexProtocolHandler(model, sourcesLabels);
429 } else if (protocol == RotelProtocol.ASCII_V1) {
430 protocolHandler = new RotelAsciiV1ProtocolHandler(model);
432 protocolHandler = new RotelAsciiV2ProtocolHandler(model);
435 if (USE_SIMULATED_DEVICE) {
436 connector = new RotelSimuConnector(model, protocolHandler, sourcesLabels, readerThreadName);
437 } else if (config.serialPort != null) {
438 connector = new RotelSerialConnector(serialPortManager, config.serialPort, model.getBaudRate(),
439 protocolHandler, readerThreadName);
441 connector = new RotelIpConnector(config.host, config.port, protocolHandler, readerThreadName);
444 if (model.hasSourceControl()) {
445 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_SOURCE),
446 getStateOptions(model.getSources(), sourcesCustomLabels));
447 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_MAIN_SOURCE),
448 getStateOptions(model.getSources(), sourcesCustomLabels));
449 stateDescriptionProvider.setStateOptions(
450 new ChannelUID(getThing().getUID(), CHANNEL_MAIN_RECORD_SOURCE),
451 getStateOptions(model.getRecordSources(), sourcesCustomLabels));
453 if (model.hasZoneSourceControl(1)) {
454 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_ZONE1_SOURCE),
455 getStateOptions(model.getZoneSources(1), sourcesCustomLabels));
457 if (model.hasZoneSourceControl(2)) {
458 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_ZONE2_SOURCE),
459 getStateOptions(model.getZoneSources(2), sourcesCustomLabels));
461 if (model.hasZoneSourceControl(3)) {
462 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_ZONE3_SOURCE),
463 getStateOptions(model.getZoneSources(3), sourcesCustomLabels));
465 if (model.hasZoneSourceControl(4)) {
466 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_ZONE4_SOURCE),
467 getStateOptions(model.getZoneSources(4), sourcesCustomLabels));
469 if (model.hasDspControl()) {
470 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_DSP),
471 model.getDspStateOptions());
472 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_MAIN_DSP),
473 model.getDspStateOptions());
476 List<CommandOption> options = model.getOtherCommandsOptions(protocol);
477 if (!options.isEmpty()) {
478 commandDescriptionProvider.setCommandOptions(new ChannelUID(getThing().getUID(), CHANNEL_OTHER_COMMAND),
480 commandDescriptionProvider
481 .setCommandOptions(new ChannelUID(getThing().getUID(), CHANNEL_MAIN_OTHER_COMMAND), options);
484 updateStatus(ThingStatus.UNKNOWN);
486 scheduleReconnectJob();
489 logger.debug("Finished initializing!");
493 public void dispose() {
494 logger.debug("Disposing handler for thing {}", getThing().getUID());
496 for (int zone = 0; zone <= model.getNumberOfZones(); zone++) {
497 cancelPowerOnZoneJob(zone);
499 cancelReconnectJob();
504 public List<StateOption> getStateOptions(List<RotelSource> list, Map<RotelSource, String> sourcesLabels) {
505 List<StateOption> options = new ArrayList<>();
506 for (RotelSource item : list) {
507 String label = sourcesLabels.get(item);
508 options.add(new StateOption(item.getName(), label == null ? ("@text/source." + item.getName()) : label));
514 public void handleCommand(ChannelUID channelUID, Command command) {
515 String channel = channelUID.getId();
517 if (getThing().getStatus() != ThingStatus.ONLINE) {
518 logger.debug("Thing is not ONLINE; command {} from channel {} is ignored", command, channel);
522 if (command instanceof RefreshType) {
523 updateChannelState(channel);
527 if (!connector.isConnected()) {
528 logger.debug("Command {} from channel {} is ignored: connection not established", command, channel);
534 case CHANNEL_ZONE1_SOURCE:
535 case CHANNEL_ZONE1_VOLUME:
536 case CHANNEL_ZONE1_MUTE:
537 case CHANNEL_ZONE1_BASS:
538 case CHANNEL_ZONE1_TREBLE:
539 case CHANNEL_ZONE1_BALANCE:
542 case CHANNEL_ZONE2_POWER:
543 case CHANNEL_ZONE2_SOURCE:
544 case CHANNEL_ZONE2_VOLUME:
545 case CHANNEL_ZONE2_VOLUME_UP_DOWN:
546 case CHANNEL_ZONE2_MUTE:
547 case CHANNEL_ZONE2_BASS:
548 case CHANNEL_ZONE2_TREBLE:
549 case CHANNEL_ZONE2_BALANCE:
552 case CHANNEL_ZONE3_POWER:
553 case CHANNEL_ZONE3_SOURCE:
554 case CHANNEL_ZONE3_VOLUME:
555 case CHANNEL_ZONE3_MUTE:
556 case CHANNEL_ZONE3_BASS:
557 case CHANNEL_ZONE3_TREBLE:
558 case CHANNEL_ZONE3_BALANCE:
561 case CHANNEL_ZONE4_POWER:
562 case CHANNEL_ZONE4_SOURCE:
563 case CHANNEL_ZONE4_VOLUME:
564 case CHANNEL_ZONE4_MUTE:
565 case CHANNEL_ZONE4_BASS:
566 case CHANNEL_ZONE4_TREBLE:
567 case CHANNEL_ZONE4_BALANCE:
576 boolean success = true;
577 synchronized (sequenceLock) {
581 case CHANNEL_MAIN_POWER:
582 case CHANNEL_ZONE2_POWER:
583 case CHANNEL_ZONE3_POWER:
584 case CHANNEL_ZONE4_POWER:
585 if (numZone == 0 || model.hasZoneCommands(numZone)) {
586 handlePowerCmd(channel, command, getPowerOnCommand(numZone), getPowerOffCommand(numZone));
587 } else if (numZone == 2 && model.getNumberOfZones() == 2) {
588 if (isPowerOn() || isPowerOn(numZone)) {
589 selectZone(2, model.getZoneSelectCmd());
591 sendCommand(RotelCommand.ZONE_SELECT);
594 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
597 case CHANNEL_ALL_POWER:
598 handlePowerCmd(channel, command, RotelCommand.POWER_ON, RotelCommand.POWER_OFF);
601 case CHANNEL_MAIN_SOURCE:
602 case CHANNEL_ZONE1_SOURCE:
603 case CHANNEL_ZONE2_SOURCE:
604 case CHANNEL_ZONE3_SOURCE:
605 case CHANNEL_ZONE4_SOURCE:
606 if (!isPowerOn(numZone)) {
608 logger.debug("Command {} from channel {} ignored: {} in standby", command, channel,
609 numZone == 0 ? "device" : "zone " + numZone);
610 } else if (numZone == 0 || model.hasZoneCommands(numZone)) {
611 src = model.getSourceFromName(command.toString());
613 cmd = model.hasOtherThanPrimaryCommands() ? src.getZoneCommand(1) : src.getCommand();
615 cmd = src.getZoneCommand(numZone);
619 if (model.canGetFrequency()) {
620 // send <new-source> returns
621 // 1.) the selected <new-source>
622 // 2.) the used frequency
624 // at response-time the frequency has the value of <old-source>
625 // so we must wait a short moment to get the frequency of <new-source>
627 sendCommand(RotelCommand.FREQUENCY);
629 updateChannelState(CHANNEL_FREQUENCY);
633 logger.debug("Command {} from channel {} failed: undefined source command", command,
636 } else if (numZone == 2 && model.getNumberOfZones() > 1) {
637 src = model.getSourceFromName(command.toString());
638 cmd = src.getCommand();
640 selectZone(2, model.getZoneSelectCmd());
642 if (model.canGetFrequency()) {
643 // send <new-source> returns
644 // 1.) the selected <new-source>
645 // 2.) the used frequency
647 // at response-time the frequency has the value of <old-source>
648 // so we must wait a short moment to get the frequency of <new-source>
650 sendCommand(RotelCommand.FREQUENCY);
652 updateChannelState(CHANNEL_FREQUENCY);
656 logger.debug("Command {} from channel {} failed: undefined source command", command,
661 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
664 case CHANNEL_MAIN_RECORD_SOURCE:
667 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
668 } else if (model.hasOtherThanPrimaryCommands()) {
669 src = model.getSourceFromName(command.toString());
670 cmd = src.getRecordCommand();
675 logger.debug("Command {} from channel {} failed: undefined record source command",
679 src = model.getSourceFromName(command.toString());
680 cmd = src.getCommand();
682 sendCommand(RotelCommand.RECORD_FONCTION_SELECT);
687 logger.debug("Command {} from channel {} failed: undefined source command", command,
693 case CHANNEL_MAIN_DSP:
696 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
698 sendCommand(model.getCommandFromDspName(command.toString()));
702 case CHANNEL_MAIN_VOLUME:
703 case CHANNEL_MAIN_VOLUME_UP_DOWN:
704 case CHANNEL_ZONE1_VOLUME:
705 case CHANNEL_ZONE2_VOLUME:
706 case CHANNEL_ZONE2_VOLUME_UP_DOWN:
707 case CHANNEL_ZONE3_VOLUME:
708 case CHANNEL_ZONE4_VOLUME:
709 if (!isPowerOn(numZone)) {
711 logger.debug("Command {} from channel {} ignored: zone {} in standby", command, channel,
712 numZone == 0 ? "device" : "zone " + numZone);
713 } else if (fixedVolumeZones[numZone]) {
715 logger.debug("Command {} from channel {} ignored: fixed volume", command, channel);
716 } else if (model.hasVolumeControl() && (numZone == 0 || model.hasZoneCommands(numZone))) {
717 handleVolumeCmd(volumes[numZone], channel, command, getVolumeUpCommand(numZone),
718 getVolumeDownCommand(numZone),
719 CHANNEL_MAIN_VOLUME_UP_DOWN.equals(channel)
720 || CHANNEL_ZONE2_VOLUME_UP_DOWN.equals(channel) ? null
721 : getVolumeSetCommand(numZone));
722 } else if (numZone == 2 && model.hasVolumeControl() && model.getNumberOfZones() > 1) {
723 selectZone(2, model.getZoneSelectCmd());
724 handleVolumeCmd(volumes[numZone], channel, command, RotelCommand.VOLUME_UP,
725 RotelCommand.VOLUME_DOWN,
726 CHANNEL_ZONE2_VOLUME_UP_DOWN.equals(channel) ? null : RotelCommand.VOLUME_SET);
729 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
733 case CHANNEL_MAIN_MUTE:
734 case CHANNEL_ZONE1_MUTE:
735 case CHANNEL_ZONE2_MUTE:
736 case CHANNEL_ZONE3_MUTE:
737 case CHANNEL_ZONE4_MUTE:
738 if (!isPowerOn(numZone)) {
740 logger.debug("Command {} from channel {} ignored: zone {} in standby", command, channel,
741 numZone == 0 ? "device" : "zone " + numZone);
742 } else if (model.hasVolumeControl() && (numZone == 0 || model.hasZoneCommands(numZone))) {
743 handleMuteCmd(numZone == 0 && protocol == RotelProtocol.HEX, channel, command,
744 getMuteOnCommand(numZone), getMuteOffCommand(numZone),
745 getMuteToggleCommand(numZone));
748 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
752 case CHANNEL_MAIN_BASS:
753 case CHANNEL_ZONE1_BASS:
754 case CHANNEL_ZONE2_BASS:
755 case CHANNEL_ZONE3_BASS:
756 case CHANNEL_ZONE4_BASS:
757 if (!isPowerOn(numZone)) {
759 logger.debug("Command {} from channel {} ignored: zone {} in standby", command, channel,
760 numZone == 0 ? "device" : "zone " + numZone);
761 } else if (tcbypass) {
763 logger.debug("Command {} from channel {} ignored: tone control bypass is ON", command,
765 } else if (model.hasToneControl() && (numZone == 0 || model.hasZoneCommands(numZone))) {
766 handleToneCmd(basses[numZone], channel, command, 2, getBassUpCommand(numZone),
767 getBassDownCommand(numZone), getBassSetCommand(numZone));
770 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
774 case CHANNEL_MAIN_TREBLE:
775 case CHANNEL_ZONE1_TREBLE:
776 case CHANNEL_ZONE2_TREBLE:
777 case CHANNEL_ZONE3_TREBLE:
778 case CHANNEL_ZONE4_TREBLE:
779 if (!isPowerOn(numZone)) {
781 logger.debug("Command {} from channel {} ignored: zone {} in standby", command, channel,
782 numZone == 0 ? "device" : "zone " + numZone);
783 } else if (tcbypass) {
785 logger.debug("Command {} from channel {} ignored: tone control bypass is ON", command,
787 } else if (model.hasToneControl() && (numZone == 0 || model.hasZoneCommands(numZone))) {
788 handleToneCmd(trebles[numZone], channel, command, 1, getTrebleUpCommand(numZone),
789 getTrebleDownCommand(numZone), getTrebleSetCommand(numZone));
792 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
795 case CHANNEL_PLAY_CONTROL:
798 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
799 } else if (command instanceof PlayPauseType && command == PlayPauseType.PLAY) {
800 sendCommand(RotelCommand.PLAY);
801 } else if (command instanceof PlayPauseType && command == PlayPauseType.PAUSE) {
802 sendCommand(RotelCommand.PAUSE);
803 if (protocol == RotelProtocol.ASCII_V1 && model != RotelModel.RCD1570
804 && model != RotelModel.RCD1572 && model != RotelModel.RCX1500) {
805 Thread.sleep(SLEEP_INTV);
806 sendCommand(RotelCommand.PLAY_STATUS);
808 } else if (command instanceof NextPreviousType && command == NextPreviousType.NEXT) {
809 sendCommand(RotelCommand.TRACK_FWD);
810 } else if (command instanceof NextPreviousType && command == NextPreviousType.PREVIOUS) {
811 sendCommand(RotelCommand.TRACK_BACK);
814 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
820 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
821 } else if (command instanceof OnOffType) {
822 sendCommand(RotelCommand.RANDOM_TOGGLE);
825 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
831 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
833 RotelRepeatMode currentMode = repeatMode;
834 RotelRepeatMode mode = RotelRepeatMode.OFF;
836 mode = RotelRepeatMode.getFromName(command.toString());
837 if (mode == currentMode) {
839 logger.debug("Command {} from channel {} ignored: no change requested", command,
842 } catch (RotelException e) {
844 logger.debug("Command {} from channel {} failed: invalid command value", command,
848 // Toggle TRACK -> DISC -> OFF
849 sendCommand(RotelCommand.REPEAT_TOGGLE);
850 if ((mode == RotelRepeatMode.OFF && currentMode == RotelRepeatMode.TRACK)
851 || (mode == RotelRepeatMode.TRACK && currentMode == RotelRepeatMode.DISC)
852 || (mode == RotelRepeatMode.DISC && currentMode == RotelRepeatMode.OFF)) {
853 Thread.sleep(SLEEP_INTV);
854 sendCommand(RotelCommand.REPEAT_TOGGLE);
859 case CHANNEL_RADIO_PRESET:
862 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
865 if (radioPreset > 0 && command instanceof IncreaseDecreaseType
866 && command == IncreaseDecreaseType.INCREASE) {
867 value = radioPreset + 1;
868 } else if (radioPreset > 0 && command instanceof IncreaseDecreaseType
869 && command == IncreaseDecreaseType.DECREASE) {
870 value = radioPreset - 1;
871 } else if (command instanceof DecimalType) {
872 value = ((DecimalType) command).intValue();
874 if (value >= 1 && value <= 30) {
875 RotelSource source = sources[0];
876 RotelCommand presetCallCmd = source == null ? null : getRadioPresetCallCommand(source);
877 if (presetCallCmd != null) {
878 sendCommand(presetCallCmd, value);
879 // In ASCII V2, the previous command will return nothing
880 RotelCommand presetGetCmd = source == null ? null
881 : getRadioPresetGetCommand(source);
882 if (protocol == RotelProtocol.ASCII_V2 && presetGetCmd != null) {
883 Thread.sleep(SLEEP_INTV);
884 sendCommand(presetGetCmd);
888 logger.debug("Command {} from channel {} ignored: current source is not radio",
893 logger.debug("Command {} from channel {} ignored: value out of bounds", command,
898 case CHANNEL_BRIGHTNESS:
899 case CHANNEL_ALL_BRIGHTNESS:
902 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
903 } else if (!model.hasDimmerControl()) {
905 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
906 } else if (command instanceof PercentType) {
907 int dimmer = (int) Math.round(((PercentType) command).doubleValue() / 100.0
908 * (model.getDimmerLevelMax() - model.getDimmerLevelMin()))
909 + model.getDimmerLevelMin();
910 sendCommand(RotelCommand.DIMMER_LEVEL_SET, dimmer);
913 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
916 case CHANNEL_TCBYPASS:
919 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
920 } else if (!model.hasToneControl() || protocol == RotelProtocol.HEX) {
922 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
924 handleTcbypassCmd(channel, command,
925 protocol == RotelProtocol.ASCII_V1 ? RotelCommand.TONE_CONTROLS_OFF
926 : RotelCommand.TCBYPASS_ON,
927 protocol == RotelProtocol.ASCII_V1 ? RotelCommand.TONE_CONTROLS_ON
928 : RotelCommand.TCBYPASS_OFF);
931 case CHANNEL_BALANCE:
932 case CHANNEL_ZONE1_BALANCE:
933 case CHANNEL_ZONE2_BALANCE:
934 case CHANNEL_ZONE3_BALANCE:
935 case CHANNEL_ZONE4_BALANCE:
936 if (!isPowerOn(numZone)) {
938 logger.debug("Command {} from channel {} ignored: zone {} in standby", command, channel,
939 numZone == 0 ? "device" : "zone " + numZone);
940 } else if (!model.hasBalanceControl() || protocol == RotelProtocol.HEX) {
942 logger.debug("Command {} from channel {} failed: unavailable feature", command, channel);
944 handleBalanceCmd(channel, command, getBalanceLeftCommand(numZone),
945 getBalanceRightCommand(numZone), getBalanceSetCommand(numZone));
948 case CHANNEL_SPEAKER_A:
951 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
953 handleSpeakerCmd(protocol == RotelProtocol.HEX, channel, command, RotelCommand.SPEAKER_A_ON,
954 RotelCommand.SPEAKER_A_OFF, RotelCommand.SPEAKER_A_TOGGLE);
957 case CHANNEL_SPEAKER_B:
960 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
962 handleSpeakerCmd(protocol == RotelProtocol.HEX, channel, command, RotelCommand.SPEAKER_B_ON,
963 RotelCommand.SPEAKER_B_OFF, RotelCommand.SPEAKER_B_TOGGLE);
966 case CHANNEL_OTHER_COMMAND:
967 case CHANNEL_MAIN_OTHER_COMMAND:
970 logger.debug("Command {} from channel {} ignored: device in standby", command, channel);
973 cmd = RotelCommand.getFromName(command.toString());
974 } catch (RotelException e) {
976 logger.debug("Command {} from channel {} failed: undefined command", command, channel);
986 logger.debug("Command {} from channel {} failed: nnexpected command", command, channel);
990 logger.debug("Command {} from channel {} succeeded", command, channel);
992 updateChannelState(channel);
994 } catch (RotelException e) {
995 logger.debug("Command {} from channel {} failed: {}", command, channel, e.getMessage());
996 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
997 "@text/offline.comm-error-sending-command");
999 scheduleReconnectJob();
1000 } catch (InterruptedException e) {
1001 logger.debug("Command {} from channel {} interrupted: {}", command, channel, e.getMessage());
1002 Thread.currentThread().interrupt();
1008 * Handle a power ON/OFF command
1010 * @param channel the channel
1011 * @param command the received channel command (OnOffType)
1012 * @param onCmd the command to be sent to the device to power it ON
1013 * @param offCmd the command to be sent to the device to power it OFF
1015 * @throws RotelException in case of communication error with the device
1017 private void handlePowerCmd(String channel, Command command, RotelCommand onCmd, RotelCommand offCmd)
1018 throws RotelException {
1019 if (command instanceof OnOffType && command == OnOffType.ON) {
1021 } else if (command instanceof OnOffType && command == OnOffType.OFF) {
1022 sendCommand(offCmd);
1024 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1029 * Handle a volume command
1031 * @param current the current volume
1032 * @param channel the channel
1033 * @param command the received channel command (IncreaseDecreaseType or DecimalType)
1034 * @param upCmd the command to be sent to the device to increase the volume
1035 * @param downCmd the command to be sent to the device to decrease the volume
1036 * @param setCmd the command to be sent to the device to set the volume at a value
1038 * @throws RotelException in case of communication error with the device
1040 private void handleVolumeCmd(int current, String channel, Command command, RotelCommand upCmd, RotelCommand downCmd,
1041 @Nullable RotelCommand setCmd) throws RotelException {
1042 if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.INCREASE) {
1044 } else if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.DECREASE) {
1045 sendCommand(downCmd);
1046 } else if (command instanceof DecimalType && setCmd == null) {
1047 int value = ((DecimalType) command).intValue();
1048 if (value >= minVolume && value <= maxVolume) {
1049 if (value > current) {
1051 } else if (value < current) {
1052 sendCommand(downCmd);
1055 } else if (command instanceof PercentType && setCmd != null) {
1056 int value = (int) Math.round(((PercentType) command).doubleValue() / 100.0 * (maxVolume - minVolume))
1058 sendCommand(setCmd, value);
1060 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1065 * Handle a mute command
1067 * @param onlyToggle true if only the toggle command must be used
1068 * @param channel the channel
1069 * @param command the received channel command (OnOffType)
1070 * @param onCmd the command to be sent to the device to mute
1071 * @param offCmd the command to be sent to the device to unmute
1072 * @param toggleCmd the command to be sent to the device to toggle the mute state
1074 * @throws RotelException in case of communication error with the device
1076 private void handleMuteCmd(boolean onlyToggle, String channel, Command command, RotelCommand onCmd,
1077 RotelCommand offCmd, RotelCommand toggleCmd) throws RotelException {
1078 if (command instanceof OnOffType) {
1080 sendCommand(toggleCmd);
1081 } else if (command == OnOffType.ON) {
1083 } else if (command == OnOffType.OFF) {
1084 sendCommand(offCmd);
1087 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1092 * Handle a tone level adjustment command (bass or treble)
1094 * @param current the current tone level
1095 * @param channel the channel
1096 * @param command the received channel command (IncreaseDecreaseType or DecimalType)
1097 * @param nbSelect the number of TONE_CONTROL_SELECT commands to be run to display the right tone (bass or treble)
1098 * @param upCmd the command to be sent to the device to increase the tone level
1099 * @param downCmd the command to be sent to the device to decrease the tone level
1100 * @param setCmd the command to be sent to the device to set the tone level at a value
1102 * @throws RotelException in case of communication error with the device
1103 * @throws InterruptedException in case of interruption during a thread sleep
1105 private void handleToneCmd(int current, String channel, Command command, int nbSelect, RotelCommand upCmd,
1106 RotelCommand downCmd, RotelCommand setCmd) throws RotelException, InterruptedException {
1107 if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.INCREASE) {
1108 selectToneControl(nbSelect);
1110 } else if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.DECREASE) {
1111 selectToneControl(nbSelect);
1112 sendCommand(downCmd);
1113 } else if (command instanceof DecimalType) {
1114 int value = ((DecimalType) command).intValue();
1115 if (value >= minToneLevel && value <= maxToneLevel) {
1116 if (protocol != RotelProtocol.HEX) {
1117 sendCommand(setCmd, value);
1118 } else if (value > current) {
1119 selectToneControl(nbSelect);
1121 } else if (value < current) {
1122 selectToneControl(nbSelect);
1123 sendCommand(downCmd);
1127 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1132 * Handle a tcbypass command (only for ASCII protocol)
1134 * @param channel the channel
1135 * @param command the received channel command (OnOffType)
1136 * @param onCmd the command to be sent to the device to bypass_on
1137 * @param offCmd the command to be sent to the device to bypass_off
1139 * @throws RotelException in case of communication error with the device
1141 private void handleTcbypassCmd(String channel, Command command, RotelCommand onCmd, RotelCommand offCmd)
1142 throws RotelException, InterruptedException {
1143 if (command instanceof OnOffType) {
1144 if (command == OnOffType.ON) {
1148 updateChannelState(CHANNEL_BASS);
1149 updateChannelState(CHANNEL_TREBLE);
1150 } else if (command == OnOffType.OFF) {
1151 sendCommand(offCmd);
1153 sendCommand(RotelCommand.BASS);
1155 sendCommand(RotelCommand.TREBLE);
1158 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1163 * Handle a speaker command
1165 * @param onlyToggle true if only the toggle command must be used
1166 * @param channel the channel
1167 * @param command the received channel command (OnOffType)
1168 * @param onCmd the command to be sent to the device to speaker_x_on
1169 * @param offCmd the command to be sent to the device to speaker_x_off
1170 * @param toggleCmd the command to be sent to the device to toggle the speaker_x state
1172 * @throws RotelException in case of communication error with the device
1174 private void handleSpeakerCmd(boolean onlyToggle, String channel, Command command, RotelCommand onCmd,
1175 RotelCommand offCmd, RotelCommand toggleCmd) throws RotelException {
1176 if (command instanceof OnOffType) {
1178 sendCommand(toggleCmd);
1179 } else if (command == OnOffType.ON) {
1181 } else if (command == OnOffType.OFF) {
1182 sendCommand(offCmd);
1185 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1190 * Handle a tone balance adjustment command (left or right) (only for ASCII protocol)
1192 * @param channel the channel
1193 * @param command the received channel command (IncreaseDecreaseType or DecimalType)
1194 * @param rightCmd the command to be sent to the device to "increase" balance (shift to the right side)
1195 * @param leftCmd the command to be sent to the device to "decrease" balance (shift to the left side)
1196 * @param setCmd the command to be sent to the device to set the balance at a value
1198 * @throws RotelException in case of communication error with the device
1199 * @throws InterruptedException in case of interruption during a thread sleep
1201 private void handleBalanceCmd(String channel, Command command, RotelCommand leftCmd, RotelCommand rightCmd,
1202 RotelCommand setCmd) throws RotelException, InterruptedException {
1203 if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.INCREASE) {
1204 sendCommand(rightCmd);
1205 } else if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.DECREASE) {
1206 sendCommand(leftCmd);
1207 } else if (command instanceof DecimalType) {
1208 int value = ((DecimalType) command).intValue();
1209 if (value >= minBalanceLevel && value <= maxBalanceLevel) {
1210 sendCommand(setCmd, value);
1213 logger.debug("Command {} from channel {} failed: invalid command value", command, channel);
1218 * Run a sequence of commands to display the current tone level (bass or treble) on the device front panel
1220 * @param nbSelect the number of TONE_CONTROL_SELECT commands to be run to display the right tone (bass or treble)
1222 * @throws RotelException in case of communication error with the device
1223 * @throws InterruptedException in case of interruption during a thread sleep
1225 private void selectToneControl(int nbSelect) throws RotelException, InterruptedException {
1226 // No tone control select command for RSX-1065
1227 if (protocol == RotelProtocol.HEX && model != RotelModel.RSX1065) {
1228 selectFeature(nbSelect, RotelCommand.RECORD_FONCTION_SELECT, RotelCommand.TONE_CONTROL_SELECT);
1233 * Run a sequence of commands to display a particular zone on the device front panel
1235 * @param zone the zone to be displayed (1 for main zone)
1236 * @param selectCommand the command to be sent to the device to switch the display between zones
1238 * @throws RotelException in case of communication error with the device
1239 * @throws InterruptedException in case of interruption during a thread sleep
1241 private void selectZone(int zone, @Nullable RotelCommand selectCommand)
1242 throws RotelException, InterruptedException {
1243 if (protocol == RotelProtocol.HEX && model.getNumberOfZones() > 1 && zone >= 1 && zone != currentZone
1244 && selectCommand != null) {
1246 if (zone < currentZone) {
1247 nbSelect = zone + model.getNumberOfZones() - 1 - currentZone;
1248 if (isPowerOn() && selectCommand == RotelCommand.RECORD_FONCTION_SELECT) {
1252 nbSelect = zone - currentZone;
1253 if (isPowerOn() && currentZone == 1 && selectCommand == RotelCommand.RECORD_FONCTION_SELECT
1254 && !selectingRecord) {
1258 selectFeature(nbSelect, null, selectCommand);
1263 * Run a sequence of commands to display a particular feature on the device front panel
1265 * @param nbSelect the number of select commands to be run
1266 * @param preCmd the initial command to be sent to the device (before the select commands)
1267 * @param selectCmd the select command to be sent to the device
1269 * @throws RotelException in case of communication error with the device
1270 * @throws InterruptedException in case of interruption during a thread sleep
1272 private void selectFeature(int nbSelect, @Nullable RotelCommand preCmd, RotelCommand selectCmd)
1273 throws RotelException, InterruptedException {
1274 if (protocol == RotelProtocol.HEX) {
1275 if (preCmd != null) {
1276 sendCommand(preCmd);
1279 for (int i = 1; i <= nbSelect; i++) {
1280 sendCommand(selectCmd);
1287 * Open the connection with the Rotel device
1289 * @return true if the connection is opened successfully or flase if not
1291 private synchronized boolean openConnection() {
1292 protocolHandler.addEventListener(this);
1295 } catch (RotelException e) {
1296 logger.debug("openConnection() failed", e);
1298 logger.debug("openConnection(): {}", connector.isConnected() ? "connected" : "disconnected");
1299 return connector.isConnected();
1303 * Close the connection with the Rotel device
1305 private synchronized void closeConnection() {
1307 protocolHandler.removeEventListener(this);
1308 logger.debug("closeConnection(): disconnected");
1312 public void onNewMessageEvent(EventObject event) {
1313 cancelPowerOffJob();
1315 RotelMessageEvent evt = (RotelMessageEvent) event;
1316 logger.debug("onNewMessageEvent: key {} = {}", evt.getKey(), evt.getValue());
1318 String key = evt.getKey();
1319 String value = evt.getValue().trim();
1320 if (!KEY_ERROR.equals(key)) {
1321 updateStatus(ThingStatus.ONLINE);
1325 case KEY_INPUT_ZONE1:
1326 case KEY_VOLUME_ZONE1:
1327 case KEY_MUTE_ZONE1:
1328 case KEY_BASS_ZONE1:
1329 case KEY_TREBLE_ZONE1:
1330 case KEY_BALANCE_ZONE1:
1331 case KEY_FREQ_ZONE1:
1334 case KEY_POWER_ZONE2:
1335 case KEY_SOURCE_ZONE2:
1336 case KEY_INPUT_ZONE2:
1337 case KEY_VOLUME_ZONE2:
1338 case KEY_MUTE_ZONE2:
1339 case KEY_BASS_ZONE2:
1340 case KEY_TREBLE_ZONE2:
1341 case KEY_BALANCE_ZONE2:
1342 case KEY_FREQ_ZONE2:
1345 case KEY_POWER_ZONE3:
1346 case KEY_SOURCE_ZONE3:
1347 case KEY_INPUT_ZONE3:
1348 case KEY_VOLUME_ZONE3:
1349 case KEY_MUTE_ZONE3:
1350 case KEY_BASS_ZONE3:
1351 case KEY_TREBLE_ZONE3:
1352 case KEY_BALANCE_ZONE3:
1353 case KEY_FREQ_ZONE3:
1356 case KEY_POWER_ZONE4:
1357 case KEY_SOURCE_ZONE4:
1358 case KEY_INPUT_ZONE4:
1359 case KEY_VOLUME_ZONE4:
1360 case KEY_MUTE_ZONE4:
1361 case KEY_BASS_ZONE4:
1362 case KEY_TREBLE_ZONE4:
1363 case KEY_BALANCE_ZONE4:
1364 case KEY_FREQ_ZONE4:
1371 if (key.startsWith(KEY_FM_PRESET)) {
1373 preset = Integer.parseInt(key.substring(KEY_FM_PRESET.length()));
1374 } catch (NumberFormatException e) {
1375 // Considering the Rotel protocol, the parsing could not fail in practice.
1376 // In case it would fail, 0 will be considered as preset, meaning undefined.
1378 key = KEY_FM_PRESET;
1379 } else if (key.startsWith(KEY_DAB_PRESET)) {
1381 preset = Integer.parseInt(key.substring(KEY_DAB_PRESET.length()));
1382 } catch (NumberFormatException e) {
1383 // Considering the Rotel protocol, the parsing could not fail in practice.
1384 // In case it would fail, 0 will be considered as preset, meaning undefined.
1386 key = KEY_DAB_PRESET;
1387 } else if (key.startsWith(KEY_IRADIO_PRESET)) {
1389 preset = Integer.parseInt(key.substring(KEY_IRADIO_PRESET.length()));
1390 } catch (NumberFormatException e) {
1391 // Considering the Rotel protocol, the parsing could not fail in practice.
1392 // In case it would fail, 0 will be considered as preset, meaning undefined.
1394 key = KEY_IRADIO_PRESET;
1400 logger.debug("Reading feedback message failed");
1401 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
1402 "@text/offline.comm-error-reading-thread");
1406 frontPanelLine1 = value;
1407 updateChannelState(CHANNEL_LINE1);
1410 frontPanelLine2 = value;
1411 updateChannelState(CHANNEL_LINE2);
1414 currentZone = Integer.parseInt(value);
1416 case KEY_RECORD_SEL:
1417 selectingRecord = MSG_VALUE_ON.equalsIgnoreCase(value);
1420 if (POWER_ON.equalsIgnoreCase(value)) {
1422 } else if (STANDBY.equalsIgnoreCase(value)) {
1424 if (model.getNumberOfZones() > 1 && !powerControlPerZone) {
1425 for (int zone = 1; zone <= model.getNumberOfZones(); zone++) {
1426 handlePowerOffZone(zone);
1429 } else if (POWER_OFF_DELAYED.equalsIgnoreCase(value)) {
1430 schedulePowerOffJob(false);
1432 throw new RotelException("Invalid value");
1435 case KEY_POWER_ZONE2:
1436 case KEY_POWER_ZONE3:
1437 case KEY_POWER_ZONE4:
1438 if (POWER_ON.equalsIgnoreCase(value)) {
1439 handlePowerOnZone(numZone);
1440 } else if (STANDBY.equalsIgnoreCase(value)) {
1441 handlePowerOffZone(numZone);
1443 throw new RotelException("Invalid value");
1446 case KEY_POWER_MODE:
1447 logger.debug("Power mode is set to {}", value);
1449 case KEY_VOLUME_MIN:
1450 minVolume = Integer.parseInt(value);
1451 if (!model.hasDirectVolumeControl()) {
1452 logger.info("Set minValue to {} for your sitemap widget attached to your volume item.",
1456 case KEY_VOLUME_MAX:
1457 maxVolume = Integer.parseInt(value);
1458 if (!model.hasDirectVolumeControl()) {
1459 logger.info("Set maxValue to {} for your sitemap widget attached to your volume item.",
1464 case KEY_VOLUME_ZONE1:
1465 case KEY_VOLUME_ZONE2:
1466 case KEY_VOLUME_ZONE3:
1467 case KEY_VOLUME_ZONE4:
1468 fixedVolumeZones[numZone] = false;
1469 if (MSG_VALUE_FIX.equalsIgnoreCase(value)) {
1470 fixedVolumeZones[numZone] = true;
1471 } else if (MSG_VALUE_MIN.equalsIgnoreCase(value)) {
1472 volumes[numZone] = minVolume;
1473 } else if (MSG_VALUE_MAX.equalsIgnoreCase(value)) {
1474 volumes[numZone] = maxVolume;
1476 volumes[numZone] = Integer.parseInt(value);
1479 updateChannelState(CHANNEL_VOLUME);
1480 updateChannelState(CHANNEL_MAIN_VOLUME);
1481 updateChannelState(CHANNEL_MAIN_VOLUME_UP_DOWN);
1483 updateGroupChannelState(numZone, CHANNEL_VOLUME);
1484 updateGroupChannelState(numZone, CHANNEL_VOLUME_UP_DOWN);
1488 case KEY_MUTE_ZONE1:
1489 case KEY_MUTE_ZONE2:
1490 case KEY_MUTE_ZONE3:
1491 case KEY_MUTE_ZONE4:
1492 if (MSG_VALUE_ON.equalsIgnoreCase(value)) {
1493 mutes[numZone] = true;
1495 updateChannelState(CHANNEL_MUTE);
1496 updateChannelState(CHANNEL_MAIN_MUTE);
1498 updateGroupChannelState(numZone, CHANNEL_MUTE);
1500 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1501 mutes[numZone] = false;
1503 updateChannelState(CHANNEL_MUTE);
1504 updateChannelState(CHANNEL_MAIN_MUTE);
1506 updateGroupChannelState(numZone, CHANNEL_MUTE);
1509 throw new RotelException("Invalid value");
1513 maxToneLevel = Integer.parseInt(value);
1514 minToneLevel = -maxToneLevel;
1516 "Set minValue to {} and maxValue to {} for your sitemap widget attached to your bass or treble item.",
1517 minToneLevel, maxToneLevel);
1520 case KEY_BASS_ZONE1:
1521 case KEY_BASS_ZONE2:
1522 case KEY_BASS_ZONE3:
1523 case KEY_BASS_ZONE4:
1524 if (MSG_VALUE_MIN.equalsIgnoreCase(value)) {
1525 basses[numZone] = minToneLevel;
1526 } else if (MSG_VALUE_MAX.equalsIgnoreCase(value)) {
1527 basses[numZone] = maxToneLevel;
1529 basses[numZone] = Integer.parseInt(value);
1532 updateChannelState(CHANNEL_BASS);
1533 updateChannelState(CHANNEL_MAIN_BASS);
1535 updateGroupChannelState(numZone, CHANNEL_BASS);
1539 case KEY_TREBLE_ZONE1:
1540 case KEY_TREBLE_ZONE2:
1541 case KEY_TREBLE_ZONE3:
1542 case KEY_TREBLE_ZONE4:
1543 if (MSG_VALUE_MIN.equalsIgnoreCase(value)) {
1544 trebles[numZone] = minToneLevel;
1545 } else if (MSG_VALUE_MAX.equalsIgnoreCase(value)) {
1546 trebles[numZone] = maxToneLevel;
1548 trebles[numZone] = Integer.parseInt(value);
1551 updateChannelState(CHANNEL_TREBLE);
1552 updateChannelState(CHANNEL_MAIN_TREBLE);
1554 updateGroupChannelState(numZone, CHANNEL_TREBLE);
1558 source = model.getSourceFromCommand(RotelCommand.getFromAsciiCommand(value));
1559 sources[0] = source;
1560 updateChannelState(CHANNEL_SOURCE);
1561 updateChannelState(CHANNEL_MAIN_SOURCE);
1562 RotelCommand presetGetCmd = getRadioPresetGetCommand(source);
1563 if (presetGetCmd != null) {
1564 // Request current preset (with a delay)
1565 scheduler.schedule(() -> {
1567 sendCommand(presetGetCmd);
1568 } catch (RotelException e) {
1569 logger.debug("Getting the radio preset failed: {}", e.getMessage());
1571 }, 250, TimeUnit.MILLISECONDS);
1574 updateChannelState(CHANNEL_RADIO_PRESET);
1578 recordSource = model.getRecordSourceFromCommand(RotelCommand.getFromAsciiCommand(value));
1579 updateChannelState(CHANNEL_MAIN_RECORD_SOURCE);
1581 case KEY_SOURCE_ZONE2:
1582 case KEY_SOURCE_ZONE3:
1583 case KEY_SOURCE_ZONE4:
1584 case KEY_INPUT_ZONE1:
1585 case KEY_INPUT_ZONE2:
1586 case KEY_INPUT_ZONE3:
1587 case KEY_INPUT_ZONE4:
1588 sources[numZone] = model.getZoneSourceFromCommand(RotelCommand.getFromAsciiCommand(value), numZone);
1589 updateGroupChannelState(numZone, CHANNEL_SOURCE);
1592 if ("dolby_pliix_movie".equals(value)) {
1593 value = "dolby_plii_movie";
1594 } else if ("dolby_pliix_music".equals(value)) {
1595 value = "dolby_plii_music";
1596 } else if ("dolby_pliix_game".equals(value)) {
1597 value = "dolby_plii_game";
1599 dsp = model.getDspFromFeedback(value);
1600 logger.debug("DSP {}", dsp.getName());
1601 updateChannelState(CHANNEL_DSP);
1602 updateChannelState(CHANNEL_MAIN_DSP);
1604 case KEY1_PLAY_STATUS:
1605 case KEY2_PLAY_STATUS:
1606 if (PLAY.equalsIgnoreCase(value)) {
1607 playStatus = RotelPlayStatus.PLAYING;
1608 updateChannelState(CHANNEL_PLAY_CONTROL);
1609 } else if (PAUSE.equalsIgnoreCase(value)) {
1610 playStatus = RotelPlayStatus.PAUSED;
1611 updateChannelState(CHANNEL_PLAY_CONTROL);
1612 } else if (STOP.equalsIgnoreCase(value)) {
1613 playStatus = RotelPlayStatus.STOPPED;
1614 updateChannelState(CHANNEL_PLAY_CONTROL);
1616 throw new RotelException("Invalid value");
1620 source = sources[0];
1621 if (source != null && source.getName().equals("CD") && !model.hasSourceControl()) {
1622 track = Integer.parseInt(value);
1623 updateChannelState(CHANNEL_TRACK);
1627 if (MSG_VALUE_ON.equalsIgnoreCase(value)) {
1629 updateChannelState(CHANNEL_RANDOM);
1630 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1632 updateChannelState(CHANNEL_RANDOM);
1634 throw new RotelException("Invalid value");
1638 if (TRACK.equalsIgnoreCase(value)) {
1639 repeatMode = RotelRepeatMode.TRACK;
1640 updateChannelState(CHANNEL_REPEAT);
1641 } else if (DISC.equalsIgnoreCase(value)) {
1642 repeatMode = RotelRepeatMode.DISC;
1643 updateChannelState(CHANNEL_REPEAT);
1644 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1645 repeatMode = RotelRepeatMode.OFF;
1646 updateChannelState(CHANNEL_REPEAT);
1648 throw new RotelException("Invalid value");
1652 case KEY_PRESET_DAB:
1653 case KEY_PRESET_IRADIO:
1654 preset = Integer.parseInt(value);
1656 case KEY_DAB_PRESET:
1657 case KEY_IRADIO_PRESET:
1658 if (preset >= 1 && preset <= 30) {
1659 radioPreset = preset;
1663 updateChannelState(CHANNEL_RADIO_PRESET);
1667 preset = Integer.parseInt(value);
1668 if (preset >= 1 && preset <= 30) {
1669 radioPreset = preset;
1670 updateChannelState(CHANNEL_RADIO_PRESET);
1674 case KEY_FREQ_ZONE1:
1675 case KEY_FREQ_ZONE2:
1676 case KEY_FREQ_ZONE3:
1677 case KEY_FREQ_ZONE4:
1678 if (MSG_VALUE_OFF.equalsIgnoreCase(value) || MSG_VALUE_NONE.equalsIgnoreCase(value)) {
1679 frequencies[numZone] = 0.0;
1681 // Suppress a potential ending "k" or "K"
1682 if (value.toUpperCase().endsWith("K")) {
1683 value = value.substring(0, value.length() - 1);
1685 frequencies[numZone] = Double.parseDouble(value);
1688 updateChannelState(CHANNEL_FREQUENCY);
1690 updateGroupChannelState(numZone, CHANNEL_FREQUENCY);
1694 brightness = Integer.parseInt(value);
1695 updateChannelState(CHANNEL_BRIGHTNESS);
1696 updateChannelState(CHANNEL_ALL_BRIGHTNESS);
1698 case KEY_UPDATE_MODE:
1699 case KEY_DISPLAY_UPDATE:
1702 if (MSG_VALUE_ON.equalsIgnoreCase(value)) {
1704 updateChannelState(CHANNEL_TCBYPASS);
1705 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1707 updateChannelState(CHANNEL_TCBYPASS);
1709 throw new RotelException("Invalid value");
1713 if (MSG_VALUE_ON.equalsIgnoreCase(value)) {
1715 updateChannelState(CHANNEL_TCBYPASS);
1716 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1718 updateChannelState(CHANNEL_TCBYPASS);
1720 throw new RotelException("Invalid value");
1724 case KEY_BALANCE_ZONE1:
1725 case KEY_BALANCE_ZONE2:
1726 case KEY_BALANCE_ZONE3:
1727 case KEY_BALANCE_ZONE4:
1728 if (MSG_VALUE_MIN.equalsIgnoreCase(value)) {
1729 balances[numZone] = minBalanceLevel;
1730 } else if (MSG_VALUE_MAX.equalsIgnoreCase(value)) {
1731 balances[numZone] = maxBalanceLevel;
1732 } else if (value.toUpperCase().startsWith("L")) {
1733 balances[numZone] = -Integer.parseInt(value.substring(1));
1734 } else if (value.toUpperCase().startsWith("R")) {
1735 balances[numZone] = Integer.parseInt(value.substring(1));
1737 balances[numZone] = Integer.parseInt(value);
1740 updateChannelState(CHANNEL_BALANCE);
1742 updateGroupChannelState(numZone, CHANNEL_BALANCE);
1746 if (MSG_VALUE_SPEAKER_A.equalsIgnoreCase(value)) {
1749 updateChannelState(CHANNEL_SPEAKER_A);
1750 updateChannelState(CHANNEL_SPEAKER_B);
1751 } else if (MSG_VALUE_SPEAKER_B.equalsIgnoreCase(value)) {
1754 updateChannelState(CHANNEL_SPEAKER_A);
1755 updateChannelState(CHANNEL_SPEAKER_B);
1756 } else if (MSG_VALUE_SPEAKER_AB.equalsIgnoreCase(value)) {
1759 updateChannelState(CHANNEL_SPEAKER_A);
1760 updateChannelState(CHANNEL_SPEAKER_B);
1761 } else if (MSG_VALUE_OFF.equalsIgnoreCase(value)) {
1764 updateChannelState(CHANNEL_SPEAKER_A);
1765 updateChannelState(CHANNEL_SPEAKER_B);
1767 throw new RotelException("Invalid value");
1771 logger.debug("Sub level is set to {}", value);
1773 case KEY_CENTER_LEVEL:
1774 logger.debug("Center level is set to {}", value);
1776 case KEY_SURROUND_RIGHT_LEVEL:
1777 logger.debug("Surround right level is set to {}", value);
1779 case KEY_SURROUND_LEFT_LEVEL:
1780 logger.debug("Surround left level is set to {}", value);
1782 case KEY_CENTER_BACK_RIGHT_LEVEL:
1783 logger.debug("Center back right level is set to {}", value);
1785 case KEY_CENTER_BACK_LEFT_LEVEL:
1786 logger.debug("Center back left level is set to {}", value);
1788 case KEY_CEILING_FRONT_RIGHT_LEVEL:
1789 logger.debug("Ceiling front right level is set to {}", value);
1791 case KEY_CEILING_FRONT_LEFT_LEVEL:
1792 logger.debug("Ceiling front left level is set to {}", value);
1794 case KEY_CEILING_REAR_RIGHT_LEVEL:
1795 logger.debug("Ceiling rear right level is set to {}", value);
1797 case KEY_CEILING_REAR_LEFT_LEVEL:
1798 logger.debug("Ceiling rear left level is set to {}", value);
1800 case KEY_PCUSB_CLASS:
1801 logger.debug("PC-USB Audio Class is set to {}", value);
1803 case KEY_PRODUCT_TYPE:
1805 getThing().setProperty(Thing.PROPERTY_MODEL_ID, value);
1807 case KEY_PRODUCT_VERSION:
1809 getThing().setProperty(Thing.PROPERTY_FIRMWARE_VERSION, value);
1812 logger.debug("onNewMessageEvent: unhandled key {}", key);
1815 } catch (NumberFormatException | RotelException e) {
1816 logger.debug("Invalid value {} for key {}", value, key);
1821 * Handle the received information that device power (main zone) is ON
1823 private void handlePowerOn() {
1824 Boolean prev = powers[0];
1826 updateChannelState(CHANNEL_POWER);
1827 updateChannelState(CHANNEL_MAIN_POWER);
1828 updateChannelState(CHANNEL_ALL_POWER);
1829 if ((prev == null) || !prev) {
1830 schedulePowerOnJob();
1835 * Handle the received information that device power (main zone) is OFF
1837 private void handlePowerOff() {
1838 cancelPowerOnZoneJob(0);
1840 updateChannelState(CHANNEL_POWER);
1841 updateChannelState(CHANNEL_SOURCE);
1842 updateChannelState(CHANNEL_DSP);
1843 updateChannelState(CHANNEL_VOLUME);
1844 updateChannelState(CHANNEL_MUTE);
1845 updateChannelState(CHANNEL_BASS);
1846 updateChannelState(CHANNEL_TREBLE);
1847 updateChannelState(CHANNEL_PLAY_CONTROL);
1848 updateChannelState(CHANNEL_TRACK);
1849 updateChannelState(CHANNEL_RANDOM);
1850 updateChannelState(CHANNEL_REPEAT);
1851 updateChannelState(CHANNEL_RADIO_PRESET);
1852 updateChannelState(CHANNEL_FREQUENCY);
1853 updateChannelState(CHANNEL_BRIGHTNESS);
1854 updateChannelState(CHANNEL_TCBYPASS);
1855 updateChannelState(CHANNEL_BALANCE);
1856 updateChannelState(CHANNEL_SPEAKER_A);
1857 updateChannelState(CHANNEL_SPEAKER_B);
1859 updateChannelState(CHANNEL_MAIN_POWER);
1860 updateChannelState(CHANNEL_MAIN_SOURCE);
1861 updateChannelState(CHANNEL_MAIN_RECORD_SOURCE);
1862 updateChannelState(CHANNEL_MAIN_DSP);
1863 updateChannelState(CHANNEL_MAIN_VOLUME);
1864 updateChannelState(CHANNEL_MAIN_VOLUME_UP_DOWN);
1865 updateChannelState(CHANNEL_MAIN_MUTE);
1866 updateChannelState(CHANNEL_MAIN_BASS);
1867 updateChannelState(CHANNEL_MAIN_TREBLE);
1869 updateChannelState(CHANNEL_ALL_POWER);
1870 updateChannelState(CHANNEL_ALL_BRIGHTNESS);
1874 * Handle the received information that a zone power is ON
1876 private void handlePowerOnZone(int numZone) {
1877 Boolean prev = powers[numZone];
1878 powers[numZone] = true;
1879 updateGroupChannelState(numZone, CHANNEL_POWER);
1880 if ((prev == null) || !prev) {
1881 schedulePowerOnZoneJob(numZone, getVolumeDownCommand(numZone), getVolumeUpCommand(numZone));
1886 * Handle the received information that a zone power is OFF
1888 private void handlePowerOffZone(int numZone) {
1889 cancelPowerOnZoneJob(numZone);
1890 powers[numZone] = false;
1891 updateGroupChannelState(numZone, CHANNEL_POWER);
1892 updateGroupChannelState(numZone, CHANNEL_SOURCE);
1893 updateGroupChannelState(numZone, CHANNEL_VOLUME);
1894 updateGroupChannelState(numZone, CHANNEL_MUTE);
1895 updateGroupChannelState(numZone, CHANNEL_BASS);
1896 updateGroupChannelState(numZone, CHANNEL_TREBLE);
1897 updateGroupChannelState(numZone, CHANNEL_BALANCE);
1898 updateGroupChannelState(numZone, CHANNEL_FREQUENCY);
1899 updateGroupChannelState(numZone, CHANNEL_VOLUME_UP_DOWN);
1903 * Schedule the job that will consider the device as OFF if no new event is received before its running
1905 * @param switchOffAllZones true if all zones have to be considered as OFF
1907 private void schedulePowerOffJob(boolean switchOffAllZones) {
1908 logger.debug("Schedule power OFF job");
1909 cancelPowerOffJob();
1910 powerOffJob = scheduler.schedule(() -> {
1911 logger.debug("Power OFF job");
1913 if (switchOffAllZones) {
1914 for (int zone = 1; zone <= model.getNumberOfZones(); zone++) {
1915 handlePowerOffZone(zone);
1918 }, 2000, TimeUnit.MILLISECONDS);
1922 * Cancel the job that will consider the device as OFF
1924 private void cancelPowerOffJob() {
1925 ScheduledFuture<?> powerOffJob = this.powerOffJob;
1926 if (powerOffJob != null && !powerOffJob.isCancelled()) {
1927 powerOffJob.cancel(true);
1928 this.powerOffJob = null;
1933 * Schedule the job to run with a few seconds delay when the device power (main zone) switched ON
1935 private void schedulePowerOnJob() {
1936 logger.debug("Schedule power ON job");
1937 cancelPowerOnZoneJob(0);
1938 powerOnZoneJobs[0] = scheduler.schedule(() -> {
1939 synchronized (sequenceLock) {
1940 logger.debug("Power ON job");
1944 if (model.getRespNbChars() <= 13 && model.hasVolumeControl()) {
1945 sendCommand(getVolumeDownCommand(0));
1947 sendCommand(getVolumeUpCommand(0));
1950 if (model.getNumberOfZones() > 1) {
1951 if (currentZone != 1
1952 && model.getZoneSelectCmd() == RotelCommand.RECORD_FONCTION_SELECT) {
1953 selectZone(1, model.getZoneSelectCmd());
1954 } else if (!selectingRecord) {
1955 sendCommand(RotelCommand.RECORD_FONCTION_SELECT);
1959 sendCommand(RotelCommand.RECORD_FONCTION_SELECT);
1962 if (model.hasToneControl()) {
1963 if (model == RotelModel.RSX1065) {
1964 // No tone control select command
1965 sendCommand(RotelCommand.TREBLE_DOWN);
1967 sendCommand(RotelCommand.TREBLE_UP);
1969 sendCommand(RotelCommand.BASS_DOWN);
1971 sendCommand(RotelCommand.BASS_UP);
1974 selectFeature(2, null, RotelCommand.TONE_CONTROL_SELECT);
1979 if (model != RotelModel.RAP1580 && model != RotelModel.RDD1580
1980 && model != RotelModel.RSP1576 && model != RotelModel.RSP1582) {
1981 sendCommand(RotelCommand.UPDATE_AUTO);
1982 Thread.sleep(SLEEP_INTV);
1984 if (model.hasSourceControl()) {
1985 sendCommand(RotelCommand.SOURCE);
1986 Thread.sleep(SLEEP_INTV);
1988 if (model.hasVolumeControl() || model.hasToneControl()) {
1989 if (model.hasVolumeControl() && model != RotelModel.RAP1580
1990 && model != RotelModel.RSP1576 && model != RotelModel.RSP1582) {
1991 sendCommand(RotelCommand.VOLUME_GET_MIN);
1992 Thread.sleep(SLEEP_INTV);
1993 sendCommand(RotelCommand.VOLUME_GET_MAX);
1994 Thread.sleep(SLEEP_INTV);
1996 if (model.hasToneControl()) {
1997 sendCommand(RotelCommand.TONE_MAX);
1998 Thread.sleep(SLEEP_INTV);
2000 // Wait enough to be sure to get the min/max values requested just before
2002 if (model.hasVolumeControl()) {
2003 sendCommand(RotelCommand.VOLUME_GET);
2004 Thread.sleep(SLEEP_INTV);
2005 if (model != RotelModel.RA11 && model != RotelModel.RA12
2006 && model != RotelModel.RCX1500) {
2007 sendCommand(RotelCommand.MUTE);
2008 Thread.sleep(SLEEP_INTV);
2011 if (model.hasToneControl()) {
2012 sendCommand(RotelCommand.BASS);
2013 Thread.sleep(SLEEP_INTV);
2014 sendCommand(RotelCommand.TREBLE);
2015 Thread.sleep(SLEEP_INTV);
2016 if (model.canGetBypassStatus()) {
2017 sendCommand(RotelCommand.TONE_CONTROLS);
2018 Thread.sleep(SLEEP_INTV);
2022 if (model.hasBalanceControl()) {
2023 sendCommand(RotelCommand.BALANCE);
2024 Thread.sleep(SLEEP_INTV);
2026 if (model.hasPlayControl()) {
2027 RotelSource source = sources[0];
2028 if (model != RotelModel.RCD1570 && model != RotelModel.RCD1572
2029 && (model != RotelModel.RCX1500 || source == null
2030 || !source.getName().equals("CD"))) {
2031 sendCommand(RotelCommand.PLAY_STATUS);
2032 Thread.sleep(SLEEP_INTV);
2034 sendCommand(RotelCommand.CD_PLAY_STATUS);
2035 Thread.sleep(SLEEP_INTV);
2038 if (model.hasDspControl()) {
2039 sendCommand(RotelCommand.DSP_MODE);
2040 Thread.sleep(SLEEP_INTV);
2042 if (model.canGetFrequency()) {
2043 sendCommand(RotelCommand.FREQUENCY);
2044 Thread.sleep(SLEEP_INTV);
2046 if (model.hasDimmerControl() && model.canGetDimmerLevel()) {
2047 sendCommand(RotelCommand.DIMMER_LEVEL_GET);
2048 Thread.sleep(SLEEP_INTV);
2050 if (model.hasSpeakerGroups()) {
2051 sendCommand(RotelCommand.SPEAKER);
2052 Thread.sleep(SLEEP_INTV);
2054 if (model != RotelModel.RAP1580 && model != RotelModel.RSP1576
2055 && model != RotelModel.RSP1582) {
2056 sendCommand(RotelCommand.MODEL);
2057 Thread.sleep(SLEEP_INTV);
2058 sendCommand(RotelCommand.VERSION);
2059 Thread.sleep(SLEEP_INTV);
2063 sendCommand(RotelCommand.UPDATE_AUTO);
2064 Thread.sleep(SLEEP_INTV);
2065 if (model.hasSourceControl()) {
2066 if (model.getNumberOfZones() > 1) {
2067 sendCommand(RotelCommand.INPUT);
2069 sendCommand(RotelCommand.SOURCE);
2071 Thread.sleep(SLEEP_INTV);
2073 if (model.hasVolumeControl()) {
2074 sendCommand(RotelCommand.VOLUME_GET);
2075 Thread.sleep(SLEEP_INTV);
2076 sendCommand(RotelCommand.MUTE);
2077 Thread.sleep(SLEEP_INTV);
2079 if (model.hasToneControl()) {
2080 sendCommand(RotelCommand.BASS);
2081 Thread.sleep(SLEEP_INTV);
2082 sendCommand(RotelCommand.TREBLE);
2083 Thread.sleep(SLEEP_INTV);
2084 if (model.canGetBypassStatus()) {
2085 sendCommand(RotelCommand.TCBYPASS);
2086 Thread.sleep(SLEEP_INTV);
2089 if (model.hasBalanceControl()) {
2090 sendCommand(RotelCommand.BALANCE);
2091 Thread.sleep(SLEEP_INTV);
2093 if (model.hasPlayControl()) {
2094 sendCommand(RotelCommand.PLAY_STATUS);
2095 Thread.sleep(SLEEP_INTV);
2096 RotelSource source = sources[0];
2097 if (source != null && source.getName().equals("CD") && !model.hasSourceControl()) {
2098 sendCommand(RotelCommand.TRACK);
2099 Thread.sleep(SLEEP_INTV);
2100 sendCommand(RotelCommand.RANDOM_MODE);
2101 Thread.sleep(SLEEP_INTV);
2102 sendCommand(RotelCommand.REPEAT_MODE);
2103 Thread.sleep(SLEEP_INTV);
2106 if (model.hasDspControl()) {
2107 sendCommand(RotelCommand.DSP_MODE);
2108 Thread.sleep(SLEEP_INTV);
2110 if (model.canGetFrequency()) {
2111 sendCommand(RotelCommand.FREQUENCY);
2112 Thread.sleep(SLEEP_INTV);
2114 if (model.hasDimmerControl() && model.canGetDimmerLevel()) {
2115 sendCommand(RotelCommand.DIMMER_LEVEL_GET);
2116 Thread.sleep(SLEEP_INTV);
2118 if (model.hasSpeakerGroups()) {
2119 sendCommand(RotelCommand.SPEAKER);
2120 Thread.sleep(SLEEP_INTV);
2122 sendCommand(RotelCommand.MODEL);
2123 Thread.sleep(SLEEP_INTV);
2124 sendCommand(RotelCommand.VERSION);
2125 Thread.sleep(SLEEP_INTV);
2128 } catch (RotelException e) {
2129 logger.debug("Init sequence failed: {}", e.getMessage());
2130 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
2131 "@text/offline.comm-error-init-sequence");
2133 } catch (InterruptedException e) {
2134 logger.debug("Init sequence interrupted: {}", e.getMessage());
2135 Thread.currentThread().interrupt();
2138 }, 2500, TimeUnit.MILLISECONDS);
2142 * Schedule the job to run with a few seconds delay when the zone power switched ON
2144 private void schedulePowerOnZoneJob(int numZone, RotelCommand volumeDown, RotelCommand volumeUp) {
2145 logger.debug("Schedule power ON zone {} job", numZone);
2146 cancelPowerOnZoneJob(numZone);
2147 powerOnZoneJobs[numZone] = scheduler.schedule(() -> {
2148 synchronized (sequenceLock) {
2149 logger.debug("Power ON zone {} job", numZone);
2151 if (protocol == RotelProtocol.HEX && model.getNumberOfZones() >= numZone) {
2152 selectZone(numZone, model.getZoneSelectCmd());
2153 sendCommand(model.hasZoneCommands(numZone) ? volumeDown : RotelCommand.VOLUME_DOWN);
2155 sendCommand(model.hasZoneCommands(numZone) ? volumeUp : RotelCommand.VOLUME_UP);
2158 } catch (RotelException e) {
2159 logger.debug("Init sequence zone {} failed: {}", numZone, e.getMessage());
2160 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
2161 String.format("@text/offline.comm-error-init-sequence-zone [\"%d\"]", numZone));
2163 } catch (InterruptedException e) {
2164 logger.debug("Init sequence zone {} interrupted: {}", numZone, e.getMessage());
2165 Thread.currentThread().interrupt();
2168 }, 2500, TimeUnit.MILLISECONDS);
2172 * Cancel the job scheduled when the device power (main zone) or a zone power switched ON
2174 private void cancelPowerOnZoneJob(int numZone) {
2175 ScheduledFuture<?> powerOnZoneJob = powerOnZoneJobs[numZone];
2176 if (powerOnZoneJob != null && !powerOnZoneJob.isCancelled()) {
2177 powerOnZoneJob.cancel(true);
2178 powerOnZoneJobs[numZone] = null;
2183 * Schedule the reconnection job
2185 private void scheduleReconnectJob() {
2186 logger.debug("Schedule reconnect job");
2187 cancelReconnectJob();
2188 reconnectJob = scheduler.scheduleWithFixedDelay(() -> {
2189 if (!connector.isConnected()) {
2190 logger.debug("Trying to reconnect...");
2193 String error = null;
2194 if (openConnection()) {
2195 synchronized (sequenceLock) {
2196 schedulePowerOffJob(true);
2198 sendCommand(model.getPowerStateCmd());
2199 } catch (RotelException e) {
2200 error = "@text/offline.comm-error-first-command-after-reconnection";
2201 logger.debug("First command after connection failed", e);
2202 cancelPowerOffJob();
2207 error = "@text/offline.comm-error-reconnection";
2209 if (error != null) {
2211 for (int zone = 1; zone <= model.getNumberOfZones(); zone++) {
2212 handlePowerOffZone(zone);
2214 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, error);
2216 updateStatus(ThingStatus.ONLINE);
2219 }, 1, POLLING_INTERVAL, TimeUnit.SECONDS);
2223 * Cancel the reconnection job
2225 private void cancelReconnectJob() {
2226 ScheduledFuture<?> reconnectJob = this.reconnectJob;
2227 if (reconnectJob != null && !reconnectJob.isCancelled()) {
2228 reconnectJob.cancel(true);
2229 this.reconnectJob = null;
2233 private void updateGroupChannelState(int numZone, String channel) {
2234 updateChannelState(String.format("zone%d#%s", numZone, channel));
2238 * Update the state of a channel
2240 * @param channel the channel
2242 private void updateChannelState(String channel) {
2243 if (!isLinked(channel)) {
2246 State state = UnDefType.UNDEF;
2247 RotelSource localSource;
2250 case CHANNEL_ZONE1_SOURCE:
2251 case CHANNEL_ZONE1_VOLUME:
2252 case CHANNEL_ZONE1_MUTE:
2253 case CHANNEL_ZONE1_BASS:
2254 case CHANNEL_ZONE1_TREBLE:
2255 case CHANNEL_ZONE1_BALANCE:
2256 case CHANNEL_ZONE1_FREQUENCY:
2259 case CHANNEL_ZONE2_POWER:
2260 case CHANNEL_ZONE2_SOURCE:
2261 case CHANNEL_ZONE2_VOLUME:
2262 case CHANNEL_ZONE2_VOLUME_UP_DOWN:
2263 case CHANNEL_ZONE2_MUTE:
2264 case CHANNEL_ZONE2_BASS:
2265 case CHANNEL_ZONE2_TREBLE:
2266 case CHANNEL_ZONE2_BALANCE:
2267 case CHANNEL_ZONE2_FREQUENCY:
2270 case CHANNEL_ZONE3_POWER:
2271 case CHANNEL_ZONE3_SOURCE:
2272 case CHANNEL_ZONE3_VOLUME:
2273 case CHANNEL_ZONE3_MUTE:
2274 case CHANNEL_ZONE3_BASS:
2275 case CHANNEL_ZONE3_TREBLE:
2276 case CHANNEL_ZONE3_BALANCE:
2277 case CHANNEL_ZONE3_FREQUENCY:
2280 case CHANNEL_ZONE4_POWER:
2281 case CHANNEL_ZONE4_SOURCE:
2282 case CHANNEL_ZONE4_VOLUME:
2283 case CHANNEL_ZONE4_MUTE:
2284 case CHANNEL_ZONE4_BASS:
2285 case CHANNEL_ZONE4_TREBLE:
2286 case CHANNEL_ZONE4_BALANCE:
2287 case CHANNEL_ZONE4_FREQUENCY:
2295 case CHANNEL_MAIN_POWER:
2296 case CHANNEL_ALL_POWER:
2297 case CHANNEL_ZONE2_POWER:
2298 case CHANNEL_ZONE3_POWER:
2299 case CHANNEL_ZONE4_POWER:
2300 Boolean powerZone = powers[numZone];
2301 if (powerZone != null) {
2302 state = OnOffType.from(powerZone.booleanValue());
2305 case CHANNEL_SOURCE:
2306 case CHANNEL_MAIN_SOURCE:
2307 case CHANNEL_ZONE1_SOURCE:
2308 case CHANNEL_ZONE2_SOURCE:
2309 case CHANNEL_ZONE3_SOURCE:
2310 case CHANNEL_ZONE4_SOURCE:
2311 localSource = sources[numZone];
2312 if (isPowerOn(numZone) && localSource != null) {
2313 state = new StringType(localSource.getName());
2316 case CHANNEL_MAIN_RECORD_SOURCE:
2317 localSource = recordSource;
2318 if (isPowerOn() && localSource != null) {
2319 state = new StringType(localSource.getName());
2323 case CHANNEL_MAIN_DSP:
2325 state = new StringType(dsp.getName());
2328 case CHANNEL_VOLUME:
2329 case CHANNEL_MAIN_VOLUME:
2330 case CHANNEL_ZONE1_VOLUME:
2331 case CHANNEL_ZONE2_VOLUME:
2332 case CHANNEL_ZONE3_VOLUME:
2333 case CHANNEL_ZONE4_VOLUME:
2334 if (isPowerOn(numZone) && !fixedVolumeZones[numZone]) {
2335 long volumePct = Math
2336 .round((double) (volumes[numZone] - minVolume) / (double) (maxVolume - minVolume) * 100.0);
2337 state = new PercentType(BigDecimal.valueOf(volumePct));
2340 case CHANNEL_MAIN_VOLUME_UP_DOWN:
2341 case CHANNEL_ZONE2_VOLUME_UP_DOWN:
2342 if (isPowerOn(numZone) && !fixedVolumeZones[numZone]) {
2343 state = new DecimalType(volumes[numZone]);
2347 case CHANNEL_MAIN_MUTE:
2348 case CHANNEL_ZONE1_MUTE:
2349 case CHANNEL_ZONE2_MUTE:
2350 case CHANNEL_ZONE3_MUTE:
2351 case CHANNEL_ZONE4_MUTE:
2352 if (isPowerOn(numZone)) {
2353 state = OnOffType.from(mutes[numZone]);
2357 case CHANNEL_MAIN_BASS:
2358 case CHANNEL_ZONE1_BASS:
2359 case CHANNEL_ZONE2_BASS:
2360 case CHANNEL_ZONE3_BASS:
2361 case CHANNEL_ZONE4_BASS:
2362 if (isPowerOn(numZone)) {
2363 state = new DecimalType(basses[numZone]);
2366 case CHANNEL_TREBLE:
2367 case CHANNEL_MAIN_TREBLE:
2368 case CHANNEL_ZONE1_TREBLE:
2369 case CHANNEL_ZONE2_TREBLE:
2370 case CHANNEL_ZONE3_TREBLE:
2371 case CHANNEL_ZONE4_TREBLE:
2372 if (isPowerOn(numZone)) {
2373 state = new DecimalType(trebles[numZone]);
2377 if (isPowerOn() && track > 0) {
2378 state = new DecimalType(track);
2381 case CHANNEL_RANDOM:
2383 state = OnOffType.from(randomMode);
2386 case CHANNEL_REPEAT:
2388 state = new StringType(repeatMode.name());
2391 case CHANNEL_PLAY_CONTROL:
2393 switch (playStatus) {
2395 state = PlayPauseType.PLAY;
2399 state = PlayPauseType.PAUSE;
2404 case CHANNEL_RADIO_PRESET:
2406 state = radioPreset == 0 ? UnDefType.UNDEF : new DecimalType(radioPreset);
2409 case CHANNEL_FREQUENCY:
2410 case CHANNEL_ZONE1_FREQUENCY:
2411 case CHANNEL_ZONE2_FREQUENCY:
2412 case CHANNEL_ZONE3_FREQUENCY:
2413 case CHANNEL_ZONE4_FREQUENCY:
2414 if (isPowerOn(numZone) && frequencies[numZone] > 0.0) {
2415 state = new DecimalType(frequencies[numZone]);
2419 state = new StringType(frontPanelLine1);
2422 state = new StringType(frontPanelLine2);
2424 case CHANNEL_BRIGHTNESS:
2425 case CHANNEL_ALL_BRIGHTNESS:
2426 if (isPowerOn() && model.hasDimmerControl()) {
2427 long dimmerPct = Math.round((double) (brightness - model.getDimmerLevelMin())
2428 / (double) (model.getDimmerLevelMax() - model.getDimmerLevelMin()) * 100.0);
2429 state = new PercentType(BigDecimal.valueOf(dimmerPct));
2432 case CHANNEL_TCBYPASS:
2434 state = OnOffType.from(tcbypass);
2437 case CHANNEL_BALANCE:
2438 case CHANNEL_ZONE1_BALANCE:
2439 case CHANNEL_ZONE2_BALANCE:
2440 case CHANNEL_ZONE3_BALANCE:
2441 case CHANNEL_ZONE4_BALANCE:
2442 if (isPowerOn(numZone)) {
2443 state = new DecimalType(balances[numZone]);
2446 case CHANNEL_SPEAKER_A:
2448 state = OnOffType.from(speakera);
2451 case CHANNEL_SPEAKER_B:
2453 state = OnOffType.from(speakerb);
2459 updateState(channel, state);
2463 * Inform about the device / main zone power state
2465 * @return true if device / main zone power state is known and known as ON
2467 private boolean isPowerOn() {
2468 return isPowerOn(0);
2472 * Inform about the power state
2474 * @param numZone the zone number (1-4) or 0 for the device or main zone
2476 * @return true if power state is known and known as ON
2478 private boolean isPowerOn(int numZone) {
2479 if (numZone < 0 || numZone > MAX_NUMBER_OF_ZONES) {
2480 throw new IllegalArgumentException("numZone must be in range 0-" + MAX_NUMBER_OF_ZONES);
2482 Boolean power = powers[numZone];
2483 return (numZone > 0 && !powerControlPerZone) ? isPowerOn(0) : power != null && power.booleanValue();
2487 * Get the command to be used for POWER ON
2489 * @param numZone the zone number (2-4) or 0 for the device or main zone
2491 * @return the command
2493 private RotelCommand getPowerOnCommand(int numZone) {
2496 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_POWER_ON : RotelCommand.POWER_ON;
2498 return RotelCommand.ZONE2_POWER_ON;
2500 return RotelCommand.ZONE3_POWER_ON;
2502 return RotelCommand.ZONE4_POWER_ON;
2504 throw new IllegalArgumentException("No power ON command defined for zone " + numZone);
2509 * Get the command to be used for POWER OFF
2511 * @param numZone the zone number (2-4) or 0 for the device or main zone
2513 * @return the command
2515 private RotelCommand getPowerOffCommand(int numZone) {
2518 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_POWER_OFF : RotelCommand.POWER_OFF;
2520 return RotelCommand.ZONE2_POWER_OFF;
2522 return RotelCommand.ZONE3_POWER_OFF;
2524 return RotelCommand.ZONE4_POWER_OFF;
2526 throw new IllegalArgumentException("No power OFF command defined for zone " + numZone);
2531 * Get the command to be used for VOLUME UP
2533 * @param numZone the zone number (1-4) or 0 for the device or main zone
2535 * @return the command
2537 private RotelCommand getVolumeUpCommand(int numZone) {
2540 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_VOLUME_UP : RotelCommand.VOLUME_UP;
2542 return RotelCommand.ZONE1_VOLUME_UP;
2544 return RotelCommand.ZONE2_VOLUME_UP;
2546 return RotelCommand.ZONE3_VOLUME_UP;
2548 return RotelCommand.ZONE4_VOLUME_UP;
2550 throw new IllegalArgumentException("No VOLUME UP command defined for zone " + numZone);
2555 * Get the command to be used for VOLUME DOWN
2557 * @param numZone the zone number (1-4) or 0 for the device or main zone
2559 * @return the command
2561 private RotelCommand getVolumeDownCommand(int numZone) {
2564 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_VOLUME_DOWN
2565 : RotelCommand.VOLUME_DOWN;
2567 return RotelCommand.ZONE1_VOLUME_DOWN;
2569 return RotelCommand.ZONE2_VOLUME_DOWN;
2571 return RotelCommand.ZONE3_VOLUME_DOWN;
2573 return RotelCommand.ZONE4_VOLUME_DOWN;
2575 throw new IllegalArgumentException("No VOLUME DOWN command defined for zone " + numZone);
2580 * Get the command to be used for VOLUME SET
2582 * @param numZone the zone number (1-4) or 0 for the device
2584 * @return the command
2586 private RotelCommand getVolumeSetCommand(int numZone) {
2589 return RotelCommand.VOLUME_SET;
2591 return RotelCommand.ZONE1_VOLUME_SET;
2593 return RotelCommand.ZONE2_VOLUME_SET;
2595 return RotelCommand.ZONE3_VOLUME_SET;
2597 return RotelCommand.ZONE4_VOLUME_SET;
2599 throw new IllegalArgumentException("No VOLUME SET command defined for zone " + numZone);
2604 * Get the command to be used for MUTE ON
2606 * @param numZone the zone number (1-4) or 0 for the device or main zone
2608 * @return the command
2610 private RotelCommand getMuteOnCommand(int numZone) {
2613 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_MUTE_ON : RotelCommand.MUTE_ON;
2615 return RotelCommand.ZONE1_MUTE_ON;
2617 return RotelCommand.ZONE2_MUTE_ON;
2619 return RotelCommand.ZONE3_MUTE_ON;
2621 return RotelCommand.ZONE4_MUTE_ON;
2623 throw new IllegalArgumentException("No MUTE ON command defined for zone " + numZone);
2628 * Get the command to be used for MUTE OFF
2630 * @param numZone the zone number (1-4) or 0 for the device or main zone
2632 * @return the command
2634 private RotelCommand getMuteOffCommand(int numZone) {
2637 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_MUTE_OFF : RotelCommand.MUTE_OFF;
2639 return RotelCommand.ZONE1_MUTE_OFF;
2641 return RotelCommand.ZONE2_MUTE_OFF;
2643 return RotelCommand.ZONE3_MUTE_OFF;
2645 return RotelCommand.ZONE4_MUTE_OFF;
2647 throw new IllegalArgumentException("No MUTE OFF command defined for zone " + numZone);
2652 * Get the command to be used for MUTE TOGGLE
2654 * @param numZone the zone number (1-4) or 0 for the device or main zone
2656 * @return the command
2658 private RotelCommand getMuteToggleCommand(int numZone) {
2661 return model.hasOtherThanPrimaryCommands() ? RotelCommand.MAIN_ZONE_MUTE_TOGGLE
2662 : RotelCommand.MUTE_TOGGLE;
2664 return RotelCommand.ZONE1_MUTE_TOGGLE;
2666 return RotelCommand.ZONE2_MUTE_TOGGLE;
2668 return RotelCommand.ZONE3_MUTE_TOGGLE;
2670 return RotelCommand.ZONE4_MUTE_TOGGLE;
2672 throw new IllegalArgumentException("No MUTE TOGGLE command defined for zone " + numZone);
2677 * Get the command to be used for BASS UP
2679 * @param numZone the zone number (1-4) or 0 for the device
2681 * @return the command
2683 private RotelCommand getBassUpCommand(int numZone) {
2686 return RotelCommand.BASS_UP;
2688 return RotelCommand.ZONE1_BASS_UP;
2690 return RotelCommand.ZONE2_BASS_UP;
2692 return RotelCommand.ZONE3_BASS_UP;
2694 return RotelCommand.ZONE4_BASS_UP;
2696 throw new IllegalArgumentException("No BASS UP command defined for zone " + numZone);
2701 * Get the command to be used for BASS DOWN
2703 * @param numZone the zone number (1-4) or 0 for the device
2705 * @return the command
2707 private RotelCommand getBassDownCommand(int numZone) {
2710 return RotelCommand.BASS_DOWN;
2712 return RotelCommand.ZONE1_BASS_DOWN;
2714 return RotelCommand.ZONE2_BASS_DOWN;
2716 return RotelCommand.ZONE3_BASS_DOWN;
2718 return RotelCommand.ZONE4_BASS_DOWN;
2720 throw new IllegalArgumentException("No BASS DOWN command defined for zone " + numZone);
2725 * Get the command to be used for BASS SET
2727 * @param numZone the zone number (1-4) or 0 for the device
2729 * @return the command
2731 private RotelCommand getBassSetCommand(int numZone) {
2734 return RotelCommand.BASS_SET;
2736 return RotelCommand.ZONE1_BASS_SET;
2738 return RotelCommand.ZONE2_BASS_SET;
2740 return RotelCommand.ZONE3_BASS_SET;
2742 return RotelCommand.ZONE4_BASS_SET;
2744 throw new IllegalArgumentException("No BASS SET command defined for zone " + numZone);
2749 * Get the command to be used for TREBLE UP
2751 * @param numZone the zone number (1-4) or 0 for the device
2753 * @return the command
2755 private RotelCommand getTrebleUpCommand(int numZone) {
2758 return RotelCommand.TREBLE_UP;
2760 return RotelCommand.ZONE1_TREBLE_UP;
2762 return RotelCommand.ZONE2_TREBLE_UP;
2764 return RotelCommand.ZONE3_TREBLE_UP;
2766 return RotelCommand.ZONE4_TREBLE_UP;
2768 throw new IllegalArgumentException("No TREBLE UP command defined for zone " + numZone);
2773 * Get the command to be used for TREBLE DOWN
2775 * @param numZone the zone number (1-4) or 0 for the device
2777 * @return the command
2779 private RotelCommand getTrebleDownCommand(int numZone) {
2782 return RotelCommand.TREBLE_DOWN;
2784 return RotelCommand.ZONE1_TREBLE_DOWN;
2786 return RotelCommand.ZONE2_TREBLE_DOWN;
2788 return RotelCommand.ZONE3_TREBLE_DOWN;
2790 return RotelCommand.ZONE4_TREBLE_DOWN;
2792 throw new IllegalArgumentException("No TREBLE DOWN command defined for zone " + numZone);
2797 * Get the command to be used for TREBLE SET
2799 * @param numZone the zone number (1-4) or 0 for the device
2801 * @return the command
2803 private RotelCommand getTrebleSetCommand(int numZone) {
2806 return RotelCommand.TREBLE_SET;
2808 return RotelCommand.ZONE1_TREBLE_SET;
2810 return RotelCommand.ZONE2_TREBLE_SET;
2812 return RotelCommand.ZONE3_TREBLE_SET;
2814 return RotelCommand.ZONE4_TREBLE_SET;
2816 throw new IllegalArgumentException("No TREBLE SET command defined for zone " + numZone);
2821 * Get the command to be used for BALANCE LEFT
2823 * @param numZone the zone number (1-4) or 0 for the device
2825 * @return the command
2827 private RotelCommand getBalanceLeftCommand(int numZone) {
2830 return RotelCommand.BALANCE_LEFT;
2832 return RotelCommand.ZONE1_BALANCE_LEFT;
2834 return RotelCommand.ZONE2_BALANCE_LEFT;
2836 return RotelCommand.ZONE3_BALANCE_LEFT;
2838 return RotelCommand.ZONE4_BALANCE_LEFT;
2840 throw new IllegalArgumentException("No BALANCE LEFT command defined for zone " + numZone);
2845 * Get the command to be used for BALANCE RIGHT
2847 * @param numZone the zone number (1-4) or 0 for the device
2849 * @return the command
2851 private RotelCommand getBalanceRightCommand(int numZone) {
2854 return RotelCommand.BALANCE_RIGHT;
2856 return RotelCommand.ZONE1_BALANCE_RIGHT;
2858 return RotelCommand.ZONE2_BALANCE_RIGHT;
2860 return RotelCommand.ZONE3_BALANCE_RIGHT;
2862 return RotelCommand.ZONE4_BALANCE_RIGHT;
2864 throw new IllegalArgumentException("No BALANCE RIGHT command defined for zone " + numZone);
2869 * Get the command to be used for BALANCE SET
2871 * @param numZone the zone number (1-4) or 0 for the device
2873 * @return the command
2875 private RotelCommand getBalanceSetCommand(int numZone) {
2878 return RotelCommand.BALANCE_SET;
2880 return RotelCommand.ZONE1_BALANCE_SET;
2882 return RotelCommand.ZONE2_BALANCE_SET;
2884 return RotelCommand.ZONE3_BALANCE_SET;
2886 return RotelCommand.ZONE4_BALANCE_SET;
2888 throw new IllegalArgumentException("No BALANCE SET command defined for zone " + numZone);
2892 private @Nullable RotelCommand getRadioPresetGetCommand(RotelSource source) {
2893 if (protocol == RotelProtocol.ASCII_V1) {
2894 switch (source.getName()) {
2898 return RotelCommand.PRESET;
2902 } else if (protocol == RotelProtocol.ASCII_V2) {
2903 switch (source.getName()) {
2905 return RotelCommand.FM_PRESET;
2907 return RotelCommand.DAB_PRESET;
2915 private @Nullable RotelCommand getRadioPresetCallCommand(RotelSource source) {
2916 switch (source.getName()) {
2918 return RotelCommand.CALL_FM_PRESET;
2920 return RotelCommand.CALL_DAB_PRESET;
2922 return RotelCommand.CALL_IRADIO_PRESET;
2929 private void sendCommand(RotelCommand cmd) throws RotelException {
2930 sendCommand(cmd, null);
2934 * Request the Rotel device to execute a command
2936 * @param cmd the command to execute
2937 * @param value the integer value to consider for volume, bass or treble adjustment
2939 * @throws RotelException - In case of any problem
2941 private void sendCommand(RotelCommand cmd, @Nullable Integer value) throws RotelException {
2944 message = protocolHandler.buildCommandMessage(cmd, value);
2945 } catch (RotelException e) {
2946 // Command not supported
2947 logger.debug("sendCommand: {}", e.getMessage());
2950 connector.writeOutput(cmd, message);
2952 if (connector instanceof RotelSimuConnector) {
2953 if ((protocol == RotelProtocol.HEX && cmd.getHexType() != 0)
2954 || (protocol == RotelProtocol.ASCII_V1 && cmd.getAsciiCommandV1() != null)
2955 || (protocol == RotelProtocol.ASCII_V2 && cmd.getAsciiCommandV2() != null)) {
2956 ((RotelSimuConnector) connector).buildFeedbackMessage(cmd, value);