Monitor Mode And Call External API When Mode Changes


(Robert Hamm) #1

Hello,

I am creating a smart app that will monitor Home and Away modes and when one changes it will send a web request with an AuthToken to our web service and arm/disarm cameras.

So far I have the Authentication flow setup and working in my smart app and can authenticate when testing using my android phone.

The only other code in my smart app is the following:

def initialize() {
    log.debug "Subscribing to mode changes..."
    subscribe(location, "mode", modeChangeHandler)
}

def modeChangeHandler(evt) {
    if (evt.value == "Home")
    {
    // Do stuff you want when mode changes to "Home"
    log.debug "modeChangeHandler called: Home - authToken is " + state.authToken
        def params = [                
            uri: "https://xxx.com",
            path: "/SmartThings/Arm",
            body: [
                    accessToken: state.authToken
                ]
        ]

        try {
            httpPostJson(params) { resp ->
                resp.headers.each {
                   log.debug "${it.name} : ${it.value}"
                }
                log.debug "response contentType: ${resp.contentType}"
                log.debug "response data: ${resp.data}"
            }
        } catch (e) {
            log.error "something went wrong: $e"
        }
    }
    if (evt.value == "Away")
    {
    //do stuff
    // Do stuff you want when mode changes to "Home"
    log.debug "modeChangeHandler called: Away - authToken is " + state.authToken
        def params = [                
            uri: "https://xxx.com",
            path: "/SmartThings/Disarm",
            body: [
                    accessToken: state.authToken
                ]
        ]

        try {
            httpPostJson(params) { resp ->
                resp.headers.each {
                   log.debug "${it.name} : ${it.value}"
                }
                log.debug "response contentType: ${resp.contentType}"
                log.debug "response data: ${resp.data}"
            }
        } catch (e) {
            log.error "something went wrong: $e"
        }
    }
}

Is there any way to test this without actual devices in the simulator? Is what I am trying to achieve going to work?


(Robert Hamm) #2

I was able to hook up a hub at home and test the auth flow and web requests to our api. All is working.


#3

Did you release app?

Wanted to monitor changes to ‘mode’ and what caused the change (e.g. Routine x, Smartthings app mode change by user, mode change by Y app, mode change by webcore, etc) not sure if that’s possible??