|
@@ -1,5 +1,8 @@
|
|
|
#include "dk.h"
|
|
|
|
|
|
+#include <vector>
|
|
|
+#include <atomic>
|
|
|
+
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
#include <freertos/task.h>
|
|
|
#include <kbf.h>
|
|
@@ -9,14 +12,85 @@
|
|
|
|
|
|
#include "settings_manager.h"
|
|
|
#include "controllers/pair_controller.h"
|
|
|
-#include "controllers/scan_controller.h"
|
|
|
#include "controllers/settings_controller.h"
|
|
|
#include "controllers/verify_controller.h"
|
|
|
|
|
|
|
|
|
+#define MESSAGE_TIMEOUT 1000 // TODO use Kconfig
|
|
|
+
|
|
|
+
|
|
|
using namespace kbf;
|
|
|
+using std::vector;
|
|
|
+using std::atomic;
|
|
|
|
|
|
namespace dk {
|
|
|
+ void handleConnect(wifi::AP::STA &sta);
|
|
|
+
|
|
|
+ void setupPairingMode();
|
|
|
+
|
|
|
+ void setupArmedMode();
|
|
|
+
|
|
|
+ void testNotification();
|
|
|
+
|
|
|
+ rtos::EventGroup eventGroup;
|
|
|
+ struct Events {
|
|
|
+ static const unsigned int SET_PAIRING_MODE = 0;
|
|
|
+ static const unsigned int SET_ARMED_MODE = 1;
|
|
|
+ };
|
|
|
+ const std::vector<int> ALL_EVENTS = {
|
|
|
+ Events::SET_PAIRING_MODE,
|
|
|
+ Events::SET_ARMED_MODE,
|
|
|
+ };
|
|
|
+
|
|
|
+ WebService pairingService;
|
|
|
+ net::MDNS *mdns;
|
|
|
+
|
|
|
+ driver::Button *button;
|
|
|
+
|
|
|
+ driver::LCD lcd = driver::LCD(32, 33, 25, 26, 27, 13); // TODO use Kconfig
|
|
|
+
|
|
|
+ [[noreturn]] void main(void *) {
|
|
|
+ ESP_LOGI(TAG, "starting");
|
|
|
+
|
|
|
+ button = new driver::Button(19); // TODO use Kconfig
|
|
|
+ button->onRelease = {[](driver::Button &button) {
|
|
|
+ ESP_LOGI(TAG, "button->onRelease()");
|
|
|
+ testNotification();
|
|
|
+ }};
|
|
|
+ button->onLongPress = {[](driver::Button &button) {
|
|
|
+ ESP_LOGI(TAG, "button->onLongPress()");
|
|
|
+ clearPairing();
|
|
|
+ }};
|
|
|
+
|
|
|
+ pairingService.attach<PairController>();
|
|
|
+ pairingService.attach<SettingsController>();
|
|
|
+ pairingService.attach<VerifyController>();
|
|
|
+
|
|
|
+ if (paired()) {
|
|
|
+ wifi::start();
|
|
|
+ setupArmedMode();
|
|
|
+ } else {
|
|
|
+ setupPairingMode();
|
|
|
+ }
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ eventGroup.waitForAny(ALL_EVENTS);
|
|
|
+ if (eventGroup.getBit(Events::SET_PAIRING_MODE)) {
|
|
|
+ setupPairingMode();
|
|
|
+ }
|
|
|
+ if (eventGroup.getBit(Events::SET_ARMED_MODE)) {
|
|
|
+ setupArmedMode();
|
|
|
+ }
|
|
|
+ eventGroup.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void showMessage(const string &message) {
|
|
|
+ lcd.println(message, 1);
|
|
|
+ kbf::sleep(MESSAGE_TIMEOUT);
|
|
|
+ lcd.println("", 1);
|
|
|
+ }
|
|
|
+
|
|
|
void handleConnect(wifi::AP::STA &sta) {
|
|
|
lcd.clear();
|
|
|
kbf::sleep(50);
|
|
@@ -25,51 +99,63 @@ namespace dk {
|
|
|
lcd.print("credentials");
|
|
|
}
|
|
|
|
|
|
- WebService webService;
|
|
|
- net::MDNS *mdns;
|
|
|
+ void setupPairingMode() {
|
|
|
+ ESP_LOGI(TAG, "setupPairingMode()");
|
|
|
+ lcd.println("DoorKeeper", 0);
|
|
|
+ lcd.println("pairing", 1);
|
|
|
|
|
|
- driver::Button *button;
|
|
|
- driver::LCD lcd = driver::LCD(32, 33, 25, 26, 27, 13);
|
|
|
+ pairingService.stop();
|
|
|
+ wifi::stop();
|
|
|
|
|
|
- void main(void *) {
|
|
|
- ESP_LOGI(TAG, "starting");
|
|
|
+ auto sta = wifi::STA::create();
|
|
|
+ auto ap = wifi::AP::create("doorkeeper", "initinit");
|
|
|
+ ap->onConnect = handleConnect;
|
|
|
|
|
|
- button = new driver::Button(19); // TODO use Kconfig
|
|
|
- button->onPress = {[](driver::Button &button) {
|
|
|
- ESP_LOGI(TAG, "sending test notification");
|
|
|
- auto pairingId = SettingsManager::instance()->pairingId();
|
|
|
- ESP_LOGI(TAG, "pairingId = %s", pairingId.c_str());
|
|
|
- auto client = http::Client();
|
|
|
- auto response = client.post("http://dev.api.doorkeeper.kraxor.net/test_notification", {{"pairing_id", pairingId}});
|
|
|
- ESP_LOGI(TAG, "test_notification response: %d", response->status);
|
|
|
- }};
|
|
|
+ wifi::start(ap, sta);
|
|
|
+ pairingService.start();
|
|
|
+ }
|
|
|
|
|
|
- if (SettingsManager::instance()->softAPEnabled()) {
|
|
|
- auto sta = wifi::STA::create();
|
|
|
- auto ap = wifi::AP::create("doorkeeper", "initinit");
|
|
|
- ap->onConnect = handleConnect;
|
|
|
- wifi::start(ap, sta);
|
|
|
-
|
|
|
- dk::lcd.clear();
|
|
|
- kbf::sleep(50);
|
|
|
- dk::lcd.print("pairing mode");
|
|
|
- dk::lcd.move(0, 1);
|
|
|
- dk::lcd.print("setting up");
|
|
|
- } else {
|
|
|
- wifi::start();
|
|
|
- dk::lcd.print("idle");
|
|
|
+ void setupArmedMode() {
|
|
|
+ ESP_LOGI(TAG, "setupArmedMode()");
|
|
|
+ lcd.println("DoorKeeper armed", 0);
|
|
|
+ lcd.println("", 1); // TODO print icons
|
|
|
+
|
|
|
+ pairingService.stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ void testNotification() {
|
|
|
+ ESP_LOGI(TAG, "testNotification()");
|
|
|
+
|
|
|
+ if (!paired()) {
|
|
|
+ ESP_LOGI(TAG, " not paired");
|
|
|
+ showMessage("not paired");
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- webService.attach<PairController>();
|
|
|
- webService.attach<ScanController>();
|
|
|
- webService.attach<SettingsController>();
|
|
|
- webService.attach<VerifyController>();
|
|
|
- webService.start();
|
|
|
+ auto pairingId = SettingsManager::instance()->pairingId();
|
|
|
+ ESP_LOGI(TAG, " pairingId = %s", pairingId.c_str());
|
|
|
+ auto client = http::Client();
|
|
|
+ auto response = client.post("http://dev.api.doorkeeper.kraxor.net/test_notification",
|
|
|
+ {{"pairing_id", pairingId}});
|
|
|
+ ESP_LOGI(TAG, "test_notification response: %d", response->status);
|
|
|
|
|
|
- dk::lcd.move(0, 1);
|
|
|
- dk::lcd.print("waiting...");
|
|
|
+ showMessage("message sent");
|
|
|
+ }
|
|
|
+
|
|
|
+ void setPairing(const string &pairingId) {
|
|
|
+ ESP_LOGI(TAG, "setPairing(%s)", pairingId.c_str());
|
|
|
+ SettingsManager::instance()->setPairingId(pairingId);
|
|
|
+ eventGroup.setBit(Events::SET_ARMED_MODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ void clearPairing() {
|
|
|
+ ESP_LOGI(TAG, "clearPairing()");
|
|
|
+ SettingsManager::instance()->setPairingId("");
|
|
|
+ eventGroup.setBit(Events::SET_PAIRING_MODE);
|
|
|
+ }
|
|
|
|
|
|
- vTaskDelete(nullptr);
|
|
|
+ bool paired() {
|
|
|
+ return !SettingsManager::instance()->pairingId().empty();
|
|
|
}
|
|
|
}
|
|
|
|