Skip to content

Commit

Permalink
Merge pull request #2 from maddox/better-host-capturing
Browse files Browse the repository at this point in the history
Better Host Capturing
  • Loading branch information
maddox committed Aug 17, 2015
2 parents c2dc3e1 + 03abfa9 commit 9cf7eb1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 54 deletions.
99 changes: 47 additions & 52 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var bodyParser = require('body-parser')
var harmonyHubDiscover = require('harmonyhubjs-discover')
var harmony = require('harmonyhubjs-client')

var harmonyIp
var harmonyHubClient
var harmonyActivitiesCache = {}
var harmonyActivityUpdateInterval = 1*60*1000 // 1 minute
var harmonyActivityUpdateTimer
Expand All @@ -28,55 +28,58 @@ if ('development' == env){
}

// Middleware
// Check to make sure we have a harmonyIp to connect to
var hasHarmonyIp = function(req, res, next) {
if (harmonyIp) {
// Check to make sure we have a harmonyHubClient to connect to
var hasHarmonyHubClient = function(req, res, next) {
if (hasHarmonyHubClient) {
next()
}else{
res.status(500).json({message: "Can not connect to hub."})
}
}
app.use(hasHarmonyIp)
app.use(hasHarmonyHubClient)


discover = new harmonyHubDiscover(61991)
var discover = new harmonyHubDiscover(61991)

discover.on('online', function(hub) {
discover.on('online', function(hubInfo) {
// Triggered when a new hub was found
console.log('discovered ' + hub.ip)
harmonyIp = hub.ip

// update the list of activities
updateActivities()
// then do it on the set interval
clearInterval(harmonyActivityUpdateTimer)
harmonyActivityUpdateTimer = setInterval(function(){ updateActivities() }, harmonyActivityUpdateInterval)
})
console.log('Hub discovered ' + hubInfo.ip + '.')

if (hubInfo.ip) {
console.log('Stopping discovery.')
discover.stop()

harmony(hubInfo.ip).then(function(harmonyClient) {
console.log('Harmony client created.')

harmonyHubClient = harmonyClient

// update the list of activities
updateActivities()
// then do it on the set interval
clearInterval(harmonyActivityUpdateTimer)
harmonyActivityUpdateTimer = setInterval(function(){ updateActivities() }, harmonyActivityUpdateInterval)
})
}

discover.on('offline', function(hub) {
// Triggered when a hub disappeared
console.log('lost ' + hub.ip)
clearInterval(harmonyActivityUpdateTimer)
harmonyActivitiesCache = {}
harmonyIp = null
})


// Look for hubs:
console.log('Starting discovery.')
discover.start()

function updateActivities(){
if (!harmonyIp) { return }
if (!harmonyHubClient) { return }
console.log('Updating activities.')

harmony(harmonyIp).then(function(harmonyClient) {
harmonyClient.getActivities().then(function(activities){
foundActivities = {}
activities.some(function(activity) {
foundActivities[activity.id] = {id: activity.id, label: activity.label, isAVActivity: activity.isAVActivity}
})

harmonyActivitiesCache = foundActivities
harmonyClient.end()
harmonyHubClient.getActivities().then(function(activities){
foundActivities = {}
activities.some(function(activity) {
foundActivities[activity.id] = {id: activity.id, label: activity.label, isAVActivity: activity.isAVActivity}
})

harmonyActivitiesCache = foundActivities
})
}

Expand All @@ -103,40 +106,32 @@ app.get('/activities', function(req, res){
})

app.get('/status', function(req, res){
harmony(harmonyIp).then(function(harmonyClient) {
harmonyClient.getCurrentActivity().then(function(activityId){
data = {off: true}
harmonyHubClient.getCurrentActivity().then(function(activityId){
data = {off: true}

activity = harmonyActivitiesCache[activityId]
activity = harmonyActivitiesCache[activityId]

if (activityId != -1 && activity) {
data = {off: false, current_activity: activity}
}else{
data = {off: true}
}
if (activityId != -1 && activity) {
data = {off: false, current_activity: activity}
}else{
data = {off: true}
}

harmonyClient.end()
res.json(data)
})
res.json(data)
})
})

app.put('/off', function(req, res){
harmony(harmonyIp).then(function(harmonyClient) {
harmonyClient.turnOff().then(function(){
harmonyClient.end()
res.json({message: "ok"})
})
harmonyHubClient.turnOff().then(function(){
res.json({message: "ok"})
})
})

app.post('/start_activity', function(req, res){
activity = activityByName(req.body.activity_name)

if (activity) {
harmony(harmonyIp).then(function(harmonyClient) {
harmonyClient.startActivity(activity.id)
harmonyClient.end()
harmonyHubClient.startActivity(activity.id).then(function(){
res.json({message: "ok"})
})
}else{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "harmony-api",
"version": "0.1.1",
"version": "0.2.0",
"description": "A simple server providing a RESTful service for controlling a Harmony Hub.",
"scripts": {
"start": "node app.js"
Expand All @@ -19,6 +19,6 @@
"morgan": "^1.6.1",
"body-parser": "^1.12.4",
"harmonyhubjs-client": "^1.1.4",
"harmonyhubjs-discover": "^1.0.0"
"harmonyhubjs-discover": "git@github.com:swissmanu/harmonyhubjs-discover.git"
}
}

0 comments on commit 9cf7eb1

Please sign in to comment.