Skip to content

Commit a8c367d

Browse files
committed
Run Prettier
1 parent 7c05070 commit a8c367d

File tree

3 files changed

+86
-81
lines changed

3 files changed

+86
-81
lines changed

iot-devices/app.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ iotRouter.get('/status', (req, res) => {
5757
});
5858

5959
iotRouter.get('/devices/:type', (req, res) => {
60-
IoTDevices.fireDevices(req.params.type);
61-
res.status(204).send();
60+
const result = IoTDevices.fireDevices(req.params.type);
61+
res.status(result ? 204 : 401).send();
6262
});
6363

6464
iotRouter.put('/devices/tractor', (req, res) => {
@@ -72,8 +72,8 @@ iotRouter.put('/devices', (req, res) => {
7272
});
7373

7474
iotRouter.get('/animals', (req, res) => {
75-
IoTDevices.fireAnimalCollars();
76-
res.status(204).send();
75+
const result = IoTDevices.fireAnimalCollars();
76+
res.status(result ? 204 : 401).send();
7777
});
7878

7979
iotRouter.put('/barndoor', (req, res) => {

iot-devices/lib/ngsi-ld.js

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,83 @@
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';
43
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">';
810

911
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+
}
1820
}
1921

2022
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;
3436
}
3537

3638
// This is a promise to make an HTTP GET request to the
3739
// /ngsi-ld/v1/entities/ end point
3840
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+
});
7053
}
7154

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+
};
8570

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+
);
8681

82+
console.log(entities);
83+
};

iot-devices/models/devices.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const debug = require('debug')('devices:devices');
1010
const Northbound = require('../controllers/iot/northbound');
1111
const Emitter = require('../lib/emitter');
1212
const Writer = require('../lib/writer');
13-
const NgsiLd = require('../lib/ngsi-ld')
13+
const NgsiLd = require('../lib/ngsi-ld');
1414

1515
// A series of constants used by our set of devices
1616

@@ -315,14 +315,16 @@ function fireAnimalCollars() {
315315
myCache.get('barn').then((state) => {
316316
if (state === 'door-open') {
317317
sendAnimalCollarReadings();
318+
return true;
318319
}
320+
return false;
319321
});
320322
}
321323

322324
function sendAnimalCollarReadings() {
323325
const deviceIds = myCache.keys();
324326
_.forEach(deviceIds, (deviceId) => {
325-
getDeviceState(deviceId).then( async (state) => {
327+
getDeviceState(deviceId).then(async (state) => {
326328
const isSensor = true;
327329
let targetRate;
328330

@@ -341,7 +343,7 @@ function sendAnimalCollarReadings() {
341343
state.d = PIG_STATE[getRandom() % 6];
342344
}
343345
} else {
344-
randomWalk(state, deviceId, 13.35073,52.51839);
346+
randomWalk(state, deviceId, 13.35073, 52.51839);
345347
if (getRandom() > 7) {
346348
state.d =
347349
getRandom() > 3 ? PIG_STATE[getRandom() % 6] : 'AT_REST';
@@ -372,7 +374,7 @@ function sendAnimalCollarReadings() {
372374
state.d = COW_STATE[getRandom() % 6];
373375
}
374376
} else {
375-
randomWalk(state, deviceId, 13.34973,52.51139);
377+
randomWalk(state, deviceId, 13.34973, 52.51139);
376378
if (getRandom() > 8) {
377379
state.d =
378380
getRandom() > 7 ? COW_STATE[getRandom() % 6] : 'GRAZING';
@@ -381,8 +383,12 @@ function sendAnimalCollarReadings() {
381383
state.s = getStatusCode(state.d);
382384
if (state.o) {
383385
state.o++;
384-
const coords = state.gps.split(',')
385-
state.near = await NgsiLd.findNeighbour(coords[0], coords[1], 'Animal')
386+
const coords = state.gps.split(',');
387+
state.near = await NgsiLd.findNeighbour(
388+
coords[0],
389+
coords[1],
390+
'Animal'
391+
);
386392
}
387393
setDeviceState(deviceId, toUltraLight(state), isSensor);
388394
break;
@@ -402,7 +408,9 @@ function fireDevices(deviceType) {
402408
sendDeviceReading(deviceType, deviceId);
403409
}
404410
});
411+
return true;
405412
}
413+
return false;
406414
});
407415
}
408416

0 commit comments

Comments
 (0)