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