[volumio] Removing compiler warnings (#15379)

Signed-off-by: miloit <MichaelLoercher@web.de>
This commit is contained in:
miloit 2023-08-14 14:14:50 +02:00 committed by GitHub
parent 98406476f8
commit c465344c01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 82 additions and 31 deletions

View File

@ -66,12 +66,12 @@ public class VolumioHandler extends BaseThingHandler {
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("channelUID: {}", channelUID); VolumioService volumioLocal = volumio;
if (volumio == null) { if (volumioLocal == null) {
logger.debug("Ignoring command {} = {} because device is offline.", channelUID.getId(), command);
if (ThingStatus.ONLINE.equals(getThing().getStatus())) { if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "device is offline"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized, cannot handle command.");
} }
return; return;
} }
@ -94,7 +94,7 @@ public class VolumioHandler extends BaseThingHandler {
case VolumioBindingConstants.CHANNEL_PLAY_RADIO_STREAM: case VolumioBindingConstants.CHANNEL_PLAY_RADIO_STREAM:
if (command instanceof StringType) { if (command instanceof StringType) {
final String uri = command.toFullString(); final String uri = command.toFullString();
volumio.replacePlay(uri, "Radio", VolumioServiceTypes.WEBRADIO); volumioLocal.replacePlay(uri, "Radio", VolumioServiceTypes.WEBRADIO);
} }
break; break;
@ -102,7 +102,7 @@ public class VolumioHandler extends BaseThingHandler {
case VolumioBindingConstants.CHANNEL_PLAY_URI: case VolumioBindingConstants.CHANNEL_PLAY_URI:
if (command instanceof StringType) { if (command instanceof StringType) {
final String uri = command.toFullString(); final String uri = command.toFullString();
volumio.replacePlay(uri, "URI", VolumioServiceTypes.WEBRADIO); volumioLocal.replacePlay(uri, "URI", VolumioServiceTypes.WEBRADIO);
} }
break; break;
@ -110,7 +110,7 @@ public class VolumioHandler extends BaseThingHandler {
case VolumioBindingConstants.CHANNEL_PLAY_FILE: case VolumioBindingConstants.CHANNEL_PLAY_FILE:
if (command instanceof StringType) { if (command instanceof StringType) {
final String uri = command.toFullString(); final String uri = command.toFullString();
volumio.replacePlay(uri, "", VolumioServiceTypes.MPD); volumioLocal.replacePlay(uri, "", VolumioServiceTypes.MPD);
} }
break; break;
@ -118,13 +118,13 @@ public class VolumioHandler extends BaseThingHandler {
case VolumioBindingConstants.CHANNEL_PLAY_PLAYLIST: case VolumioBindingConstants.CHANNEL_PLAY_PLAYLIST:
if (command instanceof StringType) { if (command instanceof StringType) {
final String playlistName = command.toFullString(); final String playlistName = command.toFullString();
volumio.playPlaylist(playlistName); volumioLocal.playPlaylist(playlistName);
} }
break; break;
case VolumioBindingConstants.CHANNEL_CLEAR_QUEUE: case VolumioBindingConstants.CHANNEL_CLEAR_QUEUE:
if ((command instanceof OnOffType) && (command == OnOffType.ON)) { if ((command instanceof OnOffType) && (command == OnOffType.ON)) {
volumio.clearQueue(); volumioLocal.clearQueue();
// Make it feel like a toggle button ... // Make it feel like a toggle button ...
updateState(channelUID, OnOffType.OFF); updateState(channelUID, OnOffType.OFF);
} }
@ -132,18 +132,18 @@ public class VolumioHandler extends BaseThingHandler {
case VolumioBindingConstants.CHANNEL_PLAY_RANDOM: case VolumioBindingConstants.CHANNEL_PLAY_RANDOM:
if (command instanceof OnOffType) { if (command instanceof OnOffType) {
boolean enableRandom = command == OnOffType.ON; boolean enableRandom = command == OnOffType.ON;
volumio.setRandom(enableRandom); volumioLocal.setRandom(enableRandom);
} }
break; break;
case VolumioBindingConstants.CHANNEL_PLAY_REPEAT: case VolumioBindingConstants.CHANNEL_PLAY_REPEAT:
if (command instanceof OnOffType) { if (command instanceof OnOffType) {
boolean enableRepeat = command == OnOffType.ON; boolean enableRepeat = command == OnOffType.ON;
volumio.setRepeat(enableRepeat); volumioLocal.setRepeat(enableRepeat);
} }
break; break;
case "REFRESH": case "REFRESH":
logger.debug("Called Refresh"); logger.debug("Called Refresh");
volumio.getState(); volumioLocal.getState();
break; break;
case VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND: case VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND:
if (command instanceof StringType) { if (command instanceof StringType) {
@ -170,8 +170,18 @@ public class VolumioHandler extends BaseThingHandler {
} }
private void sendSystemCommand(Command command) { private void sendSystemCommand(Command command) {
VolumioService volumioLocal = volumio;
if (volumioLocal == null) {
if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized, cannot handle send system command.");
}
return;
}
if (command instanceof StringType) { if (command instanceof StringType) {
volumio.sendSystemCommand(command.toString()); volumioLocal.sendSystemCommand(command.toString());
updateState(VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND, UnDefType.UNDEF); updateState(VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND, UnDefType.UNDEF);
} else if (command.equals(RefreshType.REFRESH)) { } else if (command.equals(RefreshType.REFRESH)) {
updateState(VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND, UnDefType.UNDEF); updateState(VolumioBindingConstants.CHANNEL_SYSTEM_COMMAND, UnDefType.UNDEF);
@ -188,18 +198,38 @@ public class VolumioHandler extends BaseThingHandler {
} }
private void handleVolumeCommand(Command command) { private void handleVolumeCommand(Command command) {
if (command instanceof PercentType) { VolumioService volumioLocal = volumio;
volumio.setVolume((PercentType) command);
if (volumioLocal == null) {
if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized, cannot handle volume command.");
}
return;
}
if (command instanceof PercentType commandAsPercentType) {
volumioLocal.setVolume(commandAsPercentType);
} else if (command instanceof RefreshType) { } else if (command instanceof RefreshType) {
volumio.getState(); volumioLocal.getState();
} else { } else {
logger.error("Command is not handled"); logger.error("Command is not handled");
} }
} }
private void handleStopCommand(Command command) { private void handleStopCommand(Command command) {
VolumioService volumioLocal = volumio;
if (volumioLocal == null) {
if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized, cannot handle stop command.");
}
return;
}
if (command instanceof StringType) { if (command instanceof StringType) {
volumio.stop(); volumioLocal.stop();
updateState(VolumioBindingConstants.CHANNEL_STOP, UnDefType.UNDEF); updateState(VolumioBindingConstants.CHANNEL_STOP, UnDefType.UNDEF);
} else if (command.equals(RefreshType.REFRESH)) { } else if (command.equals(RefreshType.REFRESH)) {
updateState(VolumioBindingConstants.CHANNEL_STOP, UnDefType.UNDEF); updateState(VolumioBindingConstants.CHANNEL_STOP, UnDefType.UNDEF);
@ -207,22 +237,31 @@ public class VolumioHandler extends BaseThingHandler {
} }
private void handlePlaybackCommands(Command command) { private void handlePlaybackCommands(Command command) {
VolumioService volumioLocal = volumio;
if (volumioLocal == null) {
if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized, cannot handle playback command.");
}
return;
}
if (command instanceof PlayPauseType playPauseCmd) { if (command instanceof PlayPauseType playPauseCmd) {
switch (playPauseCmd) { switch (playPauseCmd) {
case PLAY: case PLAY:
volumio.play(); volumioLocal.play();
break; break;
case PAUSE: case PAUSE:
volumio.pause(); volumioLocal.pause();
break; break;
} }
} else if (command instanceof NextPreviousType nextPreviousType) { } else if (command instanceof NextPreviousType nextPreviousType) {
switch (nextPreviousType) { switch (nextPreviousType) {
case PREVIOUS: case PREVIOUS:
volumio.previous(); volumioLocal.previous();
break; break;
case NEXT: case NEXT:
volumio.next(); volumioLocal.next();
break; break;
} }
} else if (command instanceof RewindFastforwardType fastForwardType) { } else if (command instanceof RewindFastforwardType fastForwardType) {
@ -233,7 +272,7 @@ public class VolumioHandler extends BaseThingHandler {
break; break;
} }
} else if (command instanceof RefreshType) { } else if (command instanceof RefreshType) {
volumio.getState(); volumioLocal.getState();
} else { } else {
logger.error("Command is not handled: {}", command); logger.error("Command is not handled: {}", command);
} }
@ -246,9 +285,19 @@ public class VolumioHandler extends BaseThingHandler {
* - PUSH.STATE - * - PUSH.STATE -
*/ */
private void bindDefaultListener() { private void bindDefaultListener() {
volumio.on(Socket.EVENT_CONNECT, connectListener()); VolumioService volumioLocal = volumio;
volumio.on(Socket.EVENT_DISCONNECT, disconnectListener());
volumio.on(VolumioEvents.PUSH_STATE, pushStateListener()); if (volumioLocal == null) {
if (ThingStatus.ONLINE.equals(getThing().getStatus())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Volumio service was not yet initialized.");
}
return;
}
volumioLocal.on(Socket.EVENT_CONNECT, connectListener());
volumioLocal.on(Socket.EVENT_DISCONNECT, disconnectListener());
volumioLocal.on(VolumioEvents.PUSH_STATE, pushStateListener());
} }
/** /**
@ -273,11 +322,12 @@ public class VolumioHandler extends BaseThingHandler {
} else { } else {
logger.debug("Trying to connect to Volumio on {}://{}:{}", protocol, hostname, port); logger.debug("Trying to connect to Volumio on {}://{}:{}", protocol, hostname, port);
try { try {
volumio = new VolumioService(protocol, hostname, port, timeout); VolumioService volumioLocal = new VolumioService(protocol, hostname, port, timeout);
volumio = volumioLocal;
clearChannels(); clearChannels();
bindDefaultListener(); bindDefaultListener();
updateStatus(ThingStatus.OFFLINE); updateStatus(ThingStatus.OFFLINE);
volumio.connect(); volumioLocal.connect();
} catch (Exception e) { } catch (Exception e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} }
@ -286,17 +336,18 @@ public class VolumioHandler extends BaseThingHandler {
@Override @Override
public void dispose() { public void dispose() {
if (volumio != null) { VolumioService volumioLocal = volumio;
if (volumioLocal != null) {
scheduler.schedule(() -> { scheduler.schedule(() -> {
if (volumio.isConnected()) { if (volumioLocal.isConnected()) {
logger.warn("Timeout during disconnect event"); logger.warn("Timeout during disconnect event");
} else { } else {
volumio.close(); volumioLocal.close();
} }
clearChannels(); clearChannels();
}, 30, TimeUnit.SECONDS); }, 30, TimeUnit.SECONDS);
volumio.disconnect(); volumioLocal.disconnect();
} }
} }