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