forked from SmartThingsCommunity/SmartThingsPublic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request SmartThingsCommunity#431 from SmartThingsCommunity…
…/MSA-794-2 MSA-794: Simple Control
- Loading branch information
Showing
4 changed files
with
1,483 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
devicetypes/roomieremote-agent/simple-sync.src/simple-sync.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Simple Sync | ||
* | ||
* Copyright 2015 Roomie Remote, Inc. | ||
* | ||
* Date: 2015-09-22 | ||
*/ | ||
metadata | ||
{ | ||
definition (name: "Simple Sync", namespace: "roomieremote-agent", author: "Roomie Remote, Inc.") | ||
{ | ||
capability "Media Controller" | ||
} | ||
|
||
// simulator metadata | ||
simulator | ||
{ | ||
} | ||
|
||
// UI tile definitions | ||
tiles | ||
{ | ||
standardTile("mainTile", "device.status", width: 1, height: 1, icon: "st.Entertainment.entertainment11") | ||
{ | ||
state "default", label: "Simple Sync", icon: "st.Home.home2", backgroundColor: "#55A7FF" | ||
} | ||
|
||
def detailTiles = ["mainTile"] | ||
|
||
main "mainTile" | ||
details(detailTiles) | ||
} | ||
} | ||
|
||
def parse(String description) | ||
{ | ||
def results = [] | ||
|
||
try | ||
{ | ||
def msg = parseLanMessage(description) | ||
|
||
if (msg.headers && msg.body) | ||
{ | ||
switch (msg.headers["X-Roomie-Echo"]) | ||
{ | ||
case "getAllActivities": | ||
handleGetAllActivitiesResponse(msg) | ||
break | ||
} | ||
} | ||
} | ||
catch (Throwable t) | ||
{ | ||
sendEvent(name: "parseError", value: "$t", description: description) | ||
throw t | ||
} | ||
|
||
results | ||
} | ||
|
||
def handleGetAllActivitiesResponse(response) | ||
{ | ||
def body = parseJson(response.body) | ||
|
||
if (body.status == "success") | ||
{ | ||
def json = new groovy.json.JsonBuilder() | ||
def root = json activities: body.data | ||
def data = json.toString() | ||
|
||
sendEvent(name: "activities", value: data) | ||
} | ||
} | ||
|
||
def getAllActivities(evt) | ||
{ | ||
def host = getHostAddress(device.deviceNetworkId) | ||
|
||
def action = new physicalgraph.device.HubAction(method: "GET", | ||
path: "/api/v1/activities", | ||
headers: [HOST: host, "X-Roomie-Echo": "getAllActivities"]) | ||
|
||
action | ||
} | ||
|
||
def startActivity(evt) | ||
{ | ||
def uuid = evt | ||
def host = getHostAddress(device.deviceNetworkId) | ||
def activity = new groovy.json.JsonSlurper().parseText(device.currentValue('activities') ?: "{ 'activities' : [] }").activities.find { it.uuid == uuid } | ||
def toggle = activity["toggle"] | ||
def jsonMap = ["activity_uuid": uuid] | ||
|
||
if (toggle != null) | ||
{ | ||
jsonMap << ["toggle_state": toggle ? "on" : "off"] | ||
} | ||
|
||
def json = new groovy.json.JsonBuilder(jsonMap) | ||
def jsonBody = json.toString() | ||
def headers = [HOST: host, "Content-Type": "application/json"] | ||
|
||
def action = new physicalgraph.device.HubAction(method: "POST", | ||
path: "/api/v1/runactivity", | ||
body: jsonBody, | ||
headers: headers) | ||
|
||
action | ||
} | ||
|
||
def getHostAddress(d) | ||
{ | ||
def parts = d.split(":") | ||
def ip = convertHexToIP(parts[0]) | ||
def port = convertHexToInt(parts[1]) | ||
return ip + ":" + port | ||
} | ||
|
||
def String convertHexToIP(hex) | ||
{ | ||
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".") | ||
} | ||
|
||
def Integer convertHexToInt(hex) | ||
{ | ||
Integer.parseInt(hex,16) | ||
} |
Oops, something went wrong.