]> git.basschouten.com Git - openhab-addons.git/blob
d0a8d0ea1fb0513d23a7d28dae0430a971d9f498
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lgwebos.internal;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
20 import org.openhab.binding.lgwebos.internal.handler.command.ServiceSubscription;
21 import org.openhab.binding.lgwebos.internal.handler.core.CommandConfirmation;
22 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Handles TV Control Mute Command.
31  *
32  * @author Sebastian Prehn - initial contribution
33  */
34 @NonNullByDefault
35 public class VolumeControlMute extends BaseChannelHandler<Boolean> {
36     private final Logger logger = LoggerFactory.getLogger(VolumeControlMute.class);
37
38     private final ResponseListener<CommandConfirmation> objResponseListener = createResponseListener();
39
40     @Override
41     public void onReceiveCommand(String channelId, LGWebOSHandler handler, Command command) {
42         if (RefreshType.REFRESH == command) {
43             handler.getSocket().getMute(createResponseListener(channelId, handler));
44         } else if (OnOffType.ON == command || OnOffType.OFF == command) {
45             handler.getSocket().setMute(OnOffType.ON == command, objResponseListener);
46         } else {
47             logger.info("Only accept OnOffType, RefreshType. Type was {}.", command.getClass());
48         }
49     }
50
51     @Override
52     protected Optional<ServiceSubscription<Boolean>> getSubscription(String channelId, LGWebOSHandler handler) {
53         return Optional.of(handler.getSocket().subscribeMute(createResponseListener(channelId, handler)));
54     }
55
56     private ResponseListener<Boolean> createResponseListener(String channelId, LGWebOSHandler handler) {
57         return new ResponseListener<Boolean>() {
58
59             @Override
60             public void onError(@Nullable String error) {
61                 logger.debug("Error in retrieving mute: {}.", error);
62             }
63
64             @Override
65             public void onSuccess(@Nullable Boolean value) {
66                 if (value == null) {
67                     return;
68                 }
69                 handler.postUpdate(channelId, OnOffType.from(value));
70             }
71         };
72     }
73 }