How to use the node-coap-client.CoapClient.request function in node-coap-client

To help you get started, we’ve selected a few node-coap-client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eclipse / thingweb.node-wot / packages / binding-coap / src / coaps-client.ts View on Github external
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(
        form.href /* string */,
        method /* "get" | "post" | "put" | "delete" */,
        content ? content.body : undefined /* Buffer */
    );

    return req;
  }
}
github AlCalzone / node-tradfri-client / src / tradfri-client.ts View on Github external
private async updateResource(path: string, newObj: IPSOObject, reference: IPSOObject): Promise {

		const serializedObj = newObj.serialize(reference);

		// If the serialized object contains no properties, we don't need to send anything
		if (!serializedObj || Object.keys(serializedObj).length === 0) {
			log(`updateResource(${path}) > empty object, not sending any payload`, "debug");
			return false;
		}

		// get the payload
		let payload: string | Buffer = JSON.stringify(serializedObj);
		log(`updateResource(${path}) > sending payload: ${payload}`, "debug");
		payload = Buffer.from(payload);

		await coap.request(
			`${this.requestBase}${path}`, "put", payload,
		);
		return true;
	}
github AlCalzone / node-tradfri-client / src / tradfri-client.ts View on Github external
payload?: object,
	): Promise<{
		code: string,
		payload: any,
	}> {

		// create actual payload
		let jsonPayload: string | Buffer;
		if (payload != null) {
			jsonPayload = JSON.stringify(payload);
			log("sending custom payload: " + jsonPayload, "debug");
			jsonPayload = Buffer.from(jsonPayload);
		}

		// wait for the CoAP response and respond to the message
		const resp = await coap.request(
			`${this.requestBase}${path}`,
			method,
			jsonPayload as Buffer,
		);
		return {
			code: resp.code.toString(),
			payload: parsePayload(resp),
		};
	}
}
github AlCalzone / node-tradfri-client / src / tradfri-client.ts View on Github external
public async authenticate(securityCode: string): Promise<{identity: string, psk: string}> {
		// first, check try to connect with the security code
		log("authenticate() > trying to connect with the security code", "debug");
		if (!await this.tryToConnect("Client_identity", securityCode)) {
			// that didn't work, so the code is wrong
			throw new TradfriError("The security code is wrong", TradfriErrorCodes.ConnectionFailed);
		}
		// generate a new identity
		const identity = `tradfri_${Date.now()}`;
		log(`authenticating with identity "${identity}"`, "debug");

		// request creation of new PSK
		let payload: string | Buffer = JSON.stringify({ 9090: identity });
		payload = Buffer.from(payload);
		const response = await coap.request(
			`${this.requestBase}${coapEndpoints.authentication}`,
			"post",
			payload,
		);

		// check the response
		if (response.code.toString() !== "2.01") {
			// that didn't work, so the code is wrong
			throw new TradfriError(
				`unexpected response (${response.code.toString()}) to getPSK().`,
				TradfriErrorCodes.AuthenticationFailed,
			);
		}
		// the response is a buffer containing a JSON object as a string
		const pskResponse = JSON.parse(response.payload.toString("utf8"));
		const psk = pskResponse["9091"];