//Create a class for relay switch device
package RelaySwitch
-import Timer.SimulatedTimer
-
-public class RelaySwitch {
- private String id
- private String label
- private String displayName
- private String switchState
- private String currentSwitch
- private String switchLatestValue
- def sendEvent
- def timers
-
-
- RelaySwitch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, String switchLatestValue) {
- this.sendEvent = sendEvent
- this.timers = new SimulatedTimer()
- this.currentSwitch = currentSwitch
+import SmartThing.SmartThing
+
+public class RelaySwitch extends SmartThing {
+ // id, label, and display name of the device
+ StringBuilder id = new StringBuilder()
+ StringBuilder label = new StringBuilder()
+ StringBuilder displayName = new StringBuilder()
+ // Features with string values
+ StringBuilder currentSwitch = new StringBuilder()
+ // Maps from features to values
+ HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+
+ RelaySwitch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) {
+ deviceValuesMap = deviceValueSmartThing
+ idSmartThing = id
+ labelSmartThing = label
+ displayNameSmartThing = displayName
+ sendEventSmartThings = sendEvent
+
+ // Initialization
this.id = id
this.label = label
this.displayName = displayName
- this.switchState = switchState
- this.switchLatestValue = switchLatestValue
+ this.currentSwitch = currentSwitch
+
+ deviceValuesMap.put("switch", currentSwitch)
}
- //By Apps
+ // Methods to set values
def on() {
- if (switchState != "on") {
- println("the relay switch with id:$id is on!")
- this.switchLatestValue = "on"
- this.switchState = "on"
- this.currentSwitch = "on"
- sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+ action(currentSwitch, "on", "switch")
}
def on(LinkedHashMap metaData) {
- if (switchState != "on") {
- def task = timers.runAfter(metaData["delay"]) {
- println("the relay switch with id:$id is on!")
- this.switchLatestValue = "on"
- this.switchState = "on"
- this.currentSwitch = "on"
- sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
- }
+ on()
}
def off() {
- if (switchState != "off") {
- println("the relay switch with id:$id is off!")
- this.switchLatestValue = "off"
- this.switchState = "off"
- this.currentSwitch = "off"
- sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+ action(currentSwitch, "off", "switch")
}
def off(LinkedHashMap metaData) {
- if (switchState != "off") {
- def task = timers.runAfter(metaData["delay"]) {
- println("the relay switch with id:$id is off!")
- this.switchLatestValue = "off"
- this.switchState = "off"
- this.currentSwitch = "off"
- sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
- }
+ off()
}
- //By Model Checker
- def setValue(String value) {
- println("the relay switch with id:$id is $value!")
- this.switchLatestValue = value
- this.switchState = value
- this.currentSwitch = value
- }
-
- def currentValue(String deviceFeature) {
- if (deviceFeature == "switch") {
- return switchState
+ def action(StringBuilder variable, String newValue, String feature) {
+ if (!variable.toString().equals(newValue)) {
+ String tmpID = id.toString()
+ variable.replace(0, variable.length(), newValue)
+ println("$feature of the relay switch with id:$tmpID is changed to $newValue!")
+ sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+ displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
}
}
- def latestValue(String deviceFeature) {
- if (deviceFeature == "switch") {
- return switchLatestValue
- }
+ // Methods to return values
+ def getCurrentSwitch() {
+ return currentSwitch.toString()
}
}