1 //Create a class for music player
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
8 public class MusicPlayer {
11 private String displayName
15 private int trackNumber
16 private String trackData
17 private String trackDescription
20 MusicPlayer(Closure sendEvent, String id, String label, String displayName, int level, String mute, String status, int trackNumber, String trackData, String trackDescription) {
21 this.sendEvent = sendEvent
24 this.displayName = displayName
28 this.trackNumber = trackNumber
29 this.trackData = trackData
30 this.trackDescription = trackDescription
34 def setValue(String value, String name) {
35 if ((name == "status") && (value != this.status)) {
37 println("the status of the music player with id:$id is changed to $value!")
38 } else if ((name == "level") && (value != this.level)) {
39 this.level = value.toInteger()
40 println("the level sound of the music player with id:$id is changed to $value!")
41 } else if ((name == "trackDescription") && (value != this.trackDescription)) {
42 this.trackDescription = value
43 println("the trackDescription of the music player with id:$id is changed to $value!")
44 } else if ((name == "trackData") && (value != this.trackData)) {
45 this.trackData = value
46 println("the trackData of the music player with id:$id is changed to $value!")
47 } else if ((name == "mute") && (value != this.mute)) {
49 println("the mute state of the music player with id:$id is changed to $value!")
54 def on(LinkedHashMap metaData) {
59 println("the music player with id:$id is on!")
61 sendEvent([name: "status", value: "on", deviceId: this.id, descriptionText: "",
62 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
65 def off(LinkedHashMap metaData) {
69 if (status != "off") {
70 println("the music player with id:$id is off!")
72 sendEvent([name: "status", value: "off", deviceId: this.id, descriptionText: "",
73 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
76 def mute(LinkedHashMap metaData) {
80 if (mute != "muted") {
81 println("the music player with id:$id is muted!")
83 sendEvent([name: "mute", value: "muted", deviceId: this.id, descriptionText: "",
84 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
87 def nextTrack(LinkedHashMap metaData) {
91 trackNumber = trackNumber+1
92 def trackPlaying = trackData
93 println("the $trackPlaying is selected!")
94 if (status != "play") {
96 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
97 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
99 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
100 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
101 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
102 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
104 def pause(LinkedHashMap metaData) {
108 if (status != "pause") {
109 println("the music player with id:$id is paused!")
110 this.status = "pause"
111 sendEvent([name: "status", value: "pause", deviceId: this.id, descriptionText: "",
112 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115 def play(LinkedHashMap metaData) {
119 if (status != "play") {
120 println("the music player with id:$id is starting to play!")
122 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
123 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
126 def playTrack(LinkedHashMap metaData) {
129 def playTrack(String trackToPlay) {
130 def trackPlaying = trackData
131 println("the $trackPlaying is selected to play!")
132 if (status != "play") {
134 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
135 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
137 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
138 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
139 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
140 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
142 def previousTrack(LinkedHashMap metaData) {
145 def previousTrack() {
146 if (trackNumber != 1)
147 trackNumber = trackNumber-1
148 def trackPlaying = trackData
149 println("the $trackPlaying is selected!")
150 if (status != "playing") {
151 this.status = "playing"
152 sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
153 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
155 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
156 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
157 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
158 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
160 /*def restoreTrack(String trackToRestore) {
161 musicPlayers*.restoreTrack(trackToRestore)
163 def resumeTrack(LinkedHashMap metaData) {
166 def resumeTrack(String trackToResume) {
167 def trackPlaying = trackData
168 println("the $trackPlaying is resumed!")
169 if (status != "play") {
171 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
172 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
175 def setLevel(LinkedHashMap metaData) {
178 def setLevel(int level) {
179 if (level != this.level) {
181 println("the level of sound is changed to $level!")
182 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
183 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
186 def setTrack(LinkedHashMap metaData) {
189 def setTrack(String trackToSet) {
190 def trackPlaying = trackData
191 println("the $trackPlaying is set!")
192 if (status != "play") {
194 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
195 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
197 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
198 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
199 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
200 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
202 def stop(LinkedHashMap metaData) {
206 if (status != "stop") {
207 println("the music player with id:$id is stop!")
209 sendEvent([name: "status", value: "stop", deviceId: this.id, descriptionText: "",
210 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
213 def statesSince(String info, Date dateObj) {
219 def eventsSince(Date dateObj) {
224 def evtActive = [[name: "status", value: "on", deviceId: "musicPlayerID0", descriptionText: "",
225 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
226 def evtInactive = [[name: "status", value: "off", deviceId: "musicPlayerID0", descriptionText: "",
227 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
228 def init = Verify.getInt(0,4)
230 if (init == 0) {//return empty set
232 } else if (init == 1) {//send one active event
237 } else if (init == 2) {//send two active events
245 } else if (init == 3) {//send one inactive event
250 } else if (init == 4) {//send two inactive events
260 def playText(LinkedHashMap metaData) {
263 def playText(String text) {
264 println("the music player with id:$id is playing the text:$text!")
265 if (status != "play") {
267 sendEvent([name: "status", value: "play", deviceId: this.id, descriptionText: "",
268 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
272 def currentValue(String deviceFeature) {
273 if (deviceFeature == "playpause") {
278 def latestValue(String deviceFeature) {
279 if (deviceFeature == "playpause") {