Extending REST API Example


(Cody Christian) #1

Hey guys,

I’m glad to be part of this community and hopefully able to help you guys as well.

I haven’t used Groovy much however I’m very used to working with PHP, C++, Java, Ruby, and JS (node).

Since I haven’t used it much nor am I used to this platform just yet, I need a bit of help.

I’m trying to extend the functionality of the REST API Example by providing the switch value or state in my call.

Currently you must call list and then call show to get this data for each, instead I want to combine these into a new call to cut down on the curl requests I must make from the PHP side.

Can some of you help me do this? Then hopefully I’ll grasp Groovy a bit more. Thanks

// Original Show
private show(devices, type) {
	def device = devices.find { it.id == params.id }
	if (!device) {
		httpError(404, "Device not found")
	}
	else {
		def attributeName = type == "motionSensor" ? "motion" : type
		def s = device.currentState(attributeName)
		[id: device.id, label: device.displayName, value: s?.value, unitTime: s?.date?.time, type: type]
	}
}

// Original device (list)
private device(it, type) {
	it ? [id: it.id, label: it.displayName, type: type] : null
}

// New Lister to combine previous to calls
private lister(switches) {
	switches.collect{device(it,"switch")}
    switches.each() { key, value -> show(value.id, 'switch') };
}

Original code from:
http://build.smartthings.com/blog/tutorial-creating-a-custom-rest-smartapp-endpoint/
https://support.smartthings.com/entries/21777460-Sample-Web-Services

Thanks, Cody
[Director of Engineering]


(C Chen) #2

I am also a beginner on groovy and right now I can’t test the code so I can’t guarantee that the code below would work. Anyways, you can try something like this

// Original device (list)
private device(it, type) {
    def attributeName = type == "motionSensor" ? "motion" : type
    def s = it?.currentState(attributeName)
    it ? [id: it.id, label: it.displayName, value: s?.value, unitTime: s?.date?.time, type: type] : null
}

// New Lister to combine previous to calls
private lister(switches) {
    switches.collect{device(it,"switch")}
}

(Cody Christian) #3

Thanks C. Chen, I used your device change inside of the lister function and it’s working. I however ran into issues now where the API is giving me a not found error for switches that exists. So awaiting on a response from support. Thanks again.

ps. I will probably post up my full API once its done since it will basically support all devices and their abilities for remote display/control within another app.


(Binh Nguyen) #4

Thanks C. Chen.
so you have any document ?
can you share with me ? thank so much !


(C Chen) #5

@gkmbinh - Not sure which documentation you mean. But I don’t have any beyond what’s publicly available.

For the language groovy: http://groovy.codehaus.org/Documentation

For SmartApps: click on the Documentation link in the IDE.


(mcass20) #6

@codypchristian Looking forward to seeing the API you came up with.

Cheers!
-Marc


(John) #7

These RESTful API tutorials might help you.