Skip to content

Commit

Permalink
make it optional/part of the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Sep 16, 2020
1 parent 446aa8b commit 174dcdb
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 64 deletions.
6 changes: 0 additions & 6 deletions README.md
Expand Up @@ -82,12 +82,6 @@ brick:
- [ ] TravisCI
- [ ] Turn "setRotation" into a skill instead for LED Matrix
- [ ] Investigate gateway crashes
- [ ] Use `ack` as an integrated part of skill responses, ie

```
battery
ack:battery -> 958
```

### Nice to haves
- [ ] Fix errors raised by `pio check` (mainly passing `Message` by value)
Expand Down
20 changes: 9 additions & 11 deletions examples/README.md
Expand Up @@ -13,20 +13,18 @@

### Inbox

| key | description | values |
|------------|--------------------------------------------------------------------|----------------------------|
| * | Responds with `ack:<KEY>` to all messages | |
| setSleep | Start sleep cycles lasting `value` seconds each | 0 (stop sleeping), 1-13612 |
| getBattery | Ask for battery value | |
| setOta | `pio run -t upload --upload-port 192.168.4.1` when connected to AP | |
| getSkills | Get a list of actions the Brick responds to | |
| key | description | values |
|---------|--------------------------------------------------------------------|----------------------------|
| sleep | Start sleep cycles lasting `value` seconds each | 0 (stop sleeping), 1-13612 |
| battery | Ask for battery value | |
| ota | `pio run -t upload --upload-port 192.168.4.1` when connected to AP | |
| skills | Get a list of actions the Brick responds to | |

### Outbox

| key | description | values |
|---------|-----------------------|-------------------|
| awake | Sent when woken up | <NAME> - <REASON> |
| battery | Current battery value | 0 - 1000+ |
| key | description | values |
|-------|--------------------|-------------------|
| awake | Sent when woken up | <NAME> - <REASON> |

## Planned Bricks

Expand Down
8 changes: 3 additions & 5 deletions src/Bricks.BatterySkill.cpp
@@ -1,13 +1,11 @@
#include <Bricks.BatterySkill.h>

namespace Bricks {
BatterySkill::BatterySkill(const uint8_t pin) : Skill("getBattery") {
BatterySkill::BatterySkill(const uint8_t pin) : Skill("battery") {
this->pin = pin;
}

void BatterySkill::callback(const uint8_t *macAddr, const Message message) {
char battery[5];
sprintf(battery, "%i", analogRead(pin));
ack(battery);
void BatterySkill::callback(BRICKS_CALLBACK_SIGNATURE) {
sprintf(response, "%i", analogRead(pin));
}
}
3 changes: 1 addition & 2 deletions src/Bricks.BatterySkill.h
Expand Up @@ -3,13 +3,12 @@

#include <ArduinoLog.h>
#include <Bricks.Skill.h>
#include <Bricks.Outbox.h>

namespace Bricks {
class BatterySkill : public Skill {
public:
BatterySkill(const uint8_t pin = 17); // A0 on Wemos
void callback(const uint8_t *macAddr, const Message message);
void callback(BRICKS_CALLBACK_SIGNATURE);
private:
uint8_t pin;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.Inbox.cpp
Expand Up @@ -34,7 +34,7 @@ namespace Bricks {
for(int i = MAX_SKILLS - 1; i >= 0; i--) {
if(skills[i]->respondsTo(message.key)) {
Log.trace("BRIC: Skill found [%s]" CR, message.key);
skills[i]->callback(macAddr, message);
skills[i]->process(macAddr, message);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bricks.ListSkill.cpp
Expand Up @@ -4,7 +4,7 @@ namespace Bricks {
ListSkill::ListSkill() : Skill("skills") {
}

void ListSkill::callback(const uint8_t *macAddr, const Message message) {
ack(gInbox.listSkills().c_str());
void ListSkill::callback(BRICKS_CALLBACK_SIGNATURE) {
strcpy(response, gInbox.listSkills().c_str());
}
}
3 changes: 1 addition & 2 deletions src/Bricks.ListSkill.h
Expand Up @@ -3,13 +3,12 @@

#include <Bricks.Skill.h>
#include <Bricks.Inbox.h>
#include <Bricks.Outbox.h>

namespace Bricks {
class ListSkill : public Skill {
public:
ListSkill();
void callback(const uint8_t *macAddr, const Message message);
void callback(BRICKS_CALLBACK_SIGNATURE);
};
}
#endif
4 changes: 2 additions & 2 deletions src/Bricks.OtaSkill.cpp
Expand Up @@ -8,10 +8,10 @@ namespace Bricks {
ArduinoOTA.handle();
}

void OtaSkill::callback(const uint8_t *macAddr, const Message message) {
void OtaSkill::callback(BRICKS_CALLBACK_SIGNATURE) {
initOta();
startAP();
ack("Connect to Brick - OTA");
strcpy(response, "Connect to 'Brick - OTA'");
}

void OtaSkill::initOta() {
Expand Down
3 changes: 1 addition & 2 deletions src/Bricks.OtaSkill.h
Expand Up @@ -4,14 +4,13 @@
#include <ArduinoLog.h>
#include <ArduinoOTA.h>
#include <Bricks.Skill.h>
#include <Bricks.Outbox.h>

namespace Bricks {
class OtaSkill : public Skill {
public:
OtaSkill();
void loop();
void callback(const uint8_t *macAddr, const Message message);
void callback(BRICKS_CALLBACK_SIGNATURE);
private:
void initOta();
void startAP();
Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.PublishSkill.cpp
Expand Up @@ -4,7 +4,7 @@ namespace Bricks {
PublishSkill::PublishSkill() : Skill("*") {
}

void PublishSkill::callback(const uint8_t *macAddr, const Message message) {
void PublishSkill::callback(BRICKS_CALLBACK_SIGNATURE) {
gEvents.publish(macAddr, message);
}
}
2 changes: 1 addition & 1 deletion src/Bricks.PublishSkill.h
Expand Up @@ -9,7 +9,7 @@ namespace Bricks {
class PublishSkill : public Skill {
public:
PublishSkill();
void callback(const uint8_t *macAddr, const Message message);
void callback(BRICKS_CALLBACK_SIGNATURE);
};
}
#endif
27 changes: 18 additions & 9 deletions src/Bricks.Skill.cpp
Expand Up @@ -6,22 +6,31 @@ namespace Bricks {
this->customCallback = customCallback;
}

void Skill::loop() {
// Do nothing
bool Skill::respondsTo(const char *compareKey) {
return (strcmp(key, compareKey) == 0 || strcmp(key, ANY) == 0);
}

void Skill::callback(const uint8_t *macAddr, const Message message) {
ack(message.value);
void Skill::process(const uint8_t *macAddr, const Message message) {
char response[100];
callback(macAddr, message, response);

// Doesn't work yet. Always true.
if(response) {
ack(response);
}
}

void Skill::callback(BRICKS_CALLBACK_SIGNATURE) {
customCallback(macAddr, message);
}

bool Skill::respondsTo(const char *compareKey) {
return (strcmp(key, compareKey) == 0 || strcmp(key, ANY) == 0);
void Skill::loop() {
// Do nothing by default
}

void Skill:ack(const char *value) {
void Skill::ack(const char *response) {
char ackKey[KEY_SIZE];
sprintf(ackKey, BRICKS_ACK_PREFIX "%s", key);
gOutbox.send(ackKey, value);
sprintf(ackKey, "%s:%s", ACK_PREFIX, key);
gOutbox.send(ackKey, response);
}
}
10 changes: 7 additions & 3 deletions src/Bricks.Skill.h
Expand Up @@ -4,20 +4,24 @@
#include <ArduinoLog.h>
#include <functional>
#include <Bricks.Message.h>
#include <Bricks.Outbox.h>

#define BRICKS_CALLBACK_SIGNATURE const uint8_t *macAddr, const Message message, char *response

namespace Bricks {
class Skill {
const char *ANY = "*";
const char *ACK_PREFIX = "ack:";
const char *ACK_PREFIX = "ack";

public:
Skill(const char *key = "", std::function<void(const uint8_t *macAddr, const Message message)> customCallback = nullptr);
bool respondsTo(const char* compareKey);
void process(const uint8_t *macAddr, const Message message);
virtual void callback(BRICKS_CALLBACK_SIGNATURE);
virtual void loop();
virtual void callback(const uint8_t *macAddr, const Message message);
const char *key;
private:
void ack();
void ack(const char *response);
std::function<void(const uint8_t *macAddr, const Message message)> customCallback;
};
}
Expand Down
14 changes: 10 additions & 4 deletions src/Bricks.SleepSkill.cpp
Expand Up @@ -5,7 +5,7 @@ namespace Bricks {
RTC_DATA_ATTR uint32_t rtcSleepTime = 0;
#endif

SleepSkill::SleepSkill(const char *name) : Skill("setSleep") {
SleepSkill::SleepSkill(const char *name) : Skill("sleep") {
this->name = name;
sendAwakeMessage();

Expand All @@ -21,10 +21,16 @@ namespace Bricks {
}
}

void SleepSkill::callback(const uint8_t *macAddr, const Message message) {
void SleepSkill::callback(BRICKS_CALLBACK_SIGNATURE) {
this->sleepTime = atoi(message.value);
writeSleepTime();
ack("Going to sleep for sleepTime second(s)");

if(sleepTime > 0) {
sprintf(response, "Starting %d second sleep cycles", sleepTime);
}
else {
strcpy(response, "Stopping sleep cycles");
}
}

void SleepSkill::sendAwakeMessage() {
Expand All @@ -37,7 +43,7 @@ namespace Bricks {
}

void SleepSkill::deepSleep() {
// Log.notice("SLEE: Going to sleep for %d second(s)" CR, sleepTime);
Log.notice("SLEE: Going to sleep for %d second(s)" CR, sleepTime);
#ifdef ESP8266
ESP.deepSleep(sleepTime * MICROSECONDS);
#elif ESP32
Expand Down
3 changes: 1 addition & 2 deletions src/Bricks.SleepSkill.h
Expand Up @@ -18,7 +18,6 @@ extern "C" {

#include <Bricks.Skill.h>
#include <Bricks.Constants.h>
#include <Bricks.Outbox.h>
#include <Bricks.Utils.h>

namespace Bricks {
Expand All @@ -29,7 +28,7 @@ namespace Bricks {
public:
SleepSkill(const char *name = "New Brick");
void loop();
void callback(const uint8_t *macAddr, const Message message);
void callback(BRICKS_CALLBACK_SIGNATURE);
private:
void sendAwakeMessage();
void deepSleep();
Expand Down
22 changes: 11 additions & 11 deletions test/inbox/test_inbox.cpp
Expand Up @@ -2,34 +2,34 @@
#include <ArduinoLog.h>
#include <unity.h>
#include <Bricks.Inbox.h>
#include <Bricks.AckSkill.h>
#include <Bricks.BatterySkill.h>
using namespace Bricks;

void setUp(void) {
Serial.begin(115200);
Log.begin(LOG_LEVEL_TRACE, &Serial);
}

// void test_ack() {
// const uint8_t macAddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Message message;
// strcpy(message.key, "ping");
void test_battery() {
const uint8_t macAddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Message message;
strcpy(message.key, "battery");

// gInbox.skills[0] = new AckSkill();
// gInbox.process(macAddr, message);
// }
gInbox.skills[0] = new BatterySkill();
gInbox.process(macAddr, message);
}

void test_list_skills() {
gInbox.skills[0] = new AckSkill();
gInbox.skills[0] = new BatterySkill();

TEST_ASSERT_EQUAL_STRING("*", gInbox.listSkills().c_str());
TEST_ASSERT_EQUAL_STRING("battery", gInbox.listSkills().c_str());
}

void setup() {
delay(2000); // if board doesn't support software reset via Serial.DTR/RTS

UNITY_BEGIN();
// RUN_TEST(test_ack);
RUN_TEST(test_battery);
RUN_TEST(test_list_skills);
UNITY_END();
}
Expand Down

0 comments on commit 174dcdb

Please sign in to comment.