1 //Create a class for switch device
3 import Timer.SimulatedTimer
5 public class Switches {
11 //If we have only one device
12 private String id = "switchID0"
13 private String label = "switch0"
14 private String displayName = "switch0"
15 private String switchState = "off"
16 private String currentSwitch = "off"
17 private int currentLevel = 50
18 private String switchLatestValue = "off"
20 Switches(Closure sendEvent, int deviceNumbers, boolean init) {
21 this.sendEvent = sendEvent
22 this.timers = new SimulatedTimer()
23 this.deviceNumbers = deviceNumbers
27 this.switchState = "off"
28 this.currentSwitch = "off"
29 this.switchLatestValue = "off"
30 this.currentLevel = 50
32 this.switchState = "on"
33 this.currentSwitch = "on"
34 this.switchLatestValue = "on"
35 this.currentLevel = 60
37 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
40 //Methods for closures
41 def count(Closure Input) {
47 def each(Closure Input) {
50 def find(Closure Input) {
53 def sort(Closure Input) {
56 def collect(Closure Input) {
57 switches.collect(Input)
61 def setLevel(int level) {
63 switches[0].setLevel(level)
67 switchLatestValue = "on"
73 def on(LinkedHashMap metaData) {
74 def task = timers.runAfter(metaData["delay"]) {
75 switchLatestValue = "on"
83 switchLatestValue = "off"
89 def off(LinkedHashMap metaData) {
90 def task = timers.runAfter(metaData["delay"]) {
91 switchLatestValue = "off"
99 def setValue(LinkedHashMap eventDataMap) {
100 if (eventDataMap["value"] != switches[0].switchState) {
101 this.switchState = eventDataMap["value"]
102 this.switchLatestValue = eventDataMap["value"]
103 switches[0].setValue(eventDataMap["value"])
104 sendEvent(eventDataMap)
109 def currentValue(String deviceFeature) {
110 switches[0].currentValue(deviceFeature)
113 def latestValue(String deviceFeature) {
114 switches[0].latestValue(deviceFeature)