Skip to content

Commit fcbd300

Browse files
committed
Reduced the number of loads for device definitions on production environments
1 parent 9c305d8 commit fcbd300

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

boards/Controller.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { spi0, spi1, SpiAdcBus, SpiAdcChannel } from '../spi-adc/SpiAdcBus';
1616
import { SpiAdcChips } from '../spi-adc/SpiAdcChips';
1717
import { webApp } from '../web/Server';
1818
import { utils, valueMap, vMaps } from './Constants';
19+
import { config } from "../config/Config";
1920

2021
interface IConfigItemCollection {
2122
set(data);
@@ -378,9 +379,10 @@ export class Controller extends ConfigItem {
378379
return this._spiAdcChips;
379380
}
380381
public get analogDevices() {
381-
//if (typeof this._analogDevices === 'undefined') {
382-
this._analogDevices = AnalogDevices.loadDefintions();
383-
//}
382+
if (config.development || typeof this._analogDevices === 'undefined' || !this._analogDevices) {
383+
console.log('Loading devices');
384+
this._analogDevices = AnalogDevices.loadDefintions();
385+
}
384386
return this._analogDevices;
385387
}
386388
/**************************************************
@@ -832,7 +834,6 @@ export class Controller extends ConfigItem {
832834
this.oneWire.removeInternalReferences(binding);
833835
}
834836
}
835-
836837
export class DeviceFeedCollection extends ConfigItemCollection<DeviceFeed> {
837838
constructor(data: any, name?: string) { super(data, name || 'feeds'); }
838839
public createItem(data: any): DeviceFeed { return new DeviceFeed(data); }
@@ -965,7 +966,7 @@ export class Feed {
965966
options: this.feed.options
966967
});
967968
this.lastSent = v;
968-
logger.verbose(`Feed sending ${this.feed.property}: ${JSON.stringify(v)}`);
969+
logger.verbose(`Feed sending ${this.feed.property}: ${JSON.stringify(v)} to ${this.feed.deviceBinding}`);
969970
}
970971
}
971972
catch (err) { logger.error(`Error sending device feed: ${err.message}`); }
@@ -1977,7 +1978,6 @@ export class I2cController extends ConfigItem {
19771978
public get detected(): any[] { return this.data.detected; }
19781979
public set detected(val: any[]) { this.data.detected = val; }
19791980
public get buses(): I2cBusCollection { return new I2cBusCollection(this.data, 'buses'); }
1980-
19811981
public getExtended() {
19821982
let c = this.get(true);
19831983
c.buses = this.buses.toExtendedArray();
@@ -1988,7 +1988,6 @@ export class I2cController extends ConfigItem {
19881988
for (let i = 0; i < this.buses.length; i++) await this.buses.getItemByIndex(i).deleteConnectionAsync(id);
19891989
} catch (err) { return Promise.reject(new Error(`Error removing connection from i2c controller`)); }
19901990
}
1991-
19921991
public async setDeviceState(binding: string | DeviceBinding, data: any): Promise<any> {
19931992
try {
19941993
let bind = typeof binding === 'string' ? new DeviceBinding(binding) : binding;
@@ -2013,7 +2012,6 @@ export class I2cController extends ConfigItem {
20132012
}
20142013
catch (err) { return Promise.reject(err); }
20152014
}
2016-
20172015
public async getDevice(binding: string | DeviceBinding): Promise<any> {
20182016
try {
20192017
let bind = typeof binding === 'string' ? new DeviceBinding(binding) : binding;
@@ -2092,7 +2090,6 @@ export class I2cController extends ConfigItem {
20922090
}
20932091
catch (err) { return Promise.reject(err); }
20942092
}
2095-
20962093
public async setDevice(dev): Promise<I2cDevice> {
20972094
try {
20982095
let busId = (typeof dev.busId !== 'undefined') ? parseInt(dev.busId, 10) : undefined;
@@ -2298,7 +2295,6 @@ export class I2cController extends ConfigItem {
22982295
bus.removeInternalReferences(binding);
22992296
}
23002297
}
2301-
23022298
}
23032299
export class I2cBusCollection extends ConfigItemCollection<I2cBus> {
23042300
constructor(data: any, name?: string) { super(data, name) }
@@ -2837,6 +2833,8 @@ export class I2cDevice extends ConfigItem {
28372833
let feed: DeviceFeed;
28382834
let connectionId;
28392835
let connection;
2836+
2837+
28402838
// search for existing feed; also acts as to not allow duplicate feeds
28412839
feed = this.getDeviceFeed(data); // try to find feed with matching data; useful if data is sent from njsPC
28422840
if (typeof feed !== 'undefined') {

config/Config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22
import * as fs from "fs";
3+
import { utils } from "../boards/Constants";
34
const extend = require("extend");
45
import { logger } from "../logger/Logger";
56
class Config {
@@ -8,6 +9,7 @@ class Config {
89
private _isInitialized: boolean = false;
910
private _fileTime: Date = new Date(0);
1011
private _isLoading: boolean = false;
12+
public get development() { return utils.makeBool(this._cfg.development); }
1113
constructor() {
1214
let self = this;
1315
this.cfgPath = path.posix.join(process.cwd(), "/config.json");

defaultConfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"development": false,
23
"web": {
34
"servers": {
45
"http": {
@@ -27,7 +28,7 @@
2728
"app": {
2829
"enabled": true,
2930
"level": "info",
30-
"logToFile": false
31+
"logToFile": false
3132
}
3233
},
3334
"appVersion": "1.0.0"

0 commit comments

Comments
 (0)