Skip to content

Commit

Permalink
1.35
Browse files Browse the repository at this point in the history
  • Loading branch information
klausahrenberg committed Mar 27, 2023
1 parent 806fab7 commit f2df0d9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 69 deletions.
5 changes: 2 additions & 3 deletions src/WNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,14 +624,13 @@ class WNetwork {
if (complete) {
_initialMqttSent = true;
}
}

}
device->setLastStateNotify(millis());
if ((device->deepSleepMode() != DEEP_SLEEP_NONE) &&
((!_supportsWebServer) ||
(device->areAllPropertiesRequested()))) {
_deepSleepFlag = device;
}
}
}
}

Expand Down
135 changes: 80 additions & 55 deletions src/WProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define W_PROPERTY_H

#include <Arduino.h>
#include <list>
#include "WJson.h"
#include "WList.h"
#include "WValue.h"
Expand Down Expand Up @@ -42,12 +43,15 @@ enum WPropertyType {

enum WPropertyVisibility { ALL, NONE, MQTT, WEBTHING };

class WProperty {
public:
typedef std::function<void(WProperty* property)> TOnPropertyChange;
typedef std::function<void(WProperty* property)> TOnPropertyChange;

/*struct WPropertyListener {
TOnPropertyChange onChange;
};*/

WProperty(const char* id, const char* title, WPropertyType type,
const char* atType) {
class WProperty {
public:
WProperty(const char* id, const char* title, WPropertyType type, const char* atType) {
initialize(id, title, type, atType);
}

Expand All @@ -66,7 +70,10 @@ class WProperty {
_onValueRequest = onValueRequest;
}

void setOnChange(TOnPropertyChange onChange) { _onChange = onChange; }
void addListener(TOnPropertyChange onChange) {
//WPropertyListener pl = {onChange};
_listeners.push_back(onChange);
}

void setDeviceNotification(TOnPropertyChange deviceNotification) {
_deviceNotification = deviceNotification;
Expand Down Expand Up @@ -94,7 +101,7 @@ class WProperty {
case BOOLEAN:
return 1;
case BYTE_ARRAY:
return (_valueNull ? 0 : sizeof(_value.asByteArray));
return (_valueNull ? 0 : getByteArrayLength());
}
return 0;
}
Expand Down Expand Up @@ -318,23 +325,37 @@ class WProperty {
return _value.string;
}

byte* getByteArray() { return _value.asByteArray; }
byte* getByteArray() {
if (_type != BYTE_ARRAY) {
return 0;
} else {
byte length = getByteArrayLength();
if (length > 0) {
byte* result = (byte*) malloc(length);
for (int i = 0; i < length; i++) {
result[i] = _value.asByteArray[i + 1];
}
return result;
} else {
return 0;
}
}
}

bool setByteArray(const byte* newValue) {
bool setByteArray(byte length, const byte* newValue) {
if (_type != BYTE_ARRAY) {
return false;
}
byte newLength = sizeof(newValue);
bool changed =
((_valueNull) || (newLength != (sizeof(_value.asByteArray))));
if ((!_valueNull) && (newLength != (sizeof(_value.asByteArray)))) {
free(_value.asByteArray);
_value.asByteArray = (byte*)malloc(newLength);
//byte newLength = sizeof(newValue);
bool changed = ((_valueNull) || (length != (_value.asByteArray[0])));
if ((!_valueNull) && (length != (_value.asByteArray[0]))) {
free(_value.asByteArray);
}
_value.asByteArray = (byte*)malloc(sizeof(newValue));
for (int i = 0; i < newLength; i++) {
_value.asByteArray = (byte*) malloc(length + 1);
_value.asByteArray[0] = length;
for (int i = 0; i < length; i++) {
changed = ((changed) || (_value.asByteArray[i] != newValue[i]));
_value.asByteArray[i] = newValue[i];
_value.asByteArray[i + 1] = newValue[i];
}
if (changed) {
_valueNull = false;
Expand All @@ -345,12 +366,42 @@ class WProperty {
return changed;
}

byte getByteArrayValue(byte index) { return _value.asByteArray[index]; }
byte getByteArrayLength() { return (!_valueNull ? _value.asByteArray[0] : 0); }

byte getByteArrayValue(byte index) { return _value.asByteArray[index + 1]; }

bool setByteArrayValue(byte index, byte newValue) {
if (_type != BYTE_ARRAY) {
return false;
}
bool changed = ((_valueNull) || (_value.asByteArray[index + 1] != newValue));
if (changed) {
_value.asByteArray[index + 1] = newValue;
_valueNull = false;
_changed = true;
valueChanged();
notify();
}
return changed;
}

bool getByteArrayBitValue(byte byteIndex, byte bitIndex) {
return bitRead(getByteArrayValue(byteIndex), bitIndex);
}

bool setByteArrayBitValue(byte byteIndex, byte bitIndex, bool bitValue) {
if (_type != BYTE_ARRAY) {
return false;
}
byte v = getByteArrayValue(byteIndex);
if (bitValue) {
bitSet(v, bitIndex);
} else {
bitClear(v, bitIndex);
}
return setByteArrayValue(byteIndex, v);
}

WValue getValue() { return _value; }

bool setString(const char* newValue) {
Expand Down Expand Up @@ -383,36 +434,7 @@ class WProperty {
notify();
}
return changed;
}

bool setByteArrayValue(byte index, byte newValue) {
if (_type != BYTE_ARRAY) {
return false;
}
bool changed =
((_valueNull) || (_value.asByteArray[index] != newValue));
if (changed) {
_value.asByteArray[index] = newValue;
_valueNull = false;
_changed = true;
valueChanged();
notify();
}
return changed;
}

bool setByteArrayBitValue(byte byteIndex, byte bitIndex, bool bitValue) {
if (_type != BYTE_ARRAY) {
return false;
}
byte v = getByteArrayValue(byteIndex);
if (bitValue) {
bitSet(v, bitIndex);
} else {
bitClear(v, bitIndex);
}
return setByteArrayValue(byteIndex, v);
}
}

bool isReadOnly() { return _readOnly; }

Expand Down Expand Up @@ -456,7 +478,7 @@ class WProperty {
break;
case BYTE_ARRAY:
// tbi
json->propertyByteArray(memberName, getLength(), _value.asByteArray);
json->propertyByteArray(memberName, getLength(), getByteArray());
break;
}
_requested = true;
Expand Down Expand Up @@ -651,6 +673,8 @@ class WProperty {

bool hasEnums() { return (_enums != nullptr); }

int enumsCount() { return _enums->size(); }

void addOutput(WOutput* output) {
if (_outputs == nullptr) {
_outputs = new WList<WOutput>();
Expand Down Expand Up @@ -729,7 +753,6 @@ class WProperty {
_atType = atType;
_unit = "";
_multipleOf = 0.0;
_onChange = nullptr;
_deviceNotification = nullptr;
_enums = nullptr;
_outputs = nullptr;
Expand Down Expand Up @@ -757,7 +780,7 @@ class WProperty {
bool _readOnly;
const char* _unit;
double _multipleOf;
TOnPropertyChange _onChange;
std::list<TOnPropertyChange> _listeners;
TOnPropertyChange _onValueRequest;
TOnPropertyChange _deviceNotification;
WValue _value = {false};
Expand All @@ -773,9 +796,11 @@ class WProperty {
void notify() {
if (!_valueRequesting) {
_notifying = true;
if (_onChange) {
//Custom change handling
_onChange(this);
if (!_listeners.empty()) {
for(std::list<TOnPropertyChange>::iterator f = _listeners.begin(); f != _listeners.end(); ++f ) {
//f->onChange(this);
f->operator()(this);
}
} else if (_outputs != nullptr) {
//Let the output handle the change
_outputs->forEach([this](WOutput* output){output->handleChangedProperty(_value);});
Expand Down
5 changes: 3 additions & 2 deletions src/WProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class WProps {
return new WProperty(id, title, BOOLEAN, "");
}

static WProperty* createTargetTemperatureProperty(const char* id, const char* title) {
WProperty* p = new WProperty(id, title, DOUBLE, TYPE_TARGET_TEMPERATURE_PROPERTY);
static WRangeProperty* createTargetTemperatureProperty(const char* id, const char* title) {
WRangeProperty* p = new WRangeProperty(id, title, DOUBLE, WValue::ofDouble(15.0), WValue::ofDouble(25.0), TYPE_TARGET_TEMPERATURE_PROPERTY);
p->setMultipleOf(0.5);
p->setUnit(UNIT_CELSIUS);
return p;
}
Expand Down
21 changes: 12 additions & 9 deletions src/WSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class WSettings {
}
case BYTE_ARRAY: {
const byte* ba = readByteArray(_address);
property->setByteArray(ba);
property->setByteArray(readByteArrayLength(_address), ba);
delete ba;
break;
}
Expand Down Expand Up @@ -264,15 +264,15 @@ class WSettings {
return this->setString(id, value, true);
}

WProperty* setByteArray(const char* id, const byte* value) {
WProperty* setByteArray(const char* id, byte length, const byte* value) {
WProperty* setting = getSetting(id);
if (setting == nullptr) {
setting = new WProperty(id, id, BYTE_ARRAY, "");
setting->setByteArray(value);
setting->setByteArray(length, value);
setting->setVisibility(NONE);
add(setting);
} else {
setting->setByteArray(value);
setting->setByteArray(length, value);
}
return setting;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ class WSettings {
break;
}
case BYTE_ARRAY: {
writeByteArray(_address, setting->getByteArray());
writeByteArray(_address, setting->getByteArrayLength(), setting->getByteArray());
break;
}
case STRING: {
Expand Down Expand Up @@ -384,6 +384,10 @@ class WSettings {
return setting->getLength();
}

const byte readByteArrayLength(int address) {
return EEPROM.read(address);
}

const byte* readByteArray(int address) {
byte length = EEPROM.read(address);
byte* data = new byte[length];
Expand All @@ -394,10 +398,9 @@ class WSettings {
return data;
}

void writeByteArray(int address, const byte* value) {
byte size = sizeof(value);
EEPROM.write(address, size);
for (int i = 1; i <= size; i++) {
void writeByteArray(int address, byte length, const byte* value) {
EEPROM.write(address, length);
for (int i = 1; i <= length; i++) {
EEPROM.write(address + i, value[i - 1]);
}
}
Expand Down

0 comments on commit f2df0d9

Please sign in to comment.