-
Notifications
You must be signed in to change notification settings - Fork 85
/
app.js
143 lines (128 loc) · 3.88 KB
/
app.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var https = require('https');
var cfenv = require('cfenv');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json()); // for parsing application/json
var appEnv = cfenv.getAppEnv();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var config = null;
var credentials = null;
if (process.env.VCAP_SERVICES) {
config = JSON.parse(process.env.VCAP_SERVICES);
var iotService = config['iotf-service'];
for (var index in iotService) {
if (iotService[index].name === 'discover-iot-try-service') {
credentials = iotService[index].credentials;
}
}
} else {
console.log("ERROR: IoT Service was not bound!");
}
var basicConfig = {
org: credentials.org,
apiKey: credentials.apiKey,
apiToken: credentials.apiToken
};
var options = {
host: 'internetofthings.ibmcloud.com',
port: 443,
headers: {
'Content-Type': 'application/json'
},
auth: basicConfig.apiKey + ':' + basicConfig.apiToken
};
app.get('/credentials', function(req, res) {
res.json(basicConfig);
});
app.get('/iotServiceLink', function(req, res) {
var options = {
host: basicConfig.org + '.internetofthings.ibmcloud.com',
port: 443,
headers: {
'Content-Type': 'application/json'
},
auth: basicConfig.apiKey + ':' + basicConfig.apiToken,
method: 'GET',
path: 'api/v0002/'
}
var org_req = https.request(options, function(org_res) {
var str = '';
org_res.on('data', function(chunk) {
str += chunk;
});
org_res.on('end', function() {
try {
var org = JSON.parse(str);
var url = "https://console.ng.bluemix.net/#/resources/serviceGuid=" + org.bluemix.serviceInstanceGuid + "&orgGuid=" + org.bluemix.organizationGuid + "&spaceGuid=" + org.bluemix.spaceGuid;
res.json({ url: url });
} catch (e) { console.log("Something went wrong...", str); res.send(500); }
console.log("iotServiceLink end: ", str.toString());
});
}).on('error', function(e) { console.log("ERROR", e); });
org_req.end();
});
app.post('/registerDevice', function(req, res) {
console.log(req.body);
var deviceId = null, typeId = "iot-phone", password = null;
if (req.body.deviceId) { deviceId = req.body.deviceId; }
if (req.body.typeId) { typeId = req.body.typeId; }
if (req.body.password) { password = req.body.password; }
var options = {
host: basicConfig.org + '.internetofthings.ibmcloud.com',
port: 443,
headers: {
'Content-Type': 'application/json'
},
auth: basicConfig.apiKey + ':' + basicConfig.apiToken,
method: 'POST',
path: 'api/v0002/device/types'
}
var deviceTypeDetails = {
id: typeId
}
console.log(deviceTypeDetails);
var type_req = https.request(options, function(type_res) {
var str = '';
type_res.on('data', function(chunk) {
str += chunk;
});
type_res.on('end', function() {
console.log("createDeviceType end: ", str.toString());
var dev_options = {
host: basicConfig.org + '.internetofthings.ibmcloud.com',
port: 443,
headers: {
'Content-Type': 'application/json'
},
auth: basicConfig.apiKey + ':' + basicConfig.apiToken,
method: 'POST',
path: 'api/v0002/device/types/'+typeId+'/devices'
}
var deviceDetails = {
deviceId: deviceId,
authToken: password
};
console.log(deviceDetails);
var dev_req = https.request(dev_options, function(dev_res) {
var str = '';
dev_res.on('data', function(chunk) {
str += chunk;
});
dev_res.on('end', function() {
console.log("createDevice end: ", str.toString());
res.send({ result: "Success!" });
});
}).on('error', function(e) { console.log("ERROR", e); });
dev_req.write(JSON.stringify(deviceDetails));
dev_req.end();
});
}).on('error', function(e) { console.log("ERROR", e); });
type_req.write(JSON.stringify(deviceTypeDetails));
type_req.end();
});
app.listen(appEnv.port, function() {
console.log("server starting on " + appEnv.url);
});