iv123 lin
/
connected-lights
sample_pir-lights_rgb
webapp/main.js@0:7a352727249b, 2017-06-18 (annotated)
- Committer:
- iv123
- Date:
- Sun Jun 18 10:14:56 2017 +0000
- Revision:
- 0:7a352727249b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iv123 | 0:7a352727249b | 1 | var TOKEN = 'YOUR_ACCESS_TOKEN'; |
iv123 | 0:7a352727249b | 2 | |
iv123 | 0:7a352727249b | 3 | var konekuta = require('konekuta'); |
iv123 | 0:7a352727249b | 4 | var express = require('express'); |
iv123 | 0:7a352727249b | 5 | var app = express(); |
iv123 | 0:7a352727249b | 6 | var server = require('http').Server(app); |
iv123 | 0:7a352727249b | 7 | var io = require('socket.io')(server); |
iv123 | 0:7a352727249b | 8 | var hbs = require('hbs'); |
iv123 | 0:7a352727249b | 9 | |
iv123 | 0:7a352727249b | 10 | // Some options for express (node.js web app library) |
iv123 | 0:7a352727249b | 11 | hbs.registerPartials(__dirname + '/views/partials'); |
iv123 | 0:7a352727249b | 12 | app.use(express.static(__dirname + '/public')); |
iv123 | 0:7a352727249b | 13 | app.set('view engine', 'html'); |
iv123 | 0:7a352727249b | 14 | app.set('views', __dirname + '/views'); |
iv123 | 0:7a352727249b | 15 | app.engine('html', hbs.__express); |
iv123 | 0:7a352727249b | 16 | |
iv123 | 0:7a352727249b | 17 | if (!process.env.TOKEN && TOKEN === 'YOUR_ACCESS_TOKEN') { |
iv123 | 0:7a352727249b | 18 | throw 'Please set your access token first in main.js!'; |
iv123 | 0:7a352727249b | 19 | } |
iv123 | 0:7a352727249b | 20 | |
iv123 | 0:7a352727249b | 21 | // This is how we go from device => view model |
iv123 | 0:7a352727249b | 22 | // we'll use this to render the device view, this will also be sent to the client |
iv123 | 0:7a352727249b | 23 | // when a device connects (so it knows what to render) |
iv123 | 0:7a352727249b | 24 | function mapToView(d) { |
iv123 | 0:7a352727249b | 25 | var hex = Number(d.color).toString(16); |
iv123 | 0:7a352727249b | 26 | var model = { |
iv123 | 0:7a352727249b | 27 | name: d.endpoint, |
iv123 | 0:7a352727249b | 28 | rawColor: d.color, |
iv123 | 0:7a352727249b | 29 | color: '#' + '000000'.substring(0, 6 - hex.length) + hex, |
iv123 | 0:7a352727249b | 30 | motionClass: d.status == 0 ? 'selected' : '', |
iv123 | 0:7a352727249b | 31 | onClass: d.status == 1 ? 'selected' : '', |
iv123 | 0:7a352727249b | 32 | offClass: d.status == 2 ? 'selected' : '', |
iv123 | 0:7a352727249b | 33 | timeout: d.timeout, |
iv123 | 0:7a352727249b | 34 | status: d.status |
iv123 | 0:7a352727249b | 35 | }; |
iv123 | 0:7a352727249b | 36 | |
iv123 | 0:7a352727249b | 37 | // create the device HTML |
iv123 | 0:7a352727249b | 38 | var html = hbs.handlebars.compile('{{> device}}')(model); |
iv123 | 0:7a352727249b | 39 | |
iv123 | 0:7a352727249b | 40 | return { model: model, html: html }; |
iv123 | 0:7a352727249b | 41 | } |
iv123 | 0:7a352727249b | 42 | |
iv123 | 0:7a352727249b | 43 | var options = { |
iv123 | 0:7a352727249b | 44 | endpointType: 'light-system', // what endpoint types to look for |
iv123 | 0:7a352727249b | 45 | token: TOKEN, |
iv123 | 0:7a352727249b | 46 | io: io, |
iv123 | 0:7a352727249b | 47 | deviceModel: { // building the initial device model (w/ 4 properties) |
iv123 | 0:7a352727249b | 48 | status: { |
iv123 | 0:7a352727249b | 49 | retrieve: 'led/0/permanent_status', // when device registers, retrieve value |
iv123 | 0:7a352727249b | 50 | update: { |
iv123 | 0:7a352727249b | 51 | method: 'put', // update actions |
iv123 | 0:7a352727249b | 52 | path: 'led/0/permanent_status' |
iv123 | 0:7a352727249b | 53 | } |
iv123 | 0:7a352727249b | 54 | }, |
iv123 | 0:7a352727249b | 55 | timeout: { |
iv123 | 0:7a352727249b | 56 | retrieve: 'led/0/timeout', |
iv123 | 0:7a352727249b | 57 | update: { |
iv123 | 0:7a352727249b | 58 | method: 'put', |
iv123 | 0:7a352727249b | 59 | path: 'led/0/timeout' |
iv123 | 0:7a352727249b | 60 | } |
iv123 | 0:7a352727249b | 61 | }, |
iv123 | 0:7a352727249b | 62 | color: { |
iv123 | 0:7a352727249b | 63 | retrieve: 'led/0/color', |
iv123 | 0:7a352727249b | 64 | update: { |
iv123 | 0:7a352727249b | 65 | method: 'put', |
iv123 | 0:7a352727249b | 66 | path: 'led/0/color' |
iv123 | 0:7a352727249b | 67 | } |
iv123 | 0:7a352727249b | 68 | }, |
iv123 | 0:7a352727249b | 69 | count: { |
iv123 | 0:7a352727249b | 70 | retrieve: 'pir/0/count', |
iv123 | 0:7a352727249b | 71 | subscribe: true // subscribe to updates |
iv123 | 0:7a352727249b | 72 | } |
iv123 | 0:7a352727249b | 73 | }, |
iv123 | 0:7a352727249b | 74 | mapToView: mapToView, |
iv123 | 0:7a352727249b | 75 | verbose: true // Verbose logging |
iv123 | 0:7a352727249b | 76 | }; |
iv123 | 0:7a352727249b | 77 | |
iv123 | 0:7a352727249b | 78 | // Start konekuta (connects to mbed Device Connector, and retrieves initial device model) |
iv123 | 0:7a352727249b | 79 | konekuta(options, (err, devices, ee, connector) => { |
iv123 | 0:7a352727249b | 80 | if (err) { |
iv123 | 0:7a352727249b | 81 | throw err; |
iv123 | 0:7a352727249b | 82 | } |
iv123 | 0:7a352727249b | 83 | |
iv123 | 0:7a352727249b | 84 | // Now we can start the web server |
iv123 | 0:7a352727249b | 85 | server.listen(process.env.PORT || 5265, process.env.HOST || '0.0.0.0', function() { |
iv123 | 0:7a352727249b | 86 | console.log('Web server listening on port %s!', process.env.PORT || 5265); |
iv123 | 0:7a352727249b | 87 | }); |
iv123 | 0:7a352727249b | 88 | |
iv123 | 0:7a352727249b | 89 | // And handle requests |
iv123 | 0:7a352727249b | 90 | app.get('/', function(req, res, next) { |
iv123 | 0:7a352727249b | 91 | // Render index view, with the devices based on mapToView function |
iv123 | 0:7a352727249b | 92 | res.render('index', { devices: devices.map(d => mapToView(d).model) }); |
iv123 | 0:7a352727249b | 93 | }); |
iv123 | 0:7a352727249b | 94 | }); |