Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
1 //Create a class for switch level
2 package SwitchLevel
3 import Timer.SimulatedTimer
4
5 public class SwitchLevel {
6         private String id
7         private String label
8         private String displayName
9         private String switchState
10         private String currentSwitch
11         private int level
12         private int rate
13         private String switchLatestValue
14         def sendEvent   
15         def timers
16         
17
18         SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
19                 this.sendEvent = sendEvent
20                 this.timers = new SimulatedTimer()
21                 this.id = id
22                 this.label = label
23                 this.displayName = displayName
24                 this.level = level
25                 this.rate = level
26                 this.switchState = switchState
27                 this.currentSwitch = switchState
28                 this.switchLatestValue = switchLatestValue
29         }
30
31         //By Apps
32         def setLevel(int level) {
33                 if (this.level != level) {
34                         println("the switch with id:$id is setted to level $level!")
35                         this.level = level
36                         this.rate = level
37                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
38                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
39                 }
40         }
41         
42         def setLevel(long level) {
43                 if (this.level != level) {
44                         println("the switch with id:$id is setted to level $level!")
45                         this.level = level
46                         this.rate = level
47                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
48                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
49                 }
50         }
51
52         def on() {
53                 if (this.switchState != "on") {
54                         println("the switch with id:$id is on!")
55                         this.switchLatestValue = "on"
56                         this.switchState = "on"
57                         this.currentSwitch = "on"
58                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
59                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                         sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
61                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
62                 }
63         }
64
65         def on(LinkedHashMap metaData) {
66                 if (this.switchState != "on") {
67                         def task = timers.runAfter(metaData["delay"]) {
68                                 println("the switch with id:$id is on!")
69                                 this.switchLatestValue = "on"
70                                 this.switchState = "on"
71                                 this.currentSwitch = "on"
72                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
73                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                                 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
75                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
76                         }
77                 }
78         }
79
80         def off() {
81                 if (this.switchState != "off") {
82                         println("the switch with id:$id is off!")
83                         this.switchLatestValue = "off"
84                         this.switchState = "off"
85                         this.currentSwitch = "off"
86                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
87                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88                         sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
89                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
90                 }
91         }
92
93         def off(LinkedHashMap metaData) {
94                 if (this.switchState != "off") {
95                         def task = timers.runAfter(metaData["delay"]) {
96                                 println("the switch with id:$id is off!")
97                                 this.switchLatestValue = "off"
98                                 this.switchState = "off"
99                                 this.currentSwitch = "off"
100                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
101                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
102                                 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
103                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
104                         }
105                 }
106         }
107
108         //By Model Checker
109         def setValue(String value, String name) {
110                 if (name == "switch") {
111                         println("the switch with id:$id is $value!")
112                         this.switchLatestValue = value
113                         this.switchState = value
114                         this.currentSwitch = value
115                 } else if (name == "level") {
116                         println("the switch with id:$id is setted to level $value!")
117                         this.level = value.toInteger()
118                         this.rate = value.toInteger()
119                 }
120         }
121
122
123         def currentValue(String deviceFeature) {
124                 if (deviceFeature == "level") {
125                         return level
126                 } else if (deviceFeature == "switch") {
127                         return switchState
128                 }
129         }
130
131         def latestValue(String deviceFeature) {
132                 if (deviceFeature == "level") {
133                         return level
134                 } else if (deviceFeature == "switch") {
135                         return switchState
136                 }
137         }
138 }