Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private generateRequest(form: CoapForm, dflt: string, content?: Content): any {
// url only works with http*
let requestUri = url.parse(form.href.replace(/$coaps/, "https"));
coaps.setSecurityParams(requestUri.hostname, this.authorization );
let method: string = dflt;
if (typeof form["coap:methodCode"] === "number") {
console.log("CoapsClient got Form 'methodCode'", form["coap:methodCode"]);
switch (form["coap:methodCode"]) {
case 1: method = "get"; break;
case 2: method = "post"; break;
case 3: method = "put"; break;
case 4: method = "delete"; break;
default: console.warn("CoapsClient got invalid 'methodCode', using default", method);
}
}
console.log(`CoapsClient sending ${method} to ${form.href}`);
let req = coaps.request(
public subscribeResource(form: CoapForm, next: ((value: any) => void), error?: (error: any) => void, complete?: () => void): any {
let requestUri = url.parse(form.href.replace(/$coaps/, "https"));
coaps.setSecurityParams(requestUri.hostname, this.authorization );
coaps.observe(
form.href,
"get",
next
)
.then(() => { /* observing was successfully set up */})
.catch((err: any) => { error(err); })
return new Subscription(() => { coaps.stopObserving(form.href); complete(); });
}
private async tryToConnect(identity: string, psk: string): Promise {
// initialize CoAP client
coap.reset();
coap.setSecurityParams(this.hostname, {
psk: { [identity]: psk },
});
log(`Attempting connection. Identity = ${identity}, psk = ${psk}`, "debug");
const result = await coap.tryToConnect(this.requestBase);
log(`Connection ${result ? "" : "un"}successful`, "debug");
return result;
}