package Lock
import Timer.SimulatedTimer
-public class Locks {
- private int id
- private String displayName
- private String lockState
- private String lockLatestValue
+public class Locks{
+ int deviceNumbers
+ List locks
def sendEvent
def timers
+ //When we have only one device
+ private int id = 20
+ private String label = "lock"
+ private String displayName = "lock"
+ private String lockState = "locked"
+ private String currentLock = "locked"
+ private String lockLatestValue = "locked"
- Locks(Closure sendEvent, int id, String displayName, String lockState, String lockLatestValue) {
- this.id = id
+ Locks(Closure sendEvent, int deviceNumbers) {
this.sendEvent = sendEvent
- this.displayName = displayName
- this.lockState = lockState
- this.lockLatestValue = lockLatestValue
this.timers = new SimulatedTimer()
+ this.deviceNumbers = deviceNumbers
+ this.locks = []
+ for (int i = 0;i < deviceNumbers;i++) {
+ locks.add(new Lock(sendEvent, i+20, label+i.toString(), displayName+i.toString(), this.lockState, this.lockLatestValue))
+ }
}
//By Apps
def lock() {
- println("the door with id:$id is locked!")
- this.lockLatestValue = this.lockState
- this.lockState = "locked"
- sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+ locks*.lock()
}
def lock(LinkedHashMap metaData) {
def task = timers.runAfter(metaData["delay"]) {
- println("the door with id:$id is locked!")
- this.lockLatestValue = this.lockState
- this.lockState = "locked"
- sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+ locks*.lock()
}
}
-
+
def unlock() {
- println("the door with id:$id is unlocked!")
- this.lockLatestValue = this.lockState
- this.lockState = "unlocked"
- sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+ locks*.unlock()
}
+
def unlock(LinkedHashMap metaData) {
def task = timers.runAfter(metaData["delay"]) {
- println("the door with id:$id is locked!")
- this.lockLatestValue = this.lockState
- this.lockState = "locked"
- sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+ locks*.unlock()
}
}
+ //Methods for closures
+ def count(Closure Input) {
+ locks.count(Input)
+ }
+ def size() {
+ locks.size()
+ }
+ def each(Closure Input) {
+ locks.each(Input)
+ }
+
//By Model Checker
- def setValue(String value) {
- println("the door with id:$id is $value!")
- this.lockLatestValue = this.lockState
- this.lockState = value
+ def setValue(LinkedHashMap eventDataMap) {
+ locks[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
+ if (deviceNumbers == 1)
+ this.lockState = locks[eventDataMap["deviceId"]].lockState
+ this.currentLock = locks[eventDataMap["deviceId"]].lockState
+ this.lockLatestValue = locks[eventDataMap["deviceId"]].lockLatestValue
+ sendEvent(eventDataMap)
}
-
+
def currentValue(String deviceFeature) {
- if (deviceFeature == "lock") {
- return lockState
- }
+ if (deviceNumbers == 1)
+ locks[0].currentValue(deviceFeature)
+ else
+ locks*.currentValue(deviceFeature)
}
def latestValue(String deviceFeature) {
- if (deviceFeature == "lock") {
- return lockLatestValue
- }
+ if (deviceNumbers == 1)
+ locks[0].latestValue(deviceFeature)
+ else
+ locks*.latestValue(deviceFeature)
+ }
+
+ def getAt(int ix) {
+ locks[ix]
}
}
+