This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-offerings.js
120 lines (95 loc) · 3.32 KB
/
check-offerings.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
const { consumer: BigIotConsumer, offering: BigIotOffering } = require('bigiot-js');
const jwt = require('jsonwebtoken');
const http = require('http');
function assert(expr, message) {
if (!expr) {
throw new Error(message);
}
}
// Emulate what Marketplace is doing when calling BigIotConsumer.subscribe(offering)
function fakeSubscribe(offering, providerSecret) {
if (!providerSecret) {
providerSecret = process.env.BIGIOT_PROVIDER_SECRET;
}
const token = jwt.sign({}, Buffer.from(providerSecret, 'base64'), { expiresIn: '1h', });
const sub = {
accessToken: token,
offering: offering,
}
return sub;
}
function accessViaMarketplace(config, category, offeringId, parameters) {
const consumer = new BigIotConsumer(config.id, config.secret, undefined);
return consumer.authenticate().then(() => {
const query = new BigIotOffering('Parking sites', category);
delete query.license;
delete query.extent;
delete query.price;
return consumer.discover(query)
}).then((all) => {
if (all.length < 1) {
throw new Error(`No offerings for category ${category}`);
}
const matching = all.filter((o) => o.id == offeringId );
assert(matching.length > 0, `Offering '${offeringId}' for category '${category}' was not found in marketplace`);
assert(matching.length < 2, `Duplicate offering found for ${offeringId}`);
const offering = matching[0];
const active = offering.activation.status;
assert(active, `Offering '${offeringId}' is not marked as active`);
return offering;
}).then((offering) => {
//return consumer.subscribe(offering.id); // FIXME: fails with 503
return fakeSubscribe(offering);
}).then((subscription) => {
return consumer.access(subscription, parameters);
});
}
function sendSlackMessage(slack, message) {
const payload = {
'text': message,
}
return fetch(slack.url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload),
}).then((res) => {
assert(res.status == 200, `Slack reported error ${res.status}`);
});
}
function main() {
const consumer = {
secret: process.env.BIGIOT_CONSUMER_SECRET,
id: process.env.BIGIOT_CONSUMER_ID,
}
const slack = {
url: process.env.SLACK_HOOK,
}
const providerId = process.env.BIGIOT_PROVIDER_ID;
const category = 'urn:big-iot:ParkingSiteCategory';
const access = (id, lat, lon, radius) => {
const params = { latitude: lat, longitude: lon, radius: radius }
return accessViaMarketplace(consumer, category, id, params);
}
// TODO: check timeliness of data
// TODO: check output against schema
const dbahn = access(`${providerId}-Bahn_Parking_Berlin`, 50.9375, 6.9603, 1000).then((data) => {
assert(data.length >= 3, `Too few parking sites returned: ${data.length}`);
});
const cologne = access(`${providerId}-Cologne_Parking`, 50.9375, 6.9603, 1000).then((data) => {
assert(data.length >= 10, `Too few parking sites returned: ${data.length}`);
})
Promise.all([dbahn, cologne]).then((results) => {
console.log('Offerings are OK');
}).catch((err) => {
return sendSlackMessage(slack, `Offering failed: ${err.message}`).then(() => {
console.error('ERROR', err);
process.exit(1);
})
})
}
if (!module.parent) {
main();
}