Skip to content

Commit abb813c

Browse files
committed
refactor(cli/default-serivent): use async await in run method
1 parent e78447b commit abb813c

1 file changed

Lines changed: 71 additions & 82 deletions

File tree

packages/cli/src/cli-default-servient.ts

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -229,89 +229,78 @@ export default class DefaultServient extends Servient {
229229
/**
230230
* start
231231
*/
232-
public start(): Promise<typeof WoT> {
233-
return new Promise<typeof WoT>((resolve, reject) => {
234-
super
235-
.start()
236-
.then((myWoT) => {
237-
info("DefaultServient started");
238-
this.runtime = myWoT;
239-
240-
// TODO think about builder pattern that starts with produce() ends with expose(), which exposes/publishes the Thing
241-
myWoT
242-
.produce({
243-
title: "servient",
244-
description: "node-wot CLI Servient",
245-
properties: {
246-
things: {
247-
type: "object",
248-
description: "Get things",
249-
observable: false,
250-
readOnly: true,
251-
},
252-
},
253-
actions: {
254-
setLogLevel: {
255-
description: "Set log level",
256-
input: { oneOf: [{ type: "string" }, { type: "number" }] },
257-
output: { type: "string" },
258-
},
259-
shutdown: {
260-
description: "Stop servient",
261-
output: { type: "string" },
262-
},
263-
...(this.config.servient.scriptAction === true
264-
? {
265-
runScript: {
266-
description: "Run script",
267-
input: { type: "string" },
268-
output: { type: "string" },
269-
},
270-
}
271-
: {}),
272-
},
273-
})
274-
.then((thing) => {
275-
thing.setActionHandler("setLogLevel", async (level) => {
276-
const ll = await Helpers.parseInteractionOutput(level);
277-
if (typeof ll === "number") {
278-
this.setLogLevel(ll as number);
279-
} else if (typeof ll === "string") {
280-
this.setLogLevel(ll as string);
281-
} else {
282-
// try to convert it to strings
283-
this.setLogLevel(ll + "");
284-
}
285-
return `Log level set to '${this.logLevel}'`;
286-
});
287-
thing.setActionHandler("shutdown", async () => {
288-
debug("shutting down by remote");
289-
await this.shutdown();
290-
return undefined;
291-
});
292-
if (this.config.servient.scriptAction === true) {
293-
thing.setActionHandler("runScript", async (script) => {
294-
const scriptv = await Helpers.parseInteractionOutput(script);
295-
debug("running script", scriptv);
296-
this.runScript(scriptv as string);
297-
return undefined;
298-
});
299-
}
300-
thing.setPropertyReadHandler("things", async () => {
301-
debug("returning things");
302-
return this.getThings();
303-
});
304-
thing
305-
.expose()
306-
.then(() => {
307-
// pass on WoTFactory
308-
resolve(myWoT);
309-
})
310-
.catch((err) => reject(err));
311-
});
312-
})
313-
.catch((err) => reject(err));
232+
public async start(): Promise<typeof WoT> {
233+
const superWoT = await super.start();
234+
this.runtime = superWoT;
235+
236+
info("DefaultServient started");
237+
238+
const servientProducedThing = await superWoT.produce({
239+
title: "servient",
240+
description: "node-wot CLI Servient",
241+
properties: {
242+
things: {
243+
type: "object",
244+
description: "Get things",
245+
observable: false,
246+
readOnly: true,
247+
},
248+
},
249+
actions: {
250+
setLogLevel: {
251+
description: "Set log level",
252+
input: { oneOf: [{ type: "string" }, { type: "number" }] },
253+
output: { type: "string" },
254+
},
255+
shutdown: {
256+
description: "Stop servient",
257+
output: { type: "string" },
258+
},
259+
...(this.config.servient.scriptAction === true
260+
? {
261+
runScript: {
262+
description: "Run script",
263+
input: { type: "string" },
264+
output: { type: "string" },
265+
},
266+
}
267+
: {}),
268+
},
314269
});
270+
271+
servientProducedThing.setActionHandler("setLogLevel", async (level) => {
272+
const ll = await Helpers.parseInteractionOutput(level);
273+
if (typeof ll === "number") {
274+
this.setLogLevel(ll as number);
275+
} else if (typeof ll === "string") {
276+
this.setLogLevel(ll as string);
277+
} else {
278+
// try to convert it to strings
279+
this.setLogLevel(ll + "");
280+
}
281+
return `Log level set to '${this.logLevel}'`;
282+
});
283+
servientProducedThing.setActionHandler("shutdown", async () => {
284+
debug("shutting down by remote");
285+
await this.shutdown();
286+
return undefined;
287+
});
288+
if (this.config.servient.scriptAction === true) {
289+
servientProducedThing.setActionHandler("runScript", async (script) => {
290+
const scriptv = await Helpers.parseInteractionOutput(script);
291+
debug("running script", scriptv);
292+
this.runScript(scriptv as string);
293+
return undefined;
294+
});
295+
}
296+
servientProducedThing.setPropertyReadHandler("things", async () => {
297+
debug("returning things");
298+
return this.getThings();
299+
});
300+
301+
await servientProducedThing.expose();
302+
303+
return superWoT;
315304
}
316305

317306
public async shutdown(): Promise<void> {

0 commit comments

Comments
 (0)