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: "50", deviceId: this.id, descriptionText: "",
38                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
39                 }
40         }
41
42         def on() {
43                 if (this.switchState != "on") {
44                         println("the switch with id:$id is on!")
45                         this.switchLatestValue = this.switchState
46                         this.switchState = "on"
47                         this.currentSwitch = "on"
48                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
49                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50                 }
51         }
52
53         def on(LinkedHashMap metaData) {
54                 if (this.switchState != "on") {
55                         def task = timers.runAfter(metaData["delay"]) {
56                                 println("the switch with id:$id is on!")
57                                 this.switchLatestValue = this.switchState
58                                 this.switchState = "on"
59                                 this.currentSwitch = "on"
60                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
61                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
62                         }
63                 }
64         }
65
66         def off() {
67                 if (this.switchState != "off") {
68                         println("the switch with id:$id is off!")
69                         this.switchLatestValue = this.switchState
70                         this.switchState = "off"
71                         this.currentSwitch = "off"
72                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
73                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                 }
75         }
76
77         def off(LinkedHashMap metaData) {
78                 if (this.switchState != "off") {
79                         def task = timers.runAfter(metaData["delay"]) {
80                                 println("the switch with id:$id is off!")
81                                 this.switchLatestValue = this.switchState
82                                 this.switchState = "off"
83                                 this.currentSwitch = "off"
84                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
85                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
86                         }
87                 }
88         }
89
90         //By Model Checker
91         def setValue(String value) {
92                 println("the switch with id:$id is setted to level $value!")
93                 this.level = value.toInteger()
94                 this.rate = value.toInteger()
95         }
96 }