-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (72 loc) · 3.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module.exports = function(RED)
{
//variables placed here are shared by all nodes
//NodeRED node constructor
function AthomHomeyNode(config)
{
RED.nodes.createNode(this, config);
var thisNode = this;
const PORT = 65439;
var express = require('express');
var app = express();
var config = {
api_url : "https://api.athom.com",
token_path : "/oauth2/token",
authorise_path : "/oauth2/authorise"
};
var AthomCloudAPI = require('athom-api');
var athomApi = new AthomCloudAPI({
client_id : config.client_id,
client_secret : config.client_secret,
callback_url : "http://localhost:" + PORT + "/callback",
debug : true
});
//Express app
app.get('/', function(req, res){
var auth_url = athomApi.getAuthorizationUrl();
console.log('got auth url:', auth_url);
res.redirect(auth_url);
});
app.get('/callback', function( req, res ){
console.log('got code:', req.query.code);
var token = athomApi.getAuthorizationToken(req.query.code, function( err, result){
console.log('got token:', result);
req.session.user = result;
res.redirect('/user/me');
});
});
app.get('/user/me', function( req, res ){
athomApi.get('/user/me', function(err, result){
if( err ) return res.send(err.message || err.toString());
res.json(result);
})
});
app.listen(PORT, function (){
var host = app.address().address;
var actualPort = app.address().port;
console.log('express app listening at http://%s:%s', host, actualPort);
thisNode.status({fill:"green", shape:"dot", text:"online (p:" + actualPort + ")"});
})
.on('error', function(error) {
if (error) {
thisNode.status({fill:"red", shape:"ring", text:"unable to start (p:" + PORT + ")"});
console.error(error);
return;
}
});
//Get Athom client token
athomApi.getClientToken(function(err, token){
console.log('getClientToken', token);
setInterval(function() {
athomApi.get('/user/me/', function(err, result, statusCode){
console.log('GET', '/user/me/', statusCode)
if (statusCode != 200)
console.log(err, result);
})
}, 1000);
});
}
//NodeRED registration
RED.nodes.registerType("athom-homey-ball", AthomHomeyNode, {
});
}