@thingweb/node-red-node-wot
Version:
Web of Things nodes for Node-RED using node-wot
86 lines (85 loc) • 3.49 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@node-wot/core");
const binding_http_1 = require("@node-wot/binding-http");
const binding_coap_1 = require("@node-wot/binding-coap");
const binding_websockets_1 = require("@node-wot/binding-websockets");
const binding_mqtt_1 = require("@node-wot/binding-mqtt");
class ServientWrapper {
constructor(bindingType, config) {
this.started = false;
this.things = {};
console.debug("[debug] servient constructor called. config: ", config);
this.servient = new core_1.Servient();
if (bindingType === "http") {
this.server = new binding_http_1.HttpServer(config);
}
else if (bindingType === "websocket") {
this.server = new binding_websockets_1.WebSocketServer(config);
}
else if (bindingType === "coap") {
this.server = new binding_coap_1.CoapServer(config);
}
else if (bindingType === "mqtt") {
this.server = new binding_mqtt_1.MqttBrokerServer(config);
}
this.servient.addServer(this.server);
}
isRunning() {
return this.started;
}
startServient() {
return __awaiter(this, void 0, void 0, function* () {
this.wot = yield this.servient.start();
});
}
createThing(td) {
return __awaiter(this, void 0, void 0, function* () {
const thing = yield this.wot.produce(td);
this.things[td.title] = thing;
return thing;
});
}
exposeThing(thing) {
return __awaiter(this, void 0, void 0, function* () {
yield thing.expose();
this.started = true;
const td = thing.getThingDescription();
console.debug("[debug] thing exposed.", td);
return td;
});
}
getThing(thingName) {
return this.things[thingName];
}
addCredentials(title, credentials) {
const thing = this.things[title];
const td = thing.getThingDescription();
this.servient.addCredentials({ [td.id]: credentials });
}
endServient() {
return __awaiter(this, void 0, void 0, function* () {
if (this.server) {
console.debug("[debug] endServient called.");
for (const key in this.things) {
yield this.server.destroy(this.things[key].id);
}
console.debug("[debug] server destroyed.");
yield this.server.stop();
console.debug("[debug] server stopped.");
yield this.servient.shutdown();
console.debug("[debug] servient shutdown.");
}
});
}
}
exports.default = ServientWrapper;