Skip to content

Commit

Permalink
Add @type and @context to json; Add multiple types to Thing.h
Browse files Browse the repository at this point in the history
  • Loading branch information
hobinjk committed Jun 28, 2018
1 parent 2192069 commit 0843ad3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
11 changes: 10 additions & 1 deletion ESP32WebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,16 @@ class WebThingAdapter {

void serializeDevice(JsonObject& descr, ThingDevice* device) {
descr["name"] = device->name;
descr["type"] = device->type;
descr["href"] = "/things/" + device->id;
descr["@context"] = "https://iot.mozilla.org/schemas";

JsonArray& typeJson = descr.createNestedArray("@type");
const char** type = device->type;
while ((*type) != nullptr) {
typeJson.add(*type);
type++;
}

JsonObject& props = descr.createNestedObject("properties");

ThingProperty* property = device->firstProperty;
Expand All @@ -133,6 +141,7 @@ class WebThingAdapter {
prop["type"] = "string";
break;
}
prop["@type"] = property->atType;
prop["href"] = "/things/" + device->id + "/properties/" + property->id;
property = property->next;
}
Expand Down
12 changes: 10 additions & 2 deletions ESP8266WebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,17 @@ class WebThingAdapter {

void serializeDevice(JsonObject& descr, ThingDevice* device) {
descr["name"] = device->name;
descr["type"] = device->type;
descr["href"] = "/things/" + device->id;
JsonObject& props = descr.createNestedObject("properties");
descr["@context"] = "https://iot.mozilla.org/schemas";

JsonArray& typeJson = descr.createNestedArray("@type");
const char** type = device->type;
while ((*type) != nullptr) {
typeJson.add(*type);
type++;
}

JsonObject& props = descr.createNestedObject("properties");
ThingProperty* property = device->firstProperty;
while (property != nullptr) {
JsonObject& prop = props.createNestedObject(property->id);
Expand All @@ -125,6 +132,7 @@ class WebThingAdapter {
prop["type"] = "string";
break;
}
prop["@type"] = property->atType;
prop["href"] = "/things/" + device->id + "/properties/" + property->id;
property = property->next;
}
Expand Down
10 changes: 6 additions & 4 deletions Thing.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class ThingProperty {
String id;
String description;
ThingPropertyType type;
String atType;
ThingProperty* next = nullptr;

ThingProperty(const char* id_, const char* description_, ThingPropertyType type_):
ThingProperty(const char* id_, const char* description_, ThingPropertyType type_, const char* atType_):
id(id_),
description(description_),
type(type_) {
type(type_),
atType(atType_) {
}

void setValue(ThingPropertyValue newValue) {
Expand All @@ -53,12 +55,12 @@ class ThingDevice {
public:
String id;
String name;
String type;
const char** type;
ThingDevice* next = nullptr;
ThingProperty* firstProperty = nullptr;
ThingProperty* lastProperty = nullptr;

ThingDevice(const char* _id, const char* _name, const char* _type):
ThingDevice(const char* _id, const char* _name, const char** _type):
id(_id),
name(_name),
type(_type) {
Expand Down
11 changes: 10 additions & 1 deletion WiFi101WebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,16 @@ class WebThingAdapter {

void serializeDevice(JsonObject& descr, ThingDevice* device) {
descr["name"] = device->name;
descr["type"] = device->type;
descr["href"] = "/things/" + device->id;
descr["@context"] = "https://iot.mozilla.org/schemas";

JsonArray& typeJson = descr.createNestedArray("@type");
const char** type = device->type;
while ((*type) != nullptr) {
typeJson.add(*type);
type++;
}

JsonObject& props = descr.createNestedObject("properties");

ThingProperty* property = device->firstProperty;
Expand All @@ -355,6 +363,7 @@ class WebThingAdapter {
prop["type"] = "string";
break;
}
prop["@type"] = property->atType;
prop["href"] = "/things/" + device->id + "/properties/" + property->id;
property = property->next;
}
Expand Down

0 comments on commit 0843ad3

Please sign in to comment.