Changes in classes: new concept for latest value + all types of events generated...
[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         def sendEvent
8         private String id
9         private String label
10         private String displayName
11         private int level
12         private String mute
13         private String status
14         private int trackNumber
15         private String trackData
16         private String trackDescription
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 mute() {
53                 if (mute != "muted") {
54                         println("the music player with id:$id is muted!")
55                         this.mute = "muted"
56                         sendEvent([name: "mute", value: "muted", deviceId: this.id, descriptionText: "",
57                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
58                         sendEvent([name: "mute.muted", value: "muted", deviceId: this.id, descriptionText: "",
59                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62         def nextTrack() {
63                 trackNumber = trackNumber+1
64                 def trackPlaying = trackData
65                 println("the $trackPlaying is selected!")
66                 if (status != "playing") {
67                         this.status = "playing"
68                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
69                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
70                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
71                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                 }
73                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
74                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
75                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
76                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77         }
78         def pause() {
79                 if (status != "paused") {
80                         println("the music player with id:$id is paused!")
81                         this.status = "paused"
82                         sendEvent([name: "status", value: "paused", deviceId: this.id, descriptionText: "",
83                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
84                         sendEvent([name: "status.paused", value: "paused", deviceId: this.id, descriptionText: "",
85                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
86                 }
87         }
88         def play() {
89                 if (status != "playing") {
90                         println("the music player with id:$id is starting to play!")
91                         this.status = "playing"
92                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
93                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
94                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
95                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
96                 }
97         }
98         def playTrack(String trackToPlay) {
99                 def trackPlaying = trackData
100                 println("the $trackPlaying is selected to play!")
101                 if (status != "playing") {              
102                         this.status = "playing"
103                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
104                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
105                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
106                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
107                 }
108                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
109                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
110                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
111                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
112         }
113         def previousTrack() {
114                 if (trackNumber != 1)
115                         trackNumber = trackNumber-1
116                 def trackPlaying = trackData
117                 println("the $trackPlaying is selected!")
118                 if (status != "playing") {
119                         this.status = "playing"
120                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
121                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
122                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
123                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
124                 }
125                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
126                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
127                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
128                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
129         }
130         /*def restoreTrack(String trackToRestore) {
131                 musicPlayers*.restoreTrack(trackToRestore)
132         }*/
133         def resumeTrack(String trackToResume) {
134                 def trackPlaying = trackData
135                 println("the $trackPlaying is resumed!")
136                 if (status != "playing") {
137                         this.status = "playing"
138                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
139                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
140                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
141                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
142                 }
143         }
144         def setLevel(int level) {
145                 if (level != this.level) {
146                         this.level = level
147                         println("the level of sound is changed to $level!")
148                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
149                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
150                 }
151         }
152         def setTrack(String trackToSet) {
153                 def trackPlaying = trackData
154                 println("the $trackPlaying is set!")
155                 if (status != "playing") {
156                         this.status = "playing"
157                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
158                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
159                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
160                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
161                 }
162                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
163                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
164                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
165                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
166         }
167         def stop() {
168                 if (status != "stopped") {
169                         println("the music player with id:$id is stopped!")
170                         this.status = "stopped"
171                         sendEvent([name: "status", value: "stopped", deviceId: this.id, descriptionText: "",
172                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
173                         sendEvent([name: "status.stopped", value: "stopped", deviceId: this.id, descriptionText: "",
174                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
175                 }
176         }
177         def playText(String text) {
178                 println("the music player with id:$id is playing the text:$text!")
179                 if (status != "playing") {
180                         this.status = "playing"
181                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
182                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
183                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
184                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
185                 }
186         }
187
188         def currentValue(String deviceFeature) {
189                 if (deviceFeature == "musicPlayer") {
190                         return status
191                 }
192         }
193
194         def latestValue(String deviceFeature) {
195                 if (deviceFeature == "musicPlayer") {
196                         return status
197                 }
198         }
199 }