mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-09 13:21:53 +00:00
Compare commits
1 Commits
MSA-2038-2
...
MSA-2034-2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77f8fdeb2d |
@@ -56,8 +56,6 @@ metadata {
|
||||
def installed(){
|
||||
// Device-Watch simply pings if no device events received for 32min(checkInterval)
|
||||
sendEvent(name: "checkInterval", value: 2 * 15 * 60 + 2 * 60, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
|
||||
|
||||
response(refresh())
|
||||
}
|
||||
|
||||
def updated(){
|
||||
@@ -87,17 +85,11 @@ def zwaveEvent(physicalgraph.zwave.Command cmd) {
|
||||
}
|
||||
|
||||
def open() {
|
||||
delayBetween([
|
||||
zwave.switchBinaryV1.switchBinarySet(switchValue: 0x00).format(),
|
||||
zwave.switchBinaryV1.switchBinaryGet().format()
|
||||
], 500)
|
||||
zwave.switchBinaryV1.switchBinarySet(switchValue: 0x00).format()
|
||||
}
|
||||
|
||||
def close() {
|
||||
delayBetween([
|
||||
zwave.switchBinaryV1.switchBinarySet(switchValue: 0xFF).format(),
|
||||
zwave.switchBinaryV1.switchBinaryGet().format()
|
||||
], 500)
|
||||
zwave.switchBinaryV1.switchBinarySet(switchValue: 0xFF).format()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,6 +105,6 @@ def refresh() {
|
||||
|
||||
def createEventWithDebug(eventMap) {
|
||||
def event = createEvent(eventMap)
|
||||
log.debug "Event created with ${event?.name}:${event?.value} - ${event?.descriptionText}"
|
||||
log.debug "Event created with ${event?.descriptionText}"
|
||||
return event
|
||||
}
|
||||
|
||||
412
smartapps/mujica/tcp-bulbs-connect.src/tcp-bulbs-connect.groovy
Normal file
412
smartapps/mujica/tcp-bulbs-connect.src/tcp-bulbs-connect.groovy
Normal file
@@ -0,0 +1,412 @@
|
||||
/**
|
||||
* Copyright 2015 SmartThings
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
definition(
|
||||
name: "Tcp Bulbs (Connect)",
|
||||
namespace: "mujica",
|
||||
author: "SmartThings-Ule",
|
||||
description: "Connect your TCP bulbs to SmartThings using local integration. You must have a geteway with firmware ver 2",
|
||||
category: "SmartThings Labs",
|
||||
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/tcp.png",
|
||||
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/tcp@2x.png",
|
||||
singleInstance: true
|
||||
)
|
||||
|
||||
|
||||
preferences {
|
||||
page(name: "iniSettings", title: "Connect Your TCP Lights to SmartThings", content: "iniSettings")
|
||||
page(name: "chooseBulbs", title: "Choose Bulbs to Control With SmartThings", content: "bulbDiscovery")
|
||||
}
|
||||
|
||||
def iniSettings(){
|
||||
state.loadStatus = "Inactive"
|
||||
log.trace "state.loadStatus ${state.loadStatus}"
|
||||
return dynamicPage(name:"iniSettings", title:"Connect Your TCP Lights to SmartThings", nextPage:"chooseBulbs", install:false, uninstall: true) {
|
||||
section("TCP Connected Remote Credentials") {
|
||||
input "ipGateway", "text", title: "Enter TCP Gateway IP", required: true
|
||||
paragraph "Tap 'Next' after you have entered the ip of your TCP Connected Gateway.\r\n\r\nOnce your ip are accepted, SmartThings will scan your TCP installation for Bulbs."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def bulbDiscovery() {
|
||||
debugOut "bulbDiscovery()"
|
||||
//getToken()
|
||||
state.token = "1234567890"
|
||||
|
||||
if (state.loadStatus == "Inactive"){
|
||||
state.count = 0
|
||||
state.loadStatus = "Loading"
|
||||
log.trace "state.loadStatus ${state.loadStatus}"
|
||||
deviceDiscovery()
|
||||
}
|
||||
log.trace "state.count ${state.count}"
|
||||
state.count = state.count + 1
|
||||
log.trace "state.count ${state.count}"
|
||||
if(state.loadStatus == "Loaded" ){
|
||||
def options = devicesDiscovered() ?: []
|
||||
log.trace "state.loadStatus ${state.loadStatus}"
|
||||
return dynamicPage(name:"chooseBulbs", title:"", nextPage:"", install:true, uninstall: true) {
|
||||
section("Tap Below to View Device List") {
|
||||
input "selectedBulbs", "enum", required:false, title:"Select Bulb", multiple:true, options:options
|
||||
paragraph """Tap 'Done' after you have selected the desired devices."""
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if (state.count)
|
||||
|
||||
log.trace "state.loadStatus ${state.loadStatus}"
|
||||
def msg = state.count >= 3 ? "The TCP Gateway is not responding, please verify the ip address" : "Please wait while we discover your devices. Discovery can take some minutes or more, so sit back and relax! Select your device below once discovered."
|
||||
return dynamicPage(name:"chooseBulbs", title:"", nextPage:"", refreshInterval:5) {
|
||||
section(msg) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def installed() {
|
||||
debugOut "Installed with settings: ${settings}"
|
||||
|
||||
unschedule()
|
||||
unsubscribe()
|
||||
|
||||
setupBulbs()
|
||||
|
||||
def cron = "0 0/1 * * * ?"
|
||||
log.debug "schedule('$cron', syncronizeDevices)"
|
||||
schedule(cron, syncronizeDevices)
|
||||
}
|
||||
|
||||
def updated() {
|
||||
debugOut "Updated with settings: ${settings}"
|
||||
unschedule()
|
||||
setupBulbs()
|
||||
def cron = "0 0/1 * * * ?"
|
||||
log.debug "schedule('$cron', syncronizeDevices)"
|
||||
schedule(cron, syncronizeDevices)
|
||||
}
|
||||
|
||||
def uninstalled()
|
||||
{
|
||||
unschedule() //in case we have hanging runIn()'s
|
||||
}
|
||||
|
||||
private removeChildDevices(delete)
|
||||
{
|
||||
debugOut "deleting ${delete.size()} bulbs"
|
||||
debugOut "deleting ${delete}"
|
||||
delete.each {
|
||||
deleteChildDevice(it.device.deviceNetworkId)
|
||||
}
|
||||
}
|
||||
|
||||
def uninstallFromChildDevice(childDevice)
|
||||
{
|
||||
def errorMsg = "uninstallFromChildDevice was called and "
|
||||
if (!settings.selectedBulbs) {
|
||||
debugOut errorMsg += "had empty list passed in"
|
||||
return
|
||||
}
|
||||
|
||||
def dni = childDevice.device.deviceNetworkId
|
||||
|
||||
if ( !dni ) {
|
||||
debugOut errorMsg += "could not find dni of device"
|
||||
return
|
||||
}
|
||||
|
||||
def newDeviceList = settings.selectedBulbs - dni
|
||||
app.updateSetting("selectedBulbs", newDeviceList)
|
||||
debugOut errorMsg += "completed succesfully"
|
||||
}
|
||||
|
||||
|
||||
def setupBulbs() {
|
||||
debugOut "setupBulbs()"
|
||||
def bulbs = state.devices
|
||||
def deviceFile = "TCP Bulb"
|
||||
|
||||
selectedBulbs.each { did ->
|
||||
//see if this is a selected bulb and install it if not already
|
||||
def d = getChildDevice(did)
|
||||
|
||||
if(!d) {
|
||||
def newBulb = bulbs.find { (it.did) == did }
|
||||
d = addChildDevice("mujica", deviceFile, did, null, [name: "${newBulb?.name}", label: "${newBulb?.name}", completedSetup: true,"data":["model":newBulb?.model,"nodetype":newBulb?.nodetype,"node":newBulb?.node,"dni":did]])
|
||||
} else {
|
||||
infoOut "Avoid add existent device ${did}"
|
||||
}
|
||||
}
|
||||
def delete = getChildDevices().findAll { !selectedBulbs?.contains(it.deviceNetworkId) }
|
||||
removeChildDevices(delete)
|
||||
}
|
||||
|
||||
|
||||
def deviceDiscovery() {
|
||||
log.trace "deviceDiscovery()"
|
||||
def data = "<gip><version>1</version><token>${state.token}</token></gip>"
|
||||
|
||||
def Params = [
|
||||
cmd: "RoomGetCarousel",
|
||||
data: "${data}",
|
||||
fmt: "json"
|
||||
]
|
||||
|
||||
def cmd = toQueryString(Params)
|
||||
|
||||
debugOut "deviceDiscovery()"
|
||||
|
||||
apiGet(cmd,"RoomGetCarouselHandler")
|
||||
}
|
||||
|
||||
def apiGet(String data, String calledBackHandler) {
|
||||
debugOut "apiGet($data, $calledBackHandler) $ipGateway"
|
||||
sendHubCommand(new physicalgraph.device.HubAction([
|
||||
method: "GET",
|
||||
path: "/gwr/gop.php?$data",
|
||||
headers: [
|
||||
HOST: "$ipGateway:80"
|
||||
]], getNetworkId("$ipGateway","80"), [callback: calledBackHandler]))
|
||||
}
|
||||
|
||||
|
||||
void SendCommandHandler(physicalgraph.device.HubResponse hubResponse){
|
||||
debugOut "SendCommandHandler($hubResponse)"
|
||||
debugOut "hubResponse.body ${hubResponse.body}"
|
||||
}
|
||||
void RoomGetCarouselHandler(physicalgraph.device.HubResponse hubResponse){
|
||||
debugOut "RoomGetCarouselHandler($hubResponse)"
|
||||
def bodyXml
|
||||
|
||||
if (hubResponse?.body?.contains("<gip>"))
|
||||
{ // description.xml response (application/xml)
|
||||
debugOut "body contains xml"
|
||||
bodyXml = new XmlSlurper().parseText(hubResponse.body)
|
||||
debugOut "bodyXml $bodyXml"
|
||||
}
|
||||
|
||||
|
||||
def rooms = ""
|
||||
def devices = []
|
||||
def deviceList = []
|
||||
|
||||
|
||||
rooms = bodyXml.room
|
||||
|
||||
|
||||
debugOut "rooms ${rooms[1]}"
|
||||
|
||||
rooms.each({
|
||||
devices = it.device
|
||||
debugOut "it.device ${it.device}"
|
||||
def roomName = it.name
|
||||
debugOut "roomName = ${it.name}"
|
||||
debugOut "devices[1] ${devices[1]}"
|
||||
debugOut "devices[1] != null"
|
||||
def roomId = it?.rid
|
||||
debugOut "Room Device Data: did:${roomId} roomName:${roomName}"
|
||||
devices.each({
|
||||
debugOut "Bulb Device Data: did:${it?.did} room:${roomName} BulbName:${it?.name}"
|
||||
deviceList += ["name" : "${roomName} ${it?.name}", "did" : "${it?.did}", "type" : "${devices?.type}", "node" : "${devices?.node}", "nodetype" : "${devices?.nodetype}", "model" : "${devices?.prodmodel}"]
|
||||
})
|
||||
})
|
||||
|
||||
devices = ["devices" : deviceList]
|
||||
debugOut "devices $devices"
|
||||
state.devices = devices.devices
|
||||
state.loadStatus = "Loaded"
|
||||
}
|
||||
|
||||
def getDevices()
|
||||
{
|
||||
state.devices = state.devices ?: [:]
|
||||
}
|
||||
|
||||
void RoomGetCarouselUpdateHandler(physicalgraph.device.HubResponse hubResponse){
|
||||
debugOut "RoomGetCarouselUpdateHandler($hubResponse)"
|
||||
|
||||
|
||||
debugOut "msg ${parseLanMessage(hubResponse.body)}"
|
||||
|
||||
def bodyXml
|
||||
|
||||
if (hubResponse?.body?.contains("<gip>"))
|
||||
{
|
||||
debugOut "body contains xml"
|
||||
bodyXml = new XmlSlurper().parseText(hubResponse.body)
|
||||
}
|
||||
|
||||
|
||||
def rooms = ""
|
||||
def devices = []
|
||||
def deviceList = []
|
||||
|
||||
rooms = bodyXml.room
|
||||
|
||||
rooms.each({
|
||||
devices = it.device
|
||||
devices.each({
|
||||
def dni = it.did.text()
|
||||
def bulb = getChildDevice(dni)
|
||||
if ( bulb ){
|
||||
def power = it.power ? it.power.text() as float :0
|
||||
sendEvent( dni, [name: "power", value: power*1000] )
|
||||
if (( it.state.text() == "1" ) && ( bulb?.currentValue("switch") != "on" ))
|
||||
sendEvent( dni, [name: "switch",value:"on"] )
|
||||
|
||||
if (( it.state.text() == "0" ) && ( bulb?.currentValue("switch") != "off" ))
|
||||
sendEvent( dni, [name: "switch",value:"off"] )
|
||||
|
||||
if ( it.level.text() != bulb?.currentValue("level")) {
|
||||
sendEvent( dni, [name: "level",value: "${it.level.text()}"] )
|
||||
sendEvent( dni, [name: "setLevel",value: "${it.level.text()}"] )
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Map devicesDiscovered() {
|
||||
def devices = state.devices
|
||||
def map = [:]
|
||||
if (devices instanceof java.util.Map) {
|
||||
devices.each {
|
||||
def value = "${it?.name}"
|
||||
def key = it?.did
|
||||
map["${key}"] = value
|
||||
}
|
||||
} else { //backwards compatable
|
||||
devices.each {
|
||||
def value = "${it?.name}"
|
||||
def key = it?.did
|
||||
map["${key}"] = value
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
def getToken() {
|
||||
|
||||
state.token = "1234567890"
|
||||
}
|
||||
|
||||
String toQueryString(Map m) {
|
||||
return m.collect { k, v -> "${k}=${URLEncoder.encode(v.toString())}" }.sort().join("&")
|
||||
}
|
||||
|
||||
def syncronizeDevices() {
|
||||
poll(null)
|
||||
}
|
||||
|
||||
def getNetworkId(ipaddr, port) {
|
||||
"${ipaddr.tokenize('.').collect {String.format('%02X', it.toInteger())}.join()}:${String.format('%04X', port.toInteger())}"
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
Child Device Call In Methods
|
||||
**************************************************************************/
|
||||
def on(childDevice) {
|
||||
|
||||
def dni = childDevice.device.deviceNetworkId
|
||||
def data = ""
|
||||
def cmd = ""
|
||||
|
||||
data = "<gip><version>1</version><token>$state.token</token><did>${dni}</did><type>power</type><value>1</value></gip>"
|
||||
cmd = "DeviceSendCommand"
|
||||
|
||||
def qParams = [
|
||||
cmd: cmd,
|
||||
data: "${data}",
|
||||
fmt: "json"
|
||||
]
|
||||
|
||||
cmd = toQueryString(qParams)
|
||||
apiGet(cmd,"SendCommandHandler" )
|
||||
}
|
||||
|
||||
def off(childDevice) {
|
||||
|
||||
def dni = childDevice.device.deviceNetworkId
|
||||
def data = ""
|
||||
def cmd = ""
|
||||
|
||||
data = "<gip><version>1</version><token>$state.token</token><did>${dni}</did><type>power</type><value>0</value></gip>"
|
||||
cmd = "DeviceSendCommand"
|
||||
|
||||
def qParams = [
|
||||
cmd: cmd,
|
||||
data: "${data}",
|
||||
fmt: "json"
|
||||
]
|
||||
|
||||
cmd = toQueryString(qParams)
|
||||
apiGet(cmd,"SendCommandHandler")
|
||||
|
||||
}
|
||||
|
||||
def setLevel(childDevice, value) {
|
||||
debugOut "setLevel request from child device"
|
||||
|
||||
def dni = childDevice.device.deviceNetworkId
|
||||
def data = ""
|
||||
def cmd = ""
|
||||
|
||||
data = "<gip><version>1</version><token>${state.token}</token><did>${dni}</did><type>level</type><value>${value}</value></gip>"
|
||||
cmd = "DeviceSendCommand"
|
||||
|
||||
|
||||
def qParams = [
|
||||
cmd: cmd,
|
||||
data: "${data}",
|
||||
fmt: "json"
|
||||
]
|
||||
|
||||
cmd = toQueryString(qParams)
|
||||
|
||||
apiGet(cmd,"SendCommandHandler")
|
||||
}
|
||||
|
||||
def poll(childDevice) {
|
||||
infoOut "poll()"
|
||||
def eventTime = new Date().time
|
||||
if ((state.lastPollTime ?:0) + 10000 <= eventTime ){
|
||||
state.lastPollTime = new Date().time
|
||||
def Params = [
|
||||
cmd: "RoomGetCarousel",
|
||||
data: "<gip><version>1</version><token>${state.token}</token></gip>",
|
||||
fmt: "json"
|
||||
]
|
||||
def cmd = toQueryString(Params)
|
||||
apiGet(cmd,"RoomGetCarouselUpdateHandler")
|
||||
}else{
|
||||
infoOut "Multiple poll requests avoided"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
Msg Methods
|
||||
**************************************************************************/
|
||||
|
||||
def debugOut(msg) {
|
||||
//log.debug msg
|
||||
}
|
||||
|
||||
def traceOut(msg) {
|
||||
log.trace msg
|
||||
}
|
||||
|
||||
def infoOut(msg) {
|
||||
log.info msg
|
||||
}
|
||||
@@ -1,447 +0,0 @@
|
||||
/**
|
||||
* JSON Complete API
|
||||
*
|
||||
* Copyright 2017 Paul Lovelace
|
||||
*
|
||||
*/
|
||||
definition(
|
||||
name: "JSON Complete API",
|
||||
namespace: "smartthings",
|
||||
author: "Ashok Malhotra",
|
||||
description: "API for JSON with complete set of devices",
|
||||
category: "SmartThings Labs",
|
||||
iconUrl: "https://raw.githubusercontent.com/pdlove/homebridge-smartthings/master/smartapps/JSON%401.png",
|
||||
iconX2Url: "https://raw.githubusercontent.com/pdlove/homebridge-smartthings/master/smartapps/JSON%402.png",
|
||||
iconX3Url: "https://raw.githubusercontent.com/pdlove/homebridge-smartthings/master/smartapps/JSON%403.png",
|
||||
oauth: true)
|
||||
|
||||
|
||||
preferences {
|
||||
page(name: "copyConfig")
|
||||
}
|
||||
|
||||
//When adding device groups, need to add here
|
||||
def copyConfig() {
|
||||
if (!state.accessToken) {
|
||||
createAccessToken()
|
||||
}
|
||||
dynamicPage(name: "copyConfig", title: "Configure Devices", install:true, uninstall:true) {
|
||||
section("Select devices to include in the /devices API call") {
|
||||
paragraph "Version 0.5.5"
|
||||
input "deviceList", "capability.refresh", title: "Most Devices", multiple: true, required: false
|
||||
input "sensorList", "capability.sensor", title: "Sensor Devices", multiple: true, required: false
|
||||
input "switchList", "capability.switch", title: "All Switches", multiple: true, required: false
|
||||
//paragraph "Devices Selected: ${deviceList ? deviceList?.size() : 0}\nSensors Selected: ${sensorList ? sensorList?.size() : 0}\nSwitches Selected: ${switchList ? switchList?.size() : 0}"
|
||||
}
|
||||
section("Configure Pubnub") {
|
||||
input "pubnubSubscribeKey", "text", title: "PubNub Subscription Key", multiple: false, required: false
|
||||
input "pubnubPublishKey", "text", title: "PubNub Publish Key", multiple: false, required: false
|
||||
input "subChannel", "text", title: "Channel (Can be anything)", multiple: false, required: false
|
||||
}
|
||||
section() {
|
||||
paragraph "View this SmartApp's configuration to use it in other places."
|
||||
href url:"${apiServerUrl("/api/smartapps/installations/${app.id}/config?access_token=${state.accessToken}")}", style:"embedded", required:false, title:"Config", description:"Tap, select, copy, then click \"Done\""
|
||||
}
|
||||
|
||||
section() {
|
||||
paragraph "View the JSON generated from the installed devices."
|
||||
href url:"${apiServerUrl("/api/smartapps/installations/${app.id}/devices?access_token=${state.accessToken}")}", style:"embedded", required:false, title:"Device Results", description:"View accessories JSON"
|
||||
}
|
||||
section() {
|
||||
paragraph "Enter the name you would like shown in the smart app list"
|
||||
label title:"SmartApp Label (optional)", required: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def renderDevices() {
|
||||
def deviceData = []
|
||||
deviceList.each {
|
||||
try {
|
||||
deviceData << [name: it.displayName,
|
||||
basename: it.name,
|
||||
deviceid: it.id,
|
||||
status: it.status,
|
||||
manufacturerName: it.getManufacturerName(),
|
||||
modelName: it.getModelName(),
|
||||
lastTime: it.getLastActivity(),
|
||||
capabilities: deviceCapabilityList(it),
|
||||
commands: deviceCommandList(it),
|
||||
attributes: deviceAttributeList(it)
|
||||
]
|
||||
} catch (e) {
|
||||
log.error("Error Occurred Parsing Device "+it.displayName+", Error " + e)
|
||||
}
|
||||
}
|
||||
sensorList.each {
|
||||
try {
|
||||
deviceData << [name: it.displayName,
|
||||
basename: it.name,
|
||||
deviceid: it.id,
|
||||
status: it.status,
|
||||
manufacturerName: it.getManufacturerName(),
|
||||
modelName: it.getModelName(),
|
||||
lastTime: it.getLastActivity(),
|
||||
capabilities: deviceCapabilityList(it),
|
||||
commands: deviceCommandList(it),
|
||||
attributes: deviceAttributeList(it)
|
||||
]
|
||||
} catch (e) {
|
||||
log.error("Error Occurred Parsing Device "+it.displayName+", Error " + e)
|
||||
}
|
||||
}
|
||||
switchList.each {
|
||||
try {
|
||||
deviceData << [name: it.displayName,
|
||||
basename: it.name,
|
||||
deviceid: it.id,
|
||||
status: it.status,
|
||||
manufacturerName: it.getManufacturerName(),
|
||||
modelName: it.getModelName(),
|
||||
lastTime: it.getLastActivity(),
|
||||
capabilities: deviceCapabilityList(it),
|
||||
commands: deviceCommandList(it),
|
||||
attributes: deviceAttributeList(it)
|
||||
]
|
||||
} catch (e) {
|
||||
log.error("Error Occurred Parsing Device "+it.displayName+", Error " + e)
|
||||
}
|
||||
}
|
||||
return deviceData
|
||||
}
|
||||
|
||||
def findDevice(paramid) {
|
||||
def device = deviceList.find { it.id == paramid }
|
||||
if (device) return device
|
||||
device = sensorList.find { it.id == paramid }
|
||||
if (device) return device
|
||||
device = switchList.find { it.id == paramid }
|
||||
|
||||
return device
|
||||
}
|
||||
//No more individual device group definitions after here.
|
||||
|
||||
|
||||
def installed() {
|
||||
log.debug "Installed with settings: ${settings}"
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "Updated with settings: ${settings}"
|
||||
unsubscribe()
|
||||
initialize()
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
if(!state.accessToken) {
|
||||
createAccessToken()
|
||||
}
|
||||
registerAll()
|
||||
state.subscriptionRenewed = 0
|
||||
subscribe(location, null, HubResponseEvent, [filterEvents:false])
|
||||
log.debug "0.5.5"
|
||||
}
|
||||
|
||||
def authError() {
|
||||
[error: "Permission denied"]
|
||||
}
|
||||
def renderConfig() {
|
||||
def configJson = new groovy.json.JsonOutput().toJson([
|
||||
description: "JSON API",
|
||||
platforms: [
|
||||
[
|
||||
platform: "SmartThings",
|
||||
name: "SmartThings",
|
||||
app_url: apiServerUrl("/api/smartapps/installations/"),
|
||||
app_id: app.id,
|
||||
access_token: state.accessToken
|
||||
]
|
||||
],
|
||||
])
|
||||
|
||||
def configString = new groovy.json.JsonOutput().prettyPrint(configJson)
|
||||
render contentType: "text/plain", data: configString
|
||||
}
|
||||
def renderLocation() {
|
||||
[
|
||||
latitude: location.latitude,
|
||||
longitude: location.longitude,
|
||||
mode: location.mode,
|
||||
name: location.name,
|
||||
temperature_scale: location.temperatureScale,
|
||||
zip_code: location.zipCode,
|
||||
hubIP: location.hubs[0].localIP,
|
||||
smartapp_version: '0.5.5'
|
||||
]
|
||||
}
|
||||
def CommandReply(statusOut, messageOut) {
|
||||
def replyData =
|
||||
[
|
||||
status: statusOut,
|
||||
message: messageOut
|
||||
]
|
||||
|
||||
def replyJson = new groovy.json.JsonOutput().toJson(replyData)
|
||||
render contentType: "application/json", data: replyJson
|
||||
}
|
||||
def deviceCommand() {
|
||||
log.info("Command Request")
|
||||
def device = findDevice(params.id)
|
||||
def command = params.command
|
||||
|
||||
if (!device) {
|
||||
log.error("Device Not Found")
|
||||
CommandReply("Failure", "Device Not Found")
|
||||
} else if (!device.hasCommand(command)) {
|
||||
log.error("Device "+device.displayName+" does not have the command "+command)
|
||||
CommandReply("Failure", "Device "+device.displayName+" does not have the command "+command)
|
||||
} else {
|
||||
def value1 = request.JSON?.value1
|
||||
def value2 = request.JSON?.value2
|
||||
try {
|
||||
if (value2) {
|
||||
device."$command"(value1,value2)
|
||||
} else if (value1) {
|
||||
device."$command"(value1)
|
||||
} else {
|
||||
device."$command"()
|
||||
}
|
||||
log.info("Command Successful for Device "+device.displayName+", Command "+command)
|
||||
CommandReply("Success", "Device "+device.displayName+", Command "+command)
|
||||
} catch (e) {
|
||||
log.error("Error Occurred For Device "+device.displayName+", Command "+command)
|
||||
CommandReply("Failure", "Error Occurred For Device "+device.displayName+", Command "+command)
|
||||
}
|
||||
}
|
||||
}
|
||||
def deviceAttribute() {
|
||||
def device = findDevice(params.id)
|
||||
def attribute = params.attribute
|
||||
if (!device) {
|
||||
httpError(404, "Device not found")
|
||||
} else {
|
||||
def currentValue = device.currentValue(attribute)
|
||||
[currentValue: currentValue]
|
||||
}
|
||||
}
|
||||
def deviceQuery() {
|
||||
def device = findDevice(params.id)
|
||||
if (!device) {
|
||||
device = null
|
||||
httpError(404, "Device not found")
|
||||
}
|
||||
|
||||
if (result) {
|
||||
def jsonData =
|
||||
[
|
||||
name: device.displayName,
|
||||
deviceid: device.id,
|
||||
capabilities: deviceCapabilityList(device),
|
||||
commands: deviceCommandList(device),
|
||||
attributes: deviceAttributeList(device)
|
||||
]
|
||||
def resultJson = new groovy.json.JsonOutput().toJson(jsonData)
|
||||
render contentType: "application/json", data: resultJson
|
||||
}
|
||||
}
|
||||
def deviceCapabilityList(device) {
|
||||
def i=0
|
||||
device.capabilities.collectEntries { capability->
|
||||
[
|
||||
(capability.name):1
|
||||
]
|
||||
}
|
||||
}
|
||||
def deviceCommandList(device) {
|
||||
def i=0
|
||||
device.supportedCommands.collectEntries { command->
|
||||
[
|
||||
(command.name): (command.arguments)
|
||||
]
|
||||
}
|
||||
}
|
||||
def deviceAttributeList(device) {
|
||||
device.supportedAttributes.collectEntries { attribute->
|
||||
try {
|
||||
[
|
||||
(attribute.name): device.currentValue(attribute.name)
|
||||
]
|
||||
} catch(e) {
|
||||
[
|
||||
(attribute.name): null
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
def getAllData() {
|
||||
//Since we're about to send all of the data, we'll count this as a subscription renewal and clear out pending changes.
|
||||
state.subscriptionRenewed = now()
|
||||
state.devchanges = []
|
||||
|
||||
|
||||
def deviceData =
|
||||
[ location: renderLocation(),
|
||||
deviceList: renderDevices() ]
|
||||
def deviceJson = new groovy.json.JsonOutput().toJson(deviceData)
|
||||
render contentType: "application/json", data: deviceJson
|
||||
}
|
||||
def startSubscription() {
|
||||
//This simply registers the subscription.
|
||||
state.subscriptionRenewed = now()
|
||||
def deviceJson = new groovy.json.JsonOutput().toJson([status: "Success"])
|
||||
render contentType: "application/json", data: deviceJson
|
||||
}
|
||||
def endSubscription() {
|
||||
//Because it takes too long to register for an api command, we don't actually unregister.
|
||||
//We simply blank the devchanges and change the subscription renewal to two hours ago.
|
||||
state.devchanges = []
|
||||
state.subscriptionRenewed = 0
|
||||
def deviceJson = new groovy.json.JsonOutput().toJson([status: "Success"])
|
||||
render contentType: "application/json", data: deviceJson
|
||||
}
|
||||
def registerAll() {
|
||||
//This has to be done at startup because it takes too long for a normal command.
|
||||
log.debug "Registering All Events"
|
||||
state.devchanges = []
|
||||
registerChangeHandler(deviceList)
|
||||
registerChangeHandler(sensorList)
|
||||
registerChangeHandler(switchList)
|
||||
}
|
||||
def registerChangeHandler(myList) {
|
||||
myList.each { myDevice ->
|
||||
def theAtts = myDevice.supportedAttributes
|
||||
theAtts.each {att ->
|
||||
subscribe(myDevice, att.name, changeHandler)
|
||||
log.debug "Registering ${myDevice.displayName}.${att.name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
def changeHandler(evt) {
|
||||
//Send to Pubnub if we need to.
|
||||
if (pubnubPublishKey!=null) {
|
||||
def deviceData = [device: evt.deviceId, attribute: evt.name, value: evt.value, date: evt.date]
|
||||
def changeJson = new groovy.json.JsonOutput().toJson(deviceData)
|
||||
def changeData = URLEncoder.encode(changeJson)
|
||||
def uri = "http://pubsub.pubnub.com/publish/${pubnubPublishKey}/${pubnubSubscribeKey}/0/${subChannel}/0/${changeData}"
|
||||
log.debug "${uri}"
|
||||
httpGet(uri)
|
||||
}
|
||||
|
||||
if (state.directIP!="") {
|
||||
//Send Using the Direct Mechanism
|
||||
def deviceData = [device: evt.deviceId, attribute: evt.name, value: evt.value, date: evt.date]
|
||||
//How do I control the port?!?
|
||||
log.debug "Sending Update to ${state.directIP}:${state.directPort}"
|
||||
def result = new physicalgraph.device.HubAction(
|
||||
method: "GET",
|
||||
path: "/update",
|
||||
headers: [
|
||||
HOST: "${state.directIP}:${state.directPort}",
|
||||
change_device: evt.deviceId,
|
||||
change_attribute: evt.name,
|
||||
change_value: evt.value,
|
||||
change_date: evt.date
|
||||
]
|
||||
)
|
||||
sendHubCommand(result)
|
||||
}
|
||||
|
||||
//Only add to the state's devchanges if the endpoint has renewed in the last 10 minutes.
|
||||
if (state.subscriptionRenewed>(now()-(1000*60*10))) {
|
||||
if (evt.isStateChange()) {
|
||||
state.devchanges << [device: evt.deviceId, attribute: evt.name, value: evt.value, date: evt.date]
|
||||
}
|
||||
} else if (state.subscriptionRenewed>0) { //Otherwise, clear it
|
||||
log.debug "Endpoint Subscription Expired. No longer storing changes for devices."
|
||||
state.devchanges=[]
|
||||
state.subscriptionRenewed=0
|
||||
}
|
||||
}
|
||||
def getChangeEvents() {
|
||||
//Store the changes so we can swap it out very quickly and eliminate the possibility of losing any.
|
||||
//This is mainly to make this thread safe because I'm willing to bet that a change event can fire
|
||||
//while generating/sending the JSON.
|
||||
def oldchanges = state.devchanges
|
||||
state.devchanges=[]
|
||||
state.subscriptionRenewed = now()
|
||||
if (oldchanges.size()==0) {
|
||||
def deviceJson = new groovy.json.JsonOutput().toJson([status: "None"])
|
||||
render contentType: "application/json", data: deviceJson
|
||||
} else {
|
||||
def changeJson = new groovy.json.JsonOutput().toJson([status: "Success", attributes:oldchanges])
|
||||
render contentType: "application/json", data: changeJson
|
||||
}
|
||||
}
|
||||
def enableDirectUpdates() {
|
||||
log.debug("Command Request")
|
||||
state.directIP = params.ip
|
||||
state.directPort = params.port
|
||||
log.debug("Trying ${state.directIP}:${state.directPort}")
|
||||
def result = new physicalgraph.device.HubAction(
|
||||
method: "GET",
|
||||
path: "/initial",
|
||||
headers: [
|
||||
HOST: "${state.directIP}:${state.directPort}"
|
||||
],
|
||||
query: deviceData
|
||||
)
|
||||
sendHubCommand(result)
|
||||
}
|
||||
|
||||
def HubResponseEvent(evt) {
|
||||
log.debug(evt.description)
|
||||
}
|
||||
|
||||
def locationHandler(evt) {
|
||||
def description = evt.description
|
||||
def hub = evt?.hubId
|
||||
|
||||
log.debug "cp desc: " + description
|
||||
if (description.count(",") > 4)
|
||||
{
|
||||
def bodyString = new String(description.split(',')[5].split(":")[1].decodeBase64())
|
||||
log.debug(bodyString)
|
||||
}
|
||||
}
|
||||
|
||||
def getSubscriptionService() {
|
||||
def replyData =
|
||||
[
|
||||
pubnub_publishkey: pubnubPublishKey,
|
||||
pubnub_subscribekey: pubnubSubscribeKey,
|
||||
pubnub_channel: subChannel
|
||||
]
|
||||
|
||||
def replyJson = new groovy.json.JsonOutput().toJson(replyData)
|
||||
render contentType: "application/json", data: replyJson
|
||||
}
|
||||
|
||||
mappings {
|
||||
if (!params.access_token || (params.access_token && params.access_token != state.accessToken)) {
|
||||
path("/devices") { action: [GET: "authError"] }
|
||||
path("/config") { action: [GET: "authError"] }
|
||||
path("/location") { action: [GET: "authError"] }
|
||||
path("/:id/command/:command") { action: [POST: "authError"] }
|
||||
path("/:id/query") { action: [GET: "authError"] }
|
||||
path("/:id/attribute/:attribute") { action: [GET: "authError"] }
|
||||
path("/subscribe") { action: [GET: "authError"] }
|
||||
path("/getUpdates") { action: [GET: "authError"] }
|
||||
path("/unsubscribe") { action: [GET: "authError"] }
|
||||
path("/startDirect/:ip/:port") { action: [GET: "authError"] }
|
||||
path("/getSubcriptionService") { action: [GET: "authError"] }
|
||||
|
||||
} else {
|
||||
path("/devices") { action: [GET: "getAllData"] }
|
||||
path("/config") { action: [GET: "renderConfig"] }
|
||||
path("/location") { action: [GET: "renderLocation"] }
|
||||
path("/:id/command/:command") { action: [POST: "deviceCommand"] }
|
||||
path("/:id/query") { action: [GET: "deviceQuery"] }
|
||||
path("/:id/attribute/:attribute") { action: [GET: "deviceAttribute"] }
|
||||
path("/subscribe") { action: [GET: "startSubscription"] }
|
||||
path("/getUpdates") { action: [GET: "getChangeEvents"] }
|
||||
path("/unsubscribe") { action: [GET: "endSubscription"] }
|
||||
path("/startDirect/:ip/:port") { action: [GET: "enableDirectUpdates"] }
|
||||
path("/getSubcriptionService") { action: [GET: "getSubscriptionService"] }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user