javagroovysmartthings

How classes for devices and sensors are implemented in Samsung SmartThings applications?


we want to implement an infrastructure for Samsung Groovy SmartThings. The major part for the infrastructure of course is to implement different classes for each device with their corresponding methods. For example for locks' devices, we assumed that we have a lock class with methods lock() and unlock(). The problem here is that we have this part of code in one of the SmartThings applications which are in Groovy:

def presence(evt)
{
    if (evt.value == "present") {
            //Somecode
            lock1.unlock()
    }
    else {
            //Somecode
            lock1.lock()
    }
}

So most probably, lock1 is the object for class lock, and lock() and unlock() are methods for that class. Here is the thing: Using lock1[0].unlock() command unlocks the door lock number #0, but using lock1.unlock() command unlocks all the door locks.

The question here is that how the class is created? If lock1 is a list of objects, how we can have a command like lock1.unlock().

The point here is that both of the objects should have the same name lock1, and both of the methods are the same method named lock().

Thank you in advance.


Solution

  • You have 2 options here:

    1) use Groovy's spread operator to call the method on every element of the list:

    List lock1 = [.....]
    lock1.*lock()
    

    2) Make a Lock class extend or contain a List of elements, and add a class-level method to it:

    class Lock {
      List locks
    
      def lock() {
        locks.*lock()
      }
    
      // this method allows for calls like lock1[42].lock()
      def getAt( int ix ) { 
        locks[ ix ] 
      } 
    }
    

    Actually, inheritance is BAD for IoT devices.