Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: llvm
ColumnLimit: 79
IndentWidth: 2
SortIncludes: false
6 changes: 6 additions & 0 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Lint source
uses: DoozyX/clang-format-lint-action@v0.5
with:
source: '.'
extensions: 'h,cpp,ino'
clangFormatVersion: 9
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
Expand Down
226 changes: 134 additions & 92 deletions ESPWebThingAdapter.h

Large diffs are not rendered by default.

76 changes: 37 additions & 39 deletions EthernetWebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ MDNS mdns(udp);

static const bool DEBUG = false;

enum HTTPMethod {
HTTP_ANY,
HTTP_GET,
HTTP_PUT,
HTTP_OPTIONS
};
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_PUT, HTTP_OPTIONS };

enum ReadState {
STATE_READ_METHOD,
Expand All @@ -60,9 +55,11 @@ enum ReadState {

class WebThingAdapter {
public:
WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80): name(_name), port(_port), server(_port)
WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80)
: name(_name), port(_port), server(_port)
#ifdef CONFIG_MDNS
, mdns(udp)
,
mdns(udp)
#endif
{
ip = "";
Expand All @@ -80,10 +77,7 @@ class WebThingAdapter {
#ifdef CONFIG_MDNS
mdns.begin(Ethernet.localIP(), name.c_str());

mdns.addServiceRecord("_webthing",
port,
MDNSServiceTCP,
"\x06path=/");
mdns.addServiceRecord("_webthing", port, MDNSServiceTCP, "\x06path=/");
#endif
server.begin();
}
Expand Down Expand Up @@ -130,7 +124,7 @@ class WebThingAdapter {
return;
}

switch(state) {
switch (state) {
case STATE_READ_METHOD:
if (c == ' ') {
if (methodRaw == "GET") {
Expand Down Expand Up @@ -209,7 +203,7 @@ class WebThingAdapter {
}
}

void addDevice(ThingDevice* device) {
void addDevice(ThingDevice *device) {
if (this->lastDevice == nullptr) {
this->firstDevice = device;
this->lastDevice = device;
Expand All @@ -218,6 +212,7 @@ class WebThingAdapter {
this->lastDevice = device;
}
}

private:
String name, ip;
uint16_t port;
Expand Down Expand Up @@ -281,7 +276,7 @@ class WebThingAdapter {
return;
}

ThingDevice* device = this->firstDevice;
ThingDevice *device = this->firstDevice;
while (device != nullptr) {
String deviceBase = "/things/" + device->id;

Expand All @@ -298,7 +293,7 @@ class WebThingAdapter {
} else if (uri == deviceBase + "/events") {
handleThingGetAll(device->firstEvent);
} else {
ThingProperty* property = device->firstProperty;
ThingProperty *property = device->firstProperty;
while (property != nullptr) {
String propertyBase = deviceBase + "/properties/" + property->id;
if (uri == propertyBase) {
Expand All @@ -311,7 +306,7 @@ class WebThingAdapter {
}
return;
}
property = (ThingProperty*)property->next;
property = (ThingProperty *)property->next;
}
}
}
Expand All @@ -320,9 +315,7 @@ class WebThingAdapter {
handleError();
}

void sendOk() {
client.println("HTTP/1.1 200 OK");
}
void sendOk() { client.println("HTTP/1.1 200 OK"); }

void sendHeaders() {
client.println("Access-Control-Allow-Origin: *");
Expand All @@ -338,7 +331,7 @@ class WebThingAdapter {

DynamicJsonDocument buf(1024);
JsonArray things = buf.to<JsonArray>();
ThingDevice* device = this->firstDevice;
ThingDevice *device = this->firstDevice;
while (device != nullptr) {
JsonObject descr = things.createNestedObject();
this->serializeDevice(descr, device);
Expand All @@ -351,8 +344,10 @@ class WebThingAdapter {
client.stop();
}

void serializePropertyOrEvent(JsonObject descr, ThingDevice* device, const char* type, bool isProp, ThingItem* item) {
String basePath = "/things/" + device->id + "/"+ type + "/";
void serializePropertyOrEvent(JsonObject descr, ThingDevice *device,
const char *type, bool isProp,
ThingItem *item) {
String basePath = "/things/" + device->id + "/" + type + "/";
JsonObject props = descr.createNestedObject(type);
while (item != nullptr) {
JsonObject prop = props.createNestedObject(item->id);
Expand Down Expand Up @@ -402,14 +397,15 @@ class WebThingAdapter {
}

if (isProp) {
ThingProperty* property = (ThingProperty*)item;
ThingProperty *property = (ThingProperty *)item;
const char **enumVal = property->propertyEnum;
bool hasEnum = (property->propertyEnum != nullptr) && ((*property->propertyEnum) != nullptr);
bool hasEnum = (property->propertyEnum != nullptr) &&
((*property->propertyEnum) != nullptr);

if (hasEnum) {
enumVal = property->propertyEnum;
JsonArray propEnum = prop.createNestedArray("enum");
while (property->propertyEnum != nullptr && (*enumVal) != nullptr){
while (property->propertyEnum != nullptr && (*enumVal) != nullptr) {
propEnum.add(*enumVal);
enumVal++;
}
Expand All @@ -420,7 +416,9 @@ class WebThingAdapter {
prop["@type"] = item->atType;
}

// 2.9 Property object: A links array (An array of Link objects linking to one or more representations of a Property resource, each with an implied default rel=property.)
// 2.9 Property object: A links array (An array of Link objects linking
// to one or more representations of a Property resource, each with an
// implied default rel=property.)
JsonArray inline_links = prop.createNestedArray("links");
JsonObject inline_links_prop = inline_links.createNestedObject();
inline_links_prop["href"] = basePath + item->id;
Expand All @@ -429,7 +427,7 @@ class WebThingAdapter {
}
}

void serializeDevice(JsonObject descr, ThingDevice* device) {
void serializeDevice(JsonObject descr, ThingDevice *device) {
descr["id"] = device->id;
descr["title"] = device->title;
descr["@context"] = "https://iot.mozilla.org/schemas";
Expand All @@ -439,13 +437,14 @@ class WebThingAdapter {
}
// TODO: descr["base"] = ???

JsonObject securityDefinitions = descr.createNestedObject("securityDefinitions");
JsonObject securityDefinitions =
descr.createNestedObject("securityDefinitions");
JsonObject nosecSc = securityDefinitions.createNestedObject("nosec_sc");
nosecSc["scheme"] = "nosec";
descr["security"] = "nosec_sc";

JsonArray typeJson = descr.createNestedArray("@type");
const char** type = device->type;
const char **type = device->type;
while ((*type) != nullptr) {
typeJson.add(*type);
type++;
Expand All @@ -464,18 +463,18 @@ class WebThingAdapter {
links_prop["href"] = "/things/" + device->id + "/events";
}

ThingProperty* property = device->firstProperty;
ThingProperty *property = device->firstProperty;
if (property) {
serializePropertyOrEvent(descr, device, "properties", true, property);
}

ThingEvent* event = device->firstEvent;
ThingEvent *event = device->firstEvent;
if (event) {
serializePropertyOrEvent(descr, device, "events", false, event);
}
}

void handleThing(ThingDevice* device) {
void handleThing(ThingDevice *device) {
sendOk();
sendHeaders();

Expand All @@ -488,7 +487,7 @@ class WebThingAdapter {
client.stop();
}

void serializeThingItem(ThingItem* item, JsonObject prop) {
void serializeThingItem(ThingItem *item, JsonObject prop) {
switch (item->type) {
case NO_STATE:
break;
Expand All @@ -507,7 +506,7 @@ class WebThingAdapter {
}
}

void handleThingGetItem(ThingItem* item) {
void handleThingGetItem(ThingItem *item) {
sendOk();
sendHeaders();

Expand All @@ -519,7 +518,7 @@ class WebThingAdapter {
client.stop();
}

void handleThingGetAll(ThingItem* rootItem) {
void handleThingGetAll(ThingItem *rootItem) {
sendOk();
sendHeaders();

Expand All @@ -535,7 +534,7 @@ class WebThingAdapter {
client.stop();
}

void setThingProperty(const JsonObject newProp, ThingProperty* property) {
void setThingProperty(const JsonObject newProp, ThingProperty *property) {
const JsonVariant newValue = newProp[property->id];

switch (property->type) {
Expand Down Expand Up @@ -566,7 +565,7 @@ class WebThingAdapter {
}
}

void handleThingPropertyPut(ThingProperty* property) {
void handleThingPropertyPut(ThingProperty *property) {
sendOk();
sendHeaders();
DynamicJsonDocument newBuffer(256);
Expand Down Expand Up @@ -601,7 +600,6 @@ class WebThingAdapter {
content = "";
retries = 0;
}

};

#endif // neither ESP32 nor ESP8266 defined
Expand Down
Loading