Commit aa73b06c authored by Angela Yen's avatar Angela Yen

Merge branch 'feature/memory-leak' into 'master'

Feature/memory leak

See merge request cascade-examples/rigado-node-hello-world!6
parents bb3832f8 7d5c4cea
......@@ -9,10 +9,10 @@ node_modules/
### Snapcraft ###
snap/
prime/
parts/
stage/
snap/.snapcraft/
*.snap
*.tar.bz2
\ No newline at end of file
......@@ -12,22 +12,16 @@ const LOGGING_LEVELS = {
let Thingy = null;
const BROKER_STATE_READY = 'ready';
const BROKER_STATE_CONNECTING = 'connecting';
const BROKER_STATE_CONNECTED = 'connected';
const APP_STATE_RUNNING = 'running';
const APP_STATE_STOPPING = 'stopping';
const SEND_GATEWAY_CONNECTED = 'GATEWAY_CONNECTED';
const SEND_DEVICE_CONNECTED = 'DEVICE_CONNECTED';
const BROKER_CONNECT_INTERVAL = 3000;
const DISCOVER_RESTART_TIMEOUT = 5000; // XXX: Workaround for noble-device issue
const APPLICATION_START_TIMEOUT = 5000; // XXX: Wait HCI devices on system startup
let brokerConnectTaskId = null;
let dataTransmissionTaskId = null;
let brokerConnectionState = BROKER_STATE_READY;
let applicationState = APP_STATE_RUNNING;
let mqttClient = null;
......@@ -74,49 +68,30 @@ const brokerDisconnect = () => {
};
const brokerConnect = (mqttConfig) => {
brokerConnectionState = BROKER_STATE_CONNECTING;
const mqttAddr = `${mqttConfig.host}:${mqttConfig.port}`;
log(`Connecting to: ${mqttAddr}`);
const connectionProblemsHandler = (err) => {
if (err) {
log('Connection problem, disconnecting ...', err, LOGGING_LEVELS.ERROR);
brokerDisconnect();
brokerConnectionState = BROKER_STATE_READY;
}
};
const client = MQTT.connect({
log('new MQTT client creation ...');
mqttClient = MQTT.connect({
protocol: 'mqtt',
host: mqttConfig.host,
port: mqttConfig.port
port: mqttConfig.port,
reconnecting: true
});
client.on('connect', () => {
mqttClient = client;
mqttClient.on('connect', () => {
log(`Successfully connected to: ${mqttAddr}`, '', LOGGING_LEVELS.INFO);
brokerConnectionState = BROKER_STATE_CONNECTED;
});
client.on('close', connectionProblemsHandler);
client.on('error', connectionProblemsHandler);
client.on('end', connectionProblemsHandler);
client.on('offline', connectionProblemsHandler);
};
const startBrokerConnectTask = (appConfig) => {
log('Start Broker Connect Task ...');
return setInterval(() => {
if (brokerConnectionState !== BROKER_STATE_CONNECTING
&& brokerConnectionState !== BROKER_STATE_CONNECTED) {
brokerConnect(appConfig.mqtt);
}
}, BROKER_CONNECT_INTERVAL);
};
const stopBrokerConnectTask = () => {
log('Stop Broker Connect Task ...');
clearInterval(brokerConnectTaskId);
brokerDisconnect();
mqttClient.on('close', connectionProblemsHandler);
mqttClient.on('error', connectionProblemsHandler);
mqttClient.on('end', connectionProblemsHandler);
mqttClient.on('offline', connectionProblemsHandler);
};
// Thingy Utils
......@@ -253,7 +228,7 @@ const stopSendingTask = () => {
const start = (appConfig) => {
log('Starting with Config: ', appConfig, LOGGING_LEVELS.INFO);
brokerConnectTaskId = startBrokerConnectTask(appConfig);
brokerConnect(appConfig.mqtt);
startDiscoverThingyTask(appConfig);
dataTransmissionTaskId = startSendingTask(appConfig);
};
......@@ -263,7 +238,7 @@ const stop = () => {
applicationState = APP_STATE_STOPPING;
log('Stopping ...');
stopSendingTask();
stopBrokerConnectTask();
brokerDisconnect();
stopDiscoverThingyTask();
};
......
This diff is collapsed.
......@@ -10,7 +10,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": ">=6.11.4"
"node": ">=8.12.0"
},
"files": [
"index.js",
......@@ -30,7 +30,7 @@
"author": "Rigado",
"license": "MIT",
"dependencies": {
"mqtt": "^2.17.0",
"mqtt": "^2.18.8",
"thingy52": "^1.0.4"
},
"devDependencies": {
......
......@@ -5,9 +5,16 @@ description: Example of snapped Node.js application for communications with Nord
confinement: strict
grade: stable
environment:
LD_LIBRARY_PATH: "$LD_LIBRARY_PATH:$SNAP/lib"
LD_LIBRARY_PATH: "$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH"
NODE_PATH: $SNAP/lib/node_modules
parts:
thingy-hello-world:
plugin: nodejs
node-engine: "8.12.0"
node-package-manager: yarn
source: .
build-packages:
- libudev-dev
......@@ -22,7 +29,7 @@ apps:
command: bin/thingy-hello-world
daemon: simple
restart-condition: always
plugs:
- network
- network-control
- bluetooth-control
plugs: &plugs [network, network-control, bluetooth-control]
node:
command: "bin/node"
plugs: *plugs
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment