Skip to content

Commit

Permalink
Changes for new APIs for ArduinoJson and NimBLE.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjansen committed Dec 25, 2024
1 parent 754c8a4 commit ff30a89
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 44 deletions.
26 changes: 10 additions & 16 deletions libLissabon/src/AbstractDimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,11 @@ bool AbstractDimmer::putHandler(const JsonVariant& request) {
identify();
anyChanged = true;
}
if (reqObj.containsKey("isOn")) {
isOn = reqObj["isOn"];
if (getFromRequest<bool>(reqObj, "isOn", isOn)) {
dimmerChanged = true;
}
#ifdef DIMMER_WITH_LEVEL
if (reqObj.containsKey("level")) {
level = reqObj["level"];
if (getFromRequest<float>(reqObj, "level", level)) {
dimmerChanged = true;
}
#endif // DIMMER_WITH_LEVEL
Expand All @@ -361,8 +359,7 @@ bool AbstractDimmer::putHandler(const JsonVariant& request) {
#ifdef DIMMER_WITH_ANIMATION
#endif // DIMMER_WITH_GAMMA
#ifdef DIMMER_WITH_TEMPERATURE
if (reqObj.containsKey("temperature")) {
temperature = reqObj["temperature"];
if (getFromRequest<float>(reqObj, "temperature", temperature)) {
dimmerChanged = true;
}
#endif // DIMMER_WITH_TEMPERATURE
Expand All @@ -376,32 +373,29 @@ bool AbstractDimmer::putHandler(const JsonVariant& request) {
// xxxjack the following should only be changed when in configuration mode
bool configChanged = false;
// xxxjack check permission
if (reqObj.containsKey("name")) {
String value = reqObj["name"].as<String>();
if (setName(name)) configChanged = true;
String value;
if (getFromRequest<const char*>(reqObj, "name", value)) {
if (setName(value)) configChanged = true;
}
#ifdef DIMMER_WITH_LEVEL
if (reqObj.containsKey("minLevel")) {
minLevel = reqObj["minLevel"];
if (getFromRequest<float>(reqObj, "minLevel", minLevel)) {
configChanged = true;
}
#endif // DIMMER_WITH_LEVEL
#ifdef DIMMER_WITH_GAMMA
if (reqObj.containsKey("gamma")) {
gamma = reqObj["gamma"];
if (getFromRequest<float>(reqObj, "gamma", gamma)) {
configChanged = true;
}
#endif // DIMMER_WITH_GAMMA
#ifdef DIMMER_WITH_ANIMATION
if (reqObj.containsKey("animation")) {
animationDurationMillis = reqObj["animation"];
if (getFromRequest<int>(reqObj, "animation", animationDurationMillis)) {
configChanged = true;
}
#endif // DIMMER_WITH_GAMMA
#ifdef DIMMER_WITH_TEMPERATURE
#endif // DIMMER_WITH_TEMPERATURE
#ifdef DIMMER_WITH_PWMFREQUENCY
if (reqObj.containsKey("pwmFrequency")) {
if (getFromRequest<int>(reqObj, "pwmFrequency", pwmFrequency)) {
pwmFrequency = reqObj["pwmFrequency"];
configChanged = true;
}
Expand Down
2 changes: 1 addition & 1 deletion libLissabon/src/DimmerCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void DimmerCollection::loop() {
void DimmerCollection::getHandler(JsonObject& reply) {
for (auto d : dimmers) {
String ident = "dimmer" + String(d->num);
JsonObject dimmerReply = reply.createNestedObject(ident);
JsonObject dimmerReply = reply[ident].as<JsonObject>();
d->getHandler(dimmerReply);
}
}
Expand Down
12 changes: 6 additions & 6 deletions libLissabon/src/iotsaBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void IotsaBLEClientMod::setup() {
void IotsaBLEClientMod::setupScanner() {
// The scanner is a singleton. We initialize it once.
scanner = BLEDevice::getScan();
scanner->setAdvertisedDeviceCallbacks(this, false);
scanner->setScanCallbacks(this, false);
scanner->setActiveScan(true);
scanner->setInterval(scan_interval);
scanner->setWindow(scan_window);
Expand Down Expand Up @@ -136,7 +136,7 @@ bool IotsaBLEClientMod::getHandler(const char *path, JsonObject& reply) {
reply["scan_interval"] = scan_interval;
reply["scan_window"] = scan_window;
if (unknownDevices.size()) {
JsonArray unknownReply = reply.createNestedArray("unassigned");
JsonArray unknownReply = reply["unassigned"].as<JsonArray>();
for (auto it : unknownDevices) {
unknownReply.add((char *)it.c_str());
}
Expand All @@ -148,11 +148,11 @@ bool IotsaBLEClientMod::putHandler(const char *path, const JsonVariant& request,
bool anyChanged = false;
bool _startScanUnknown = false;
JsonObject reqObj = request.as<JsonObject>();
if (reqObj.containsKey("scan_interval")) {
if (getFromRequest<int>(reqObj, "scan_interval", scan_interval)) {
scan_interval = reqObj["scan_interval"];
anyChanged = true;
}
if (reqObj.containsKey("scan_window")) {
if (getFromRequest<int>(reqObj, "scan_window", scan_window)) {
scan_window = reqObj["scan_window"];
anyChanged = true;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ void IotsaBLEClientMod::startScanning() {
// Now start the scan
scanner = BLEDevice::getScan();
scanningMod = this;
if (!scanner->start(11, nullptr, false)) {
if (!scanner->start(11000)) {
scanner = nullptr;
IFDEBUG IotsaSerial.println("BLEClient: cannot start scan, retry in 1s");
shouldUpdateScanAtMillis = millis() + SCAN_START_RETRY_MS;
Expand Down Expand Up @@ -349,7 +349,7 @@ void IotsaBLEClientMod::loop() {
}
}

void IotsaBLEClientMod::onResult(BLEAdvertisedDevice *advertisedDevice) {
void IotsaBLEClientMod::onResult(const BLEAdvertisedDevice *advertisedDevice) {
#ifdef DEBUG_PRINT_ALL_CLIENTS
IotsaSerial.printf("BLEClientMod::onResult(%s)\n", advertisedDevice->toString().c_str());
#endif
Expand Down
7 changes: 4 additions & 3 deletions libLissabon/src/iotsaBLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include "iotsaBLEClientConnection.h"

#include <set>
#include <map>

typedef std::function<void(BLEAdvertisedDevice&)> BleDeviceFoundCallback;
typedef std::function<void(const BLEAdvertisedDevice&)> BleDeviceFoundCallback;
typedef const char *UUIDString;

class IotsaBLEClientMod : public IotsaApiMod, public BLEAdvertisedDeviceCallbacks {
class IotsaBLEClientMod : public IotsaApiMod, public NimBLEScanCallbacks {
public:
//xxxjack IotsaBLEClientMod(IotsaApplication& app) : IotsaApiMod(app) {}
using IotsaApiMod::IotsaApiMod;
Expand Down Expand Up @@ -64,7 +65,7 @@ class IotsaBLEClientMod : public IotsaApiMod, public BLEAdvertisedDeviceCallback
protected:
void configLoad();
void configSave();
void onResult(BLEAdvertisedDevice *advertisedDevice);
void onResult(const BLEAdvertisedDevice *advertisedDevice);
void setupScanner();
void updateScanning();
void startScanning();
Expand Down
6 changes: 3 additions & 3 deletions libLissabon/src/iotsaBLEClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

IotsaBLEClientConnection::IotsaBLEClientConnection(std::string& _name, std::string _address)
: name(_name),
address(_address), // Public is default for address type for nimble
address(_address, 0), // Public is default for address type for nimble
#ifdef IOTSA_WITHOUT_NIMBLE
addressType(BLE_ADDR_TYPE_PUBLIC),
#endif
Expand Down Expand Up @@ -33,7 +33,7 @@ std::string IotsaBLEClientConnection::getAddress() {
return address.toString();
}

bool IotsaBLEClientConnection::receivedAdvertisement(BLEAdvertisedDevice& _device) {
bool IotsaBLEClientConnection::receivedAdvertisement(const BLEAdvertisedDevice& _device) {
// Check whether the address is the same, then we don't have to add anything.
if (addressValid
#ifdef IOTSA_WITHOUT_NIMBLE
Expand Down Expand Up @@ -201,7 +201,7 @@ bool IotsaBLEClientConnection::getAsNotification(BLEUUID& serviceUUID, BLEUUID&
if (characteristic == NULL) return false;
if (!characteristic->canNotify()) return false;
_staticCallback = callback;
characteristic->registerForNotify(_staticCallbackCaller);
characteristic->subscribe(true, _staticCallbackCaller);
return false;
}
#endif // IOTSA_WITH_BLE
2 changes: 1 addition & 1 deletion libLissabon/src/iotsaBLEClientConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IotsaBLEClientConnection {
public:
IotsaBLEClientConnection(std::string& _name, std::string _address="");
~IotsaBLEClientConnection();
bool receivedAdvertisement(BLEAdvertisedDevice& _device);
bool receivedAdvertisement(const BLEAdvertisedDevice& _device);
void clearDevice();
bool available();
bool connect();
Expand Down
17 changes: 9 additions & 8 deletions lissabonController/mainLedstripController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class IotsaLedstripControllerMod : public IotsaBLEClientMod, public DimmerCallba
void _setupDisplay();
bool getHandler(const char *path, JsonObject& reply);
bool putHandler(const char *path, const JsonVariant& request, JsonObject& reply);
void unknownBLEDimmerFound(BLEAdvertisedDevice& device);
void knownBLEDimmerChanged(BLEAdvertisedDevice& device);
void unknownBLEDimmerFound(const BLEAdvertisedDevice& device);
void knownBLEDimmerChanged(const BLEAdvertisedDevice& device);
virtual String formHandler_field_perdevice(const char *deviceName) override;
virtual void scanningChanged() override;
virtual void showMessage(const char *message) override;
Expand Down Expand Up @@ -395,14 +395,15 @@ bool IotsaLedstripControllerMod::putHandler(const char *path, const JsonVariant&
anyChanged = IotsaBLEClientMod::putHandler(path, request, reply);
anyChanged |= dimmers.putHandler(request);
// This is a hack. We don't implement DELETE so we add a funny value
if (request.containsKey("clearall") && request["clearall"]) {
bool clearall;
if (getFromRequest<bool>(request, "clearall", clearall) && clearall) {
dimmers.clear();
anyChanged = true;
}
// This is another hack.
if (request.containsKey("add")) {
String newDimmerName = request["add"];
if (newDimmerName != "" && dimmers.find(newDimmerName) == nullptr) {
String newDimmerName;
if (getFromRequest<String>(request, "add", newDimmerName)) {
if (dimmers.find(newDimmerName) == nullptr) {
dimmers.push_back_new(newDimmerName);
dimmers.setup();
anyChanged = true;
Expand Down Expand Up @@ -480,12 +481,12 @@ void IotsaLedstripControllerMod::_setupDisplay() {
}


void IotsaLedstripControllerMod::unknownBLEDimmerFound(BLEAdvertisedDevice& deviceAdvertisement) {
void IotsaLedstripControllerMod::unknownBLEDimmerFound(const BLEAdvertisedDevice& deviceAdvertisement) {
LOG_BLE IotsaSerial.printf("LissabonController: unknownDeviceFound: device \"%s\"\n", deviceAdvertisement.getName().c_str());
unknownDevices.insert(deviceAdvertisement.getName());
}

void IotsaLedstripControllerMod::knownBLEDimmerChanged(BLEAdvertisedDevice& deviceAdvertisement) {
void IotsaLedstripControllerMod::knownBLEDimmerChanged(const BLEAdvertisedDevice& deviceAdvertisement) {
std::string name = deviceAdvertisement.getName();
LOG_BLE IotsaSerial.printf("LissabonController: knownDeviceChanged: device \"%s\"\n", name.c_str());
dimmerAvailableChanged();
Expand Down
2 changes: 1 addition & 1 deletion lissabonController/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default_envs = lissabon-controller

[common]
framework = arduino
lib_ldf_mode = deep+
lib_ldf_mode = chain
lib_compat_mode = strict
lib_deps =
https://github.com/cwi-dis/iotsa.git#develop
Expand Down
1 change: 1 addition & 0 deletions lissabonDimmer/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ src_dir = .
default_envs =
lissabon-plugin-dimmer
lissabon-buttons-dimmer
lissabon-hidden-dimmer

[common]
framework = arduino
Expand Down
6 changes: 3 additions & 3 deletions lissabonExample/lissabonExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ IotsaBLEClientMod bleClientMod(application);

using namespace Lissabon;

class IotsaBLEDimmerMod : public IotsaApiMod, public DimmerCallbacks {
class IotsaBLEDimmerMod : public IotsaRestApiMod, public DimmerCallbacks {
public:
IotsaBLEDimmerMod(IotsaApplication &_app, IotsaAuthenticationProvider *_auth=NULL)
: IotsaRestApiMod(_app, _auth),
Expand All @@ -63,7 +63,7 @@ class IotsaBLEDimmerMod : public IotsaApiMod, public DimmerCallbacks {
private:
void dimmerOnOffChanged() override;
void dimmerValueChanged() override;
void dimmerAvailableChanged(bool available, bool connected) override {};
void dimmerAvailableChanged() override {};
void handler();
BLEDimmer dimmer1;
#ifdef WITH_BLESERVER
Expand Down Expand Up @@ -109,7 +109,7 @@ String IotsaBLEDimmerMod::info() {

bool IotsaBLEDimmerMod::getHandler(const char *path, JsonObject& reply) {
// xxxjack need to distinguish between config and operational parameters
JsonObject dimmer1Reply = reply.createNestedObject("dimmer1");
JsonObject dimmer1Reply = reply["dimmer1"].as<JsonObject>();
dimmer1.getHandler(dimmer1Reply);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions lissabonRemote/lissabonRemote.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public:
protected:
bool getHandler(const char *path, JsonObject& reply) override;
bool putHandler(const char *path, const JsonVariant& request, JsonObject& reply) override;
void unknownBLEDimmerFound(BLEAdvertisedDevice& deviceAdvertisement);
void unknownBLEDimmerFound(const BLEAdvertisedDevice& deviceAdvertisement);
private:
void dimmerOnOffChanged() override;
void dimmerValueChanged() override;
Expand Down Expand Up @@ -266,7 +266,7 @@ void LissabonRemoteMod::setup() {
}

// xxxjack move to IotsaBLEClientMod?
void LissabonRemoteMod::unknownBLEDimmerFound(BLEAdvertisedDevice& deviceAdvertisement) {
void LissabonRemoteMod::unknownBLEDimmerFound(const BLEAdvertisedDevice& deviceAdvertisement) {
IFDEBUG IotsaSerial.printf("unknownBLEDimmerFound: iotsaLedstrip/iotsaDimmer \"%s\"\n", deviceAdvertisement.getName().c_str());
unknownDevices.insert(deviceAdvertisement.getName());
}
Expand Down

0 comments on commit ff30a89

Please sign in to comment.