@@ -9,19 +9,20 @@ const MyCache = require('./lib/cache');
99
1010/* global MQTT_CLIENT */
1111const 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
1617function 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
2728const iot = express ( ) ;
@@ -32,84 +33,89 @@ const mqttBrokerUrl = process.env.MQTT_BROKER_URL || 'mqtt://mosquitto';
3233global . MQTT_CLIENT = mqtt . connect ( mqttBrokerUrl ) ;
3334
3435const 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
3740if ( 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
4649iotRouter . get ( '/status' , ( req , res ) => {
47- IoTDevices . emitOverallFarmStatus ( ) ;
48- res . status ( 200 ) . send ( ) ;
50+ IoTDevices . emitOverallFarmStatus ( ) ;
51+ res . status ( 200 ) . send ( ) ;
4952} ) ;
5053
5154iotRouter . 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
5659iotRouter . put ( '/devices/tractor' , ( req , res ) => {
57- IoTDevices . updateTractorStatus ( ) ;
58- res . status ( 204 ) . send ( ) ;
60+ IoTDevices . updateTractorStatus ( ) ;
61+ res . status ( 204 ) . send ( ) ;
5962} ) ;
6063
6164iotRouter . 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
6669iotRouter . get ( '/animals' , ( req , res ) => {
67- IoTDevices . fireAnimalCollars ( ) ;
68- res . status ( 204 ) . send ( ) ;
70+ IoTDevices . fireAnimalCollars ( ) ;
71+ res . status ( 204 ) . send ( ) ;
6972} ) ;
7073
7174iotRouter . put ( '/barndoor' , ( req , res ) => {
72- IoTDevices . barnDoor ( ) ;
73- res . status ( 204 ) . send ( ) ;
75+ IoTDevices . barnDoor ( ) ;
76+ res . status ( 204 ) . send ( ) ;
7477} ) ;
7578iotRouter . 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} ) ;
7982iotRouter . 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} ) ;
8487iot . use ( '/' , iotRouter ) ;
8588iot . 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.
8992if ( 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
111117iot . use ( function ( req , res ) {
112- res . status ( 404 ) . send ( new createError . NotFound ( ) ) ;
118+ res . status ( 404 ) . send ( new createError . NotFound ( ) ) ;
113119} ) ;
114120
115121module . exports = iot ;
0 commit comments