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