Update influxdb-logger.groovy
[smartapps.git] / official / web-services-smartapp.groovy
1 /**
2  *  Web Services Tutorial
3  *
4  *  Copyright 2015 SmartThings
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Web Services Tutorial",
18     namespace: "smartthings",
19     author: "SmartThings",
20     description: "web services tutorial",
21     category: "",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
24     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
25     oauth: [displayName: "web services tutorial ", displayLink: "http://localhost:4567"])
26
27
28 preferences {
29   section ("Allow external service to control these things...") {
30     input "switches", "capability.switch", multiple: true, required: true
31   }
32 }
33
34 mappings {
35   path("/switches") {
36     action: [
37       GET: "listSwitches"
38     ]
39   }
40   path("/switches/:command") {
41     action: [
42       PUT: "updateSwitches"
43     ]
44   }
45 }
46
47 // returns a list like
48 // [[name: "kitchen lamp", value: "off"], [name: "bathroom", value: "on"]]
49 def listSwitches() {
50
51     def resp = []
52     switches.each {
53         resp << [name: it.displayName, value: it.currentValue("switch")]
54     }
55     return resp
56 }
57
58 void updateSwitches() {
59     // use the built-in request object to get the command parameter
60     def command = params.command
61
62     // all switches have the comand
63     // execute the command on all switches
64     // (note we can do this on the array - the command will be invoked on every element
65     switch(command) {
66         case "on":
67             switches.on()
68             break
69         case "off":
70             switches.off()
71             break
72         default:
73             httpError(400, "$command is not a valid command for all switches specified")
74     }
75
76 }
77 def installed() {}
78
79 def updated() {}