-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhue-express.js
More file actions
112 lines (96 loc) · 2.63 KB
/
hue-express.js
File metadata and controls
112 lines (96 loc) · 2.63 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
// set the view engine to ejs
app.set('view engine', 'ejs');
/**function tryGetStatus(id) {
var request = require("request");
var url = "http://192.168.86.111/api/ce3fdkAFNsSPVulaUl9eAtdjhEkRfJ9Knwi17wTz/lights/"+id;
console.log(url);
request({
uri: url,
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
//console.log(body);
return body;
});
} **/
app.get('/', function (req, res) {
res.render('pages/hello');
//res.send('welcome to my hue app');
});
app.get('/alllights', function(req, res) {
var urlOri = "http://192.168.86.111/api/ce3fdkAFNsSPVulaUl9eAtdjhEkRfJ9Knwi17wTz/lights/";
var url = urlOri;
//var url = +id;
console.log(url);
var options = {
uri: url,
json: true
}
var rp = require('request-promise');
rp(options)
.then(function(body) {
console.log(body);
res.json(body);
})
.catch(function(err){
console.log(err);
});
});
app.get('/lights/:id', function(req, res) {
var id = req.params.id;
var urlOri = "http://192.168.86.111/api/ce3fdkAFNsSPVulaUl9eAtdjhEkRfJ9Knwi17wTz/lights/";
var url = urlOri + id;
//var url = +id;
console.log(url);
var options = {
uri: url,
json: true
}
var rp = require('request-promise');
rp(options)
.then(function(body) {
console.log(body);
res.json(body);
})
.catch(function(err){
console.log(err);
});
});
app.post('/', function (req, res) {
res.render('pages/hello');
//res.send('POST request to the homepage');
});
// URL: curl -H "Content-Type: application/json" -X POST -d '{"on":false}' http://localhost:9000/state/4
app.post('/state/:id', function(req, res) {
console.log(req.body); // your JSON
var body = req.body;
var id = req.params.id;
var urlOri = "http://192.168.86.111/api/ce3fdkAFNsSPVulaUl9eAtdjhEkRfJ9Knwi17wTz/lights/";
var url = urlOri + id + "/state/";
//var url = +id;
console.log(url);
var options = {
method: 'PUT',
uri: url,
json: body
}
var rp = require('request-promise');
rp(options)
.then(function(body) {
console.log(body);
res.json(body);
})
.catch(function(err){
console.log(err);
res.send("bad operation.");
});
//res.json(req.body); // echo the result back
});
app.listen(9000);
console.log("starting express server on port 9000...");