Commit 5d602b88 authored by Mikhail Vataleu's avatar Mikhail Vataleu
Browse files

Add selection of HCI device; Refactoring of exitHandlers; Update README;

parent da6397ff
......@@ -8,7 +8,7 @@ The application is daemon which discovers Nordic Thingy:52 and resends data to M
Configuration file: [./config.json](./config.json)
* `ble` - section store configuration related with Bluetooth
* `ble.deviceMac` - string can be `"*"` and connects to any available Nordic Thingy:52 or `"CF:AA:13:A1:5C:A5"` and connects to particular device
* `ble.deviceMAC` - string can be `"*"` and connects to any available Nordic Thingy:52 or `"CF:AA:13:A1:5C:A5"` and connects to particular device
* `mqtt` - configuration of connection to MQTT Broker
* `mqtt.topic` - can contain placeholder `{hostname}` which will be replaced by gateway hostname on application startup
......@@ -90,4 +90,15 @@ This application is using [Nordic-Thingy52-Nodejs](https://github.com/NordicPlay
sudo bluez.bluetoothctl
power on
scan on
```
#### ERR code CERT_NOT_YET_VALID when snapcraft
* Switch into `classic` mode.
* Execute following commands:
```sh
sudo update-ca-certificates
sudo service ntp stop
sudo ntpdate -s time.nist.gov
sudo service ntp start
```
\ No newline at end of file
{
"ble": {
"deviceMac": "*"
"hciDeviceNum": 0,
"deviceMAC": "*"
},
"mqtt": {
"host": "broker.hivemq.com",
......
......@@ -3,8 +3,8 @@
'use strict';
const os = require('os');
const Thingy = require('thingy52');
const MQTT = require('mqtt');
let Thingy = null;
let brokerConnectTaskId = null;
let dataTransmissionTaskId = null;
......@@ -27,8 +27,8 @@ const thingyState = {
// Main
// ==========
init();
start();
const config = init();
start(config);
// App Utils
// ==========
......@@ -42,16 +42,23 @@ function loadConfig() {
}
function init() {
process.stdin.resume();
process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null));
process.on('SIGUSR1', exitHandler.bind(null));
process.on('SIGUSR2', exitHandler.bind(null));
process.on('uncaughtException', exitHandler.bind(null));
const config = loadConfig();
// Setup noble lib
process.env['NOBLE_HCI_DEVICE_ID'] = config.ble.hciDeviceNum;
Thingy = require('thingy52');
// Set exit handlers
process.on('exit', function () {
stop();
});
process.on('uncaughtException', function (err) {
console.error('uncaughtException:', err);
stop();
process.exit(-1);
});
return config;
}
function start() {
const config = loadConfig();
function start(config) {
console.log('=== Thingy:52 to MQTT ===');
console.log('Configuration:', config);
console.log('=========================');
......@@ -70,21 +77,6 @@ function stop() {
stopDiscoverThingyTask();
}
function exitHandler(options, err) {
try {
stop();
} catch (e) {
console.log(e.stack);
process.exit(-1);
}
if (err) {
console.log(err.stack);
process.exit(-1);
} else {
process.exit();
}
}
// Broker Utils
// ==========
......@@ -125,7 +117,6 @@ function brokerConnect(mqttConfig) {
mqttClient = client;
console.log('[MQTT] Successfully connected to: ' + mqttAddr);
connectingToBroker = false;
}
function connectionProblemsHandler(err) {
......@@ -149,7 +140,7 @@ function brokerDisconnect() {
function startDiscoverThingyTask(config) {
console.log('[BLE] Start Discovery Task ...');
const id = macToId(config.ble.deviceMac);
const id = macToId(config.ble.deviceMAC);
Thingy.discoverWithFilter(function(device) {
console.log('[BLE] Discover:', device.id, 'target:', id);
if (id === '*') return true;
......
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