Skip to content

Commit 0111d27

Browse files
committed
Add Agri-Farm
1 parent 0dd0bdc commit 0111d27

11 files changed

Lines changed: 137 additions & 10 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:farm');
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 displayAgriFarm(req, res) {
8+
debug('displayAgriFarm');
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 farm = await ngsiLD.readEntity(
17+
req.params.id,
18+
{ options: 'keyValues' },
19+
ngsiLD.setHeaders(req.session.access_token, LinkHeader)
20+
);
21+
return res.render('agri-farm', { title: farm.name, farm });
22+
} catch (error) {
23+
const errorDetail = error.error | error;
24+
debug(errorDetail);
25+
// If no farm 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: displayAgriFarm
38+
};

app/controllers/ngsi-ld/land.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function displayLand(req, res) {
2222
} catch (error) {
2323
const errorDetail = error.error;
2424
debug(errorDetail);
25-
// If no animal has been found, display an error screen
25+
// If no agri-parcel has been found, display an error screen
2626
return res.render('error', {
2727
title: `Error: ${errorDetail.title}`,
2828
message: errorDetail.detail,

app/controllers/ngsi-ld/pest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function displayPest(req, res) {
2222
} catch (error) {
2323
const errorDetail = error.error;
2424
debug(errorDetail);
25-
// If no animal has been found, display an error screen
25+
// If no pest has been found, display an error screen
2626
return res.render('error', {
2727
title: `Error: ${errorDetail.title}`,
2828
message: errorDetail.detail,

app/controllers/ngsi-ld/soil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function displaySoil(req, res) {
2222
} catch (error) {
2323
const errorDetail = error.error;
2424
debug(errorDetail);
25-
// If no animal has been found, display an error screen
25+
// If no soil has been found, display an error screen
2626
return res.render('error', {
2727
title: `Error: ${errorDetail.title}`,
2828
message: errorDetail.detail,

app/controllers/ngsi-ld/weather.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function displayWeather(req, res) {
2222
} catch (error) {
2323
const errorDetail = error.error;
2424
debug(errorDetail);
25-
// If no animal has been found, display an error screen
25+
// If no weather has been found, display an error screen
2626
return res.render('error', {
2727
title: `Error: ${errorDetail.title}`,
2828
message: errorDetail.detail,

app/routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Crop = require('../controllers/ngsi-ld/crop');
88
const Soil = require('../controllers/ngsi-ld/soil');
99
const Pest = require('../controllers/ngsi-ld/pest');
1010
const AgriDevice = require('../controllers/ngsi-ld/agri-device');
11+
const AgriFarm = require('../controllers/ngsi-ld/agri-farm');
1112
const Weather = require('../controllers/ngsi-ld/weather');
1213
const Land = require('../controllers/ngsi-ld/land');
1314
const Person = require('../controllers/ngsi-ld/person');
@@ -218,6 +219,7 @@ router.get('/app/soil/:id', Security.authenticate, Soil.display);
218219
router.get('/app/pest/:id', Security.authenticate, Pest.display);
219220
router.get('/app/weather/:id', Security.authenticate, Weather.display);
220221
router.get('/app/agri-device/:id', Security.authenticate, AgriDevice.display);
222+
router.get('/app/agri-farm/:id', Security.authenticate, AgriFarm.display);
221223

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

app/views/agri-device.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ block content
3434
div.row
3535
- if (device.batteryLevel)
3636
dt.col-lg-3 Battery Level:
37-
dd.col-lg-9 #{device.batteryLevel}
37+
dd.col-lg-9 #{device.batteryLevel * 100} %
3838
div.row
3939
- if (device.supportedProtocol)
4040
dt.col-lg-3 Protocol:
41-
dd.col-lg-9 #{device.supportedProtocol}
41+
dd.col-lg-9 #{device.supportedProtocol.toUpperCase()}
4242
div.row
4343
- if (device.deviceModel)
4444
dt.col-lg-3 Description:

app/views/agri-farm.pug

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
div.col-sm-4
13+
dl.row
14+
dt address:
15+
dd
16+
address
17+
| #{farm.address.streetAddress} #[br]
18+
| #{farm.address.addressLocality} #[br]
19+
| #{farm.address.postalCode} #[br]
20+
| #{farm.address.addressRegion} #[br]
21+
div.row
22+
- if (farm.description)
23+
dt.col-lg-3 Description:
24+
dd.col-lg-9 #{farm.description}
25+
div.row
26+
- if (farm.category)
27+
dt.col-lg-3 Category:
28+
dd.col-lg-9
29+
code #{farm.category.vocab}
30+
div.row
31+
- if (farm.controlledProperty)
32+
dt.col-lg-3 Controlled Property:
33+
dd.col-lg-9 #{farm.controlledProperty}
34+
div.row
35+
- if (farm.hasAgriParcel)
36+
dt.col-lg-3 Agri Parcel:
37+
dd.col-lg-9
38+
ul
39+
each item in farm.hasAgriParcel
40+
li
41+
a(class="link-underline link-underline-opacity-0" href=`/app/agriparcel/${item}`) #{item}
42+
43+
div.row
44+
- if (farm.hasBuilding)
45+
dt.col-lg-3 Buildings:
46+
dd.col-lg-9
47+
ul
48+
each item in farm.hasBuilding
49+
li
50+
a(class="link-underline link-underline-opacity-0" href=`/app/building/${item}`) #{item}
51+
52+
div.row
53+
- if (farm.location)
54+
dt.col-lg-3 Location:
55+
dd.col-lg-9 #{farm.location.coordinates[1]}&deg;N, #{farm.location.coordinates[0]}&deg;E
56+
57+
div.row
58+
- if (farm.owner)
59+
dt.col-lg-3 Owned By:
60+
dd.col-lg-9
61+
a(class="link-underline link-underline-opacity-0" href=`/app/person/${farm.owner}`) #{farm.owner}
62+
63+
div.row
64+
div.col
65+
div(class="card m-2")
66+
div.card-body
67+
summary.list-unstyled
68+
details
69+
pre #{JSON.stringify(farm, null, 4)}

app/views/animal.pug

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,27 @@ block content
8181
- if (animal.locatedAt.object.includes('Building'))
8282
a(class="link-underline link-underline-opacity-0" href=`/app/building/${animal.locatedAt.object}`) #{animal.locatedAt.object}
8383
- if (animal.location && animal.location.value.coordinates)
84-
dt.col-lg-3 Current Location:
84+
dt.col-lg-3
85+
| Current Location:
86+
- if (animal.location && animal.location.providedBy)
87+
br
88+
small
89+
| Data Provided by
8590
dd.col-lg-3
8691
| #{animal.location.value.coordinates[1]}&deg;N, #{animal.location.value.coordinates[0]}&deg;E
92+
93+
- if (animal.location && animal.location.providedBy)
94+
br
95+
small
96+
a(class="link-underline link-underline-opacity-0" href=`/app/agri-device/${animal.location.providedBy.object}`) #{animal.location.providedBy.object}
8797
- if (animal.ownedBy)
8898
dt.col-lg-3 Owned By:
8999
dd.col-lg-9
90100
a(class="link-underline link-underline-opacity-0" href=`/app/person/${animal.ownedBy.object}`) #{animal.ownedBy.object}
91101

102+
103+
104+
92105
- if (animal.comment)
93106
div.row
94107
div.col

app/views/building.pug

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ block content
3838
dd.col-lg-9 #{building.location.coordinates[1]}&deg;N, #{building.location.coordinates[0]}&deg;E
3939
dt.col-lg-3 Category:
4040
- if (building.category.vocab)
41-
dd.col-lg-9 #{building.category.vocab}
41+
dd.col-lg-9
42+
code #{building.category.vocab}
4243
- else
43-
dd.col-lg-9 #{building.category}
44+
dd.col-lg-9
45+
code #{building.category}
4446
dt.col-lg-3 Owner:
4547
dd.col-lg-9
4648
a(class="link-underline link-underline-opacity-0" href=`/app/person/${building.owner}`) #{building.owner}

0 commit comments

Comments
 (0)