Fixing a bug in MusicPlayer.
[smartthings-infrastructure.git] / MusicPlayer / MusicPlayer.groovy
index 6de93c1b8b96c168beb3a890291c7d4c063018aa..8d1a95d67809030b76c11fd56dcf0230a6050cef 100644 (file)
@@ -11,9 +11,12 @@ public class MusicPlayer {
        private String mute
        private String status
        private int trackNumber
-       private List trackData
+       private String trackData
+       private String trackDescription
+       def sendEvent
        
-       MusicPlayer(String id, String label, String displayName, int level, String mute, String status, int trackNumber, List trackData) {
+       MusicPlayer(Closure sendEvent, String id, String label, String displayName, int level, String mute, String status, int trackNumber, String trackData, String trackDescription) {
+               this.sendEvent = sendEvent
                this.id = id
                this.label = label
                this.displayName = displayName
@@ -22,67 +25,164 @@ public class MusicPlayer {
                this.status = status
                this.trackNumber = trackNumber
                this.trackData = trackData
+               this.trackDescription = trackDescription
+       }
+
+       //By model checker
+       def setValue(String value, String name) {
+               if ((name == "status") && (value != this.status)) {
+                       this.status = value
+                       println("the status of the music player with id:$id is changed to $value!")
+               } else if ((name == "level") && (value != this.level)) {
+                       this.level = value.toInteger()
+                       println("the level sound of the music player with id:$id is changed to $value!")
+               } else if ((name == "trackDescription") && (value != this.trackDescription)) {
+                       this.trackDescription = value
+                       println("the trackDescription of the music player with id:$id is changed to $value!")
+               } else if ((name == "trackData") && (value != this.trackData)) {
+                       this.trackData = value
+                       println("the trackData of the music player with id:$id is changed to $value!")
+               } else if ((name == "mute") && (value != this.mute)) {
+                       this.mute = value
+                       println("the mute state of the music player with id:$id is changed to $value!")
+               }
        }
 
        //methods
        def mute() {
-               println("the music player with id:$id is muted!")
-               this.mute = "muted"
+               if (mute != "muted") {
+                       println("the music player with id:$id is muted!")
+                       this.mute = "muted"
+                       sendEvent([name: "mute", value: "muted", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "mute.muted", value: "muted", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
        def nextTrack() {
-               if (trackNumber != trackData.size()-1)
-                       trackNumber = trackNumber+1
-               else
-                       trackNumber = 0
-               def trackPlaying = trackData[trackNumber] 
+               trackNumber = trackNumber+1
+               def trackPlaying = trackData
                println("the $trackPlaying is selected!")
-               this.status = "playing"
+               if (status != "playing") {
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+               sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
        }
        def pause() {
-               println("the music player with id:$id is paused!")
-               this.status = "paused"
+               if (status != "paused") {
+                       println("the music player with id:$id is paused!")
+                       this.status = "paused"
+                       sendEvent([name: "status", value: "paused", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.paused", value: "paused", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
        def play() {
-               println("the music player with id:$id is starting to play!")
-               this.status = "playing"
+               if (status != "playing") {
+                       println("the music player with id:$id is starting to play!")
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
        def playTrack(String trackToPlay) {
-               trackNumber = list.indexOf(trackToPlay)
-               def trackPlaying = trackData[trackNumber]
+               def trackPlaying = trackData
                println("the $trackPlaying is selected to play!")
-               this.status = "playing"
+               if (status != "playing") {              
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+               sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
        }
        def previousTrack() {
-               if (trackNumber != 0)
+               if (trackNumber != 1)
                        trackNumber = trackNumber-1
-               else
-                       trackNumber = trackData.size()-1
-               def trackPlaying = trackData[trackNumber] 
+               def trackPlaying = trackData
                println("the $trackPlaying is selected!")
-               this.status = "playing"
+               if (status != "playing") {
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+               sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
        }
        /*def restoreTrack(String trackToRestore) {
                musicPlayers*.restoreTrack(trackToRestore)
        }*/
        def resumeTrack(String trackToResume) {
-               trackNumber = list.indexOf(trackToResume)
-               def trackPlaying = trackData[trackNumber]
+               def trackPlaying = trackData
                println("the $trackPlaying is resumed!")
-               this.status = "playing"
+               if (status != "playing") {
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
        def setLevel(int level) {
-               this.level = level
-               println("the level of sound is changed to $level!")
+               if (level != this.level) {
+                       this.level = level
+                       println("the level of sound is changed to $level!")
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
        def setTrack(String trackToSet) {
-               trackNumber = list.indexOf(trackToSet)
-               def trackPlaying = trackData[trackNumber]
+               def trackPlaying = trackData
                println("the $trackPlaying is set!")
-               this.status = "playing"
+               if (status != "playing") {
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+               sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
+                          displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
        }
        def stop() {
-               println("the music player with id:$id is stopped!")
-               this.status = "stopped"
+               if (status != "stopped") {
+                       println("the music player with id:$id is stopped!")
+                       this.status = "stopped"
+                       sendEvent([name: "status", value: "stopped", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.stopped", value: "stopped", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+       def playText(String text) {
+               println("the music player with id:$id is playing the text:$text!")
+               if (status != "playing") {
+                       this.status = "playing"
+                       sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
+                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "status.playing", value: "playing", deviceId: this.id, descriptionText: "",
+                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
 
        def currentValue(String deviceFeature) {
@@ -90,4 +190,10 @@ public class MusicPlayer {
                        return status
                }
        }
+
+       def latestValue(String deviceFeature) {
+               if (deviceFeature == "musicPlayer") {
+                       return status
+               }
+       }
 }