]> git.basschouten.com Git - openhab-addons.git/blob
63de251768cded54ab321f7ea74e64de1669e32a
[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.denonmarantz.internal;
14
15 import java.math.BigDecimal;
16
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.types.State;
22
23 /**
24  * Represents the state of the handled DenonMarantz AVR
25  *
26  * @author Jan-Willem Veldhuis - Initial contribution
27  *
28  */
29 public class DenonMarantzState {
30
31     private State power;
32     private State mainZonePower;
33     private State mute;
34     private State mainVolume;
35     private State mainVolumeDB;
36     private State input;
37     private State surroundProgram;
38
39     private State artist;
40     private State album;
41     private State track;
42
43     // ------ Zones ------
44     private State zone2Power;
45     private State zone2Volume;
46     private State zone2VolumeDB;
47     private State zone2Mute;
48     private State zone2Input;
49
50     private State zone3Power;
51     private State zone3Volume;
52     private State zone3VolumeDB;
53     private State zone3Mute;
54     private State zone3Input;
55
56     private State zone4Power;
57     private State zone4Volume;
58     private State zone4VolumeDB;
59     private State zone4Mute;
60     private State zone4Input;
61
62     private DenonMarantzStateChangedListener handler;
63
64     public DenonMarantzState(DenonMarantzStateChangedListener handler) {
65         this.handler = handler;
66     }
67
68     public void connectionError(String errorMessage) {
69         handler.connectionError(errorMessage);
70     }
71
72     public State getStateForChannelID(String channelID) {
73         switch (channelID) {
74             case DenonMarantzBindingConstants.CHANNEL_POWER:
75                 return power;
76             case DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER:
77                 return mainZonePower;
78             case DenonMarantzBindingConstants.CHANNEL_MUTE:
79                 return mute;
80             case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME:
81                 return mainVolume;
82             case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB:
83                 return mainVolumeDB;
84             case DenonMarantzBindingConstants.CHANNEL_INPUT:
85                 return input;
86             case DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM:
87                 return surroundProgram;
88
89             case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST:
90                 return artist;
91             case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM:
92                 return album;
93             case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK:
94                 return track;
95
96             case DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER:
97                 return zone2Power;
98             case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME:
99                 return zone2Volume;
100             case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB:
101                 return zone2VolumeDB;
102             case DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE:
103                 return zone2Mute;
104             case DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT:
105                 return zone2Input;
106
107             case DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER:
108                 return zone3Power;
109             case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME:
110                 return zone3Volume;
111             case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB:
112                 return zone3VolumeDB;
113             case DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE:
114                 return zone3Mute;
115             case DenonMarantzBindingConstants.CHANNEL_ZONE3_INPUT:
116                 return zone3Input;
117
118             case DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER:
119                 return zone4Power;
120             case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME:
121                 return zone4Volume;
122             case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB:
123                 return zone4VolumeDB;
124             case DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE:
125                 return zone4Mute;
126             case DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT:
127                 return zone4Input;
128
129             default:
130                 return null;
131         }
132     }
133
134     public void setPower(boolean power) {
135         OnOffType newVal = OnOffType.from(power);
136         if (newVal != this.power) {
137             this.power = newVal;
138             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_POWER, this.power);
139         }
140     }
141
142     public void setMainZonePower(boolean mainPower) {
143         OnOffType newVal = OnOffType.from(mainPower);
144         if (newVal != this.mainZonePower) {
145             this.mainZonePower = newVal;
146             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER, this.mainZonePower);
147         }
148     }
149
150     public void setMute(boolean mute) {
151         OnOffType newVal = OnOffType.from(mute);
152         if (newVal != this.mute) {
153             this.mute = newVal;
154             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MUTE, this.mute);
155         }
156     }
157
158     public void setMainVolume(BigDecimal volume) {
159         PercentType newVal = new PercentType(volume);
160         if (!newVal.equals(this.mainVolume)) {
161             this.mainVolume = newVal;
162             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME, this.mainVolume);
163             // update the main volume in dB too
164             this.mainVolumeDB = DecimalType.valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
165             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB, this.mainVolumeDB);
166         }
167     }
168
169     public void setInput(String input) {
170         StringType newVal = StringType.valueOf(input);
171         if (!newVal.equals(this.input)) {
172             this.input = newVal;
173             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_INPUT, this.input);
174         }
175     }
176
177     public void setSurroundProgram(String surroundProgram) {
178         StringType newVal = StringType.valueOf(surroundProgram);
179         if (!newVal.equals(this.surroundProgram)) {
180             this.surroundProgram = newVal;
181             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM, this.surroundProgram);
182         }
183     }
184
185     public void setNowPlayingArtist(String artist) {
186         StringType newVal = artist == null || artist.isBlank() ? StringType.EMPTY : StringType.valueOf(artist);
187         if (!newVal.equals(this.artist)) {
188             this.artist = newVal;
189             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST, this.artist);
190         }
191     }
192
193     public void setNowPlayingAlbum(String album) {
194         StringType newVal = album == null || album.isBlank() ? StringType.EMPTY : StringType.valueOf(album);
195         if (!newVal.equals(this.album)) {
196             this.album = newVal;
197             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM, this.album);
198         }
199     }
200
201     public void setNowPlayingTrack(String track) {
202         StringType newVal = track == null || track.isBlank() ? StringType.EMPTY : StringType.valueOf(track);
203         if (!newVal.equals(this.track)) {
204             this.track = newVal;
205             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK, this.track);
206         }
207     }
208
209     public void setZone2Power(boolean power) {
210         OnOffType newVal = OnOffType.from(power);
211         if (newVal != this.zone2Power) {
212             this.zone2Power = newVal;
213             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER, this.zone2Power);
214         }
215     }
216
217     public void setZone2Volume(BigDecimal volume) {
218         PercentType newVal = new PercentType(volume);
219         if (!newVal.equals(this.zone2Volume)) {
220             this.zone2Volume = newVal;
221             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME, this.zone2Volume);
222             // update the volume in dB too
223             this.zone2VolumeDB = DecimalType
224                     .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
225             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB, this.zone2VolumeDB);
226         }
227     }
228
229     public void setZone2Mute(boolean mute) {
230         OnOffType newVal = OnOffType.from(mute);
231         if (newVal != this.zone2Mute) {
232             this.zone2Mute = newVal;
233             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE, this.zone2Mute);
234         }
235     }
236
237     public void setZone2Input(String zone2Input) {
238         StringType newVal = StringType.valueOf(zone2Input);
239         if (!newVal.equals(this.zone2Input)) {
240             this.zone2Input = newVal;
241             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone2Input);
242         }
243     }
244
245     public void setZone3Power(boolean power) {
246         OnOffType newVal = OnOffType.from(power);
247         if (newVal != this.zone3Power) {
248             this.zone3Power = newVal;
249             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER, this.zone3Power);
250         }
251     }
252
253     public void setZone3Volume(BigDecimal volume) {
254         PercentType newVal = new PercentType(volume);
255         if (!newVal.equals(this.zone3Volume)) {
256             this.zone3Volume = newVal;
257             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME, this.zone3Volume);
258             // update the volume in dB too
259             this.zone3VolumeDB = DecimalType
260                     .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
261             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB, this.zone3VolumeDB);
262         }
263     }
264
265     public void setZone3Mute(boolean mute) {
266         OnOffType newVal = OnOffType.from(mute);
267         if (newVal != this.zone3Mute) {
268             this.zone3Mute = newVal;
269             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE, this.zone3Mute);
270         }
271     }
272
273     public void setZone3Input(String zone3Input) {
274         StringType newVal = StringType.valueOf(zone3Input);
275         if (!newVal.equals(this.zone3Input)) {
276             this.zone3Input = newVal;
277             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone3Input);
278         }
279     }
280
281     public void setZone4Power(boolean power) {
282         OnOffType newVal = OnOffType.from(power);
283         if (newVal != this.zone4Power) {
284             this.zone4Power = newVal;
285             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER, this.zone4Power);
286         }
287     }
288
289     public void setZone4Volume(BigDecimal volume) {
290         PercentType newVal = new PercentType(volume);
291         if (!newVal.equals(this.zone4Volume)) {
292             this.zone4Volume = newVal;
293             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME, this.zone4Volume);
294             // update the volume in dB too
295             this.zone4VolumeDB = DecimalType
296                     .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
297             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB, this.zone4VolumeDB);
298         }
299     }
300
301     public void setZone4Mute(boolean mute) {
302         OnOffType newVal = OnOffType.from(mute);
303         if (newVal != this.zone4Mute) {
304             this.zone4Mute = newVal;
305             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE, this.zone4Mute);
306         }
307     }
308
309     public void setZone4Input(String zone4Input) {
310         StringType newVal = StringType.valueOf(zone4Input);
311         if (!newVal.equals(this.zone4Input)) {
312             this.zone4Input = newVal;
313             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT, this.zone4Input);
314         }
315     }
316 }