Commit #7: Events thread-based + new easier Extractor.py + our own Timer class
[smartthings-infrastructure.git] / Switch / Switching.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 public class Switching{
6         int deviceNumbers       
7         List switches
8         def timers
9         def sendEvent
10
11         //If we have only one device
12         private int id = 0      
13         private String displayName = "switch0"
14         private String switchState = "off"
15         private String switchLatestValue = "off"
16
17         Switching(Closure sendEvent, int deviceNumbers) {
18                 this.sendEvent = sendEvent
19                 this.timers = new SimulatedTimer()
20                 this.deviceNumbers = deviceNumbers
21                 this.switches = []
22                 if (deviceNumbers == 1) {
23                         switches = [new Switches(sendEvent, this.id, this.displayName, this.switchState, this.switchLatestValue)]
24                 } else if (deviceNumbers == 2) {
25                         switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")]
26                 } else if (deviceNumbers == 3) {
27                         switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")
28                                    ,new Switches(sendEvent, 2, "switch2", "off", "off")]
29                 }
30         }
31
32         //By Apps
33         def on() {
34                 switches*.on()
35         }
36
37         def on(LinkedHashMap metaData) {
38                 def task = timers.runAfter(metaData["delay"]) {
39                         switches*.on()
40                 }
41         }
42
43         def off() {
44                 switches*.off()
45         }
46
47         def off(LinkedHashMap metaData) {
48                 def task = timers.runAfter(metaData["delay"]) {
49                         switches*.off()
50                 }
51         }
52
53         //By Model Checker
54         def setValue(LinkedHashMap eventDataMap) {
55                 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
56                 if (deviceNumbers == 1)
57                         this.switchState = switches[eventDataMap["deviceId"]].switchState
58                         this.switchLatestValue = switches[eventDataMap["deviceId"]].switchLatestValue
59                 sendEvent(eventDataMap)
60         }
61
62
63         def currentValue(String deviceFeature) {
64                 if (deviceNumbers == 1)
65                         switches[0].currentValue(deviceFeature)
66                 else
67                         switches*.currentValue(deviceFeature)
68         }
69
70         def latestValue(String deviceFeature) {
71                 if (deviceNumbers == 1)
72                         switches[0].latestValue(deviceFeature)
73                 else
74                         switches*.latestValue(deviceFeature)
75         }
76
77         def getAt(int ix) {
78                 switches[ix]
79         }
80 }