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

Add optional repeat query parameter to repeat commands. #43

Merged
merged 1 commit into from
Nov 29, 2016
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
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,30 @@ is `on` or `off`. There will a topic for every activity on your hub.

### Command Topics

You can also command harmony-api to change activities by publishing topics.
harmony-api listens to this topic and will change to the activity when it sees
You can also command harmony-api to change activities, and issue device and acivity commands by publishing topics.
harmony-api listens to these topics and will change to the activity, or issue the command when it sees
it.

#### Switching activities
Just provide the slug of the hub and activity you want to switch to and `on` as
the message. Any use of this topic with the message `off` will turn everything
off.

`harmony-api/hubs/family-room/activities/watch-tv/command` `on`

#### Device commands
Just provide the slug of the hub and the device to control with the command you want to execute.
`harmony-api/hubs/family-room/devices/tv/command` `volume-down`

To optionally repeat the command any number of times, provide an optional repeat integer.
`harmony-api/hubs/family-room/devices/tv/command` `volume-down:5`

#### Current activity commands
Just provide the slug of the hub and the command you want to execute.
`harmony-api/hubs/family-room/current_activity/command` `volume-down`

To optionally repeat the command any number of times, provide an optional repeat integer.
`harmony-api/hubs/family-room/current_activity/command` `volume-down:5`

## HTTP API Docs

Expand Down Expand Up @@ -226,16 +240,21 @@ These are the endpoints you can hit to do things.

GET /hubs => {"hubs": ["family-room", "bedroom"] }
GET /hubs/:hub_slug/status => StatusResource
GET /hubs/:hub_slug/commands => {"commands": [CommandResource, CommandResource, ...]}
GET /hubs/:hub_slug/activities => {"activities": [ActivityResource, ActivityResource, ...]}
GET /hubs/:hub_slug/activities/:activity_slug/commands => {"commands": [CommandResource, CommandResource, ...]}
GET /hubs/:hub_slug/devices => {"devices": [DeviceResource, DeviceResource, ...]}
GET /hubs/:hub_slug/devices/:device_slug/commands => {"commands": [CommandResource, CommandResource, ...]}

#### Control
Use these endpoints to control your devices through your Harmony Hub.

PUT /hubs/:hub_slug/off => {message: "ok"}
POST /hubs/:hub_slug/commands/:command_slug => {message: "ok"}
POST /hubs/:hub_slug/commands/:command_slug?repeat=3 => {message: "ok"}
POST /hubs/:hub_slug/activities/:activity_slug => {message: "ok"}
POST /hubs/:hub_slug/devices/:device_slug/commands/:command_slug => {message: "ok"}
POST /hubs/:hub_slug/devices/:device_slug/commands/:command_slug?repeat=3 => {message: "ok"}

## Contributions

Expand Down
27 changes: 17 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,17 @@ mqttClient.on('message', function (topic, message) {
if (currentActivityCommandMatches) {
var hubSlug = currentActivityCommandMatches[1]
activitySlug = harmonyHubStates[hubSlug].current_activity.slug
var commandSlug = message.toString()
var messageComponents = message.toString().split(':')
var commandSlug = messageComponents[0]
var repeat = messageComponents[1]

activity = activityBySlugs(hubSlug, activitySlug)
if (!activity) { return }

command = activity.commands[commandSlug]
if (!command) { return }

sendAction(hubSlug, command.action)
sendAction(hubSlug, command.action, repeat)

} else if (activityCommandMatches) {
var hubSlug = activityCommandMatches[1]
Expand All @@ -125,12 +127,14 @@ mqttClient.on('message', function (topic, message) {
} else if (deviceCommandMatches) {
var hubSlug = deviceCommandMatches[1]
var deviceSlug = deviceCommandMatches[2]
var command = message.toString()
var messageComponents = message.toString().split(':')
var command = messageComponents[0]
var repeat = messageComponents[1]

command = commandBySlugs(hubSlug, deviceSlug, command)
if (!command) { return }

sendAction(hubSlug, command.action)
sendAction(hubSlug, command.action, repeat)
}

});
Expand Down Expand Up @@ -364,15 +368,18 @@ function startActivity(hubSlug, activityId){
})
}

function sendAction(hubSlug, action){
function sendAction(hubSlug, action, repeat){
repeat = Number.parseInt(repeat) || 1;
harmonyHubClient = harmonyHubClients[hubSlug]
if (!harmonyHubClient) { return }

var pressAction = 'action=' + action + ':status=press:timestamp=0';
var releaseAction = 'action=' + action + ':status=release:timestamp=55';
harmonyHubClient.send('holdAction', pressAction).then(function (){
harmonyHubClient.send('holdAction', releaseAction)
})
for (var i = 0; i < repeat; i++) {
harmonyHubClient.send('holdAction', pressAction).then(function (){
harmonyHubClient.send('holdAction', releaseAction)
})
}
}

function publish(topic, message, options){
Expand Down Expand Up @@ -472,7 +479,7 @@ app.post('/hubs/:hubSlug/commands/:commandSlug', function(req, res){
activity = activityBySlugs(hubSlug, activitySlug)
if (activity && commandSlug in activity.commands)
{
sendAction(hubSlug, activity.commands[commandSlug].action)
sendAction(hubSlug, activity.commands[commandSlug].action, req.query.repeat)

res.json({message: "ok"})
}else{
Expand Down Expand Up @@ -521,7 +528,7 @@ app.post('/hubs/:hubSlug/devices/:deviceSlug/commands/:commandSlug', function(re
command = commandBySlugs(req.params.hubSlug, req.params.deviceSlug, req.params.commandSlug)

if (command) {
sendAction(req.params.hubSlug, command.action)
sendAction(req.params.hubSlug, command.action, req.query.repeat)

res.json({message: "ok"})
}else{
Expand Down