Compare commits

...

7 Commits

Author SHA1 Message Date
Tim Slagle
4a14421ef0 test: test 2017-02-01 14:46:54 -08:00
Vinay Rao
de1894bfbf Merge pull request #1630 from SmartThingsCommunity/staging
Rolling down staging to master
2017-01-31 13:47:45 -08:00
Jason Botello
d0f8ec87bd Merge pull request #1616 from jasonbio/plantlink-exceptions
Plant link Exceptions
2017-01-30 15:01:18 -08:00
Jason Botello
1383ab1e07 Merge pull request #1615 from jasonbio/netatmo-exceptions
Netatmo Exceptions
2017-01-30 15:01:05 -08:00
Vinay Rao
54da556c17 Merge pull request #1620 from SmartThingsCommunity/staging
Rolling down staging hotfix to master
2017-01-25 15:48:58 -08:00
Jason Botello
41adc9777a Plant link Exceptions
Adding try/catch blocks to reduce HTTP exceptions due to rate limits or
unauthorized errors.
2017-01-24 14:53:23 -08:00
Jason Botello
f334f6505a Netatmo Exceptions
Adding try/catch blocks to reduce HTTP exceptions due to rate limits or
unauthorized errors. Also updating the “devicelist” endpoint to
“getstationdata” as the former is deprecated
2017-01-24 14:30:59 -08:00
3 changed files with 174 additions and 94 deletions

View File

@@ -0,0 +1,46 @@
/**
* Stateless On/Off Button Tile
*
* Author: Ronald Gouldner
*
* Date: 2015-05-14
*/
metadata {
// Automatically generated. Make future change here.
definition (name: "Stateless On-Off Button Tile", namespace: "gouldner", author: "Ronald Gouldner") {
capability "Actuator"
capability "Switch"
capability "Sensor"
}
// simulator metadata
simulator {
}
// UI tile definitions
tiles {
standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "offReady", label: 'Off', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "onReady"
state "onReady", label: 'On', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821", nextState: "offReady"
state "off", label: 'Off', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
state "on", label: 'On', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
}
main "button"
details "button"
}
}
def parse(String description) {
}
def on() {
log.debug "Stateless On/Off Button Tile Virtual Switch ${device.name} turned on"
sendEvent(name: "switch", value: "on")
sendEvent(name: "switch", value: "onReady")
}
def off() {
log.debug "Stateless On/Off Button Tile Virtual Switch ${device.name} turned off"
sendEvent(name: "switch", value: "off")
sendEvent(name: "switch", value: "offReady")
}

View File

@@ -73,7 +73,7 @@ def authPage() {
return dynamicPage(name: "Credentials", title: "Authorize Connection", nextPage:"listDevices", uninstall: uninstallAllowed, install:false) { return dynamicPage(name: "Credentials", title: "Authorize Connection", nextPage:"listDevices", uninstall: uninstallAllowed, install:false) {
section() { section() {
paragraph "Tap below to log in to the netatmo and authorize SmartThings access." paragraph "Tap below to log in to the netatmo and authorize SmartThings access."
href url:redirectUrl, style:"embedded", required:false, title:"Connect to ${getVendorName()}:", description:description href url:redirectUrl, style:"embedded", required:false, title:"Connect to ${getVendorName()}", description:description
} }
} }
} else { } else {
@@ -146,18 +146,23 @@ def callback() {
// log.debug "PARAMS: ${params}" // log.debug "PARAMS: ${params}"
try {
httpPost(params) { resp -> httpPost(params) { resp ->
def slurper = new JsonSlurper() def slurper = new JsonSlurper()
resp.data.each { key, value -> resp.data.each { key, value ->
def data = slurper.parseText(key) def data = slurper.parseText(key)
log.debug "Data: $data"
state.refreshToken = data.refresh_token state.refreshToken = data.refresh_token
state.authToken = data.access_token state.authToken = data.access_token
//state.accessToken = data.access_token
state.tokenExpires = now() + (data.expires_in * 1000) state.tokenExpires = now() + (data.expires_in * 1000)
// log.debug "swapped token: $resp.data" // log.debug "swapped token: $resp.data"
} }
}
} catch (Exception e) {
log.debug "callback: Call failed $e"
} }
// Handle success and failure here, and render stuff accordingly // Handle success and failure here, and render stuff accordingly
@@ -387,18 +392,18 @@ def getDeviceList() {
state.deviceDetail = [:] state.deviceDetail = [:]
state.deviceState = [:] state.deviceState = [:]
apiGet("/api/devicelist") { response -> apiGet("/api/getstationsdata") { response ->
response.data.body.devices.each { value -> response.data.body.devices.each { value ->
def key = value._id def key = value._id
deviceList[key] = "${value.station_name}: ${value.module_name}" deviceList[key] = "${value.station_name}: ${value.module_name}"
state.deviceDetail[key] = value state.deviceDetail[key] = value
state.deviceState[key] = value.dashboard_data state.deviceState[key] = value.dashboard_data
value.modules.each { value2 ->
def key2 = value2._id
deviceList[key2] = "${value.station_name}: ${value2.module_name}"
state.deviceDetail[key2] = value2
state.deviceState[key2] = value2.dashboard_data
} }
response.data.body.modules.each { value ->
def key = value._id
deviceList[key] = "${state.deviceDetail[value.main_device].station_name}: ${value.module_name}"
state.deviceDetail[key] = value
state.deviceState[key] = value.dashboard_data
} }
} }
@@ -448,6 +453,7 @@ def listDevices() {
} }
def apiGet(String path, Map query, Closure callback) { def apiGet(String path, Map query, Closure callback) {
if(now() >= state.tokenExpires) { if(now() >= state.tokenExpires) {
refreshToken(); refreshToken();
} }
@@ -469,9 +475,13 @@ def apiGet(String path, Map query, Closure callback) {
log.debug "apiGet: Call failed $e" log.debug "apiGet: Call failed $e"
if(refreshToken()) { if(refreshToken()) {
log.debug "apiGet: Trying again after refreshing token" log.debug "apiGet: Trying again after refreshing token"
try {
httpGet(params) { response -> httpGet(params) { response ->
callback.call(response) callback.call(response)
} }
} catch (Exception f) {
log.debug "apiGet: Call failed $f"
}
} }
} }
} }

View File

@@ -137,6 +137,7 @@ def dock_sensor(device_serial, expected_plant_name) {
contentType: "application/json", contentType: "application/json",
] ]
log.debug "Creating new plant on myplantlink.com - ${expected_plant_name}" log.debug "Creating new plant on myplantlink.com - ${expected_plant_name}"
try {
httpPost(docking_params) { docking_response -> httpPost(docking_params) { docking_response ->
if (parse_api_response(docking_response, "Docking a link")) { if (parse_api_response(docking_response, "Docking a link")) {
if (docking_response.data.plants.size() == 0) { if (docking_response.data.plants.size() == 0) {
@@ -145,6 +146,7 @@ def dock_sensor(device_serial, expected_plant_name) {
plant_post_body_map['links_key'] = [docking_response.data.key] plant_post_body_map['links_key'] = [docking_response.data.key]
def plant_post_body_json_builder = new JsonBuilder(plant_post_body_map) def plant_post_body_json_builder = new JsonBuilder(plant_post_body_map)
plant_post_params["body"] = plant_post_body_json_builder.toString() plant_post_params["body"] = plant_post_body_json_builder.toString()
try {
httpPost(plant_post_params) { plant_post_response -> httpPost(plant_post_params) { plant_post_response ->
if(parse_api_response(plant_post_response, 'creating plant')){ if(parse_api_response(plant_post_response, 'creating plant')){
def attached_map = atomicState.attached_sensors def attached_map = atomicState.attached_sensors
@@ -152,6 +154,9 @@ def dock_sensor(device_serial, expected_plant_name) {
atomicState.attached_sensors = attached_map atomicState.attached_sensors = attached_map
} }
} }
} catch (Exception f) {
log.debug "call failed $f"
}
} else { } else {
def plant = docking_response.data.plants[0] def plant = docking_response.data.plants[0]
def attached_map = atomicState.attached_sensors def attached_map = atomicState.attached_sensors
@@ -161,6 +166,9 @@ def dock_sensor(device_serial, expected_plant_name) {
} }
} }
} }
} catch (Exception e) {
log.debug "call failed $e"
}
return true return true
} }
@@ -178,9 +186,13 @@ def checkAndUpdatePlantIfNeeded(plant, expected_plant_name){
] ]
def plant_put_body_json_builder = new JsonBuilder(plant_put_body_map) def plant_put_body_json_builder = new JsonBuilder(plant_put_body_map)
plant_put_params["body"] = plant_put_body_json_builder.toString() plant_put_params["body"] = plant_put_body_json_builder.toString()
try {
httpPut(plant_put_params) { plant_put_response -> httpPut(plant_put_params) { plant_put_response ->
parse_api_response(plant_put_response, 'updating plant name') parse_api_response(plant_put_response, 'updating plant name')
} }
} catch (Exception e) {
log.debug "call failed $e"
}
} }
} }
@@ -198,6 +210,7 @@ def moistureHandler(event){
contentType: "application/json", contentType: "application/json",
body: event.value body: event.value
] ]
try {
httpPost(measurement_post_params) { measurement_post_response -> httpPost(measurement_post_params) { measurement_post_response ->
if (parse_api_response(measurement_post_response, 'creating moisture measurement') && if (parse_api_response(measurement_post_response, 'creating moisture measurement') &&
measurement_post_response.data.size() >0){ measurement_post_response.data.size() >0){
@@ -218,6 +231,9 @@ def moistureHandler(event){
} }
} }
} }
} catch (Exception e) {
log.debug "call failed $e"
}
} }
} }
@@ -235,9 +251,13 @@ def batteryHandler(event){
contentType: "application/json", contentType: "application/json",
body: event.value body: event.value
] ]
try {
httpPost(measurement_post_params) { measurement_post_response -> httpPost(measurement_post_params) { measurement_post_response ->
parse_api_response(measurement_post_response, 'creating battery measurement') parse_api_response(measurement_post_response, 'creating battery measurement')
} }
} catch (Exception e) {
log.debug "call failed $e"
}
} }
} }
@@ -275,9 +295,13 @@ def swapToken(){
] ]
def jsonMap def jsonMap
try {
httpPost(postParams) { resp -> httpPost(postParams) { resp ->
jsonMap = resp.data jsonMap = resp.data
} }
} catch (Exception e) {
log.debug "call failed $e"
}
atomicState.refreshToken = jsonMap.refresh_token atomicState.refreshToken = jsonMap.refresh_token
atomicState.authToken = jsonMap.access_token atomicState.authToken = jsonMap.access_token