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