Skip to content

Commit

Permalink
Merge pull request SmartThingsCommunity#431 from SmartThingsCommunity…
Browse files Browse the repository at this point in the history
…/MSA-794-2

MSA-794: Simple Control
  • Loading branch information
jasonbio committed Mar 8, 2016
2 parents e83d08c + da02900 commit 56d991b
Show file tree
Hide file tree
Showing 4 changed files with 1,483 additions and 0 deletions.
128 changes: 128 additions & 0 deletions devicetypes/roomieremote-agent/simple-sync.src/simple-sync.groovy
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)
}
Loading

0 comments on commit 56d991b

Please sign in to comment.