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