Infrastruction modification
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevels.groovy
index 773c1ccfad8e37d48107045e1b7768a2a2e2cc48..8e06b66bb2be89592ca51aa3a93f66adae179034 100644 (file)
 //Create a class for switch level
 package SwitchLevel
-import Timer.SimulatedTimer
+import SmartThing.SmartThings
 
-public class SwitchLevels {
-       int deviceNumbers       
-       List switchLevels
-       def timers
-       def sendEvent
+public class SwitchLevels extends SmartThings {
+       List switchLevels = new ArrayList()
 
-       //If we have only one device
-       private String id = "switchLevelID0"
-       private String label = "switchLevel0"
-       private String displayName = "switchLevel0"
-       private int level = 50
-       private int currentLevel = 50
-       private int rate = 50
-       private int currentRate = 50
-       private int hue = 30
-       private int currentHue = 30
-       private int saturation = 70
-       private int currentSaturation = 70
-       private String switchState = "on"
-       private String currentSwitch = "on"
-       private String switchLatestValue = "on"
+       SwitchLevels(Closure sendEvent, boolean init) {
+               // Only initialize one time since we only have one device for each capability
+               switchLevels = smartThings
 
-       SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
-               this.sendEvent = sendEvent
-               this.timers = new SimulatedTimer()
-               this.deviceNumbers = deviceNumbers
-               this.switchLevels = []
+               // Initialization
+               StringBuilder id = new StringBuilder("switchLevelID0")
+               StringBuilder label = new StringBuilder("switchLevel")
+               StringBuilder displayName = new StringBuilder("switchLevel0")
+               MutableInteger level = new MutableInteger()
 
-               if (init) {
-                       this.level = 50
-                       this.currentLevel = 50
-                       this.rate = 50
-                       this.currentRate = 50
-                       this.hue = 30
-                       this.currentHue = 30
-                       this.saturation = 70
-                       this.currentSaturation = 70
-                       this.switchState = "off"
-                       this.currentSwitch = "off"
-                       this.switchLatestValue = "off"
-               } else {
-                       this.level = 60
-                       this.currentLevel = 60
-                       this.rate = 60
-                       this.currentRate = 60
-                       this.hue = 50
-                       this.currentHue = 50
-                       this.saturation = 90
-                       this.currentSaturation = 90
-                       this.switchState = "on"
-                       this.currentSwitch = "on"
-                       this.switchLatestValue = "on"
-               }
-               switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
-       }
-
-       //Methods for closures
-       def count(Closure Input) {
-               switchLevels.count(Input)
-       }
-       def size() {
-               switchLevels.size()
-       }
-       def each(Closure Input) {
-               switchLevels.each(Input)
-       }
-       def find(Closure Input) {
-               switchLevels.find(Input)
-       }
-       def sort(Closure Input) {
-               switchLevels.sort(Input)
-       }
-       def collect(Closure Input) {
-               switchLevels.collect(Input)
-       }
+               if (init)
+                       level.setValue(50)
+               else
+                       level.setValue(60)
 
-       //By Apps
-       def setColor(LinkedHashMap metaData) {
-               if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
-                       this.level = metaData["level"]
-                       this.currentLevel = metaData["level"]
-                       this.rate = metaData["level"]
-                       this.currentRate = metaData["level"]
-                       this.hue = metaData["hue"]
-                       this.currentHue = metaData["hue"]
-                       this.saturation = metaData["saturation"]
-                       this.currentSaturation = metaData["saturation"]
-                       switchLevels[0].setColor(metaData)
-               }
-       }
-       
-       def setLevel(String level) {
-               def newLevel = level.toInteger()
-               setLevel(newLevel)
+               switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, level))
        }
 
-       def setLevel(int level) {
-               if (this.level != level) {
-                       switchLevels[0].setLevel(level)
-                       this.level = level
-                       this.currentLevel = level
-                       this.rate = level
-                       this.currentRate = level
-               }
-       }
-       
-       def setLevel(long level) {
-               if (this.level != level) {
-                       switchLevels[0].setLevel(level)
-                       this.level = level
-                       this.rate = level
-                       this.currentLevel = level
-                       this.currentRate = level
-               }
+       // Methods to set values
+       def setLevel(String newValue) {
+               setLevel(newValue.toInteger())
        }
 
-       def on() {
-               switchLatestValue = "on"
-               switchState = "on"
-               currentSwitch = "on"
-               switchLevels[0].on()
+       def setLevel(long newValue) {
+               setLevel((int) newValue)
        }
 
-       def on(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
-                       switchLatestValue = "on"
-                       switchState = "on"
-                       currentSwitch = "on"
-                       switchLevels[0].on()
-               }
-       }
-
-       def off() {
-               switchLatestValue = "off"
-               switchState = "off"
-               currentSwitch = "off"
-               switchLevels[0].off()
-       }
-
-       def off(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
-                       switchLatestValue = "off"
-                       switchState = "off"
-                       currentSwitch = "off"
-                       switchLevels[0].off()
-               }
-       }
-
-       //By Model Checker
-       def setValue(LinkedHashMap eventDataMap) {
-               if (eventDataMap["name"] == "switch") {
-                       if (eventDataMap["value"] != switchLevels[0].switchState) {
-                               this.switchState = eventDataMap["value"]
-                               this.switchLatestValue = eventDataMap["value"]
-                               this.currentSwitch = eventDataMap["value"]
-                               switchLevels[0].setValue(eventDataMap["value"], "switch")
-                               sendEvent(eventDataMap)
-                       }
-               } else if (eventDataMap["name"] == "level") {
-                       if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
-                               this.level = eventDataMap["value"].toInteger()
-                               this.currentLevel = eventDataMap["value"].toInteger()
-                               this.currentRate = eventDataMap["value"].toInteger()
-                               this.rate = eventDataMap["value"].toInteger()
-                               switchLevels[0].setValue(eventDataMap["value"], "level")
-                               sendEvent(eventDataMap)
-                       }
-               }
-       }
-
-       def currentValue(String deviceFeature) {
-               switchLevels[0].currentValue(deviceFeature)
-       }
-       
-       def latestValue(String deviceFeature) {
-               switchLevels[0].latestValue(deviceFeature)
+       def setLevel(int newValue) {
+               switchLevels[0].setLevel(newValue)
        }
-       
 
-       def getAt(int ix) {
-               switchLevels[ix]
+       // Methods to return values
+       def getCurrentLevel() {
+               List tmpValues = new ArrayList()
+               tmpValues.add(switchLevels[0].getCurrentLevel())
+               return tmpValues
        }
 }