Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add semantic composed thing #1174

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions packages/core/src/semantic-consumed-thing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/********************************************************************************
* Copyright (c) 2023 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 * as WoT from "wot-typescript-definitions";

import ConsumedThing from "./consumed-thing";

/**
* Represents a semantic composed thing (i.e. helper wrapper around ConsumedThing) to allow interactions
* with a thing based on semantic types.
* @experimental
*/
export default class SemanticConsumedThing extends ConsumedThing implements WoT.ConsumedThing {
/**
* Read a property based on semantic type
*/
// Open Questions
// - define semantic input as string and/or array
// - allow AND / OR combinations? e.g., @type must have XYZ but should not have CBA
// - do we want to make the selection on output type as well ?
async readSemanticProperty(semanticType: string, options?: WoT.InteractionOptions): Promise<WoT.InteractionOutput> {
// try to find property by semantic look-up
const propertyName = this.getSemanticPropertyName(semanticType);
if (propertyName != null) {
// found property -> hand over to core functionality
return this.readProperty(propertyName, options);
}
// no match found
throw new Error(
`SemanticConsumedThing '${this.title}' did not find suitable semantic property for ${semanticType}`
);
}

private getSemanticPropertyName(semanticType: string): string | undefined {
// walk over available properties and find (the) right one
// e.g., .read( {"@type": "iot:BinarySwitchData"}
for (const propertyName in this.properties) {
const property = this.properties[propertyName];
if (property["@type"] != null) {
if (typeof property["@type"] === "string") {
if (property["@type"] === semanticType) {
return propertyName;
}
} else if (Array.isArray(property["@type"])) {
const types = property["@type"];
if (types.includes(semanticType)) {
return propertyName;
}
}
}
}
return undefined;
}

// Open Questions
// - is a method necessary to check for availability ?
public isSemanticPropertyAvailable(semanticType: string): boolean {
return this.getSemanticPropertyName(semanticType) != null;
}

/**
* Write a property based on semantic type
*/
// Open Questions
// - how to know the input value up-front ?
// - how to select desired property based on input type?
async writeSemanticProperty(
propertyName: string,
value: WoT.InteractionInput,
options?: WoT.InteractionOptions
): Promise<void> {
throw new Error(`Not implemented`);
}

// ..., invokeAction, readAllProperties, readMultipleProperties, ...
// w.r.t. interaction that need input and/or provide output we might want to know the input/output type as well
}
Loading