Infrastructure that works for all the locks' group!
[smartthings-infrastructure.git] / MusicPlayer / MusicPlayer.groovy
1 //Create a class for music player
2 package MusicPlayer
3 import Timer.SimulatedTimer
4
5
6 public class MusicPlayer {
7         private String id
8         private String label
9         private String displayName
10         private int level
11         private String mute
12         private String status
13         private int trackNumber
14         private String trackData
15         private String trackDescription
16         
17         MusicPlayer(String id, String label, String displayName, int level, String mute, String status, int trackNumber, String trackData, String trackDescription) {
18                 this.id = id
19                 this.label = label
20                 this.displayName = displayName
21                 this.level = level
22                 this.mute = mute
23                 this.status = status
24                 this.trackNumber = trackNumber
25                 this.trackData = trackData
26                 this.trackDescription = trackDescription
27         }
28
29         //By model checker
30         def setValue(String value, String name) {
31                 if ((name == "status") && (value != this.status)) {
32                         this.status = value
33                         println("the status of the music player with id:$id is changed to $value!")
34                 } else if ((name == "level") && (value != this.level)) {
35                         this.level = value.toInteger()
36                         println("the level sound of the music player with id:$id is changed to $value!")
37                 } else if ((name == "trackDescription") && (value != this.trackDescription)) {
38                         this.trackDescription = value
39                         println("the trackDescription of the music player with id:$id is changed to $value!")
40                 } else if ((name == "trackData") && (value != this.trackData)) {
41                         this.trackData = value
42                         println("the trackData of the music player with id:$id is changed to $value!")
43                 } else if ((name == "mute") && (value != this.mute)) {
44                         this.mute = value
45                         println("the mute state of the music player with id:$id is changed to $value!")
46                 }
47         }
48
49         //methods
50         def mute() {
51                 if (mute != "muted") {
52                         println("the music player with id:$id is muted!")
53                         this.mute = "muted"
54                         sendEvent([name: "mute", value: "mute", deviceId: this.id, descriptionText: "",
55                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
56                 }
57         }
58         def nextTrack() {
59                 trackNumber = trackNumber+1
60                 def trackPlaying = trackData
61                 println("the $trackPlaying is selected!")
62                 if (status != "playing") {
63                         this.status = "playing"
64                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
65                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
66                 }
67                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
68                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
69                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
70                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
71         }
72         def pause() {
73                 if (status != "paused") {
74                         println("the music player with id:$id is paused!")
75                         this.status = "paused"
76                         sendEvent([name: "status", value: "paused", deviceId: this.id, descriptionText: "",
77                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78                 }
79         }
80         def play() {
81                 if (status != "playing") {
82                         println("the music player with id:$id is starting to play!")
83                         this.status = "playing"
84                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
85                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
86                 }
87         }
88         def playTrack(String trackToPlay) {
89                 def trackPlaying = trackData
90                 println("the $trackPlaying is selected to play!")
91                 if (status != "playing") {              
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                 }
96                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
97                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
98                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
99                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
100         }
101         def previousTrack() {
102                 if (trackNumber != 1)
103                         trackNumber = trackNumber-1
104                 def trackPlaying = trackData
105                 println("the $trackPlaying is selected!")
106                 if (status != "playing") {
107                         this.status = "playing"
108                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
109                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
110                 }
111                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
112                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
113                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
114                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115         }
116         /*def restoreTrack(String trackToRestore) {
117                 musicPlayers*.restoreTrack(trackToRestore)
118         }*/
119         def resumeTrack(String trackToResume) {
120                 def trackPlaying = trackData
121                 println("the $trackPlaying is resumed!")
122                 if (status != "playing") {
123                         this.status = "playing"
124                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
125                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
126                 }
127         }
128         def setLevel(int level) {
129                 if (level != this.level) {
130                         this.level = level
131                         println("the level of sound is changed to $level!")
132                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
133                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
134                 }
135         }
136         def setTrack(String trackToSet) {
137                 def trackPlaying = trackData
138                 println("the $trackPlaying is set!")
139                 if (status != "playing") {
140                         this.status = "playing"
141                         sendEvent([name: "status", value: "playing", deviceId: this.id, descriptionText: "",
142                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
143                 }
144                 sendEvent([name: "trackDescription", value: "someDescriptions", deviceId: this.id, descriptionText: "",
145                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
146                 sendEvent([name: "trackData", value: "someTrack", deviceId: this.id, descriptionText: "",
147                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
148         }
149         def stop() {
150                 if (status != "stopped") {
151                         println("the music player with id:$id is stopped!")
152                         this.status = "stopped"
153                         sendEvent([name: "status", value: "stopped", deviceId: this.id, descriptionText: "",
154                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
155                 }
156         }
157
158         def currentValue(String deviceFeature) {
159                 if (deviceFeature == "musicPlayer") {
160                         return status
161                 }
162         }
163 }