iv123 lin
/
connected-lights
sample_pir-lights_rgb
example-nodejs/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 CloudApi = require('mbed-connector-api'); |
iv123 | 0:7a352727249b | 4 | var api = new CloudApi({ |
iv123 | 0:7a352727249b | 5 | accessKey: process.env.TOKEN || TOKEN |
iv123 | 0:7a352727249b | 6 | }); |
iv123 | 0:7a352727249b | 7 | |
iv123 | 0:7a352727249b | 8 | // Start notification channel |
iv123 | 0:7a352727249b | 9 | api.startLongPolling(function(err) { |
iv123 | 0:7a352727249b | 10 | if (err) throw err; |
iv123 | 0:7a352727249b | 11 | |
iv123 | 0:7a352727249b | 12 | // Find all lights |
iv123 | 0:7a352727249b | 13 | api.getEndpoints(function(err, devices) { |
iv123 | 0:7a352727249b | 14 | if (err) throw err; |
iv123 | 0:7a352727249b | 15 | |
iv123 | 0:7a352727249b | 16 | console.log('Found', devices.length, 'lights', devices); |
iv123 | 0:7a352727249b | 17 | |
iv123 | 0:7a352727249b | 18 | devices.forEach(function(d) { |
iv123 | 0:7a352727249b | 19 | // For every light, we will request notifications on the PIR resource |
iv123 | 0:7a352727249b | 20 | api.putResourceSubscription(d.name, '/pir/0/count', function(err) { |
iv123 | 0:7a352727249b | 21 | console.log('subscribed to resource', err); |
iv123 | 0:7a352727249b | 22 | }); |
iv123 | 0:7a352727249b | 23 | |
iv123 | 0:7a352727249b | 24 | // and set the color to orange, as your's truly is Dutch |
iv123 | 0:7a352727249b | 25 | var orange = 0xff6400; |
iv123 | 0:7a352727249b | 26 | api.putResourceValue(d.name, '/led/0/color', orange, function(err) { |
iv123 | 0:7a352727249b | 27 | if (err) console.error('Setting led/0/color for', d.name, 'failed', err); |
iv123 | 0:7a352727249b | 28 | console.log('Set color of', d.name, 'to orange!'); |
iv123 | 0:7a352727249b | 29 | }); |
iv123 | 0:7a352727249b | 30 | |
iv123 | 0:7a352727249b | 31 | }); |
iv123 | 0:7a352727249b | 32 | |
iv123 | 0:7a352727249b | 33 | }, { parameters: { type: 'light-system' } }); |
iv123 | 0:7a352727249b | 34 | }); |
iv123 | 0:7a352727249b | 35 | |
iv123 | 0:7a352727249b | 36 | // Notifications |
iv123 | 0:7a352727249b | 37 | api.on('notification', function(notification) { |
iv123 | 0:7a352727249b | 38 | console.log('Got a notification', notification); |
iv123 | 0:7a352727249b | 39 | }); |