Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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         def sendEvent
18         
19         MusicPlayer(Closure sendEvent, String id, String label, String displayName, int level, String mute, String status, int trackNumber, String trackData, String trackDescription) {
20                 this.sendEvent = sendEvent
21                 this.id = id
22                 this.label = label
23                 this.displayName = displayName
24                 this.level = level
25                 this.mute = mute
26                 this.status = status
27                 this.trackNumber = trackNumber
28                 this.trackData = trackData
29                 this.trackDescription = trackDescription
30         }
31
32         //By model checker
33         def setValue(String value, String name) {
34                 if ((name == "status") && (value != this.status)) {
35                         this.status = value
36                         println("the status of the music player with id:$id is changed to $value!")
37                 } else if ((name == "level") && (value != this.level)) {
38                         this.level = value.toInteger()
39                         println("the level sound of the music player with id:$id is changed to $value!")
40                 } else if ((name == "trackDescription") && (value != this.trackDescription)) {
41                         this.trackDescription = value
42                         println("the trackDescription of the music player with id:$id is changed to $value!")
43                 } else if ((name == "trackData") && (value != this.trackData)) {
44                         this.trackData = value
45                         println("the trackData of the music player with id:$id is changed to $value!")
46                 } else if ((name == "mute") && (value != this.mute)) {
47                         this.mute = value
48                         println("the mute state of the music player with id:$id is changed to $value!")
49                 }
50         }
51
52         //methods
53         def mute() {
54                 if (mute != "muted") {
55                         println("the music player with id:$id is muted!")
56                         this.mute = "muted"
57                         sendEvent([name: "mute", value: "muted", deviceId: this.id, descriptionText: "",
58                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                         sendEvent([name: "mute.muted", value: "muted", deviceId: this.id, descriptionText: "",
60                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61                 }
62         }
63         def nextTrack() {
64                 trackNumber = trackNumber+1
65                 def trackPlaying = trackData
66                 println("the $trackPlaying is selected!")
67                 if (status != "playing") {
68                         this.status = "playing"
69                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
70                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
71                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
72                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
73                 }
74                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
75                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
76                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
77                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78         }
79         def pause() {
80                 if (status != "paused") {
81                         println("the music player with id:$id is paused!")
82                         this.status = "paused"
83                         sendEvent([name: "status", value: "paused", deviceId: this.id, descriptionText: "",
84                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
85                         sendEvent([name: "status.paused", value: "paused", deviceId: this.id, descriptionText: "",
86                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
87                 }
88         }
89         def play() {
90                 if (status != "playing") {
91                         println("the music player with id:$id is starting to play!")
92                         this.status = "playing"
93                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
94                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
95                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
96                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97                 }
98         }
99         def playTrack(String trackToPlay) {
100                 def trackPlaying = trackData
101                 println("the $trackPlaying is selected to play!")
102                 if (status != "playing") {              
103                         this.status = "playing"
104                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
105                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
106                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
107                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
108                 }
109                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
110                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
111                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
112                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
113         }
114         def previousTrack() {
115                 if (trackNumber != 1)
116                         trackNumber = trackNumber-1
117                 def trackPlaying = trackData
118                 println("the $trackPlaying is selected!")
119                 if (status != "playing") {
120                         this.status = "playing"
121                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
122                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
123                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
124                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
125                 }
126                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
127                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
128                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
129                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
130         }
131         /*def restoreTrack(String trackToRestore) {
132                 musicPlayers*.restoreTrack(trackToRestore)
133         }*/
134         def resumeTrack(String trackToResume) {
135                 def trackPlaying = trackData
136                 println("the $trackPlaying is resumed!")
137                 if (status != "playing") {
138                         this.status = "playing"
139                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
140                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
141                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
142                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
143                 }
144         }
145         def setLevel(int level) {
146                 if (level != this.level) {
147                         this.level = level
148                         println("the level of sound is changed to $level!")
149                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
150                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
151                 }
152         }
153         def setTrack(String trackToSet) {
154                 def trackPlaying = trackData
155                 println("the $trackPlaying is set!")
156                 if (status != "playing") {
157                         this.status = "playing"
158                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
159                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
160                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
161                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
162                 }
163                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
164                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
165                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
166                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
167         }
168         def stop() {
169                 if (status != "stopped") {
170                         println("the music player with id:$id is stopped!")
171                         this.status = "stopped"
172                         sendEvent([name: "status", value: "stopped", deviceId: this.id, descriptionText: "",
173                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
174                         sendEvent([name: "status.stopped", value: "stopped", deviceId: this.id, descriptionText: "",
175                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
176                 }
177         }
178         def playText(String text) {
179                 println("the music player with id:$id is playing the text:$text!")
180                 if (status != "playing") {
181                         this.status = "playing"
182                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
183                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
184                         sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
185                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
186                 }
187         }
188
189         def currentValue(String deviceFeature) {
190                 if (deviceFeature == "musicPlayer") {
191                         return status
192                 }
193         }
194
195         def latestValue(String deviceFeature) {
196                 if (deviceFeature == "musicPlayer") {
197                         return status
198                 }
199         }
200 }