-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
304 lines (248 loc) · 12.9 KB
/
index.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EOSIoT RFID Demo</title>
<script src='dist-web/eosjs-api.js'></script>
<script src='dist-web/eosjs-jsonrpc.js'></script>
<script src='dist-web/eosjs-jssig.js'></script>
<script src="dist-web/ngeohash.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<link rel="stylesheet" href="style.css" />
<link href="favicon.ico" type="image/x-icon" rel="icon">
<link href="favicon.ico" type="image/x-icon" rel="shortcut icon">
<meta name="description" content="Display data captured from an RFID tag data by an IoT device through the EOS blockchain on a map">
<meta name="keywords" content="rfid, iot, eos, blockchain, map, location, position, latitude, longitude, geohash">
</head>
<body>
<div id="header">
<img src="eosiot_logo_small.png">
<H1>EOSIoT RFID Demo</H1>
</div>
<div style="width: 740px; text-align: left; margin: auto;">
<p>This simple web-app is running entirely in your browser.
The EOS blockchain is the server.
</p>
<p>Your scanned RFID tag data is displayed below.
Scan a tag and see it show up here within seconds.</p>
<h2>Chain Info</h2>
<div id="chain_name">Name: EOS Jungle2.0 Testnet</div>
<div id="chain_id">ID: searching...</div>
<h2>RFID Scanner Data</h2>
Enter your RFID scanner device ID (e.g. 942140182):<br>
<input type="text" id="devid" value="0">
<button type="button" onclick="setDevId()">Apply</button>
<br><br>
Current scanner ID:
<p id="user_devid"></p>
<textarea name="textarea" rows="10" cols="100">Your RFID scanner data on the EOS blockchain shows up here when you enter a valid scanner device ID.
</textarea>
<br><br>
<script>
// These are records pushed by the scanner for use with the map: location and time
var dataRecords = [];
// Specify an EOS blockchain API endpoint to connect to.
// Note: use https rather than http to prevent the browser from blocking if accessed
// via http.
// Connect to the EOS Jungle2.0 Testnet. The testnet performs and operates
// similarly to the mainnet but with unlimited free-to-access resources. Because
// resources are free, this network is operated on a best-effort basis.
const rpc = new eosjs_jsonrpc.JsonRpc('https://jungle2.cryptolions.io');
// The EOS mainnet is the flagship network that rewards block miners (producers)
// with tokens that exchange for real fiat currency - so there is incentive to maintain this
// network. However because there is real value in the tokens powering this network,
// resources are scarce and come with a cost. Currently this cost is too high (CPU).
// We will be using the testnet for demonstration until the EOS core group figures out a
// solution to the CPU crunch.
//const rpc = new eosjs_jsonrpc.JsonRpc('https://eos.eoscafeblock.com');
let pre = document.getElementsByTagName('textarea')[0];
document.getElementById('devid').addEventListener('keydown', function onEvent(event) {
if (event.key === "Enter") {
setDevId();
return false;
}
});
var HasDevId = false;
var DevId;
function setDevId() {
try {
var devIdBoxValue = document.getElementById('devid').value;
DevId = parseInt(devIdBoxValue, 10);
document.getElementById("user_devid").innerHTML = DevId.toString(10);
HasDevId = true;
dataRecords.length = 0; // reset the data records
} catch (e) {
alert("invalid device ID. Require number." + e);
}
}
// try to connect to the chain
async function pollChain() {
try {
const resp = await rpc.get_info({
json: true // Get the response as json
});
document.getElementById("chain_id").innerHTML = "ID: " + resp.chain_id;
} catch (e) {
pre.textContent = 'Error contacting blockchain! Caught exception: ' + e;
if (e instanceof eosjs_jsonrpc.RpcError)
pre.textContent += '\n\n' + JSON.stringify(e.json, null, 2);
}
}
pollChain();
async function pollData() {
var featureData = [];
if (!HasDevId) return;
/*
{
"rows": [
{
"key": "eosiot11node",
"latency_stats": {
"min": "3.00000000000000000",
"max": "1556324782.00000000000000000",
"var": "0.00000000000000000",
"mean": "42062839.10810810327529907"
},
"scan_data": [
{
"scan_time": 4567,
"recv_time": 1556329349,
"dev_id": 1234,
"tag_id": [
1,
2,
3,
4,
5,
6,
7
],
"geohash": "abcdef"
},
*/
try {
const resp = await rpc.get_table_rows({
json: true, // Get the response as json
code: 'eosiot12rfid', // Contract that we target
scope: 'eosiot12rfid', // Account that owns the data
table: 'scanners', // Table name
limit: 10, // Maximum number of rows (scanners) that we want to get
});
pre.textContent = "Request submitted\n";
var rows = resp.rows;
// Construct list of scanned data that matches the given devID
var scannedData = [];
// There is one row for each scanner account registered in the system with data
// For the demo there is just the one demo account
for (var row in rows) {
console.log(rows[row]);
var accountName = rows[row].key;
if (accountName == "eosiot11node") {
var data = rows[row].scan_data;
// Scanner data is provided oldest first, newest last -> we want to reverse that
// and show the newest first.
for (var i = data.length - 1; i >= 0; i--) {
var d = data[i];
console.log(d.dev_id, DevId);
if (d.dev_id == DevId) {
// This record matches device ID
var scanTime = new Date(d.scan_time * 1000);
var tagId = "";
for (var j = 0; j < d.tag_id.length; j++) {
tagId += d.tag_id[j].toString(16);
if (j < d.tag_id.length - 1) tagId += " ";
}
var geohash = "";
if (d.geohash != undefined) {
geohash = d.geohash;
}
// Construct strings to print in the testbox window.
var record = scanTime.toISOString() + " | " + tagId + " | " + geohash;
if (geohash != undefined && geohash != "" && geohash.length > 0) {
// Contruct data records for use with the map.
var loc = ngeohash.decode(geohash);
if (loc != undefined) {
console.log("decoded: " + loc.latitude, loc.longitude);
var dataRecord = {
time: scanTime, // date object
lat: loc.latitude,
lon: loc.longitude,
}
record += " | " + loc.latitude + ", " + loc.longitude;
//dataRecords.length = 0;
//dataRecords.push(dataRecord);
var geojsonFeature = {
"type": "Feature",
"properties": {
"account": accountName,
"id": d.dev_id,
"popupContent": record
},
"geometry": {
"type": "Point",
"coordinates": [loc.longitude, loc.latitude]
}
};
var feature = L.geoJSON(geojsonFeature, {
onEachFeature: onEachFeature
});
feature.addTo(map);
featureData.push(feature);
}
}
scannedData.push(record);
console.log(record);
}
}
}
}
if (scannedData.length > 0) {
for (var d in scannedData) {
pre.textContent += scannedData[d] + "\n";
}
} else {
pre.textContent += "No data found for ID " + DevId + "\n";
}
} catch (e) {
pre.textContent = 'Error polling blockchain! Caught exception: ' + e;
if (e instanceof eosjs_jsonrpc.RpcError)
pre.textContent += '\n\n' + JSON.stringify(e.json, null, 2);
}
var group = new L.featureGroup(featureData);
map.fitBounds(group.getBounds().pad(0.5));
}
setInterval(pollData, 5000);
</script>
</div>
<div id="map" style="height: 500px; width: 1000px"></div>
<script>
function onEachFeature(feature, layer) {
var popupContent = "";
if (feature.properties && feature.properties.popupContent) {
popupContent += "<p>Account: " + feature.properties.account + " id: " + feature.properties.id + "</p>";
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
}
// Create a map
var map = L.map('map')
.setView([0, 0], 2);
// Create a tile layer and add it to the map.
L.tileLayer('http://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
</script>
<br />
<div style="width: 740px; text-align: left; margin: auto;">
<ul>
<li><a href="https://github.com/EOSIoT/rfid-scanner-node">Open source RFID device software.</a></li>
<li><a href="https://github.com/EOSIoT/rfid-contract">The EOS RFID smart contract.</a></li>
<li><a href="https://github.com/EOSIoT/rfid-html">This web-app's source code.</a></li>
</ul>
</div>
<br>
©EOSIoT 2020. EOSIoT builds value by bringing IoT and blockchain together.
</body>
</html>