Skip to content

Commit e89f437

Browse files
committed
Add hidden fields
1 parent 22ff2d4 commit e89f437

File tree

13 files changed

+930
-767
lines changed

13 files changed

+930
-767
lines changed

iot-devices/app.js

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ const MyCache = require('./lib/cache');
99

1010
/* global MQTT_CLIENT */
1111
const DEVICE_TRANSPORT = process.env.DUMMY_DEVICES_TRANSPORT || 'HTTP';
12+
const DEVICE_PAYLOAD = process.env.DUMMY_DEVICES_PAYLOAD || 'ultralight';
1213

1314
// The motion sensor offers no commands, hence it does not need an endpoint.
1415

1516
// parse everything as a stream of text
1617
function rawBody(req, res, next) {
17-
req.setEncoding('utf8');
18-
req.body = '';
19-
req.on('data', function (chunk) {
20-
req.body += chunk;
21-
});
22-
req.on('end', function () {
23-
next();
24-
});
18+
req.setEncoding('utf8');
19+
req.body = '';
20+
req.on('data', function (chunk) {
21+
req.body += chunk;
22+
});
23+
req.on('end', function () {
24+
next();
25+
});
2526
}
2627

2728
const iot = express();
@@ -32,84 +33,89 @@ const mqttBrokerUrl = process.env.MQTT_BROKER_URL || 'mqtt://mosquitto';
3233
global.MQTT_CLIENT = mqtt.connect(mqttBrokerUrl);
3334

3435
const iotRouter = express.Router();
36+
37+
debug(`Devices use a ${DEVICE_PAYLOAD} payload`);
3538
// If the Ultralight Dummy Devices are configured to use the HTTP transport, then
3639
// listen to the command endpoints using HTTP
3740
if (DEVICE_TRANSPORT === 'HTTP') {
38-
debug('Listening on HTTP endpoints: /iot/water, iot/tractor, /iot/filling');
39-
// The router listening on the IoT port is responding to commands going southbound only.
40-
// Therefore we need a route for each actuator
41-
iotRouter.post('/iot/water:id', Southbound.HTTP.water);
42-
iotRouter.post('/iot/tractor:id', Southbound.HTTP.tractor);
43-
iotRouter.post('/iot/filling:id', Southbound.HTTP.filling);
41+
debug('Listening on HTTP endpoints: /iot/water, iot/tractor, /iot/filling');
42+
// The router listening on the IoT port is responding to commands going southbound only.
43+
// Therefore we need a route for each actuator
44+
iotRouter.post('/iot/water:id', Southbound.HTTP.water);
45+
iotRouter.post('/iot/tractor:id', Southbound.HTTP.tractor);
46+
iotRouter.post('/iot/filling:id', Southbound.HTTP.filling);
4447
}
4548

4649
iotRouter.get('/status', (req, res) => {
47-
IoTDevices.emitOverallFarmStatus();
48-
res.status(200).send();
50+
IoTDevices.emitOverallFarmStatus();
51+
res.status(200).send();
4952
});
5053

5154
iotRouter.get('/devices/:type', (req, res) => {
52-
IoTDevices.fireDevices(req.params.type);
53-
res.status(204).send();
55+
IoTDevices.fireDevices(req.params.type);
56+
res.status(204).send();
5457
});
5558

5659
iotRouter.put('/devices/tractor', (req, res) => {
57-
IoTDevices.updateTractorStatus();
58-
res.status(204).send();
60+
IoTDevices.updateTractorStatus();
61+
res.status(204).send();
5962
});
6063

6164
iotRouter.put('/devices', (req, res) => {
62-
MyCache.setCacheValues(JSON.parse(req.body));
63-
res.status(204).send();
65+
MyCache.setCacheValues(JSON.parse(req.body));
66+
res.status(204).send();
6467
});
6568

6669
iotRouter.get('/animals', (req, res) => {
67-
IoTDevices.fireAnimalCollars();
68-
res.status(204).send();
70+
IoTDevices.fireAnimalCollars();
71+
res.status(204).send();
6972
});
7073

7174
iotRouter.put('/barndoor', (req, res) => {
72-
IoTDevices.barnDoor();
73-
res.status(204).send();
75+
IoTDevices.barnDoor();
76+
res.status(204).send();
7477
});
7578
iotRouter.put('/weather', (req, res) => {
76-
IoTDevices.alterWeather(req.body.action);
77-
res.status(204).send();
79+
IoTDevices.alterWeather(req.body.action);
80+
res.status(204).send();
7881
});
7982
iotRouter.put('/temperature/:id', (req, res) => {
80-
IoTDevices.initDevices();
81-
IoTDevices.alterTemperature(req.params.id, req.body.raise);
82-
res.status(204).send();
83+
IoTDevices.initDevices();
84+
IoTDevices.alterTemperature(req.params.id, req.body.raise);
85+
res.status(204).send();
8386
});
8487
iot.use('/', iotRouter);
8588
iot.use('/health', require('express-healthcheck')());
8689

8790
// If the IoT Devices are configured to use the MQTT transport, then
8891
// subscribe to the assoicated topics for each device.
8992
if (DEVICE_TRANSPORT === 'MQTT') {
90-
const apiKeys = process.env.DUMMY_DEVICES_API_KEYS || process.env.DUMMY_DEVICES_API_KEY || '1234';
91-
92-
MQTT_CLIENT.on('connect', () => {
93-
apiKeys.split(',').forEach((apiKey) => {
94-
const topic = '/' + apiKey + '/#';
95-
debug('Subscribing to MQTT Broker: ' + mqttBrokerUrl + ' ' + topic);
96-
MQTT_CLIENT.subscribe(topic);
97-
MQTT_CLIENT.subscribe(topic + '/#');
98-
});
93+
const apiKeys =
94+
process.env.DUMMY_DEVICES_API_KEYS ||
95+
process.env.DUMMY_DEVICES_API_KEY ||
96+
'1234';
97+
98+
MQTT_CLIENT.on('connect', () => {
99+
apiKeys.split(',').forEach((apiKey) => {
100+
const topic = '/' + apiKey + '/#';
101+
debug('Subscribing to MQTT Broker: ' + mqttBrokerUrl + ' ' + topic);
102+
MQTT_CLIENT.subscribe(topic);
103+
MQTT_CLIENT.subscribe(topic + '/#');
99104
});
105+
});
100106

101-
mqtt.connect(mqttBrokerUrl);
107+
mqtt.connect(mqttBrokerUrl);
102108

103-
MQTT_CLIENT.on('message', function (topic, message) {
104-
// message is a buffer. The IoT devices will be listening and
105-
// responding to commands going southbound.
106-
Southbound.MQTT.process(topic.toString(), message.toString());
107-
});
109+
MQTT_CLIENT.on('message', function (topic, message) {
110+
// message is a buffer. The IoT devices will be listening and
111+
// responding to commands going southbound.
112+
Southbound.MQTT.process(topic.toString(), message.toString());
113+
});
108114
}
109115

110116
// catch 404 and forward to error handler
111117
iot.use(function (req, res) {
112-
res.status(404).send(new createError.NotFound());
118+
res.status(404).send(new createError.NotFound());
113119
});
114120

115121
module.exports = iot;

iot-devices/bin/devices.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ const PORT = process.env.DUMMY_DEVICES_PORT || 3001;
55
const clusterWorkerSize = os.cpus().length;
66

77
if (clusterWorkerSize > 1) {
8-
if (cluster.isMaster) {
9-
for (let i = 0; i < clusterWorkerSize; i++) {
10-
cluster.fork();
11-
}
12-
cluster.on('exit', function (worker) {
13-
debug('Worker', worker.id, ' has exited.');
14-
cluster.fork();
15-
});
16-
} else {
17-
// mount `exampleProxy` in web server
18-
const app = require('../app');
19-
app.listen(PORT, function () {
20-
debug(`Server listening on port ${PORT} and worker ${process.pid}`);
21-
console.log(`Server listening on port ${PORT} and worker ${process.pid}`);
22-
});
8+
if (cluster.isMaster) {
9+
for (let i = 0; i < clusterWorkerSize; i++) {
10+
cluster.fork();
2311
}
24-
} else {
12+
cluster.on('exit', function (worker) {
13+
debug('Worker', worker.id, ' has exited.');
14+
cluster.fork();
15+
});
16+
} else {
17+
// mount `exampleProxy` in web server
2518
const app = require('../app');
2619
app.listen(PORT, function () {
27-
debug(`Server listening on port ${PORT} with the single worker ${process.pid}`);
20+
debug(`Server listening on port ${PORT} and worker ${process.pid}`);
21+
console.log(`Server listening on port ${PORT} and worker ${process.pid}`);
2822
});
23+
}
24+
} else {
25+
const app = require('../app');
26+
app.listen(PORT, function () {
27+
debug(
28+
`Server listening on port ${PORT} with the single worker ${process.pid}`
29+
);
30+
});
2931
}

iot-devices/bin/healthcheck.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@ const path = process.env.HEALTHCHECK_PATH || '/health';
66
const httpCode = process.env.HEALTHCHECK_CODE || 200;
77

88
const options = {
9-
host: 'localhost',
10-
port,
11-
timeout: 2000,
12-
method: 'GET',
13-
path
9+
host: 'localhost',
10+
port,
11+
timeout: 2000,
12+
method: 'GET',
13+
path,
1414
};
1515

1616
const request = http.request(options, (result) => {
17-
// eslint-disable-next-line no-console
18-
console.info(`Performed health check, result ${result.statusCode}`);
19-
if (result.statusCode === httpCode) {
20-
process.exit(0);
21-
} else {
22-
process.exit(1);
23-
}
17+
// eslint-disable-next-line no-console
18+
console.info(`Performed health check, result ${result.statusCode}`);
19+
if (result.statusCode === httpCode) {
20+
process.exit(0);
21+
} else {
22+
process.exit(1);
23+
}
2424
});
2525

2626
request.on('error', (err) => {
27-
// eslint-disable-next-line no-console
28-
console.error(`An error occurred while performing health check, error: ${err}`);
29-
process.exit(1);
27+
// eslint-disable-next-line no-console
28+
console.error(
29+
`An error occurred while performing health check, error: ${err}`
30+
);
31+
process.exit(1);
3032
});
3133

3234
request.end();

iot-devices/controllers/iot/northbound.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@ module.exports = {
7979
Measure.sendAsMQTT(deviceId, state);
8080
}
8181
},
82+
format(state) {
83+
return Measure.format(state);
84+
},
8285
};
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
const OAuth2 = require('../lib/oauth2').OAuth2;
22
const keyrockPort = process.env.KEYROCK_PORT || '3005';
3-
const keyrockUrl = (process.env.KEYROCK_URL || 'http://localhost') + ':' + keyrockPort;
4-
const keyrockIPAddress = (process.env.KEYROCK_IP_ADDRESS || 'http://127.0.0.1') + ':' + keyrockPort;
5-
const clientId = process.env.KEYROCK_CLIENT_ID || 'tutorial-dckr-site-0000-xpresswebapp';
6-
const clientSecret = process.env.KEYROCK_CLIENT_SECRET || 'tutorial-dkcr-site-0000-clientsecret';
3+
const keyrockUrl =
4+
(process.env.KEYROCK_URL || 'http://localhost') + ':' + keyrockPort;
5+
const keyrockIPAddress =
6+
(process.env.KEYROCK_IP_ADDRESS || 'http://127.0.0.1') + ':' + keyrockPort;
7+
const clientId =
8+
process.env.KEYROCK_CLIENT_ID || 'tutorial-dckr-site-0000-xpresswebapp';
9+
const clientSecret =
10+
process.env.KEYROCK_CLIENT_SECRET || 'tutorial-dkcr-site-0000-clientsecret';
711
const port = process.env.WEB_APP_PORT || '3000';
8-
const callbackURL = process.env.CALLBACK_URL || 'http://localhost:' + port + '/login';
12+
const callbackURL =
13+
process.env.CALLBACK_URL || 'http://localhost:' + port + '/login';
914

1015
// Creates oauth library object with the config data
1116
const oa = new OAuth2(
12-
clientId,
13-
clientSecret,
14-
keyrockUrl,
15-
keyrockIPAddress,
16-
'/oauth2/authorize',
17-
'/oauth2/token',
18-
callbackURL
17+
clientId,
18+
clientSecret,
19+
keyrockUrl,
20+
keyrockIPAddress,
21+
'/oauth2/authorize',
22+
'/oauth2/token',
23+
callbackURL
1924
);
2025

2126
module.exports = {
22-
oa
27+
oa,
2328
};

iot-devices/lib/cache.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,54 @@ const redis = require('redis');
44
const keys = [];
55
let client;
66

7-
const REDIS_URL = 'redis://' + (process.env.REDIS_HOST || 'localhost') + ':' + (process.env.REDIS_PORT || 6379);
7+
const REDIS_URL =
8+
'redis://' +
9+
(process.env.REDIS_HOST || 'localhost') +
10+
':' +
11+
(process.env.REDIS_PORT || 6379);
812

913
exports.init = async function () {
10-
client = await redis
11-
.createClient({
12-
url: REDIS_URL
13-
})
14-
.on('error', (err) => debug('Redis Client Error', err))
15-
.connect();
14+
client = await redis
15+
.createClient({
16+
url: REDIS_URL,
17+
})
18+
.on('error', (err) => debug('Redis Client Error', err))
19+
.connect();
1620
};
1721

1822
exports.get = async function (key) {
19-
const value = await client.get(key);
20-
return value;
23+
const value = await client.get(key);
24+
return value;
2125
};
2226

2327
exports.set = async function (key, value) {
24-
if (keys.includes(key) === false) {
25-
keys.push(key);
26-
}
27-
return await client.set(key, value);
28+
if (keys.includes(key) === false) {
29+
keys.push(key);
30+
}
31+
return await client.set(key, value);
2832
};
2933

3034
exports.setCacheValues = function (data) {
31-
Object.keys(data).forEach(async (key) => {
32-
if (keys.includes(key) === false) {
33-
keys.push(key);
34-
}
35-
debug(`${key}, ${data[key]}`);
36-
await client.set(key, data[key]);
37-
});
35+
Object.keys(data).forEach(async (key) => {
36+
if (keys.includes(key) === false) {
37+
keys.push(key);
38+
}
39+
debug(`${key}, ${data[key]}`);
40+
await client.set(key, data[key]);
41+
});
3842
};
3943

4044
exports.keys = function () {
41-
return keys;
45+
return keys;
4246
};
4347

4448
exports.exists = function (key) {
45-
return client.exists(key, (err, reply) => {
46-
if (err) {
47-
debug(err);
48-
return false;
49-
} else {
50-
return reply === 1;
51-
}
52-
});
49+
return client.exists(key, (err, reply) => {
50+
if (err) {
51+
debug(err);
52+
return false;
53+
} else {
54+
return reply === 1;
55+
}
56+
});
5357
};

0 commit comments

Comments
 (0)