|
1 | | - |
2 | | - |
3 | | -const BASE_PATH = process.env.CONTEXT_BROKER || 'http://localhost:1026/ngsi-ld/v1'; |
| 1 | +const BASE_PATH = |
| 2 | + process.env.CONTEXT_BROKER || 'http://localhost:1026/ngsi-ld/v1'; |
4 | 3 | const JSON_LD_HEADER = 'application/ld+json'; |
5 | | -const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld'; |
6 | | -const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">'; |
7 | | - |
| 4 | +const Context = |
| 5 | + process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld'; |
| 6 | +const LinkHeader = |
| 7 | + '<' + |
| 8 | + Context + |
| 9 | + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">'; |
8 | 10 |
|
9 | 11 | async function parse(response) { |
10 | | - let text = ''; |
11 | | - try { |
12 | | - text = await response.text(); |
13 | | - const data = JSON.parse(text); |
14 | | - return data; |
15 | | - } catch (err) { |
16 | | - return text; |
17 | | - } |
| 12 | + let text = ''; |
| 13 | + try { |
| 14 | + text = await response.text(); |
| 15 | + const data = JSON.parse(text); |
| 16 | + return data; |
| 17 | + } catch (err) { |
| 18 | + return text; |
| 19 | + } |
18 | 20 | } |
19 | 21 |
|
20 | 22 | function setHeaders(accessToken, link, contentType) { |
21 | | - const headers = {}; |
22 | | - if (accessToken) { |
23 | | - // If the system has been secured and we have logged in, |
24 | | - // add the access token to the request to the PEP Proxy |
25 | | - headers['X-Auth-Token'] = accessToken; |
26 | | - } |
27 | | - if (link) { |
28 | | - headers.Link = link; |
29 | | - } |
30 | | - if (contentType) { |
31 | | - headers['Content-Type'] = contentType || JSON_LD_HEADER; |
32 | | - } |
33 | | - return headers; |
| 23 | + const headers = {}; |
| 24 | + if (accessToken) { |
| 25 | + // If the system has been secured and we have logged in, |
| 26 | + // add the access token to the request to the PEP Proxy |
| 27 | + headers['X-Auth-Token'] = accessToken; |
| 28 | + } |
| 29 | + if (link) { |
| 30 | + headers.Link = link; |
| 31 | + } |
| 32 | + if (contentType) { |
| 33 | + headers['Content-Type'] = contentType || JSON_LD_HEADER; |
| 34 | + } |
| 35 | + return headers; |
34 | 36 | } |
35 | 37 |
|
36 | 38 | // This is a promise to make an HTTP GET request to the |
37 | 39 | // /ngsi-ld/v1/entities/ end point |
38 | 40 | function listEntities(opts, headers = {}) { |
39 | | - |
40 | | - console.log(`${BASE_PATH}/entities/?${new URLSearchParams(opts)}`) |
41 | | - return fetch(`${BASE_PATH}/entities/?${new URLSearchParams(opts)}`, { |
42 | | - method: 'GET', |
43 | | - headers |
44 | | - }) |
45 | | - .then((r) => parse(r).then((data) => ({ status: r.status, body: data }))) |
46 | | - .then((data) => { |
47 | | - if (data.status !== 200) { |
48 | | - throw new Error(data.body); |
49 | | - } |
50 | | - return data.body; |
51 | | - }); |
52 | | -} |
53 | | - |
54 | | - |
55 | | -exports.findNeighbour = async function (lat, lng, type){ |
56 | | - const headers = setHeaders(null, LinkHeader); |
57 | | - const entities = await listEntities( |
58 | | - { |
59 | | - type, |
60 | | - pick: 'id', |
61 | | - limit: 2, |
62 | | - geometry: "Point", |
63 | | - coordinates: `[${lat},${lng}]`, |
64 | | - georel: "near;maxDistance==8000" |
65 | | - }, |
66 | | - headers |
67 | | - ); |
68 | | - |
69 | | - return entities[1].id; |
| 41 | + console.log(`${BASE_PATH}/entities/?${new URLSearchParams(opts)}`); |
| 42 | + return fetch(`${BASE_PATH}/entities/?${new URLSearchParams(opts)}`, { |
| 43 | + method: 'GET', |
| 44 | + headers, |
| 45 | + }) |
| 46 | + .then((r) => parse(r).then((data) => ({ status: r.status, body: data }))) |
| 47 | + .then((data) => { |
| 48 | + if (data.status !== 200) { |
| 49 | + throw new Error(data.body); |
| 50 | + } |
| 51 | + return data.body; |
| 52 | + }); |
70 | 53 | } |
71 | 54 |
|
72 | | -exports.findTargetInField = async function (type, locatedAt){ |
73 | | - const headers = setHeaders(null, LinkHeader); |
74 | | - const entities = await listEntities( |
75 | | - { |
76 | | - type, |
77 | | - pick: 'id,location', |
78 | | - limit: 6 |
79 | | - }, |
80 | | - headers |
81 | | - ); |
82 | | - |
83 | | - console.log(entities); |
84 | | -} |
| 55 | +exports.findNeighbour = async function (lat, lng, type) { |
| 56 | + const headers = setHeaders(null, LinkHeader); |
| 57 | + const entities = await listEntities( |
| 58 | + { |
| 59 | + type, |
| 60 | + pick: 'id', |
| 61 | + limit: 2, |
| 62 | + geometry: 'Point', |
| 63 | + coordinates: `[${lat},${lng}]`, |
| 64 | + georel: 'near;maxDistance==8000', |
| 65 | + }, |
| 66 | + headers |
| 67 | + ); |
| 68 | + return entities[1].id; |
| 69 | +}; |
85 | 70 |
|
| 71 | +exports.findTargetInField = async function (type, locatedAt) { |
| 72 | + const headers = setHeaders(null, LinkHeader); |
| 73 | + const entities = await listEntities( |
| 74 | + { |
| 75 | + type, |
| 76 | + pick: 'id,location', |
| 77 | + limit: 6, |
| 78 | + }, |
| 79 | + headers |
| 80 | + ); |
86 | 81 |
|
| 82 | + console.log(entities); |
| 83 | +}; |
0 commit comments