Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
********************************************************************************/
// node-wot implementation of W3C WoT Servient
import { Servient } from "@node-wot/core";
// exposed protocols
import { FujitsuServer } from "@node-wot/binding-fujitsu";
import { HttpServer } from "@node-wot/binding-http";
// consuming protocols
import { CoapClientFactory } from "@node-wot/binding-coap";
import { FileClientFactory } from "@node-wot/binding-file";
let servient = new Servient();
servient.addServer(new HttpServer());
servient.addServer(new FujitsuServer("ws://wot.f-ncs.ad.jp/websocket/"));
servient.addClientFactory(new CoapClientFactory());
servient.addClientFactory(new FileClientFactory());
// get WoT object for privileged script
servient.start().then(async (WoT) => {
console.info("FujitsuLocalProxy started");
let thing = WoT.produce({
id: "urn:dev:wot:siemens:festofake",
name: "FestoFake"
}
);
console.info(thing.name + " produced");
function main() {
// init hardware
setBrightness(100);
setAll(0, 0, 0);
console.info("UnicornHAT initilized");
let servient = new Servient();
servient.addServer(new HttpServer());
servient.addServer(new CoapServer());
// get WoT object for privileged script
servient.start().then( (myWoT) => {
console.info("RaspberryServient started");
try {
let template: WoT.ThingFragment = { name: "Unicorn" };
let thing = myWoT.produce(template);
unicorn = thing;
unicorn
.addProperty(
// init config
this.config = (typeof config === "object") ? config : BridgeServient.defaultConfig;
if (!this.config.http) this.config.http = BridgeServient.defaultConfig.http;
// load credentials from config
this.addCredentials(this.config.credentials);
// remove secrets from original for displaying config (already added)
if(this.config.credentials) delete this.config.credentials;
// display
console.info("BridgeServient configured with");
console.dir(this.config);
// http server for local control and monitoring
let httpServer = (typeof this.config.http.port === "number") ? new HttpServer(this.config.http.port) : new HttpServer();
this.addServer(httpServer);
// remote proxy bridges
if (this.config.fujitsu) this.addServer(new FujitsuServer(this.config.fujitsu.remote));
if (this.config.oracle) this.addServer(new OracleServer(this.config.oracle.store, password));
// clients for consuming
this.addClientFactory(new FileClientFactory());
this.addClientFactory(new HttpClientFactory(this.config.http));
this.addClientFactory(new HttpsClientFactory(this.config.http));
this.addClientFactory(new CoapClientFactory());
this.addClientFactory(new CoapsClientFactory());
}
// exposed protocols
import { OracleServer } from "@node-wot/binding-oracle";
import { HttpServer } from "@node-wot/binding-http";
// consuming protocols
import { CoapClientFactory } from "@node-wot/binding-coap";
import { FileClientFactory } from "@node-wot/binding-file";
import { ConsumedThing } from "wot-typescript-definitions";
console.debug = () => {};
console.log = () => {};
let servient = new Servient();
servient.addServer(new HttpServer());
servient.addServer(new OracleServer());
servient.addClientFactory(new CoapClientFactory());
servient.addClientFactory(new FileClientFactory());
// get WoT object for privileged script
servient.start().then(async (WoT) => {
console.info("OracleServient started");
// choose false for mockup
var live = false;
var PumpP101: ConsumedThing, ValveV102: ConsumedThing;
if (live) {
// remove secrets from original for displaying config (already added)
if(this.config.credentials) delete this.config.credentials;
// display
console.info("DefaultServient configured with");
console.dir(this.config);
// apply config
if (typeof this.config.servient.staticAddress === "string") {
Helpers.setStaticAddress(this.config.servient.staticAddress);
}
if (!this.config.servient.clientOnly) {
if (this.config.http !== undefined) {
let httpServer = new HttpServer(this.config.http);
this.addServer(httpServer);
// re-use httpServer (same port)
this.addServer(new WebSocketServer(httpServer));
}
if (this.config.coap !== undefined) {
// var to reuse below in CoapClient
var coapServer = (typeof this.config.coap.port === "number") ? new CoapServer(this.config.coap.port) : new CoapServer();
this.addServer(coapServer);
}
if (this.config.mqtt !== undefined) {
let mqttBrokerServer = new MqttBrokerServer(this.config.mqtt.broker,
(typeof this.config.mqtt.username === "string") ? this.config.mqtt.username : undefined,
(typeof this.config.mqtt.password === "string") ? this.config.mqtt.password : undefined,
(typeof this.config.mqtt.clientId === "string") ? this.config.mqtt.clientId : undefined,
(typeof this.config.mqtt.protocolVersion === "number") ? this.config.mqtt.protocolVersion : undefined);