Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Host Capturing #2

Merged
merged 9 commits into from
Aug 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}