Skip to content

Commit

Permalink
fix(coap-server): add missing cov:observe subprotocol
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 12, 2024
1 parent 9d04652 commit 55e197c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
49 changes: 34 additions & 15 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { MdnsIntroducer } from "./mdns-introducer";
import { PropertyElement, DataSchema, ActionElement, EventElement } from "wot-thing-description-types";
import { CoapServerConfig } from "./coap";
import { DataSchemaValue } from "wot-typescript-definitions";
import { filterPropertyObserveOperations, getPropertyOpValues } from "./util";

const { debug, warn, info, error } = createLoggers("binding-coap", "coap-server");

Expand Down Expand Up @@ -233,27 +234,42 @@ export default class CoapServer implements ProtocolServer {
private addFormToAffordance(form: TD.Form, affordance: AffordanceElement): void {
const affordanceForms = affordance.forms;
if (affordanceForms == null) {
affordance.forms ??= [form];
affordance.forms = [form];
} else {
affordanceForms.push(form);
}
}

private fillInPropertyBindingData(thing: ExposedThing, base: string, offeredMediaType: string) {
for (const [propertyName, property] of Object.entries(thing.properties)) {
const opValues = ProtocolHelpers.getPropertyOpValues(property);
const form = this.createAffordanceForm(
base,
this.PROPERTY_DIR,
offeredMediaType,
opValues,
thing.uriVariables,
propertyName,
property.uriVariables
);
const [readWriteOpValues, observeOpValues] = getPropertyOpValues(property);
for (const formOpValues of [observeOpValues, readWriteOpValues]) {
if (formOpValues.length === 0) {
continue;
}

this.addFormToAffordance(form, property);
this.logHrefAssignment(form, "Property", propertyName);
let subprotocol: string | undefined;

const observeOpValues = filterPropertyObserveOperations(formOpValues);

if (observeOpValues.length > 0) {
subprotocol = "cov:observe";
}

const form = this.createAffordanceForm(
base,
this.PROPERTY_DIR,
offeredMediaType,
formOpValues,
thing.uriVariables,
propertyName,
property.uriVariables,
subprotocol
);

this.addFormToAffordance(form, property);
this.logHrefAssignment(form, "Property", propertyName);
}
}
}

Expand Down Expand Up @@ -283,7 +299,8 @@ export default class CoapServer implements ProtocolServer {
["subscribeevent", "unsubscribeevent"],
thing.uriVariables,
eventName,
event.uriVariables
event.uriVariables,
"cov:observe"
);

this.addFormToAffordance(form, event);
Expand All @@ -298,7 +315,8 @@ export default class CoapServer implements ProtocolServer {
opValues: string | string[],
thingUriVariables: PropertyElement["uriVariables"],
affordanceName?: string,
affordanceUriVariables?: PropertyElement["uriVariables"]
affordanceUriVariables?: PropertyElement["uriVariables"],
subprotocol?: string
): TD.Form {
const affordanceNamePattern = Helpers.updateInteractionNameWithUriVariablePattern(
affordanceName ?? "",
Expand All @@ -314,6 +332,7 @@ export default class CoapServer implements ProtocolServer {

const form = new TD.Form(href, offeredMediaType);
form.op = opValues;
form.subprotocol = subprotocol;

return form;
}
Expand Down
69 changes: 69 additions & 0 deletions packages/binding-coap/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/

import { ProtocolHelpers } from "@node-wot/core";
import { PropertyElement } from "wot-thing-description-types";

const observeOpFilter = ["observeproperty", "unobserveproperty"];
const readWriteOpFilter = ["readproperty", "writeproperty"];

function filterOpValues(opValues: string[], filterValues: string[]) {
return opValues.filter((opValue) => filterValues.includes(opValue));
}

/**
* Convenience function to filter out the `op` values "observeproperty" and
* "unobserveproperty" from a string array.
*
* @param opValues The `op` values to be filtered.
* @returns A filtered array that might be empty.
*/
export function filterPropertyObserveOperations(opValues: string[]) {
return filterOpValues(opValues, observeOpFilter);
}

/**
* Convenience function to filter out the `op` values "readproperty" and
* "writeproperty" from a string array.
*
* @param opValues The `op` values to be filtered.
* @returns A filtered array that might be empty.
*/
function filterPropertyReadWriteOperations(opValues: string[]) {
return filterOpValues(opValues, readWriteOpFilter);
}

/**
* Function to (potentially) generate two arrays of `op` values: One with the
* values "readproperty" and "writeproperty", and one with
* "observerproperty" and "unobserveproperty".
*
* This CoAP-specific distinction is made to be able to generate
* separate forms for the observe-related operations, where the addition
* of a `subprotocol` field with a value of `cov:observe` has to be added.
*
* @param property The property for which the forms are going to be
* generated.
* @returns A tuple consisting of two op value arrays (one for read and
* write operations, one for observe-related operations).
*/
export function getPropertyOpValues(property: PropertyElement) {
const opValues = ProtocolHelpers.getPropertyOpValues(property);

const readWriteOpValues = filterPropertyReadWriteOperations(opValues);
const observeOpValues = filterPropertyObserveOperations(opValues);

return [readWriteOpValues, observeOpValues];
}

0 comments on commit 55e197c

Please sign in to comment.