X-Git-Url: http://plrg.eecs.uci.edu/git/?p=smartthings-infrastructure.git;a=blobdiff_plain;f=Valve%2FValve.groovy;h=0cd43a9fe7b415725226e732dd755782de9fad17;hp=7680e162562afe16c44d2e7e2dd6f3ee07f45e2b;hb=d0b538d93e64c63d2673796db08570953b57f947;hpb=2932def9bb947d617975235763f7338360f0e5a4 diff --git a/Valve/Valve.groovy b/Valve/Valve.groovy index 7680e16..0cd43a9 100644 --- a/Valve/Valve.groovy +++ b/Valve/Valve.groovy @@ -1,88 +1,62 @@ //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 deviceValuesMap = new HashMap() + + 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() } }