]> git.basschouten.com Git - openhab-addons.git/blob
5e81d6dae76993ad15248ecf1ef4e316c57ec3d1
[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",
76                 Float.valueOf(volume).toString());
77         return handler.sendMessage(messageToSend);
78     }
79
80     @Override
81     public void handleCommand(Command command) {
82         if (command instanceof OnOffType) {
83             if (command == OnOffType.ON) {
84                 if (handler.sendMessage(new MessageVolumeMute())) {
85                     updateMyState(OnOffType.ON);
86                 }
87             } else if (command == OnOffType.OFF) {
88                 if (handler.sendMessage(new MessageVolumeUnmute())) {
89                     updateMyState(OnOffType.OFF);
90                     // if configured, we can restore the volume to a fixed amount
91                     // usefull as a workaround for the broken Mycroft volume behavior
92                     if (volumeRestorationLevel > 0) {
93                         // we must wait 100ms for Mycroft to handle the message and
94                         // setting old volume before forcing to our value
95                         try {
96                             Thread.sleep(100);
97                         } catch (InterruptedException e) {
98                         }
99                         sendVolumeSetMessage(Float.valueOf(volumeRestorationLevel));
100                     }
101                 }
102             }
103         }
104     }
105 }