1 //Create a class for music player
3 import Timer.SimulatedTimer
6 public class MusicPlayer {
9 private String displayName
13 private int trackNumber
14 private String trackData
15 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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
118 /*def restoreTrack(String trackToRestore) {
119 musicPlayers*.restoreTrack(trackToRestore)
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"}'])
130 def setLevel(int level) {
131 if (level != this.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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
168 def currentValue(String deviceFeature) {
169 if (deviceFeature == "musicPlayer") {
174 def latestValue(String deviceFeature) {
175 if (deviceFeature == "musicPlayer") {