X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=Valve%2FValve.groovy;fp=Valve%2FValve.groovy;h=4aaf5b3067f465602595f112dbdf56b8a2921895;hb=31da25f02c14814f402e84b35a174f3c0958cd38;hp=0000000000000000000000000000000000000000;hpb=7bc2c68bdfb07a54d6e8399e7514d715c78a2398;p=smartthings-infrastructure.git diff --git a/Valve/Valve.groovy b/Valve/Valve.groovy new file mode 100644 index 0000000..4aaf5b3 --- /dev/null +++ b/Valve/Valve.groovy @@ -0,0 +1,88 @@ +//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() + this.id = id + this.label = label + this.displayName = displayName + this.valve = valve + this.valveLatestValue = valveLatestValue + } + + //By Apps + def open() { + if (valve != "open") { + println("the valve with id:$id is open!") + this.valveLatestValue = this.valve + this.valve = "open" + sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) + } + } + + def open(LinkedHashMap metaData) { + if (valve != "open") { + def task = timers.runAfter(metaData["delay"]) { + println("the valve with id:$id is open!") + this.valveLatestValue = this.valve + this.valve = "open" + sendEvent([name: "contact", value: "open", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) + } + } + } + + def close() { + if (valve != "closed") { + println("the valve with id:$id is closed!") + this.valveLatestValue = this.valve + this.valve = "closed" + sendEvent([name: "contact", value: "closed", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) + } + } + + def close(LinkedHashMap metaData) { + if (valve != "closed") { + def task = timers.runAfter(metaData["delay"]) { + println("the valve with id:$id is closed!") + this.valveLatestValue = this.valve + 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 = this.valve + this.valve = value + } + + def currentValue(String deviceFeature) { + if (deviceFeature == "valve") { + return valve + } + } + + def latestValue(String deviceFeature) { + if (deviceFeature == "valve") { + return valveLatestValue + } + } +}