Skip to content

Commit 0dd0bdc

Browse files
committed
Display added data
1 parent 0a9149b commit 0dd0bdc

15 files changed

Lines changed: 516 additions & 4 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('tutorial:device');
2+
const monitor = require('../../lib/monitoring');
3+
const ngsiLD = require('../../lib/ngsi-ld');
4+
const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld';
5+
const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">';
6+
7+
async function displayAgriDevice(req, res) {
8+
debug('displayAgriDevice');
9+
// If the user is not authorized, display the main page.
10+
if (!res.locals.authorized) {
11+
req.flash('error', 'Access Denied');
12+
return res.redirect('/');
13+
}
14+
try {
15+
monitor('NGSI', 'readEntity ' + req.params.id);
16+
const device = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('agri-device', { title: device.name, device });
22+
} catch (error) {
23+
const errorDetail = error.error | error;
24+
debug(errorDetail);
25+
// If no animal has been found, display an error screen
26+
return res.render('error', {
27+
title: `Error: ${errorDetail.title}`,
28+
message: errorDetail.detail,
29+
error: {
30+
stack: errorDetail.title
31+
}
32+
});
33+
}
34+
}
35+
36+
module.exports = {
37+
display: displayAgriDevice
38+
};

app/controllers/ngsi-ld/crop.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('tutorial:crop');
2+
const monitor = require('../../lib/monitoring');
3+
const ngsiLD = require('../../lib/ngsi-ld');
4+
const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld';
5+
const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">';
6+
7+
async function displayCrop(req, res) {
8+
debug('displayCrop');
9+
// If the user is not authorized, display the main page.
10+
if (!res.locals.authorized) {
11+
req.flash('error', 'Access Denied');
12+
return res.redirect('/');
13+
}
14+
try {
15+
monitor('NGSI', 'readEntity ' + req.params.id);
16+
const crop = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('crop', { title: crop.name, crop });
22+
} catch (error) {
23+
const errorDetail = error.error;
24+
debug(errorDetail);
25+
// If no animal has been found, display an error screen
26+
return res.render('error', {
27+
title: `Error: ${errorDetail.title}`,
28+
message: errorDetail.detail,
29+
error: {
30+
stack: errorDetail.title
31+
}
32+
});
33+
}
34+
}
35+
36+
module.exports = {
37+
display: displayCrop
38+
};

app/controllers/ngsi-ld/pest.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('tutorial:pest');
2+
const monitor = require('../../lib/monitoring');
3+
const ngsiLD = require('../../lib/ngsi-ld');
4+
const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld';
5+
const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">';
6+
7+
async function displayPest(req, res) {
8+
debug('displayPest');
9+
// If the user is not authorized, display the main page.
10+
if (!res.locals.authorized) {
11+
req.flash('error', 'Access Denied');
12+
return res.redirect('/');
13+
}
14+
try {
15+
monitor('NGSI', 'readEntity ' + req.params.id);
16+
const pest = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('pest', { title: pest.name, pest });
22+
} catch (error) {
23+
const errorDetail = error.error;
24+
debug(errorDetail);
25+
// If no animal has been found, display an error screen
26+
return res.render('error', {
27+
title: `Error: ${errorDetail.title}`,
28+
message: errorDetail.detail,
29+
error: {
30+
stack: errorDetail.title
31+
}
32+
});
33+
}
34+
}
35+
36+
module.exports = {
37+
display: displayPest
38+
};

app/controllers/ngsi-ld/soil.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('tutorial:soil');
2+
const monitor = require('../../lib/monitoring');
3+
const ngsiLD = require('../../lib/ngsi-ld');
4+
const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld';
5+
const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">';
6+
7+
async function displaySoil(req, res) {
8+
debug('displaySoil');
9+
// If the user is not authorized, display the main page.
10+
if (!res.locals.authorized) {
11+
req.flash('error', 'Access Denied');
12+
return res.redirect('/');
13+
}
14+
try {
15+
monitor('NGSI', 'readEntity ' + req.params.id);
16+
const soil = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('soil', { title: soil.name, soil });
22+
} catch (error) {
23+
const errorDetail = error.error;
24+
debug(errorDetail);
25+
// If no animal has been found, display an error screen
26+
return res.render('error', {
27+
title: `Error: ${errorDetail.title}`,
28+
message: errorDetail.detail,
29+
error: {
30+
stack: errorDetail.title
31+
}
32+
});
33+
}
34+
}
35+
36+
module.exports = {
37+
display: displaySoil
38+
};

app/controllers/ngsi-ld/weather.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const debug = require('debug')('tutorial:weather');
2+
const monitor = require('../../lib/monitoring');
3+
const ngsiLD = require('../../lib/ngsi-ld');
4+
const Context = process.env.IOTA_JSON_LD_CONTEXT || 'http://context/ngsi-context.jsonld';
5+
const LinkHeader = '<' + Context + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json">';
6+
7+
async function displayWeather(req, res) {
8+
debug('displayWeather');
9+
// If the user is not authorized, display the main page.
10+
if (!res.locals.authorized) {
11+
req.flash('error', 'Access Denied');
12+
return res.redirect('/');
13+
}
14+
try {
15+
monitor('NGSI', 'readEntity ' + req.params.id);
16+
const weather = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('weather', { title: weather.name, weather });
22+
} catch (error) {
23+
const errorDetail = error.error;
24+
debug(errorDetail);
25+
// If no animal has been found, display an error screen
26+
return res.render('error', {
27+
title: `Error: ${errorDetail.title}`,
28+
message: errorDetail.detail,
29+
error: {
30+
stack: errorDetail.title
31+
}
32+
});
33+
}
34+
}
35+
36+
module.exports = {
37+
display: displayWeather
38+
};

app/routes/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const monitor = require('../lib/monitoring');
44
const Device = require('../controllers/ngsi-ld/device');
55
const Farm = require('../controllers/ngsi-ld/farm');
66
const Animal = require('../controllers/ngsi-ld/animal');
7+
const Crop = require('../controllers/ngsi-ld/crop');
8+
const Soil = require('../controllers/ngsi-ld/soil');
9+
const Pest = require('../controllers/ngsi-ld/pest');
10+
const AgriDevice = require('../controllers/ngsi-ld/agri-device');
11+
const Weather = require('../controllers/ngsi-ld/weather');
712
const Land = require('../controllers/ngsi-ld/land');
813
const Person = require('../controllers/ngsi-ld/person');
914
const History = require('../controllers/history');
@@ -208,6 +213,11 @@ router.get('/app/animals/locations.json', Animal.geojson);
208213
router.get('/app/animals', Security.authenticate, Animal.displayMap);
209214
router.get('/app/animal/:id', Security.authenticate, Animal.display);
210215
router.get('/app/agriparcel/:id', Security.authenticate, Land.display);
216+
router.get('/app/crop/:id', Security.authenticate, Crop.display);
217+
router.get('/app/soil/:id', Security.authenticate, Soil.display);
218+
router.get('/app/pest/:id', Security.authenticate, Pest.display);
219+
router.get('/app/weather/:id', Security.authenticate, Weather.display);
220+
router.get('/app/agri-device/:id', Security.authenticate, AgriDevice.display);
211221

212222
router.get('/app/building/:id', Security.authenticate, Farm.display);
213223
router.get('/app/person/:id', Security.authenticate, Person.display);

app/views/agri-device.pug

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
6+
div.row
7+
div.col
8+
div(class="card m-2")
9+
div.card-body
10+
div.container
11+
div.row
12+
- if (device.description)
13+
dt.col-lg-3 Description:
14+
dd.col-lg-9 #{device.description}
15+
div.row
16+
- if (device.category)
17+
dt.col-lg-3 Category:
18+
dd.col-lg-9
19+
code #{device.category.vocab}
20+
div.row
21+
- if (device.controlledProperty)
22+
dt.col-lg-3 Controlled Property:
23+
dd.col-lg-9 #{device.controlledProperty}
24+
div.row
25+
- if (device.controlledAsset)
26+
dt.col-lg-3 ControlledAsset:
27+
dd.col-lg-9
28+
- if (device.controlledAsset.includes('AgriParcel'))
29+
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${device.controlledAsset}`) #{device.controlledAsset}
30+
- if (device.controlledAsset.includes('Animal'))
31+
a(class="link-underline link-underline-opacity-0" href=`/app/animal/${device.controlledAsset}`) #{device.controlledAsset}
32+
- if (device.controlledAsset.includes('Building'))
33+
a(class="link-underline link-underline-opacity-0" href=`/app/building/${device.controlledAsset}`) #{device.controlledAsset}
34+
div.row
35+
- if (device.batteryLevel)
36+
dt.col-lg-3 Battery Level:
37+
dd.col-lg-9 #{device.batteryLevel}
38+
div.row
39+
- if (device.supportedProtocol)
40+
dt.col-lg-3 Protocol:
41+
dd.col-lg-9 #{device.supportedProtocol}
42+
div.row
43+
- if (device.deviceModel)
44+
dt.col-lg-3 Description:
45+
dd.col-lg-9 #{device.deviceModel}
46+
div.row
47+
- if (device.location)
48+
dt.col-lg-3 Location:
49+
dd.col-lg-9 #{device.location.coordinates[1]}&deg;N, #{device.location.coordinates[0]}&deg;E
50+
51+
div.row
52+
- if (device.status)
53+
dt.col-lg-3 Status:
54+
dd.col-lg-9 #{device.status}
55+
56+
div.row
57+
div.col
58+
div(class="card m-2")
59+
div.card-body
60+
summary.list-unstyled
61+
details
62+
pre #{JSON.stringify(device, null, 4)}

app/views/animal.pug

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,24 @@ block content
7070
dt.col-lg-3 Located At:
7171
- if (animal.location)
7272
dd.col-lg-3
73-
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${animal.locatedAt.object}`) #{animal.locatedAt.object}
73+
- if (animal.locatedAt.object.includes('AgriParcel'))
74+
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${animal.locatedAt.object}`) #{animal.locatedAt.object}
75+
- if (animal.locatedAt.object.includes('Building'))
76+
a(class="link-underline link-underline-opacity-0" href=`/app/building/${animal.locatedAt.object}`) #{animal.locatedAt.object}
7477
- else
7578
dd.col-lg-9
76-
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${animal.locatedAt.object}`) #{animal.locatedAt.object}
79+
- if (animal.locatedAt.object.includes('AgriParcel'))
80+
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${animal.locatedAt.object}`) #{animal.locatedAt.object}
81+
- if (animal.locatedAt.object.includes('Building'))
82+
a(class="link-underline link-underline-opacity-0" href=`/app/building/${animal.locatedAt.object}`) #{animal.locatedAt.object}
7783
- if (animal.location && animal.location.value.coordinates)
7884
dt.col-lg-3 Current Location:
7985
dd.col-lg-3
8086
| #{animal.location.value.coordinates[1]}&deg;N, #{animal.location.value.coordinates[0]}&deg;E
8187
- if (animal.ownedBy)
8288
dt.col-lg-3 Owned By:
8389
dd.col-lg-9
84-
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${animal.ownedBy.object}`) #{animal.ownedBy.object}
90+
a(class="link-underline link-underline-opacity-0" href=`/app/person/${animal.ownedBy.object}`) #{animal.ownedBy.object}
8591

8692
- if (animal.comment)
8793
div.row

app/views/building.pug

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ block content
3737
dt.col-lg-3 Location:
3838
dd.col-lg-9 #{building.location.coordinates[1]}&deg;N, #{building.location.coordinates[0]}&deg;E
3939
dt.col-lg-3 Category:
40-
dd.col-lg-9 #{building.category}
40+
- if (building.category.vocab)
41+
dd.col-lg-9 #{building.category.vocab}
42+
- else
43+
dd.col-lg-9 #{building.category}
4144
dt.col-lg-3 Owner:
4245
dd.col-lg-9
4346
a(class="link-underline link-underline-opacity-0" href=`/app/person/${building.owner}`) #{building.owner}

app/views/crop.pug

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
6+
div.row
7+
div.col
8+
div(class="card m-2")
9+
div.card-body
10+
div.container
11+
div.row
12+
- if (crop.description)
13+
dt.col-lg-3 Description:
14+
dd.col-lg-9 #{crop.description}
15+
- if (crop.hasAgriSoil)
16+
dt.col-lg-3 Preferred Soils:
17+
dd.col-lg-9
18+
ul
19+
each item in crop.hasAgriSoil
20+
li
21+
a(class="link-underline link-underline-opacity-0" href=`/app/soil/${item}`) #{item}
22+
- if (crop.hasAgriPest)
23+
dt.col-lg-3 Pests:
24+
dd.col-lg-9
25+
ul
26+
each item in crop.hasAgriPest
27+
li
28+
a(class="link-underline link-underline-opacity-0" href=`/app/pest/${item}`) #{item}
29+
30+
div.row
31+
div.col
32+
div(class="card m-2")
33+
div.card-body
34+
summary.list-unstyled
35+
details
36+
pre #{JSON.stringify(crop, null, 4)}

0 commit comments

Comments
 (0)