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 2
/
consumer.html
83 lines (68 loc) · 2.15 KB
/
consumer.html
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
<html>
<meta charset="utf-8" />
<head>
</head>
<body>
<p>Connecting to BIG IoT...</p>
</body>
<!-- > can also use https://flowhub.github.io/bigiot-js/$VERSION/bigiot.js <-->
<script type='text/javascript' src='https://flowhub.github.io/bigiot-js/bigiot.js'></script>
<script type='text/javascript'>
const { consumer: BigIotConsumer, offering: BigIotOffering } = bigiot;
// Configure your consumer API keys
const config = {
id: '',
secret: '',
//corsproxy: 'https://cors-anywhere.herokuapp.com', // Rate-limited, testing only
corsproxy: 'https://bigiot-cors.herokuapp.com', // Rate-limited, testing only
marketplace: undefined, // default
};
// Put the output on the page
function log(status, data) {
try {
console.log(status, data);
const s = document.createElement('p');
s.innerHTML = status;
document.body.appendChild(s);
if (typeof data !== 'undefined') {
if (typeof data.message !== 'undefined') {
data = data.message;
}
const d = document.createElement('p');
d.innerHTML = JSON.stringify(data, null, 2);
document.body.appendChild(d);
}
} catch (e) {
console.error('error logging', e);
}
}
// Create a new consumer instance with your credentials
const consumer = new BigIotConsumer(config.id, config.secret, undefined, config.corsproxy);
// Authenticate your consumer
consumer.authenticate().then(() => {
// authentication is done
log('Consumer authentication successful!');
const query = new BigIotOffering('Parking spaces', 'urn:big-iot:ParkingSpaceCategory');
// Remove requirements we don't care about
delete query.license;
delete query.extent;
delete query.price;
consumer.subscribe('Thingful-Thingful-barcelona_air_temperature')
.then((offering) => {
log('Offering subscription successful!');
const accessParameters = {
longitude: 9.2,
latitude: 42.0,
radius: 10000000,
};
return consumer.access(offering, accessParameters).then((response) => {
log('Response: ', response);
});
}, (err) => {
log('subscription error', err);
});
}).catch((err) => {
log('auth err', err);
});
</script>
</html>