PennApps Hackathon

Forum for discussion relating to the PennApps Hackathon, September 12-14, 2014.

1 Like

if you’re here come see us!

Sample app defining API endpoints, from this morning’s session:

/**
 *  PennApps Demo 2
 *
 *  Copyright 2014 Demo User
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "PennApps Demo 2",
    namespace: "demo",
    author: "Demo User",
    description: "Illustrate app creation and API integration",
    category: "",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    oauth: true)


preferences {
    section("Select device") {
        input "contact1", "capability.contactSensor", title: "Select a contact sensor", multiple: false
        input "light1", "capability.switch", title: "Select a light or outlet"
    }
}

mappings {
  path("/switch") {
    action: [
      GET: "getSwitch",
      PUT: "setSwitch"
    ]
  }
}

def installed() {
    log.debug "Installed with settings: ${settings}"

    initialize()
}

def updated() {
    log.debug "Updated with settings: ${settings}"

    unsubscribe()
    initialize()
}

def initialize() {
    subscribe contact1, "contact.open", openHandler
    subscribe contact1, "contact.closed", closedHandler
}

def openHandler(evt) {
    log.debug "$evt.name: $evt.value"
    light1.on()
}

def closedHandler(evt) {
    log.debug "$evt.name: $evt.value"
    light1.off()
}

def getSwitch() {
    light1.currentState("switch")
}

def setSwitch() {
    // params.someAttribute  ?someAttribute=XXX
    if (request.JSON.value == "on") {
        light1.on()
    }
    else if (request.JSON.value == "off") {
        light1.off()
    }
    else {
        log.error "Invalid value: $request.JSON.value"
    }
}
1 Like

Documentation links shown during the session:

Web Services Documentation
Reference Documents

1 Like