"First commit!"
[smartthings-infrastructure.git] / Methods / EventHandler.groovy
1 /////////////////////////////////////////////////////////////////////
2 def EventHandler() {
3         while(true) {
4                 List evt = []
5                 print "Waiting for an event...\n"
6                 def EVENT = System.in.newReader().readLine()
7                 SepLine = EVENT.split()
8                 for (int i = 0; i < EventList.size(); i++) {
9                         if (EventList[i] == SepLine[0]) {
10                                 println("The following effect: \n")
11                                 evt.add(new Event())
12                                 switch(SepLine[0]) { 
13                                         case "Touched":
14                                                 ObjectList[i].touched = 1
15                                                 evt[-1].value = "Touched"
16                                                 evt[-1].linkText = "touched by user"
17                                                 evt[-1].name = "TouchSensor"
18                                                 evt[-1].descriptionText = "Touching"
19                                                 break
20                                         case "lock":
21                                                 if (SepLine[1] == "0") {
22                                                         ObjectList[i][0].lock()
23                                                         evt[-1].deviceId = 0
24                                                         evt[-1].value = "locked"
25                                                         evt[-1].linkText = "lock0"
26                                                         evt[-1].displayName = "lock0"
27                                                         evt[-1].name = "lock"
28                                                         evt[-1].descriptionText = "locking"
29                                                 } else if (SepLine[1] == "1") {
30                                                         ObjectList[i][1].lock()
31                                                         evt[-1].deviceId = 1
32                                                         evt[-1].value = "locked"
33                                                         evt[-1].linkText = "lock1"
34                                                         evt[-1].displayName = "lock1"
35                                                         evt[-1].name = "lock"
36                                                         evt[-1].descriptionText = "locking"
37                                                 } else if (SepLine[1] == "2") {
38                                                         ObjectList[i][2].lock()
39                                                         evt[-1].deviceId = 2
40                                                         evt[-1].value = "locked"
41                                                         evt[-1].linkText = "lock2"
42                                                         evt[-1].displayName = "lock2"
43                                                         evt[-1].name = "lock"
44                                                         evt[-1].descriptionText = "locking"
45                                                 }
46                                                 break
47                                         case "unlock":
48                                                 if (SepLine[1] == "0") {
49                                                         ObjectList[i][0].unlock()
50                                                         evt[-1].deviceId = 0
51                                                         evt[-1].value = "unlocked"
52                                                         evt[-1].linkText = "lock0"
53                                                         evt[-1].displayName = "lock0"
54                                                         evt[-1].name = "lock"
55                                                         evt[-1].descriptionText = "unlocking"   
56                                                 } else if (SepLine[1] == "1") {
57                                                         ObjectList[i][1].unlock()
58                                                         evt[-1].deviceId = 0
59                                                         evt[-1].value = "unlocked"
60                                                         evt[-1].linkText = "lock1"
61                                                         evt[-1].displayName = "lock1"
62                                                         evt[-1].name = "lock"
63                                                         evt[-1].descriptionText = "unlocking"
64                                                 } else if (SepLine[1] == "2") {
65                                                         ObjectList[i][2].unlock()
66                                                         evt[-1].deviceId = 2
67                                                         evt[-1].value = "unlocked"
68                                                         evt[-1].linkText = "lock2"
69                                                         evt[-1].displayName = "lock2"
70                                                         evt[-1].name = "lock"
71                                                         evt[-1].descriptionText = "unlocking"
72                                                 }
73                                                 break
74                                         case "contact.open":
75                                                 if (SepLine[1] == "0") {
76                                                         ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
77                                                         ObjectList[i][0].currentContact = "open"
78                                                         evt[-1].deviceId = 0
79                                                         evt[-1].value = "contact.open"
80                                                         evt[-1].linkText = "contact0"
81                                                         evt[-1].displayName = "contact0"
82                                                         evt[-1].name = "ContactSensor"
83                                                         evt[-1].descriptionText = "opening"     
84                                                 } else if (SepLine[1] == "1") {
85                                                         ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
86                                                         ObjectList[i][1].currentContact = "open"
87                                                         evt[-1].deviceId = 1
88                                                         evt[-1].value = "contact.open"
89                                                         evt[-1].linkText = "contact1"
90                                                         evt[-1].displayName = "contact1"
91                                                         evt[-1].name = "ContactSensor"
92                                                         evt[-1].descriptionText = "opening"
93                                                 } else if (SepLine[1] == "2") {
94                                                         ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
95                                                         ObjectList[i][2].currentContact = "open"
96                                                         evt[-1].deviceId = 2
97                                                         evt[-1].value = "contact.open"
98                                                         evt[-1].linkText = "contact2"
99                                                         evt[-1].displayName = "contact2"
100                                                         evt[-1].name = "ContactSensor"
101                                                         evt[-1].descriptionText = "opening"
102                                                 }
103                                                 break
104                                         case "contact.closed":
105                                                 if (SepLine[1] == "0") {
106                                                         ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
107                                                         ObjectList[i][0].currentContact = "closed"
108                                                         evt[-1].deviceId = 0
109                                                         evt[-1].value = "contact.closed"
110                                                         evt[-1].linkText = "contact0"
111                                                         evt[-1].displayName = "contact0"
112                                                         evt[-1].name = "ContactSensor"
113                                                         evt[-1].descriptionText = "closing"
114                                                 } else if (SepLine[1] == "1") {
115                                                         ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
116                                                         ObjectList[i][1].currentContact = "closed"
117                                                         evt[-1].deviceId = 1
118                                                         evt[-1].value = "contact.closed"
119                                                         evt[-1].linkText = "contact1"
120                                                         evt[-1].displayName = "contact1"
121                                                         evt[-1].name = "ContactSensor"
122                                                         evt[-1].descriptionText = "closing"
123                                                 } else if (SepLine[1] == "2") {
124                                                         ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
125                                                         ObjectList[i][2].currentContact = "closed"
126                                                         evt[-1].deviceId = 2
127                                                         evt[-1].value = "contact.closed"
128                                                         evt[-1].linkText = "contact2"
129                                                         evt[-1].displayName = "contact2"
130                                                         evt[-1].name = "ContactSensor"
131                                                         evt[-1].descriptionText = "closing"
132                                                 }
133                                                 break
134                                         default:
135                                                 break
136                                 }
137                                 FunctionList[i](evt[-1])
138                         }
139                 }
140         }
141 }