-
Notifications
You must be signed in to change notification settings - Fork 59
Add support for arbitrary HTTP devices. #38
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
base: osur
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ var https = require('https'); | |
| var fs = require('fs'); | ||
| var gpio = require('./lib/gpio'); | ||
| var macros = require('./lib/macros'); | ||
| var request = require('request'); | ||
|
|
||
| // Precompile templates | ||
| var JST = { | ||
|
|
@@ -122,8 +123,10 @@ labelFor = labels(config.remoteLabels, config.commandLabels); | |
| // Index | ||
| app.get('/', function (req, res) { | ||
| var refinedRemotes = refineRemotes(lircNode.remotes); | ||
|
|
||
| res.send(JST.index({ | ||
| remotes: refinedRemotes, | ||
| devices: config.devices, | ||
| macros: config.macros, | ||
| repeaters: config.repeaters, | ||
| gpios: config.gpios, | ||
|
|
@@ -138,6 +141,14 @@ app.get('/refresh', function (req, res) { | |
| res.redirect('/'); | ||
| }); | ||
|
|
||
| // Get all capabilities of remote | ||
| app.get('/capabilities.json', function (req, res) { | ||
| res.json({ | ||
| devices: config.devices, | ||
| remotes: lircNode.remotes, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OvisMaximus do you think |
||
| }); | ||
| }); | ||
|
|
||
| // List all remotes in JSON format | ||
| app.get('/remotes.json', function (req, res) { | ||
| res.json(refineRemotes(lircNode.remotes)); | ||
|
|
@@ -180,6 +191,70 @@ app.get('/macros/:macro.json', function (req, res) { | |
| } | ||
| }); | ||
|
|
||
| // List all devices in JSON format | ||
| app.get('/devices.json', function (req, res) { | ||
| // TODO: Should return a nicely formatted response, not just the config object | ||
| res.json(config.devices); | ||
| }); | ||
|
|
||
| // List all commands for :device in JSON format | ||
| app.get('/devices/:device.json', function (req, res) { | ||
| if (config.devices && config.devices[req.params.device]) { | ||
| // TODO: Should return a nicely formatted response, not just the config object | ||
| res.json(config.devices[req.params.device]); | ||
| } else { | ||
| res.send(404); | ||
| } | ||
| }); | ||
|
|
||
| // Get device state | ||
| app.get('/devices/:device', function (req, res) { | ||
| var state = null; | ||
| var bodyJson = null; | ||
| var stateReq = null; | ||
|
|
||
| if (config.devices && | ||
| config.devices[req.params.device] && | ||
| config.devices[req.params.device].state) { | ||
| state = config.devices[req.params.device].state; | ||
| bodyJson = null; | ||
|
|
||
| // Build request to make to get device state | ||
| stateReq = { | ||
| method: state.method || 'GET', | ||
| url: state.url, | ||
| form: state.body, | ||
| }; | ||
|
|
||
| // Make request for state, include body of response in response | ||
| request(stateReq, function (error, response, body) { | ||
| // TODO: How to parse the response and only return relevant portion? | ||
| bodyJson = JSON.parse(body); | ||
| res.json(bodyJson); | ||
| }); | ||
| } else { | ||
| // Device doesn't have a state, return 404 | ||
| res.sendStatus(404); | ||
| } | ||
| }); | ||
|
|
||
| // Execute device action | ||
| app.post('/devices/:device/:command', function (req, res) { | ||
| var device = config.devices[req.params.device]; | ||
| var command = device.commands[req.params.command]; | ||
|
|
||
| var commandReq = { | ||
| method: command.method, | ||
| url: command.url, | ||
| form: command.body, | ||
| }; | ||
|
|
||
| request(commandReq); | ||
|
|
||
| res.setHeader('Cache-Control', 'no-cache'); | ||
| res.sendStatus(200); | ||
| }); | ||
|
|
||
| // Send :remote/:command one time | ||
| app.post('/remotes/:remote/:command', function (req, res) { | ||
| lircNode.irsend.send_once(req.params.remote, req.params.command, function () {}); | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,16 @@ | |
| {% endfor %} | ||
| </ul> | ||
|
|
||
| <hr /> | ||
|
|
||
| <ul class="devices-nav"> | ||
| {% for device in devices %} | ||
| {% set deviceName = device.name %} | ||
| <li><a class="btn btn-wide btn-large btn-inverse" href="#{{ deviceName|url_encode }}">{{ deviceName }}</a></li> | ||
| {% endfor %} | ||
| </ul> | ||
|
|
||
|
|
||
| <hr /> | ||
|
|
||
| <ul class="macros-nav"> | ||
|
|
@@ -53,13 +63,32 @@ | |
| <ul class="commands"> | ||
| {% for command in remote %} | ||
| <li class="command"> | ||
| <button class="command-link {% if repeaters[remoteName] && repeaters[remoteName][command] %}command-repeater{% else %}command-once{% endif %} btn btn-wide btn-large btn-primary" href="/remotes/{{ remoteName|url_encode }}/{{ command|url_encode }}">{{ labelForCommand(remoteName, command) }}</button> | ||
| <button class="remote-link command-link {% if repeaters[remoteName] && repeaters[remoteName][command] %}command-repeater{% else %}command-once{% endif %} btn btn-wide btn-large btn-primary" href="/remotes/{{ remoteName|url_encode }}/{{ command|url_encode }}">{{ labelForCommand(remoteName, command) }}</button> | ||
| </li> | ||
| {% endfor %} | ||
| </ul> | ||
| </li> | ||
| {% endfor %} | ||
| </ul> | ||
|
|
||
| <hr /> | ||
|
|
||
| <ul class="remotes"> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be |
||
| {% for device in devices %} | ||
| {% set deviceName = device.name %} | ||
| <li class="remote" id="{{ deviceName|url_encode }}"> | ||
| <ul class="commands"> | ||
| {% for command in device.commands %} | ||
| {% set commandName = loop.key %} | ||
| <li class="command"> | ||
| <button class="device-link command-link {% if repeaters[remoteName] && repeaters[remoteName][command] %}command-repeater{% else %}command-once{% endif %} btn btn-wide btn-large btn-primary" href="/devices/{{ deviceName }}/{{ commandName|url_encode }}">{{ commandName }}</button> | ||
| </li> | ||
| {% endfor %} | ||
| </ul> | ||
| </li> | ||
| {% endfor %} | ||
| </ul> | ||
|
|
||
| </div> | ||
|
|
||
| <div class="next"></div> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,5 +60,26 @@ | |
| "pin": 16}, | ||
| {"name": "Xbox", | ||
| "pin": 13} | ||
| ] | ||
| ], | ||
| "devices": { | ||
| "GarageDoor": { | ||
| "name": "GarageDoor", | ||
| "commands": { | ||
| "TriggerDoor": { | ||
| "url": "https://api.spark.io/v1/devices/123/do_thing", | ||
| "method": "POST", | ||
| "body": { | ||
| "access_token": "abcdefg" | ||
| } | ||
| } | ||
| }, | ||
| "state": { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't used, so could be removed. |
||
| "url": "https://api.spark.io/v1/devices/123/get_state", | ||
| "method": "POST", | ||
| "body": { | ||
| "access_token": "abcdefg" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
httpDevicesorwebDevicesmay make more sense here.