Skip to content

Commit

Permalink
feat: combine all properties reporting into one call (for http for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Jan 29, 2019
1 parent 2712b15 commit 01ceeec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
37 changes: 27 additions & 10 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class HttpServer implements ProtocolServer {
for (let type of ContentSerdes.get().getOfferedMediaTypes()) {
let base: string = this.scheme + "://" + address + ":" + this.getPort() + "/" + encodeURIComponent(name);

if(thing.propertiesReadAll && thing.propertiesReadAll != null) {
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) {
Expand Down Expand Up @@ -342,17 +342,34 @@ export default class HttpServer implements ProtocolServer {
if (segments[2] === this.ALL_DIR) {
if(this.ALL_PROPERTIES == segments[3]) {
if (req.method === "GET") {
// let value = "TODO";
// let schema: WoT.DataSchema = {"type": "string"};
// let content = ContentSerdes.get().valueToContent(value, schema);
// res.setHeader("Content-Type", content.type);
var obj: {[k: string]: any} = {};
for (let key in thing.properties) {
obj[key] = "Todo";
let obj: {[k: string]: any} = {};
let promises = [];

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

res.writeHead(200);
res.end(JSON.stringify(obj)); // content.body);
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");
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
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 01ceeec

Please sign in to comment.