Update lighting-director.groovy
[smartapps.git] / third-party / JSON.groovy
1 /**
2  *  JSON
3  *
4  *  Copyright 2015 Jesse Newland
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: "JSON API",
18     namespace: "jnewland",
19     author: "Jesse Newland",
20     description: "A JSON API for SmartThings",
21     category: "SmartThings Labs",
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: true)
26
27
28 def installed() {
29     initialize()
30 }
31
32 def updated() {
33     unsubscribe()
34     initialize()
35 }
36
37 def initialize() {
38     if (!state.accessToken) {
39         createAccessToken()
40     }
41 }
42
43 preferences {
44     page(name: "copyConfig")
45 }
46
47 def copyConfig() {
48     if (!state.accessToken) {
49         createAccessToken()
50     }
51     dynamicPage(name: "copyConfig", title: "Config", install:true) {
52         section("Select devices to include in the /devices API call") {
53             input "switches", "capability.switch", title: "Switches", multiple: true, required: false
54             input "hues", "capability.colorControl", title: "Hues", multiple: true, required: false
55         }
56
57         section() {
58             paragraph "View this SmartApp's configuration to use it in other places."
59             href url:"https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/config?access_token=${state.accessToken}", style:"embedded", required:false, title:"Config", description:"Tap, select, copy, then click \"Done\""
60         }
61
62         section() {
63             href url:"https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/devices?access_token=${state.accessToken}", style:"embedded", required:false, title:"Debug", description:"View accessories JSON"
64         }
65     }
66 }
67
68 def renderConfig() {
69     def configJson = new groovy.json.JsonOutput().toJson([
70         description: "JSON API",
71         platforms: [
72             [
73                 platform: "SmartThings",
74                 name: "SmartThings",
75                 app_id:        app.id,
76                 access_token:  state.accessToken
77             ]
78         ],
79     ])
80
81     def configString = new groovy.json.JsonOutput().prettyPrint(configJson)
82     render contentType: "text/plain", data: configString
83 }
84
85 def deviceCommandMap(device, type) {
86   device.supportedCommands.collectEntries { command->
87       def commandUrl = "https://graph.api.smartthings.com/api/smartapps/installations/${app.id}/${type}/${device.id}/command/${command.name}?access_token=${state.accessToken}"
88       [
89         (command.name): commandUrl
90       ]
91   }
92 }
93
94 def authorizedDevices() {
95     [
96         switches: switches,
97         hues: hues
98     ]
99 }
100
101 def renderDevices() {
102     def deviceData = authorizedDevices().collectEntries { devices->
103         [
104             (devices.key): devices.value.collect { device->
105                 [
106                     name: device.displayName,
107                     commands: deviceCommandMap(device, devices.key)
108                 ]
109             }
110         ]
111     }
112     def deviceJson    = new groovy.json.JsonOutput().toJson(deviceData)
113     def deviceString  = new groovy.json.JsonOutput().prettyPrint(deviceJson)
114     render contentType: "application/json", data: deviceString
115 }
116
117 def deviceCommand() {
118   def device  = authorizedDevices()[params.type].find { it.id == params.id }
119   def command = params.command
120   if (!device) {
121       httpError(404, "Device not found")
122   } else {
123       if (params.value) {
124         device."$command"(params.value)
125       } else {
126         device."$command"()
127       }
128   }
129 }
130
131 mappings {
132     if (!params.access_token || (params.access_token && params.access_token != state.accessToken)) {
133         path("/devices")                      { action: [GET: "authError"] }
134         path("/config")                       { action: [GET: "authError"] }
135         path("/:type/:id/command/:command")   { action: [PUT: "authError"] }
136     } else {
137         path("/devices")                      { action: [GET: "renderDevices"]  }
138         path("/config")                       { action: [GET: "renderConfig"]  }
139         path("/:type/:id/command/:command")   { action: [PUT: "deviceCommand"] }
140     }
141 }
142
143 def authError() {
144     [error: "Permission denied"]
145 }