-
Notifications
You must be signed in to change notification settings - Fork 59
Zwave - DO NOT MERGE (see comments) #36
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: master
Are you sure you want to change the base?
Changes from all commits
249bf5b
64e0dd3
6f65930
f97d438
8c09c76
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 |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ node_modules/ | |
| .settings/ | ||
| *.swp | ||
| npm-debug.log | ||
| zwscene.xml | ||
| zwcfg* | ||
| OZW_Log.txt | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,11 @@ var consolidate = require('consolidate'); | |
| var swig = require('swig'); | ||
| var labels = require('./lib/labels'); | ||
| var https = require('https'); | ||
| var request = require('request'); | ||
| var fs = require('fs'); | ||
| var macros = require('./lib/macros'); | ||
| var request = require('request'); | ||
| var OpenZWave = require('openzwave-shared'); | ||
|
|
||
| // Precompile templates | ||
| var JST = { | ||
|
|
@@ -47,7 +50,10 @@ app.use(express.static(__dirname + '/static')); | |
| function _init() { | ||
| var home = process.env.HOME; | ||
|
|
||
| lircNode.init(); | ||
| lircNode.init().then(function() { | ||
| console.log("LIRC connection initialized"); | ||
| console.log(lircNode.remotes); | ||
| }); | ||
|
|
||
| // Config file is optional | ||
| try { | ||
|
|
@@ -117,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, | ||
| labelForRemote: labelFor.remote, | ||
|
|
@@ -139,6 +147,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": lirc_node.remotes | ||
| }); | ||
| }); | ||
|
|
||
| // List all remotes in JSON format | ||
| app.get('/remotes.json', function (req, res) { | ||
| res.json(refineRemotes(lircNode.remotes)); | ||
|
|
@@ -167,6 +183,66 @@ 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) { | ||
| if (config.devices && | ||
| config.devices[req.params.device] && | ||
| config.devices[req.params.device]['state']) { | ||
|
|
||
| var state = config.devices[req.params.device]['state']; | ||
|
|
||
| // Build request to make to get device state | ||
| var 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? | ||
| var bodyJson = JSON.parse(body); | ||
| res.json(bodyJson); | ||
| }); | ||
|
|
||
| } else { | ||
| // Device doesn't have a state, return 404 | ||
| res.send(404); | ||
| } | ||
| }); | ||
|
|
||
| // Execute device action | ||
| app.post('/devices/:device/:command', function(req, res) { | ||
|
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. How is this related to the direct zwave endpoint? this is more flexible and follows the remote endpoint. Also maybe use a get to get the current status? Most of these devices have two-way communication and the status can be queried.
Owner
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. I was trying to define what an abstract device would need and settled on two types of functionality:
So you could "GET" the device to trigger requesting the current state, or you could POST to an endpoint to trigger a command. IR devices wouldn't have state, though.
Contributor
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. I need some clarity when talking about device - in the past hours I used If I got it right, you are using 'device' for a single device like the Bottomline: I think it is possible if not probable that we'll see IR On Sun, Jan 17, 2016 at 11:20 PM, Alex Bain notifications@github.com
Owner
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. Yeah, I think that a 'device' should represent a physical artifact within the region the universal remote is responsible for controlling. A device could use any number of protocols but would be presented in a 'unified' way for the UI / API to interact with. I hadn't considered a device that uses different protocols to get state vs send commands from, but it seems like something that could be supported. |
||
| 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) { | ||
|
|
@@ -202,24 +278,50 @@ app.post('/macros/:macro', function (req, res) { | |
| } | ||
| }); | ||
|
|
||
| // ZWAVE | ||
| var zwaveNodes = []; | ||
| var zwave = new OpenZWave({ saveconfig: true }); | ||
| zwave.connect('/dev/ttyACM0'); | ||
|
|
||
| app.post('/zwave/:id/on', function(req, res) { | ||
| var id = req.params.id; | ||
| zwave.setValue(id, 37, 1, 0, true); | ||
| res.json({ "success": true }); | ||
| }); | ||
|
|
||
| app.post('/zwave/:id/off', function(req, res) { | ||
| var id = req.params.id; | ||
| zwave.setValue(id, 37, 1, 0, false); | ||
| res.json({ "success": true }); | ||
| }); | ||
|
|
||
| zwave.on('scan complete', function() { | ||
| console.log("Zwave scan complete..."); | ||
| }); | ||
|
|
||
| // Listen (http) | ||
| if (config.server && config.server.port) { | ||
| port = config.server.port; | ||
| } | ||
| // only start server, when called as application | ||
|
|
||
| if (!module.parent) { | ||
| app.listen(port); | ||
| console.log('Open Source Universal Remote UI + API has started on port ' + port + ' (http).'); | ||
| } | ||
|
|
||
| // Listen (https) | ||
| if (config.server && config.server.ssl && config.server.ssl_cert && config.server.ssl_key && config.server.ssl_port) { | ||
| sslOptions = { | ||
| key: fs.readFileSync(config.server.ssl_key), | ||
| cert: fs.readFileSync(config.server.ssl_cert), | ||
| }; | ||
| // Listen (https) | ||
| if (config.server && config.server.ssl && config.server.ssl_cert && config.server.ssl_key && config.server.ssl_port) { | ||
| sslOptions = { | ||
| key: fs.readFileSync(config.server.ssl_key), | ||
| cert: fs.readFileSync(config.server.ssl_cert), | ||
| }; | ||
|
|
||
| https.createServer(sslOptions, app).listen(config.server.ssl_port); | ||
| https.createServer(sslOptions, app).listen(config.server.ssl_port); | ||
|
|
||
| console.log('Open Source Universal Remote UI + API has started on port ' + config.server.ssl_port + ' (https).'); | ||
| console.log('Open Source Universal Remote UI + API has started on port ' + config.server.ssl_port + ' (https).'); | ||
| } | ||
| } | ||
|
|
||
| process.on('SIGINT', function() { | ||
| zwave.disconnect('/dev/ttyACM0'); | ||
| process.exit(); | ||
| }); | ||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
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.
Greate idea