1 //Create a class for switch device
3 import Timer.SimulatedTimer
5 public class Switching{
11 //If we have only one device
13 private String displayName = "switch0"
14 private String switchState = "off"
15 private String switchLatestValue = "off"
17 Switching(Closure sendEvent, int deviceNumbers) {
18 this.sendEvent = sendEvent
19 this.timers = new SimulatedTimer()
20 this.deviceNumbers = deviceNumbers
22 if (deviceNumbers == 1) {
23 switches = [new Switches(sendEvent, this.id, this.displayName, this.switchState, this.switchLatestValue)]
24 } else if (deviceNumbers == 2) {
25 switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")]
26 } else if (deviceNumbers == 3) {
27 switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")
28 ,new Switches(sendEvent, 2, "switch2", "off", "off")]
37 def on(LinkedHashMap metaData) {
38 def task = timers.runAfter(metaData["delay"]) {
47 def off(LinkedHashMap metaData) {
48 def task = timers.runAfter(metaData["delay"]) {
54 def setValue(LinkedHashMap eventDataMap) {
55 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
56 if (deviceNumbers == 1)
57 this.switchState = switches[eventDataMap["deviceId"]].switchState
58 this.switchLatestValue = switches[eventDataMap["deviceId"]].switchLatestValue
59 sendEvent(eventDataMap)
63 def currentValue(String deviceFeature) {
64 if (deviceNumbers == 1)
65 switches[0].currentValue(deviceFeature)
67 switches*.currentValue(deviceFeature)
70 def latestValue(String deviceFeature) {
71 if (deviceNumbers == 1)
72 switches[0].latestValue(deviceFeature)
74 switches*.latestValue(deviceFeature)