Fixing a bug: missing a closing parenthesis.
[smartthings-infrastructure.git] / MusicPlayer / MusicPlayers.groovy
1 //Create a class for music player
2 package MusicPlayer
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class MusicPlayers {
9         private int deviceNumbers
10         private List musicPlayers
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "musicPlayerID0"
15         private String label = "musicPlayer0"
16         private String displayName = "musicPlayer0"
17         private int level = 20
18         private String mute = "unmuted"
19         private String status = "paused"
20         private int trackNumber = 1
21         private String trackData = "someTrack"
22         private String trackDescription = "someDescriptions"
23
24         
25                 
26         MusicPlayers(Closure sendEvent, int deviceNumbers) {
27                 this.sendEvent = sendEvent
28                 this.deviceNumbers = deviceNumbers
29                 this.musicPlayers = []
30
31                 /*def initLevel = Verify.getIntFromList(10, 20, 30)
32                 this.level = initLevel
33                 def initMute = Verify.getBoolean()
34                 if (initMute) {
35                         this.mute = "unmuted"
36                 } else {
37                         this.mute = "muted"
38                 }
39                 def initStatus = Verify.getInt(0,2)
40                 if (initStatus == 0) {
41                         this.status = "paused"
42                 } else if (initStatus == 1) {
43                         this.status = "playing"
44                 } else {
45                         this.status = "stopped"
46                 }
47                 def initTrack = Verify.getIntFromList(1, 2, 3)
48                 this.trackNumber = initTrack 
49                 def initData = Verify.getBoolean()
50                 if (initData) {
51                         this.trackData = "someTrack"
52                 } else {
53                         this.trackData = "someOtherTrack"
54                 }
55                 def initDesc = Verify.getBoolean()
56                 if (initDesc) {
57                         this.trackDescription = "someDescriptions"
58                 } else {
59                         this.trackDescription = "someOtherDescriptions"
60                 }*/
61                 musicPlayers.add(new MusicPlayer(sendEvent, id, label, displayName, this.level, this.mute, this.status, this.trackNumber, this.trackData, this.trackDescription))
62         }
63
64         //By model checker
65         def setValue(LinkedHashMap eventDataMap) {
66                 if (eventDataMap["name"] == "status") {
67                         if (eventDataMap["value"] != musicPlayers[0].status) {
68                                 this.status = eventDataMap["value"]
69                                 musicPlayers[0].setValue(eventDataMap["value"], "status")
70                                 sendEvent(eventDataMap)
71                         }       
72                 } else if (eventDataMap["name"] == "level") {
73                         if (eventDataMap["value"].toInteger() != musicPlayers[0].level) {
74                                 this.level = eventDataMap["value"].toInteger()
75                                 musicPlayers[0].setValue(eventDataMap["value"], "level")
76                                 sendEvent(eventDataMap)
77                         }
78                 } else if (eventDataMap["name"] == "trackDescription") {
79                         if (eventDataMap["value"] != musicPlayers[0].trackDescription) {
80                                 this.trackDescription = eventDataMap["value"]
81                                 musicPlayers[0].setValue(eventDataMap["value"], "trackDescription")
82                                 sendEvent(eventDataMap)
83                         }
84                 } else if (eventDataMap["name"] == "trackData") {
85                         if (eventDataMap["value"] != musicPlayers[0].trackData) {
86                                 this.trackData = eventDataMap["value"]
87                                 musicPlayers[0].setValue(eventDataMap["value"], "trackData")                            
88                                 sendEvent(eventDataMap)
89                         }
90                 } else if (eventDataMap["name"] == "mute") {
91                         if (eventDataMap["value"] != musicPlayers[0].mute) {
92                                 this.mute = eventDataMap["value"]
93                                 musicPlayers[0].setValue(eventDataMap["value"], "mute")
94                                 sendEvent(eventDataMap)
95                         }
96                 }
97         }
98
99         //Methods for closures
100         def count(Closure Input) {
101                 musicPlayers.count(Input)
102         }
103         def size() {
104                 musicPlayers.size()
105         }
106         def each(Closure Input) {
107                 musicPlayers.each(Input)
108         }
109         def find(Closure Input) {
110                 musicPlayers.find(Input)
111         }
112         def sort(Closure Input) {
113                 musicPlayers.sort(Input)
114         }
115         def collect(Closure Input) {
116                 musicPlayers.collect(Input)
117         }
118
119         //methods
120         def mute() {
121                 if (mute != "muted") {
122                         this.mute = "muted"
123                         musicPlayers[0].mute()
124                 }
125         }
126         def nextTrack() {
127                 if (status != "playing") {
128                         this.status = "playing"
129                 }
130                 this.trackNumber = this.trackNumber+1
131                 musicPlayers[0].nextTrack()
132         }
133         def pause() {
134                 if (status != "paused") {
135                         this.status = "paused"
136                         musicPlayers[0].pause()                 
137                 }
138         }
139         def play() {
140                 if (status != "playing") {
141                         this.status = "playing"
142                         musicPlayers[0].play()
143                 }
144         }
145         def playTrack(String trackToPlay) {
146                 if (status != "playing") {
147                         this.status = "playing"
148                 }
149                 musicPlayers[0].playTrack(trackToPlay)
150         }
151         def previousTrack() {
152                 if (status != "playing") {
153                         this.status = "playing"
154                 }
155                 if (this.trackNumber != 1)
156                         this.trackNumber = this.trackNumber-1
157                 musicPlayers[0].previousTrack()
158         }
159         def restoreTrack(String trackToRestore) {
160                 musicPlayers[0].restoreTrack(trackToRestore)
161         }
162         def resumeTrack(String trackToResume) {
163                 if (status != "playing") {
164                         this.status = "playing"
165                 }
166                 musicPlayers[0].resumeTrack(trackToResume)
167         }
168         def setLevel(int level) {
169                 if (level != this.level) {
170                         this.level = level      
171                         musicPlayers[0].setLevel(level)
172                 }
173         }
174         def setTrack(String trackToSet) {
175                 if (status != "playing") {
176                         this.status = "playing"
177                 }
178                 musicPlayers[0].setTrack(trackToSet)
179         }
180         def stop() {
181                 if (status != "stopped") {
182                         this.status = "stopped"
183                         musicPlayers[0].stop()
184                 }
185         }
186         def playText(String text) {
187                 if (status != "playing") {
188                         this.status = "playing"
189                 }
190                 musicPlayers[0].playText(text)
191         }
192
193         def currentValue(String deviceFeature) {
194                 musicPlayers[0].currentValue(deviceFeature)
195         }
196
197         def latestValue(String deviceFeature) {
198                 musicPlayers[0].latestValue(deviceFeature)
199         }
200
201         def getAt(int ix) {
202                 musicPlayers[ix]
203         }
204 }