Refactored wifi and ble into typescript and converted logic to async/await for most methods. Compiles without errors ATM.
This commit is contained in:
parent
de481ee185
commit
07229c7925
|
@ -161,6 +161,7 @@ class BLE {
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
constructor(bleGetState) {
|
constructor(bleGetState) {
|
||||||
|
this.listeners = {};
|
||||||
log.info('Starting bluetooth service');
|
log.info('Starting bluetooth service');
|
||||||
getState = bleGetState;
|
getState = bleGetState;
|
||||||
bleno_1.default.on('stateChange', state => {
|
bleno_1.default.on('stateChange', state => {
|
||||||
|
@ -209,7 +210,6 @@ class BLE {
|
||||||
let result = {};
|
let result = {};
|
||||||
let utf8;
|
let utf8;
|
||||||
let obj;
|
let obj;
|
||||||
let fn;
|
|
||||||
if (offset) {
|
if (offset) {
|
||||||
log.warn(`Offset scenario`);
|
log.warn(`Offset scenario`);
|
||||||
result = bleno_1.default.Characteristic.RESULT_ATTR_NOT_LONG;
|
result = bleno_1.default.Characteristic.RESULT_ATTR_NOT_LONG;
|
||||||
|
@ -218,9 +218,8 @@ class BLE {
|
||||||
utf8 = data.toString('utf8');
|
utf8 = data.toString('utf8');
|
||||||
obj = JSON.parse(utf8);
|
obj = JSON.parse(utf8);
|
||||||
result = bleno_1.default.Characteristic.RESULT_SUCCESS;
|
result = bleno_1.default.Characteristic.RESULT_SUCCESS;
|
||||||
fn = `_on${capitalize(obj.type)}`;
|
if (obj.type && this.listeners[obj.type]) {
|
||||||
if (obj.type && this[fn]) {
|
return this.listeners[obj.type](obj, () => {
|
||||||
return this[fn](obj, () => {
|
|
||||||
callback(result);
|
callback(result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -241,7 +240,7 @@ class BLE {
|
||||||
* @param {function} callback Invoked when the event is triggered
|
* @param {function} callback Invoked when the event is triggered
|
||||||
*/
|
*/
|
||||||
on(eventName, callback) {
|
on(eventName, callback) {
|
||||||
this[`_on${capitalize(eventName)}`] = callback;
|
this.listeners[eventName] = callback;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
module.exports = BLE;
|
module.exports = BLE;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -9,7 +9,7 @@ const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"';
|
||||||
const iwgetid = '/sbin/iwgetid';
|
const iwgetid = '/sbin/iwgetid';
|
||||||
const log = require('../log')('wifi');
|
const log = require('../log')('wifi');
|
||||||
const child_process_1 = require("child_process");
|
const child_process_1 = require("child_process");
|
||||||
const fs_1 = require("fs");
|
const fs_extra_1 = require("fs-extra");
|
||||||
/** Class representing the wifi features */
|
/** Class representing the wifi features */
|
||||||
class Wifi {
|
class Wifi {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -59,7 +59,7 @@ class Wifi {
|
||||||
let parsed;
|
let parsed;
|
||||||
let current;
|
let current;
|
||||||
try {
|
try {
|
||||||
data = await fs_1.readFile(filePath, 'utf8');
|
data = await fs_extra_1.readFile(filePath, 'utf8');
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
log.error('_readConfig', err);
|
log.error('_readConfig', err);
|
||||||
|
@ -85,7 +85,7 @@ class Wifi {
|
||||||
*/
|
*/
|
||||||
async _writeConfig(data) {
|
async _writeConfig(data) {
|
||||||
try {
|
try {
|
||||||
await fs_1.writeFile(filePath, data, { encoding: 'utf-8' });
|
await fs_extra_1.writeFile(filePath, data, 'utf8');
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
log.error('_readConfigCb', err);
|
log.error('_readConfigCb', err);
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -183,8 +183,11 @@ function capitalize (str : string) {
|
||||||
return str[0].toUpperCase() + str.slice(1)
|
return str[0].toUpperCase() + str.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type functionKeys = "_onRead" | "_onWrite";
|
||||||
|
|
||||||
/** Class representing the bluetooth interface */
|
/** Class representing the bluetooth interface */
|
||||||
class BLE {
|
class BLE {
|
||||||
|
listeners : any = {}
|
||||||
/**
|
/**
|
||||||
* Establishes Bluetooth Low Energy services, accessible to process through this class
|
* Establishes Bluetooth Low Energy services, accessible to process through this class
|
||||||
*
|
*
|
||||||
|
@ -228,6 +231,7 @@ class BLE {
|
||||||
|
|
||||||
this._refreshWifi()
|
this._refreshWifi()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _refreshWifi () {
|
private async _refreshWifi () {
|
||||||
let ssid : string
|
let ssid : string
|
||||||
|
|
||||||
|
@ -241,11 +245,11 @@ class BLE {
|
||||||
currentAddr = getIp()
|
currentAddr = getIp()
|
||||||
log.info('wifi.getNetwork', {ssid : ssid, ip : currentAddr })
|
log.info('wifi.getNetwork', {ssid : ssid, ip : currentAddr })
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onWrite (data : any, offset : number, withoutResponse : Function, callback : Function) {
|
private _onWrite (data : any, offset : number, withoutResponse : Function, callback : Function) {
|
||||||
let result = {}
|
let result : any = {}
|
||||||
let utf8
|
let utf8 : string
|
||||||
let obj
|
let obj : any
|
||||||
let fn
|
|
||||||
|
|
||||||
if (offset) {
|
if (offset) {
|
||||||
log.warn(`Offset scenario`)
|
log.warn(`Offset scenario`)
|
||||||
|
@ -256,17 +260,16 @@ class BLE {
|
||||||
utf8 = data.toString('utf8')
|
utf8 = data.toString('utf8')
|
||||||
obj = JSON.parse(utf8)
|
obj = JSON.parse(utf8)
|
||||||
result = bleno.Characteristic.RESULT_SUCCESS
|
result = bleno.Characteristic.RESULT_SUCCESS
|
||||||
fn = `_on${capitalize(obj.type)}`
|
|
||||||
|
|
||||||
if (obj.type && this[fn]) {
|
if (obj.type && this.listeners[obj.type]) {
|
||||||
return this[fn](obj, () => {
|
return this.listeners[obj.type](obj, () => {
|
||||||
callback(result)
|
callback(result)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return callback(result)
|
return callback(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _onRead (offset : number, callback : Function) {
|
private _onRead (offset : number, callback : Function) {
|
||||||
const result = bleno.Characteristic.RESULT_SUCCESS
|
const result = bleno.Characteristic.RESULT_SUCCESS
|
||||||
const state = getState()
|
const state = getState()
|
||||||
|
@ -280,7 +283,7 @@ class BLE {
|
||||||
* @param {function} callback Invoked when the event is triggered
|
* @param {function} callback Invoked when the event is triggered
|
||||||
*/
|
*/
|
||||||
on (eventName : string, callback : Function) {
|
on (eventName : string, callback : Function) {
|
||||||
this[`_on${capitalize(eventName)}`] = callback
|
this.listeners[eventName] = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ const iwgetid : string = '/sbin/iwgetid'
|
||||||
|
|
||||||
const log : any = require('../log')('wifi')
|
const log : any = require('../log')('wifi')
|
||||||
import { exec } from 'child_process'
|
import { exec } from 'child_process'
|
||||||
import { readFile, writeFile } from 'fs'
|
import { readFile, writeFile } from 'fs-extra'
|
||||||
import { reject } from 'q'
|
import { reject } from 'q'
|
||||||
|
|
||||||
interface Network {
|
interface Network {
|
||||||
|
@ -97,7 +97,7 @@ export class Wifi {
|
||||||
*/
|
*/
|
||||||
private async _writeConfig (data : string) {
|
private async _writeConfig (data : string) {
|
||||||
try {
|
try {
|
||||||
await writeFile(filePath, data, { encoding : 'utf-8' })
|
await writeFile(filePath, data, 'utf8')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error('_readConfigCb', err)
|
log.error('_readConfigCb', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
Loading…
Reference in New Issue