-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.js
91 lines (77 loc) · 2.03 KB
/
subscribe.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
var config = require('./config');
var httpClient = require('./lib/HTTPClient.js');
var fs = require('fs');
var subFile = "./subscription.json";
var fs = require('fs');
if (fs.existsSync(subFile)) {
console.log('Subscribtion file exists already.');
fs.readFile(subFile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log('Verify subscribtion ' + data);
var options = {
host: config.orion_host,
port: config.orion_port,
path: data,
method: 'GET',
headers: {
'Accept' : 'application/json',
'Fiware-Service' : 'organicity'
}
};
httpClient.sendData(config.orion_protocol, options, payloadString, undefined, function (status, responseText, headers) {
console.log('OK');
}, function(status, resp) {
console.log('ERROR', status, resp);
fs.unlinkSync(subFile); // Let's remove the file
console.log(subFile + ' deleted!');
});
});
} else {
var payloadJson = {
"subject": {
"entities": [
{
"idPattern": ".*",
"typePattern": ".*"
}
],
"condition": {
"attrs": []
}
},
"notification": {
"http": {
"url": config.subscription_url
}
},
"expires": "2020-04-05T14:00:00.00Z",
"throttling": config.subscription_throttling
};
console.log('Subscribe now...');
var payloadString = JSON.stringify(payloadJson);
var options = {
host: config.orion_host,
port: config.orion_port,
path: '/v2/subscriptions',
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json',
'Fiware-Service' : 'organicity'
}
};
httpClient.sendData(config.orion_protocol, options, payloadString, undefined, function (status, responseText, headers) {
console.log('Successful: ' + headers.location);
fs.writeFile(subFile, headers.location, function(err) {
if(err) {
return console.log(err);
}
console.log("Subscription saved in subscription.json!");
});
console.log('Successful: ' + headers.location);
}, function(status, resp) {
console.log('ERROR', status, resp);
});
}