]> git.basschouten.com Git - openhab-addons.git/blob
bf038715e6e6834bb01c0d351f464586fa6b9ffd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mycroft.internal.channels;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.mycroft.internal.MycroftBindingConstants;
20 import org.openhab.binding.mycroft.internal.MycroftHandler;
21 import org.openhab.binding.mycroft.internal.api.MessageType;
22 import org.openhab.binding.mycroft.internal.api.dto.BaseMessage;
23 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeMute;
24 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeSet;
25 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeUnmute;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.types.Command;
28
29 /**
30  * The channel responsible for muting the Mycroft speaker
31  *
32  * @author Gwendal Roulleau - Initial contribution
33  */
34 @NonNullByDefault
35 public class MuteChannel extends MycroftChannel<OnOffType> {
36
37     private int volumeRestorationLevel;
38
39     public MuteChannel(MycroftHandler handler, int volumeRestorationLevel) {
40         super(handler, MycroftBindingConstants.VOLUME_MUTE_CHANNEL);
41         this.volumeRestorationLevel = volumeRestorationLevel;
42     }
43
44     @Override
45     public List<MessageType> getMessageToListenTo() {
46         // we don't listen to mute/unmute message because duck/unduck seems sufficient
47         // and we don't want to change state twice for the same event
48         // but it should be tested on mark I, as volume is handled differently
49         return Arrays.asList(MessageType.mycroft_volume_duck, MessageType.mycroft_volume_unduck,
50                 MessageType.mycroft_volume_set, MessageType.mycroft_volume_increase);
51     }
52
53     @Override
54     public void messageReceived(BaseMessage message) {
55         switch (message.type) {
56             case mycroft_volume_mute:
57             case mycroft_volume_duck:
58                 updateMyState(OnOffType.ON);
59                 break;
60             case mycroft_volume_unmute:
61             case mycroft_volume_unduck:
62             case mycroft_volume_increase:
63                 updateMyState(OnOffType.OFF);
64                 break;
65             case mycroft_volume_set:
66                 if (((MessageVolumeSet) message).data.percent > 0) {
67                     updateMyState(OnOffType.OFF);
68                 }
69                 break;
70             default:
71         }
72     }
73
74     private boolean sendVolumeSetMessage(float volume) {
75         String messageToSend = VolumeChannel.VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME", Float.toString(volume));
76         return handler.sendMessage(messageToSend);
77     }
78
79     @Override
80     public void handleCommand(Command command) {
81         if (command instanceof OnOffType) {
82             if (command == OnOffType.ON) {
83                 if (handler.sendMessage(new MessageVolumeMute())) {
84                     updateMyState(OnOffType.ON);
85                 }
86             } else if (command == OnOffType.OFF) {
87                 if (handler.sendMessage(new MessageVolumeUnmute())) {
88                     updateMyState(OnOffType.OFF);
89                     // if configured, we can restore the volume to a fixed amount
90                     // usefull as a workaround for the broken Mycroft volume behavior
91                     if (volumeRestorationLevel > 0) {
92                         // we must wait 100ms for Mycroft to handle the message and
93                         // setting old volume before forcing to our value
94                         try {
95                             Thread.sleep(100);
96                         } catch (InterruptedException e) {
97                         }
98                         sendVolumeSetMessage(Float.valueOf(volumeRestorationLevel));
99                     }
100                 }
101             }
102         }
103     }
104 }