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
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
23 this.displayName = displayName
27 this.trackNumber = trackNumber
28 this.trackData = trackData
29 this.trackDescription = trackDescription
33 def setValue(String value, String name) {
34 if ((name == "status") && (value != this.status)) {
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)) {
48 println("the mute state of the music player with id:$id is changed to $value!")
54 if (mute != "muted") {
55 println("the music player with id:$id is 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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
131 /*def restoreTrack(String trackToRestore) {
132 musicPlayers*.restoreTrack(trackToRestore)
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"}'])
145 def setLevel(int level) {
146 if (level != this.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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
189 def currentValue(String deviceFeature) {
190 if (deviceFeature == "musicPlayer") {
195 def latestValue(String deviceFeature) {
196 if (deviceFeature == "musicPlayer") {