83727722d5fbbe4e9b565ba181b95c81d1de4f4c
[smartthings-infrastructure.git] / MusicPlayer / MusicPlayer.groovy
1 //Create a class for music player
2 package MusicPlayer
3 import Timer.SimulatedTimer
4
5
6 public class MusicPlayer {
7         private String id
8         private String label
9         private String displayName
10         private int level
11         private String mute
12         private String status
13         private int trackNumber
14         private String trackData
15         private String trackDescription
16         def sendEvent
17         
18         MusicPlayer(Closure sendEvent, String id, String label, String displayName, int level, String mute, String status, int trackNumber, String trackData, String trackDescription) {
19                 this.sendEvent = sendEvent
20                 this.id = id
21                 this.label = label
22                 this.displayName = displayName
23                 this.level = level
24                 this.mute = mute
25                 this.status = status
26                 this.trackNumber = trackNumber
27                 this.trackData = trackData
28                 this.trackDescription = trackDescription
29         }
30
31         //By model checker
32         def setValue(String value, String name) {
33                 if ((name == "status") && (value != this.status)) {
34                         this.status = value
35                         println("the status of the music player with id:$id is changed to $value!")
36                 } else if ((name == "level") && (value != this.level)) {
37                         this.level = value.toInteger()
38                         println("the level sound of the music player with id:$id is changed to $value!")
39                 } else if ((name == "trackDescription") && (value != this.trackDescription)) {
40                         this.trackDescription = value
41                         println("the trackDescription of the music player with id:$id is changed to $value!")
42                 } else if ((name == "trackData") && (value != this.trackData)) {
43                         this.trackData = value
44                         println("the trackData of the music player with id:$id is changed to $value!")
45                 } else if ((name == "mute") && (value != this.mute)) {
46                         this.mute = value
47                         println("the mute state of the music player with id:$id is changed to $value!")
48                 }
49         }
50
51         //methods
52         def on(LinkedHashMap metaData) {
53                 on()
54         }
55         def on() {
56                 if (status != "on") {
57                         println("the music player with id:$id is on!")
58                         this.status = "on"
59                         sendEvent([name: "status", value: "on", deviceId: this.id, descriptionText: "",
60                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61                 }
62         }
63         def off(LinkedHashMap metaData) {
64                 off()
65         }
66         def off() {
67                 if (status != "off") {
68                         println("the music player with id:$id is off!")
69                         this.status = "off"
70                         sendEvent([name: "status", value: "off", deviceId: this.id, descriptionText: "",
71                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                 }
73         }
74         def mute(LinkedHashMap metaData) {
75                 mute()
76         }
77         def mute() {
78                 if (mute != "muted") {
79                         println("the music player with id:$id is muted!")
80                         this.mute = "muted"
81                         sendEvent([name: "mute", value: "muted", deviceId: this.id, descriptionText: "",
82                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
83                 }
84         }
85         def nextTrack(LinkedHashMap metaData) {
86                 nextTrack()
87         }
88         def nextTrack() {
89                 trackNumber = trackNumber+1
90                 def trackPlaying = trackData
91                 println("the $trackPlaying is selected!")
92                 if (status != "play") {
93                         this.status = "play"
94                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
95                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
96                 }
97                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
98                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
99                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
100                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
101         }
102         def pause(LinkedHashMap metaData) {
103                 pause()
104         }
105         def pause() {
106                 if (status != "pause") {
107                         println("the music player with id:$id is paused!")
108                         this.status = "pause"
109                         sendEvent([name: "status", value: "pause", deviceId: this.id, descriptionText: "",
110                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
111                 }
112         }
113         def play(LinkedHashMap metaData) {
114                 play()
115         }
116         def play() {
117                 if (status != "play") {
118                         println("the music player with id:$id is starting to play!")
119                         this.status = "play"
120                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
121                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
122                 }
123         }
124         def playTrack(LinkedHashMap metaData) {
125                 playTrack()
126         }
127         def playTrack(String trackToPlay) {
128                 def trackPlaying = trackData
129                 println("the $trackPlaying is selected to play!")
130                 if (status != "play") {         
131                         this.status = "play"
132                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
133                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
134                 }
135                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
136                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
137                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
138                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
139         }
140         def previousTrack(LinkedHashMap metaData) {
141                 previousTrack()
142         }
143         def previousTrack() {
144                 if (trackNumber != 1)
145                         trackNumber = trackNumber-1
146                 def trackPlaying = trackData
147                 println("the $trackPlaying is selected!")
148                 if (status != "playing") {
149                         this.status = "playing"
150                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
151                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
152                 }
153                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
154                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
155                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
156                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
157         }
158         /*def restoreTrack(String trackToRestore) {
159                 musicPlayers*.restoreTrack(trackToRestore)
160         }*/
161         def resumeTrack(LinkedHashMap metaData) {
162                 resumeTrack()
163         }
164         def resumeTrack(String trackToResume) {
165                 def trackPlaying = trackData
166                 println("the $trackPlaying is resumed!")
167                 if (status != "play") {
168                         this.status = "play"
169                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
170                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
171                 }
172         }
173         def setLevel(LinkedHashMap metaData) {
174                 setLevel()
175         }
176         def setLevel(int level) {
177                 if (level != this.level) {
178                         this.level = level
179                         println("the level of sound is changed to $level!")
180                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
181                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
182                 }
183         }
184         def setTrack(LinkedHashMap metaData) {
185                 setTrack()
186         }
187         def setTrack(String trackToSet) {
188                 def trackPlaying = trackData
189                 println("the $trackPlaying is set!")
190                 if (status != "play") {
191                         this.status = "play"
192                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
193                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
194                 }
195                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
196                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
197                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
198                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
199         }
200         def stop(LinkedHashMap metaData) {
201                 stop()
202         }
203         def stop() {
204                 if (status != "stop") {
205                         println("the music player with id:$id is stop!")
206                         this.status = "stop"
207                         sendEvent([name: "status", value: "stop", deviceId: this.id, descriptionText: "",
208                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
209                 }
210         }
211         def statesSince(String info, Date dateObj) {
212                 statesSince()
213         }
214         def statesSince() {
215                 eventsSince()
216         }
217         def eventsSince(Date dateObj) {
218                 eventsSince()
219         }
220
221         def eventsSince() {
222                 def evtActive = [[name: "status", value: "on", deviceId: "musicPlayerID0", descriptionText: "",
223                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
224                 def evtInactive = [[name: "status", value: "off", deviceId: "musicPlayerID0", descriptionText: "",
225                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
226                 def init = Verify.getInt(0,4)
227                 def evtToSend = []
228                 if (init == 0) {//return empty set
229                         return evtToSend
230                 } else if (init == 1) {//send one active event
231                         evtActive.each{
232                                 evtToSend.add(it)
233                         }
234                         return evtToSend
235                 } else if (init == 2) {//send two active events
236                         evtActive.each{
237                                 evtToSend.add(it)
238                         }
239                         evtActive.each{
240                                 evtToSend.add(it)
241                         }
242                         return evtToSend
243                 } else if (init == 3) {//send one inactive event
244                         evtInactive.each{
245                                 evtToSend.add(it)
246                         }
247                         return evtToSend
248                 } else if (init == 4) {//send two inactive events
249                         evtInactive.each{
250                                 evtToSend.add(it)
251                         }
252                         evtInactive.each{
253                                 evtToSend.add(it)
254                         }
255                         return evtToSend
256                 }
257         }
258         def playText(LinkedHashMap metaData) {
259                 playText()
260         }
261         def playText(String text) {
262                 println("the music player with id:$id is playing the text:$text!")
263                 if (status != "play") {
264                         this.status = "play"
265                         sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
266                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
267                 }
268         }
269
270         def currentValue(String deviceFeature) {
271                 if (deviceFeature == "playpause") {
272                         return status
273                 }
274         }
275
276         def latestValue(String deviceFeature) {
277                 if (deviceFeature == "playpause") {
278                         return status
279                 }
280         }
281 }