Allow jsonl for existing objects

This commit is contained in:
fvanroie 2020-12-31 03:34:00 +01:00
parent 0112980ad1
commit 977ee521d1

View File

@ -224,11 +224,11 @@ void hasp_send_obj_attribute_int(lv_obj_t * obj, const char * attribute, int32_t
void hasp_send_obj_attribute_color(lv_obj_t * obj, const char * attribute, lv_color_t color) void hasp_send_obj_attribute_color(lv_obj_t * obj, const char * attribute, lv_color_t color)
{ {
char buffer[32]; char buffer[40]; // "#ffffff","r":"255","g":"255","b":"255"
lv_color32_t c32; lv_color32_t c32;
c32.full = lv_color_to32(color); c32.full = lv_color_to32(color);
snprintf(buffer, sizeof(buffer), PSTR("#%02x%02x%02x\",\"r\":\"%d\",\"g\":\"%d\",\"b\":\"%d"), c32.ch.red, c32.ch.green, c32.ch.blue, c32.ch.red, snprintf(buffer, sizeof(buffer), PSTR("#%02x%02x%02x\",\"r\":\"%d\",\"g\":\"%d\",\"b\":\"%d"), c32.ch.red,
c32.ch.green, c32.ch.blue); c32.ch.green, c32.ch.blue, c32.ch.red, c32.ch.green, c32.ch.blue);
hasp_send_obj_attribute_str(obj, attribute, buffer); hasp_send_obj_attribute_str(obj, attribute, buffer);
} }
@ -442,8 +442,8 @@ static void cpicker_event_handler(lv_obj_t * obj, lv_event_t event)
char color[6]; char color[6];
snprintf_P(color, sizeof(color), PSTR("color")); snprintf_P(color, sizeof(color), PSTR("color"));
// if(event == LV_EVENT_VALUE_CHANGED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj)); if(event == LV_EVENT_VALUE_CHANGED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
if(event == LV_EVENT_RELEASED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj)); // if(event == LV_EVENT_RELEASED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
} }
/** /**
@ -500,28 +500,21 @@ void hasp_process_attribute(uint8_t pageid, uint8_t objid, const char * attr, co
*/ */
void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id) void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
{ {
/* Validate page */ /* Page selection: page is the default parent_obj */
// uint8_t pageid = config[F("page")].isNull() ? haspGetPage() : config[F("page")].as<uint8_t>();
uint8_t pageid = config[F("page")].isNull() ? saved_page_id : config[F("page")].as<uint8_t>(); uint8_t pageid = config[F("page")].isNull() ? saved_page_id : config[F("page")].as<uint8_t>();
lv_obj_t * parent_obj = get_page_obj(pageid);
/* Page selection */ if(!parent_obj) {
lv_obj_t * page = get_page_obj(pageid);
if(!page) {
return Log.warning(TAG_HASP, F("Page ID %u not defined"), pageid); return Log.warning(TAG_HASP, F("Page ID %u not defined"), pageid);
} else { } else {
saved_page_id = pageid; /* save the current pageid */ saved_page_id = pageid; /* save the current pageid */
} }
/* Validate type */ // lv_obj_t * parent_obj = page;
if(config[F("objid")].isNull()) return; // comments
lv_obj_t * parent_obj = page;
if(!config[F("parentid")].isNull()) { if(!config[F("parentid")].isNull()) {
uint8_t parentid = config[F("parentid")].as<uint8_t>(); uint8_t parentid = config[F("parentid")].as<uint8_t>();
parent_obj = hasp_find_obj_from_parent_id(page, parentid); parent_obj = hasp_find_obj_from_parent_id(parent_obj, parentid);
if(!parent_obj) { if(!parent_obj) {
return Log.warning(TAG_HASP, F("Parent ID p[%u].b[%u] not found, skipping..."), pageid, parentid); return Log.warning(TAG_HASP, F("Parent ID p[%u].b[%u] not found, skipping..."), pageid, parentid);
// parent_obj = page; // don't create on the page instead ??
} else { } else {
Log.verbose(TAG_HASP, F("Parent ID p[%u].b[%u] found"), pageid, parentid); Log.verbose(TAG_HASP, F("Parent ID p[%u].b[%u] found"), pageid, parentid);
} }
@ -534,8 +527,12 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
/* Define Objects*/ /* Define Objects*/
lv_obj_t * obj = hasp_find_obj_from_parent_id(parent_obj, id); lv_obj_t * obj = hasp_find_obj_from_parent_id(parent_obj, id);
if(obj) { if(obj) {
return Log.warning(TAG_HASP, F("Object ID %u already exists!"), id); // return Log.warning(TAG_HASP, F("Object ID %u already exists!"), id);
}
} else {
/* Validate type */
if(config[F("objid")].isNull()) return; // comments
switch(objid) { switch(objid) {
/* ----- Basic Objects ------ */ /* ----- Basic Objects ------ */
@ -766,7 +763,7 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
/* No object was actually created */ /* No object was actually created */
if(!obj) { if(!obj) {
return Log.warning(TAG_HASP, F("Object ID %u is NULL, skipping..."), id); return Log.error(TAG_HASP, F("Object ID %u is NULL, skipping..."), id);
} }
// Prevent losing press when the press is slid out of the objects. // Prevent losing press when the press is slid out of the objects.
@ -779,20 +776,6 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
obj->user_data.objid = objid; //& 0b11111; obj->user_data.objid = objid; //& 0b11111;
obj->user_data.groupid = groupid; // & 0b111; obj->user_data.groupid = groupid; // & 0b111;
/* do not process these attributes */
config.remove(F("page"));
config.remove(F("id"));
config.remove(F("objid"));
config.remove(F("parentid"));
String v((char *)0);
v.reserve(64);
for(JsonPair keyValue : config) {
v = keyValue.value().as<String>();
hasp_process_obj_attribute(obj, keyValue.key().c_str(), v.c_str(), true);
// Log.verbose(TAG_HASP,F(" * %s => %s"), keyValue.key().c_str(), v.c_str());
}
/** testing start **/ /** testing start **/
uint8_t temp; uint8_t temp;
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) { if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
@ -810,3 +793,19 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
return Log.error(TAG_HASP, F("Objects DO NOT match!")); return Log.error(TAG_HASP, F("Objects DO NOT match!"));
} }
} }
/* do not process these attributes */
config.remove(F("page"));
config.remove(F("id"));
config.remove(F("objid"));
config.remove(F("parentid"));
String v((char *)0);
v.reserve(64);
for(JsonPair keyValue : config) {
v = keyValue.value().as<String>();
hasp_process_obj_attribute(obj, keyValue.key().c_str(), v.c_str(), true);
// Log.verbose(TAG_HASP,F(" * %s => %s"), keyValue.key().c_str(), v.c_str());
}
}