1 //Create a class for music player
3 import Timer.SimulatedTimer
6 public class MusicPlayer {
10 private String displayName
14 private int trackNumber
15 private String trackData
16 private String trackDescription
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
22 this.displayName = displayName
26 this.trackNumber = trackNumber
27 this.trackData = trackData
28 this.trackDescription = trackDescription
32 def setValue(String value, String name) {
33 if ((name == "status") && (value != this.status)) {
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)) {
47 println("the mute state of the music player with id:$id is changed to $value!")
53 if (mute != "muted") {
54 println("the music player with id:$id is 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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
130 /*def restoreTrack(String trackToRestore) {
131 musicPlayers*.restoreTrack(trackToRestore)
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"}'])
144 def setLevel(int level) {
145 if (level != this.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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
188 def currentValue(String deviceFeature) {
189 if (deviceFeature == "musicPlayer") {
194 def latestValue(String deviceFeature) {
195 if (deviceFeature == "musicPlayer") {