Refactored wifi and ble into typescript and converted logic to async/await for most methods. Compiles without errors ATM.

This commit is contained in:
mmcwilliams 2019-11-26 22:10:59 -05:00
parent de481ee185
commit 07229c7925
6 changed files with 23 additions and 21 deletions

View File

@ -161,6 +161,7 @@ class BLE {
* @constructor
*/
constructor(bleGetState) {
this.listeners = {};
log.info('Starting bluetooth service');
getState = bleGetState;
bleno_1.default.on('stateChange', state => {
@ -209,7 +210,6 @@ class BLE {
let result = {};
let utf8;
let obj;
let fn;
if (offset) {
log.warn(`Offset scenario`);
result = bleno_1.default.Characteristic.RESULT_ATTR_NOT_LONG;
@ -218,9 +218,8 @@ class BLE {
utf8 = data.toString('utf8');
obj = JSON.parse(utf8);
result = bleno_1.default.Characteristic.RESULT_SUCCESS;
fn = `_on${capitalize(obj.type)}`;
if (obj.type && this[fn]) {
return this[fn](obj, () => {
if (obj.type && this.listeners[obj.type]) {
return this.listeners[obj.type](obj, () => {
callback(result);
});
}
@ -241,7 +240,7 @@ class BLE {
* @param {function} callback Invoked when the event is triggered
*/
on(eventName, callback) {
this[`_on${capitalize(eventName)}`] = callback;
this.listeners[eventName] = callback;
}
}
module.exports = BLE;

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ const iwlist = '/sbin/iwlist wlan0 scanning | grep "ESSID:"';
const iwgetid = '/sbin/iwgetid';
const log = require('../log')('wifi');
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 Wifi {
constructor() {
@ -59,7 +59,7 @@ class Wifi {
let parsed;
let current;
try {
data = await fs_1.readFile(filePath, 'utf8');
data = await fs_extra_1.readFile(filePath, 'utf8');
}
catch (err) {
log.error('_readConfig', err);
@ -85,7 +85,7 @@ class Wifi {
*/
async _writeConfig(data) {
try {
await fs_1.writeFile(filePath, data, { encoding: 'utf-8' });
await fs_extra_1.writeFile(filePath, data, 'utf8');
}
catch (err) {
log.error('_readConfigCb', err);

File diff suppressed because one or more lines are too long

View File

@ -183,8 +183,11 @@ function capitalize (str : string) {
return str[0].toUpperCase() + str.slice(1)
}
type functionKeys = "_onRead" | "_onWrite";
/** Class representing the bluetooth interface */
class BLE {
listeners : any = {}
/**
* Establishes Bluetooth Low Energy services, accessible to process through this class
*
@ -228,6 +231,7 @@ class BLE {
this._refreshWifi()
}
private async _refreshWifi () {
let ssid : string
@ -241,11 +245,11 @@ class BLE {
currentAddr = getIp()
log.info('wifi.getNetwork', {ssid : ssid, ip : currentAddr })
}
private _onWrite (data : any, offset : number, withoutResponse : Function, callback : Function) {
let result = {}
let utf8
let obj
let fn
let result : any = {}
let utf8 : string
let obj : any
if (offset) {
log.warn(`Offset scenario`)
@ -256,17 +260,16 @@ class BLE {
utf8 = data.toString('utf8')
obj = JSON.parse(utf8)
result = bleno.Characteristic.RESULT_SUCCESS
fn = `_on${capitalize(obj.type)}`
if (obj.type && this[fn]) {
return this[fn](obj, () => {
if (obj.type && this.listeners[obj.type]) {
return this.listeners[obj.type](obj, () => {
callback(result)
})
} else {
return callback(result)
}
}
private _onRead (offset : number, callback : Function) {
const result = bleno.Characteristic.RESULT_SUCCESS
const state = getState()
@ -280,7 +283,7 @@ class BLE {
* @param {function} callback Invoked when the event is triggered
*/
on (eventName : string, callback : Function) {
this[`_on${capitalize(eventName)}`] = callback
this.listeners[eventName] = callback
}
}

View File

@ -11,7 +11,7 @@ const iwgetid : string = '/sbin/iwgetid'
const log : any = require('../log')('wifi')
import { exec } from 'child_process'
import { readFile, writeFile } from 'fs'
import { readFile, writeFile } from 'fs-extra'
import { reject } from 'q'
interface Network {
@ -97,7 +97,7 @@ export class Wifi {
*/
private async _writeConfig (data : string) {
try {
await writeFile(filePath, data, { encoding : 'utf-8' })
await writeFile(filePath, data, 'utf8')
} catch (err) {
log.error('_readConfigCb', err)
throw err