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 eventsSince(Date dateObj, LinkedHashMap metaData) {
62 return switches[0].eventsSince()
65 def setLevel(int level) {
67 switches[0].setLevel(level)
71 switchLatestValue = "on"
77 def on(LinkedHashMap metaData) {
78 def task = timers.runAfter(metaData["delay"]) {
79 switchLatestValue = "on"
87 switchLatestValue = "off"
93 def off(LinkedHashMap metaData) {
94 def task = timers.runAfter(metaData["delay"]) {
95 switchLatestValue = "off"
103 def setValue(LinkedHashMap eventDataMap) {
104 if (eventDataMap["value"] != switches[0].switchState) {
105 this.switchState = eventDataMap["value"]
106 this.switchLatestValue = eventDataMap["value"]
107 switches[0].setValue(eventDataMap["value"])
108 sendEvent(eventDataMap)
113 def currentValue(String deviceFeature) {
114 switches[0].currentValue(deviceFeature)
117 def latestValue(String deviceFeature) {
118 switches[0].latestValue(deviceFeature)