Skip to content

Commit

Permalink
Merge pull request #66 from eclipse/readAllProperties
Browse files Browse the repository at this point in the history
Read all properties
  • Loading branch information
danielpeintner committed Jan 29, 2019
2 parents e92838c + 01ceeec commit cd99e8f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 9 deletions.
86 changes: 86 additions & 0 deletions examples/scripts/counterReadAllProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/********************************************************************************
* 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
********************************************************************************/

const NAME_PROPERTY_COUNT = "count";
const NAME_PROPERTY_LAST_CHANGE = "lastChange";
const NAME_ACTION_INCREMENT = "increment";
const NAME_ACTION_DECREMENT = "decrement";
const NAME_ACTION_RESET = "reset";

let thing = WoT.produce({
name: "counter",
description: "counter example Thing",
"@context": ["http://www.w3.org/ns/td", {"iot": "http://example.org/iot"}],
});

console.log("Produced " + thing.name);

thing.addProperty(
NAME_PROPERTY_COUNT,
{
type: "integer",
description: "current counter value",
"iot:Custom": "example annotation",
observable: true,
writable: true
},
0);
thing.addProperty(
NAME_PROPERTY_LAST_CHANGE,
{
type: "string",
description: "last change of counter value",
observable: true,
writable: false
},
(new Date()).toUTCString());

thing.addAction(
NAME_ACTION_INCREMENT,
{},
() => {
console.log("Incrementing");
return thing.properties[NAME_PROPERTY_COUNT].read().then( (count) => {
let value = count + 1;
thing.properties[NAME_PROPERTY_COUNT].write(value);
thing.properties[NAME_PROPERTY_LAST_CHANGE].write((new Date()).toUTCString());
});
});

thing.addAction(
NAME_ACTION_DECREMENT,
{},
() => {
console.log("Decrementing");
return thing.properties[NAME_PROPERTY_COUNT].read().then( (count) => {
let value = count - 1;
thing.properties[NAME_PROPERTY_COUNT].write(value);
thing.properties[NAME_PROPERTY_LAST_CHANGE].write((new Date()).toUTCString());
});
});

thing.addAction(
NAME_ACTION_RESET,
{},
() => {
console.log("Resetting");
thing.properties[NAME_PROPERTY_COUNT].write(0);
thing.properties[NAME_PROPERTY_LAST_CHANGE].write((new Date()).toUTCString());
});

// test setting metadata
thing["support"] = "git://github.com/eclipse/thingweb.node-wot.git";

thing.expose().then( () => { console.info(thing.name + " ready"); } );
51 changes: 50 additions & 1 deletion packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default class HttpServer implements ProtocolServer {

public readonly scheme: "http" | "https";

private readonly ALL_DIR = "all";
private readonly ALL_PROPERTIES = "properties";
private readonly PROPERTY_DIR = "properties";
private readonly ACTION_DIR = "actions";
private readonly EVENT_DIR = "events";
Expand Down Expand Up @@ -163,6 +165,15 @@ export default class HttpServer implements ProtocolServer {
for (let type of ContentSerdes.get().getOfferedMediaTypes()) {
let base: string = this.scheme + "://" + address + ":" + this.getPort() + "/" + encodeURIComponent(name);

if(true) { // make reporting of all properties optional?
let href = base + "/" + this.ALL_DIR + "/" + encodeURIComponent(this.ALL_PROPERTIES);
let form = new TD.Form(href, type);
if(!thing.forms) {
thing.forms = [];
}
thing.forms.push(form);
}

for (let propertyName in thing.properties) {
let href = base + "/" + this.PROPERTY_DIR + "/" + encodeURIComponent(propertyName);
let form = new TD.Form(href, type);
Expand Down Expand Up @@ -328,7 +339,45 @@ export default class HttpServer implements ProtocolServer {
return;
}

if (segments[2] === this.PROPERTY_DIR) {
if (segments[2] === this.ALL_DIR) {
if(this.ALL_PROPERTIES == segments[3]) {
if (req.method === "GET") {
let obj: {[k: string]: any} = {};
let promises = [];

for (let key in thing.properties) {
let property = thing.properties[key].read();
promises.push(property);
}

Promise.all(promises)
.then((value) => {
let index = 0;
for (let key in thing.properties) {
// TODO proper contentType handling
// let property = thing.properties[key];
// let content = ContentSerdes.get().valueToContent(value[index], <any>property);
// obj[key] = content.body;
obj[key] = value[index];
index++;
}
// res.setHeader("Content-Type", content.type);
res.writeHead(200);
res.end(JSON.stringify(obj));
})
.catch(err => {
console.error(`HttpServer on port ${this.getPort()} got internal error on read '${requestUri.pathname}': ${err.message}`);
res.writeHead(500);
res.end(err.message);
});
} else {
res.writeHead(405);
res.end("Method Not Allowed");
}
// resource found and response sent
return;
}
} else if (segments[2] === this.PROPERTY_DIR) {
// sub-path -> select Property
let property = thing.properties[segments[3]];
if (property) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import Helpers from "./helpers";
import { Content } from "./protocol-interfaces";

export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {

//private restListeners: Map<string, ResourceListener> = new Map<string, ResourceListener>();

security: Array<String>;
securityDefinitions: { [key: string]: WoT.Security };

id: string;
name: string;
base: string;
forms: Array<WoT.Form>;

/** A map of interactable Thing Properties with read()/write()/subscribe() functions */
properties: {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/servient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export default class Servient {

console.log(`Servient exposing '${thing.name}'`);

// initiatlizing forms fields
// initializing forms fields
thing.forms = [];
for (let name in thing.properties) {
thing.properties[name].forms = [];
}
Expand Down
12 changes: 8 additions & 4 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ export function serializeTD(thing: Thing): string {
copy.security = ["nosec_sc"];
}

if (!copy.properties || Object.keys(copy.properties).length === 0) {
if (copy.forms && copy.forms.length === 0) {
delete copy.forms;
}

if (copy.properties && Object.keys(copy.properties).length === 0) {
delete copy.properties;
} else {
} else if(copy.properties) {
// add mandatory fields (if missing): observable, writeOnly, and readOnly
for (let propName in copy.properties) {
let prop = copy.properties[propName];
Expand All @@ -163,9 +167,9 @@ export function serializeTD(thing: Thing): string {
}
}

if (!copy.actions || Object.keys(copy.actions).length === 0) {
if (copy.actions && Object.keys(copy.actions).length === 0) {
delete copy.actions;
} else {
} else if (copy.actions) {
// add mandatory fields (if missing): idempotent and safe
for (let actName in copy.actions) {
let act = copy.actions[actName];
Expand Down

0 comments on commit cd99e8f

Please sign in to comment.