Merge pull request #7580 from s-hadinger/zigbee_27

Minor Zigbee improvements
This commit is contained in:
Theo Arends 2020-01-22 22:49:28 +01:00 committed by GitHub
commit 85cff52822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 9 deletions

View File

@ -123,6 +123,7 @@ public:
// Mark data as 'dirty' and requiring to save in Flash // Mark data as 'dirty' and requiring to save in Flash
void dirty(void); void dirty(void);
void clean(void); // avoid writing to flash the last changes
// Find device by name, can be short_addr, long_addr, number_in_array or name // Find device by name, can be short_addr, long_addr, number_in_array or name
uint16_t parseDeviceParam(const char * param, bool short_must_be_known = false) const; uint16_t parseDeviceParam(const char * param, bool short_must_be_known = false) const;
@ -469,22 +470,28 @@ void Z_Devices::setManufId(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr); Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found if (&device == nullptr) { return; } // don't crash if not found
_updateLastSeen(device); _updateLastSeen(device);
if (!device.manufacturerId.equals(str)) {
dirty();
}
device.manufacturerId = str; device.manufacturerId = str;
dirty();
} }
void Z_Devices::setModelId(uint16_t shortaddr, const char * str) { void Z_Devices::setModelId(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr); Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found if (&device == nullptr) { return; } // don't crash if not found
_updateLastSeen(device); _updateLastSeen(device);
if (!device.modelId.equals(str)) {
dirty();
}
device.modelId = str; device.modelId = str;
dirty();
} }
void Z_Devices::setFriendlyName(uint16_t shortaddr, const char * str) { void Z_Devices::setFriendlyName(uint16_t shortaddr, const char * str) {
Z_Device & device = getShortAddr(shortaddr); Z_Device & device = getShortAddr(shortaddr);
if (&device == nullptr) { return; } // don't crash if not found if (&device == nullptr) { return; } // don't crash if not found
_updateLastSeen(device); _updateLastSeen(device);
if (!device.friendlyName.equals(str)) {
dirty();
}
device.friendlyName = str; device.friendlyName = str;
dirty();
} }
const String * Z_Devices::getFriendlyName(uint16_t shortaddr) const { const String * Z_Devices::getFriendlyName(uint16_t shortaddr) const {
@ -691,6 +698,9 @@ void Z_Devices::jsonPublishNow(uint16_t shortaddr, JsonObject & values) {
void Z_Devices::dirty(void) { void Z_Devices::dirty(void) {
_saveTimer = kZigbeeSaveDelaySeconds * 1000 + millis(); _saveTimer = kZigbeeSaveDelaySeconds * 1000 + millis();
} }
void Z_Devices::clean(void) {
_saveTimer = 0;
}
// Parse the command parameters for either: // Parse the command parameters for either:
// - a short address starting with "0x", example: 0x1234 // - a short address starting with "0x", example: 0x1234

View File

@ -268,6 +268,7 @@ void loadZigbeeDevices(void) {
buf.addBuffer(z_dev_start + sizeof(z_flashdata_t), buf_len); buf.addBuffer(z_dev_start + sizeof(z_flashdata_t), buf_len);
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee devices data in Flash (%d bytes)"), buf_len); AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee devices data in Flash (%d bytes)"), buf_len);
hidrateDevices(buf); hidrateDevices(buf);
zigbee_devices.clean(); // don't write back to Flash what we just loaded
} else { } else {
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "No zigbee devices data in Flash")); AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "No zigbee devices data in Flash"));
} }
@ -308,6 +309,7 @@ void saveZigbeeDevices(void) {
// Erase the flash area containing the ZigbeeData // Erase the flash area containing the ZigbeeData
void eraseZigbeeDevices(void) { void eraseZigbeeDevices(void) {
zigbee_devices.clean(); // avoid writing data to flash after erase
// first copy SPI buffer into ram // first copy SPI buffer into ram
uint8_t *spi_buffer = (uint8_t*) malloc(z_spi_len); uint8_t *spi_buffer = (uint8_t*) malloc(z_spi_len);
if (!spi_buffer) { if (!spi_buffer) {

View File

@ -40,11 +40,11 @@ const Z_CommandConverter Z_Commands[] = {
{ "Color", "0300!07/xxxxyyyy0A00" }, // x, y (uint16) { "Color", "0300!07/xxxxyyyy0A00" }, // x, y (uint16)
{ "CT", "0300!0A/xxxx0A00" }, // Color Temperature Mireds (uint16) { "CT", "0300!0A/xxxx0A00" }, // Color Temperature Mireds (uint16)
{ "Shutter", "0102!xx" }, { "Shutter", "0102!xx" },
{ "ShutterOpen", "0102!00"}, { "ShutterOpen", "0102!00" },
{ "ShutterClose", "0102!01"}, { "ShutterClose", "0102!01" },
{ "ShutterStop", "0102!02"}, { "ShutterStop", "0102!02" },
{ "ShutterLift", "0102!05xx"}, // Lift percentage, 0%=open, 100%=closed { "ShutterLift", "0102!05xx" }, // Lift percentage, 0%=open, 100%=closed
{ "ShutterTilt", "0102!08xx"}, // Tilt percentage { "ShutterTilt", "0102!08xx" }, // Tilt percentage
}; };
#define ZLE(x) ((x) & 0xFF), ((x) >> 8) // Little Endian #define ZLE(x) ((x) & 0xFF), ((x) >> 8) // Little Endian

View File

@ -463,7 +463,7 @@ void CmndZigbeeSend(void) {
if (nullptr != &val_device) { device = strToUInt(val_device); } if (nullptr != &val_device) { device = strToUInt(val_device); }
const JsonVariant &val_endpoint = getCaseInsensitive(json, PSTR("endpoint")); const JsonVariant &val_endpoint = getCaseInsensitive(json, PSTR("endpoint"));
if (nullptr != &val_endpoint) { endpoint = strToUInt(val_endpoint); } if (nullptr != &val_endpoint) { endpoint = strToUInt(val_endpoint); }
const JsonVariant val_cmd = getCaseInsensitive(json, PSTR("Send")); const JsonVariant &val_cmd = getCaseInsensitive(json, PSTR("Send"));
if (nullptr != &val_cmd) { if (nullptr != &val_cmd) {
// probe the type of the argument // probe the type of the argument
// If JSON object, it's high level commands // If JSON object, it's high level commands