forked from CNXTEoEorg/guide-conveyor-rasp-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
200 lines (145 loc) · 5.51 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const express = require('express')
, path = require('path')
, http = require('http')
, bodyParser = require('body-parser')
, proxy = require('http-proxy-middleware')
, socketServer = require('socket.io')
, iotf = require('ibmiotf');
var io;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'src/app/assets')));
app.use(express.static(path.join(__dirname, 'static')));
app.use('/assets/img/carbon-icons.svg', express.static(__dirname + '/node_modules/carbon-icons/dist/carbon-icons.svg'));
// IoT Platform Connectivity related info
var basicConfig;
function configureCredentials(config) {
basicConfig = config;
}
try {
var BASIC_CONFIG = require(__dirname + '/basicConfig.json');
configureCredentials(BASIC_CONFIG);
} catch (error) {
console.log("Please add 'basicConfig.json' into the root folder with your IoT Platform credentials", error);
}
const org = basicConfig.org
, apiKey = basicConfig.apiKey
, apiToken = basicConfig.apiToken
, appId = "test2f23f232";
/* ===== IBM IoT Client Configs - START ===== */
var appClientConfig = {
org: basicConfig.org,
id: "test2f23f232",
"domain": "internetofthings.ibmcloud.com",
"auth-key": basicConfig.apiKey,
"auth-token": basicConfig.apiToken
};
var appClient = new iotf.IotfApplication(appClientConfig);
//setting the log level to trace. By default its 'warn'
appClient.log.setLevel('info');
var appClient = new iotf.IotfApplication(appClientConfig);
/* ===== IBM IoT Client Configs - END ===== */
var mqttClient;
var socketsOpen = [];
var devicesToSubscribeTo = [];
/* ===== Proxy calls to /api/** to the IBM IoT APIs ===== */
app.use('/api/**', proxy(
{
target: `https://${org}.internetofthings.ibmcloud.com/api/v0002`,
changeOrigin: true,
auth: `${apiKey}:${apiToken}`,
// onProxyRes: function(proxyRes, req, res) {
// console.log(proxyRes.path);
// },
pathRewrite: {
'^/api/' : ''
}
}
));
/* ===== MQTT client - LIVE DATA ===== */
const iot_host = `wss://${org}.messaging.internetofthings.ibmcloud.com`
, iot_clientid = `a:${org}:${appId}`;
// When the IoT client connects successfully
appClient.on("connect", function () {
console.log("IoTF client connected");
console.log(devicesToSubscribeTo);
if (devicesToSubscribeTo.length > 0) {
for (deviceId of devicesToSubscribeTo) {
appClient.subscribeToDeviceEvents("iot-conveyor-belt", deviceId, "sensorData", "json");
console.log(`Subscribed to ${deviceId}`);
}
devicesToSubscribeTo = [];
}
io.emit('message', {
type:'mqtt_status', text: {connected: true}
});
});
appClient.on("disconnect", function () {
console.log("IoTF client disconnected");
});
// When there's a new device Event
appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {
console.log("Device Event from :: " + deviceType + " : " + deviceId + " of event " + eventType + " with payload : " + payload);
io.emit('message', {type: 'new_sensorData', text: payload.toString()});
});
function mqttConnect() {
try {
if (!appClient.isConnected) appClient.connect();
} catch (e) {
console.error("Connect Unsuccessful", e);
}
}
function mqttDisconnect() {
try {
if (appClient.isConnected) appClient.disconnect();
} catch (e) {
console.error("Disconnect Unsuccessful", e);
}
}
/* ===== MQTT mqttClient --> END ===== */
app.get('*', (req, res) => {
res.sendFile(path.resolve('./static/index.html'));
});
const port = process.env.PORT || '3000';
const httpServer = http.createServer(app);
httpServer.listen(port, () => {
console.log(`APP running on localhost:${port}`);
});
io = socketServer(httpServer);
/* ===== socket.io client - LIVE DATA ===== */
io.on('connection', (socket) => {
console.log(`Socket ${socket.id} connected`);
if (!appClient.isConnected) mqttConnect();
socketsOpen.push(socket.id);
console.log("Sockets Open: ", socketsOpen);
socket.on('disconnect', function(){
console.log(`Socket ${socket.id} disconnected`);
var index = socketsOpen.indexOf(socket.id);
socketsOpen.splice(index, 1);
console.log("Sockets Open: ", socketsOpen);
if (socketsOpen.length === 0) mqttDisconnect();
});
socket.on('new-data', (message) => {
console.log("New Data: ", message);
io.emit('message', {type:'new-data', text: message});
});
socket.on('mqtt_status_inquiry', (message) => {
console.log("MQTT_STATUS_INQUIRY", appClient.isConnected ? "Connected" : "Disconnected");
io.emit('message', {type:'mqtt_status', text: {connected: appClient.isConnected}});
});
socket.on('mqtt_set', (message) => {
console.log("Set MQTT message: ", message);
var payload = JSON.parse(message);
if (appClient.isConnected) {
console.log((payload.turnOn ? '' : 'Un-') + 'Subscribed' + (payload.turnOn ? ' to ' : ' from ') + `${payload.deviceId}`);
if (payload.turnOn) appClient.subscribeToDeviceEvents("iot-conveyor-belt", payload.deviceId, "sensorData", "json");
else appClient.unsubscribeToDeviceEvents("iot-conveyor-belt", payload.deviceId, "sensorData", "json");
} else if (!appClient.isConnected && payload.turnOn) {
devicesToSubscribeTo.push(payload.deviceId);
}
});
});
/* ===== socket.io client --> END ===== */
//Track Deployment
require("cf-deployment-tracker-client").track();