//Create a class for valve
package Valve
-import Timer.SimulatedTimer
-
-public class Valve {
- private String id
- private String label
- private String displayName
- private String valve
- private String valveLatestValue
- def sendEvent
- def timers
-
-
- Valve(Closure sendEvent, String id, String label, String displayName, String valve, String valveLatestValue) {
- this.sendEvent = sendEvent
- this.timers = new SimulatedTimer()
+import SmartThing.SmartThing
+
+public class Valve 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 currentValve = new StringBuilder()
+ // Maps from features to values
+ HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+
+ Valve(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentValve) {
+ deviceValuesMap = deviceValueSmartThing
+ idSmartThing = id
+ labelSmartThing = label
+ displayNameSmartThing = displayName
+ sendEventSmartThings = sendEvent
+
+ // Initialization
this.id = id
this.label = label
this.displayName = displayName
- this.valve = valve
- this.valveLatestValue = valveLatestValue
+ this.currentValve = currentValve
+
+ deviceValuesMap.put("valve", currentValve)
}
- //By Apps
+ // Methods to set values
def open() {
- if (valve != "open") {
- println("the valve with id:$id is open!")
- this.valveLatestValue = "open"
- this.valve = "open"
- sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+ action(currentValve, "open", "contact")
}
def open(LinkedHashMap metaData) {
- if (valve != "open") {
- def task = timers.runAfter(metaData["delay"]) {
- println("the valve with id:$id is open!")
- this.valveLatestValue = "open"
- this.valve = "open"
- sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
- }
+ open()
}
def close() {
- if (valve != "closed") {
- println("the valve with id:$id is closed!")
- this.valveLatestValue = "closed"
- this.valve = "closed"
- sendEvent([name: "contact", value: "closed", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+ action(currentValve, "closed", "contact")
}
def close(LinkedHashMap metaData) {
- if (valve != "closed") {
- def task = timers.runAfter(metaData["delay"]) {
- println("the valve with id:$id is closed!")
- this.valveLatestValue = "closed"
- this.valve = "closed"
- sendEvent([name: "contact", value: "closed", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
- }
- }
-
- //By Model Checker
- def setValue(String value) {
- println("the valve with id:$id is $value!")
- this.valveLatestValue = value
- this.valve = value
+ close()
}
- def currentValue(String deviceFeature) {
- if (deviceFeature == "valve") {
- return valve
+ 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 valve with id:$tmpID is $newValue!")
+ sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+ displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
}
}
- def latestValue(String deviceFeature) {
- if (deviceFeature == "valve") {
- return valveLatestValue
- }
+ // Methods to return values
+ def getCurrentValve() {
+ return currentValve.toString()
}
}