Skip to content

Commit 1f0c896

Browse files
committed
prefer "unknown" over "any", fix eslint warnings
1 parent 15197b6 commit 1f0c896

7 files changed

+55
-53
lines changed

server/ConfigCache.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ export class ConfigCache {
1414
public router = express.Router();
1515

1616
private defaultOptions: unknown = { timeout: 4000 };
17-
private options: unknown = {};
17+
private options: { [key: string]: any } = {};
1818

1919
private dService = DeviceDiscovery.getInstance();
2020

2121
// cache for device configurations
22-
private netConfigs: { [hostname: string]: unknown } = {};
22+
private netConfigs: { [hostname: string]: any } = {};
2323

2424

2525
constructor(options: unknown = {}) {
2626
this.options = Object.assign({}, this.defaultOptions, options);
2727

2828
// express function: list all found devices.
29-
this.router.get('/', async(req, res) => {
29+
this.router.get('/', async (req, res) => {
3030
res.json({});
3131
});
3232

3333
// express function: list all found devices.
34-
this.router.get('/:hostname', async(req, res) => {
34+
this.router.get('/:hostname', async (req, res) => {
3535
let cnf = {};
3636
if (req.params.hostname) {
3737
cnf = await this.get(req.params.hostname);
@@ -55,30 +55,30 @@ export class ConfigCache {
5555
this.netConfigs = {};
5656
}
5757

58-
async get(hostname: string) {
58+
async get(hostname: string): Promise<{ [key: string]: any }> {
5959
const host: string = hostname.replace(/\.local/, '');
6060
if (!this.dService.isOnline(host)) {
6161
Logger.error(`not online: ${host}`); // , err
6262
} else if (!this.netConfigs[host]) {
6363
try {
64-
let eObj: unknown = {}, cObj: unknown = {};
64+
let eObj: { [key: string]: any } = {}, cObj: { [key: string]: any } = {};
6565

6666
const eReq = await fetch(`http://${hostname}/env.json`, { signal: timeoutSignal(this.options.timeout) });
6767
if (eReq.status === 200) {
68-
eObj = await eReq.json();
68+
eObj = await eReq.json() as { [key: string]: any };
6969
}
7070

7171
const cReq = await fetch(`http://${hostname}/config.json`, { signal: timeoutSignal(this.options.timeout) });
7272
if (eReq.status === 200) {
73-
cObj = await cReq.json();
73+
cObj = await cReq.json() as { [key: string]: any };
7474
}
7575

7676
// use device.title as default on all elements
7777
const conf = { ...eObj, ...cObj };
7878
const title = conf.device[0].title;
7979

8080
for (const t in conf) for (const i in conf[t]) {
81-
if (!conf[t][i].title) conf[t][i].title = title;
81+
if (!conf[t][i].title) conf[t][i].title = title;
8282
}
8383

8484

server/Discover.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class DeviceDiscovery {
1616
public router = express.Router();
1717

1818
private defaultOptions: unknown = { refresh: 1 * 60 };
19-
private options: unknown = {};
19+
private options: { [key: string]: any } = {};
2020

2121
private mdns: mDNS.MulticastDNS;
2222

@@ -96,7 +96,7 @@ export class DeviceDiscovery {
9696
Logger.trace('Device:', response);
9797
// Logger.trace('Device:', JSON.stringify(response));
9898

99-
const hdd = {
99+
const hdd : { [key: string]: any } = {
100100
host: '',
101101
target: '',
102102
room: '',
@@ -130,7 +130,7 @@ export class DeviceDiscovery {
130130
.split(',')
131131
.forEach(e => {
132132
const p = e.split('=');
133-
(<unknown>hdd)[p[0]] = p[1];
133+
hdd[p[0]] = p[1];
134134
});
135135
});
136136

server/EventBus.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class EventBusClass {
1616
this._registry = registry;
1717
}
1818

19-
addElement(typeName: string, id: string, conf: unknown) {
19+
addElement(typeName: string, id: string, conf: { [key: string]: any }) {
2020
Logger.trace(`add ${typeName}/${id}: ${conf}`);
2121
const elem = this._registry?.newElement(typeName, id);
2222
if (elem) {
@@ -26,13 +26,13 @@ export class EventBusClass {
2626
} // addElement()
2727

2828
/** Activate virtual elements for all configured elements. */
29-
startup(allConfig: unknown) {
29+
startup(allConfig: { [key: string]: any }) {
3030
Object.entries(allConfig).forEach(([typeName, elements]) => {
31-
Object.entries(elements as unknown).forEach(([id, conf]) => {
32-
this.addElement(typeName, id, conf);
31+
Object.entries(elements as any).forEach(([id, conf]) => {
32+
this.addElement(typeName, id, conf as { [key: string]: any });
3333
});
3434
});
35-
Object.entries(this.activeVirtuals).forEach(([_id, _v]) => {
35+
Object.entries(this.activeVirtuals).forEach(([_id, v]) => {
3636
v.doAction({});
3737
});
3838
} // startup()
@@ -55,7 +55,7 @@ export class EventBusClass {
5555
const e = this.activeVirtuals[typeId];
5656
Logger.trace("execute:", e, args);
5757
if (e) {
58-
const actions = {} as unknown;
58+
const actions:{ [key: string]: any } = {};
5959
args.split('&').forEach(a => {
6060
const [key, val] = a.split('=');
6161
actions[key] = val;
@@ -92,7 +92,7 @@ export class EventBusClass {
9292

9393
/** return the state of all virtual elements */
9494
async allState() {
95-
const all: unknown = {};
95+
const all: { [key: string]: any } = {};
9696

9797
for (const eId in this.activeVirtuals) {
9898
all[eId] = await this.activeVirtuals[eId].getState();

server/HomeDingServer.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export class HomeDingServer {
4343
};
4444

4545
// file based settings
46-
private _settings = {} as unknown;
46+
private _settings: { [key: string]: any } = {};
4747
private _boardFileName = '';
4848

4949
/** The current config & state for mocked elements. */
50-
private _allConfig: { [e: string]: unknown; } = {};
51-
private _boardState: unknown = null;
50+
private _allConfig: { [key: string]: any; } = {};
51+
private _boardState: { [key: string]: any } | undefined = undefined;
5252

5353
private _caseFolder?: string;
5454

@@ -165,11 +165,11 @@ export class HomeDingServer {
165165
JSON.parse(fs.readFileSync(this._caseFolder + 'env.json', 'utf8')),
166166
JSON.parse(fs.readFileSync(this._caseFolder + 'config.json', 'utf8'))
167167
);
168-
this._boardState = null;
168+
this._boardState = undefined;
169169

170170
// ===== watch for changes of $board
171171
fs.watch(this._boardFileName, (_eventName, _filename) => {
172-
this._boardState = null;
172+
this._boardState = undefined;
173173
});
174174
mock.register(this.registry);
175175

@@ -253,7 +253,7 @@ export class HomeDingServer {
253253
res.send();
254254
/* no await */ this.eventBus.dispatch(id, req.query);
255255
this.eventBus.executeEvents();
256-
} else {
256+
} else if (this._boardState) {
257257
// Update and return status of a single element
258258
this._boardState[id] = Object.assign(this._boardState[id], this.eventBus.state(id));
259259
res.json(this._boardState[id]);
@@ -270,10 +270,10 @@ export class HomeDingServer {
270270
this._app.get(/^\/api\/elements/, this.expressNoCache, handleElements);
271271

272272
const handleScan = async (_req: express.Request, res: express.Response) => {
273-
const elems = [{"id": "net01"}, {"id": "net02"}, {"id": "net03"}];
273+
const elems = [{ "id": "net01" }, { "id": "net02" }, { "id": "net03" }];
274274
res.json(elems);
275275
}; // handleScan
276-
276+
277277
this._app.get(/^\/api\/scan/, this.expressNoCache, handleScan);
278278

279279

@@ -292,13 +292,15 @@ export class HomeDingServer {
292292
}
293293
}
294294

295-
// Update status of all elements
296-
const vState = await this.eventBus.allState();
297-
this._boardState = Object.assign(this._boardState, vState);
295+
if (this._boardState) {
296+
// Update status of all elements
297+
const vState = await this.eventBus.allState();
298+
this._boardState = Object.assign(this._boardState, vState);
298299

299-
// debugSend('send:' , boardStatus);
300-
res.type('application/json');
301-
res.send(JSON.stringify(this._boardState, null, 2));
300+
// debugSend('send:' , boardStatus);
301+
res.type('application/json');
302+
res.send(JSON.stringify(this._boardState, null, 2));
303+
}
302304
}; // handleState
303305

304306

server/MockElements.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { RegistryClass } from './Registry.js';
66
import { VirtualBaseElement } from './VirtualBaseElement.js';
77

88
export class MockSwitch extends VirtualBaseElement {
9-
async doAction(action: unknown) {
9+
async doAction(action: { [key: string]: any }) {
1010
if (action.value != null) { this.state.value = action.value; }
1111
if (action.toggle != null) { this.state.value = (this.state.value ? 0 : 1); }
1212
super.doAction(action);
@@ -17,7 +17,7 @@ export class MockSwitch extends VirtualBaseElement {
1717
export class MockReference extends VirtualBaseElement {
1818
private _lastState = { reference: undefined, invalue: undefined, value: 0 };
1919

20-
async doAction(action: unknown) {
20+
async doAction(action: { [key: string]: any }) {
2121
if (action.value != null) { this.state.invalue = Number(action.value); }
2222
if (action.reference != null) { this.state.reference = Number(action.reference); }
2323
super.doAction(action);
@@ -41,7 +41,7 @@ export class MockDHT extends VirtualBaseElement {
4141
private _defaultConfig = { readtime: 60 };
4242
private _lastState = { temperature: undefined, humidity: undefined };
4343

44-
setConfig(bus: EventBusClass, config: unknown) {
44+
setConfig(bus: EventBusClass, config: { [key: string]: any }) {
4545
super.setConfig(bus, config, this._defaultConfig);
4646
// this.state = Object.assign(this.state, {
4747
// temperature: this.config.temperature,
@@ -83,14 +83,14 @@ export class MockDHT extends VirtualBaseElement {
8383
export class MockValue extends VirtualBaseElement {
8484
private _defaultConfig = { step: 1, value: 0 };
8585

86-
setConfig(bus: EventBusClass, config: unknown) {
86+
setConfig(bus: EventBusClass, config: { [key: string]: any }) {
8787
super.setConfig(bus, config, this._defaultConfig);
8888
this.state = Object.assign(this.state, {
8989
value: this.config.value
9090
});
9191
}
9292

93-
async doAction(action: unknown) {
93+
async doAction(action: { [key: string]: any }) {
9494
const step = this.config.step;
9595
const v = this.state.value;
9696
if (action.value != null) { this.state.value = action.value; }
@@ -114,15 +114,15 @@ export class MockDevice extends VirtualBaseElement {
114114
return (this.state);
115115
}
116116

117-
async doAction(action: unknown) {
117+
async doAction(action: { [key: string]: any }) {
118118
if (action.log !== null) { Logger.info('>>', action.log); }
119119
super.doAction(action);
120120
}
121121
} // MockDevice
122122

123123

124124
export class MockTime extends VirtualBaseElement {
125-
async getState(): Promise<unknown> {
125+
async getState(): Promise<{ [key: string]: any }> {
126126
const now = new Date().toISOString();
127127
this.state.now =
128128
this.state.value = now.substring(0, 19).replace(/T/, ' ');
@@ -133,7 +133,7 @@ export class MockTime extends VirtualBaseElement {
133133
export class MockBL0937 extends VirtualBaseElement {
134134
private _defaultConfig = { step: 1, value: 0 };
135135

136-
setConfig(bus: EventBusClass, config: unknown) {
136+
setConfig(bus: EventBusClass, config: { [key: string]: any }) {
137137
super.setConfig(bus, config, this._defaultConfig);
138138
}
139139

@@ -152,7 +152,7 @@ export class MockBL0937 extends VirtualBaseElement {
152152
return (this.state);
153153
}
154154

155-
async doAction(action: unknown) {
155+
async doAction(action: { [key: string]: any }) {
156156
if ((action.mode === 'current') || (action.mode === 'voltage')) {
157157
this.state.mode = action.mode;
158158
}
@@ -168,12 +168,12 @@ export class MockStandard extends VirtualBaseElement {
168168
super(typeName, id);
169169
}
170170

171-
setConfig(bus: EventBusClass, config: unknown) {
171+
setConfig(bus: EventBusClass, config: { [key: string]: any }) {
172172
super.setConfig(bus, config);
173173
this.state.value = config.value || 0;
174174
}
175175

176-
async doAction(action: unknown) {
176+
async doAction(action: { [key: string]: any }) {
177177
super.doAction(action);
178178
if (action.value != null) { this.state.value = action.value; }
179179
if (action.mode != null) { this.state.mode = action.mode; }
@@ -187,14 +187,14 @@ export class MockTimer extends VirtualBaseElement {
187187
restart = false;
188188
startTime = Date.now();
189189

190-
setConfig(bus: EventBusClass, config: unknown) {
190+
setConfig(bus: EventBusClass, config: { [key: string]: any }) {
191191
super.setConfig(bus, config);
192192
this.restart = Boolean(config.restart);
193193
this.state.value = config.value || 0;
194194
this.state.mode = config.mode || 'timer';
195195
}
196196

197-
async doAction(action: unknown) {
197+
async doAction(action: { [key: string]: any }) {
198198
super.doAction(action);
199199
if (action.mode != null) { this.state.mode = action.mode; }
200200
if (action.start != null) {
@@ -203,7 +203,7 @@ export class MockTimer extends VirtualBaseElement {
203203
}
204204
}
205205

206-
async getState(): Promise<unknown> {
206+
async getState(): Promise<{ [key: string]: any }> {
207207
if (this.state.mode === 'on') {
208208
this.state.value = 1;
209209
} else if (this.state.mode === 'off') {

server/ProxyElement.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ProxyElement extends VirtualBaseElement {
2929
private TIMEOUT_MS = 3 * 1000; // timeout for getting resaults from a device.
3030
private NEXT_TRY_MS = 8 * 1000; // duration for next data from device
3131

32-
setConfig(_bus: EventBusClass, config: unknown, _default = {}) {
32+
setConfig(_bus: EventBusClass, config: { [key: string]: any }, _default = {}) {
3333
this.eventBus = _bus;
3434
this.config = Object.assign({}, _default, config);
3535

@@ -77,7 +77,7 @@ export class ProxyElement extends VirtualBaseElement {
7777
} else {
7878
try {
7979
const r = await fetch(this.url, { signal: timeoutSignal(this.TIMEOUT_MS) });
80-
const j = await r.json() as unknown;
80+
const j = await r.json() as { [key: string]: any };
8181
const rs = j[`${this.type}/${this.id}`];
8282
this.state = Object.assign(this.state, rs);
8383
} catch (e) {
@@ -92,7 +92,7 @@ export class ProxyElement extends VirtualBaseElement {
9292

9393

9494
// pass action to real element
95-
async doAction(action: unknown) {
95+
async doAction(action: { [key: string]: any }) {
9696
for (const a in action) {
9797
await fetch(this.url + '?' + a + '=' + action[a], { signal: timeoutSignal(this.TIMEOUT_MS) });
9898
this.nextTry = 0; // asap.

server/VirtualBaseElement.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ export class VirtualBaseElement {
1717

1818
protected eventBus!: EventBusClass;
1919

20-
config: unknown;
21-
state: unknown = { active: true };
20+
config: { [key: string]: any } = {};
21+
state: { [key: string]: any } = { active: true };
2222

2323
constructor(type: string, id: string) {
2424
this.type = type;
2525
this.id = id;
2626
this.typeId = `${type}/${id}`;
2727
}
2828

29-
setConfig(_bus: EventBusClass, config: unknown, _default = {}) {
29+
setConfig(_bus: EventBusClass, config: { [key: string]: any }, _default = {}) {
3030
this.eventBus = _bus;
3131
this.config = Object.assign({}, _default, config);
3232
}

0 commit comments

Comments
 (0)