]> git.basschouten.com Git - openhab-addons.git/blob
54f4c11bb44b8c1e4bae944aa3ee5aaf74171dd0
[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.DecimalType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Handles TV Control Volume Commands. Allows to set a volume to an absolute number or increment and decrement the
35  * volume. If used with On Off type commands it will mute volume when receiving OFF and unmute when receiving ON.
36  *
37  * @author Sebastian Prehn - initial contribution
38  */
39 @NonNullByDefault
40 public class VolumeControlVolume extends BaseChannelHandler<Float> {
41     private final Logger logger = LoggerFactory.getLogger(VolumeControlVolume.class);
42
43     private final ResponseListener<CommandConfirmation> objResponseListener = createResponseListener();
44
45     @Override
46     public void onReceiveCommand(String channelId, LGWebOSHandler handler, Command command) {
47         final PercentType percent;
48         if (RefreshType.REFRESH == command) {
49             handler.getSocket().getVolume(createResponseListener(channelId, handler));
50             return;
51         }
52         if (command instanceof PercentType percentCommand) {
53             percent = percentCommand;
54         } else if (command instanceof DecimalType decimalCommand) {
55             percent = new PercentType(decimalCommand.toBigDecimal());
56         } else if (command instanceof StringType stringCommand) {
57             percent = new PercentType(stringCommand.toString());
58         } else {
59             percent = null;
60         }
61
62         if (percent != null) {
63             handler.getSocket().setVolume(percent.floatValue() / 100.0f, objResponseListener);
64         } else if (IncreaseDecreaseType.INCREASE == command) {
65             handler.getSocket().volumeUp(objResponseListener);
66         } else if (IncreaseDecreaseType.DECREASE == command) {
67             handler.getSocket().volumeDown(objResponseListener);
68         } else if (OnOffType.OFF == command || OnOffType.ON == command) {
69             handler.getSocket().setMute(OnOffType.OFF == command, objResponseListener);
70         } else {
71             logger.info("Only accept PercentType, DecimalType, StringType, RefreshType. Type was {}.",
72                     command.getClass());
73         }
74     }
75
76     @Override
77     protected Optional<ServiceSubscription<Float>> getSubscription(String channelUID, LGWebOSHandler handler) {
78         return Optional.of(handler.getSocket().subscribeVolume(createResponseListener(channelUID, handler)));
79     }
80
81     private ResponseListener<Float> createResponseListener(String channelUID, LGWebOSHandler handler) {
82         return new ResponseListener<Float>() {
83
84             @Override
85             public void onError(@Nullable String error) {
86                 logger.debug("Error in retrieving volume: {}.", error);
87             }
88
89             @Override
90             public void onSuccess(@Nullable Float value) {
91                 if (value != null && !Float.isNaN(value)) {
92                     handler.postUpdate(channelUID, new PercentType(Math.round(value * 100)));
93                 }
94             }
95         };
96     }
97 }