diff --git a/obuspa/Makefile b/obuspa/Makefile index e23aed86a..b0c916d26 100644 --- a/obuspa/Makefile +++ b/obuspa/Makefile @@ -5,7 +5,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=obuspa -PKG_VERSION:=6.0.0.7 +PKG_VERSION:=6.0.0.8 LOCAL_DEV:=0 ifneq ($(LOCAL_DEV),1) diff --git a/obuspa/files/etc/init.d/obuspa b/obuspa/files/etc/init.d/obuspa index 70066e573..e3ad65348 100755 --- a/obuspa/files/etc/init.d/obuspa +++ b/obuspa/files/etc/init.d/obuspa @@ -59,6 +59,17 @@ db_set_sql() fi } +db_del_sql() +{ + local param + + param="${1}" + + if [ -n "${param}" ]; then + ${PROG} -f ${SQL_DB_FILE} -c dbdel "${param}" >/dev/null 2>&1 + fi +} + db_set() { # if sql db present, update sql db @@ -296,7 +307,6 @@ publish_endpoint() local AgentEndpointID serial oui user pass if ! uci -q get obuspa.localmqtt; then - log "Remote mqtt broker configured, skip publishing endpoint" return 0; fi @@ -914,7 +924,7 @@ sync_update_sec() fi } -sync_db_with_uci() +sync_uci_with_db() { if [ ! -f "${DB_DUMP}" ]; then return 0; @@ -935,12 +945,67 @@ sync_db_with_uci() uci_commit obuspa } +delete_sql_db_entry_with_pattern() +{ + local params pattern + + pattern="${1}" + if [ ! -f "${DB_DUMP}" ]; then + return 0; + fi + + if [ "${#pattern}" -lt 7 ]; then + log "Invalid pattern ${1}" + return 0; + fi + + params="$(grep ${pattern} ${DB_DUMP}|awk '{print $1}')" + for p in ${params}; do + db_del_sql "${p}" + done +} + +check_n_delete_db() +{ + local sec t r path + + sec="${1}" + if uci -q get obuspa.${sec}; then + return 0 + fi + + t="${2}" + r="${3}" + sec="${sec/${t}_/cpe-}" + + path=$(grep "${r}\d.Alias => ${sec}" ${DB_DUMP}) + path=${path%.*} + + delete_sql_db_entry_with_pattern "${path}" +} + +reverse_update_db_with_uci() +{ + if [ ! -f "${DB_DUMP}" ]; then + return 0; + fi + + export UCI_CONFIG_DIR="/tmp/obuspa" + config_load obuspa + config_foreach check_n_delete_db controller controller "Device.LocalAgent.Controller." + config_foreach check_n_delete_db mtp mtp "Device.LocalAgent.MTP." + config_foreach check_n_delete_db mqtt mqtt "Device.MQTT.Client." + config_foreach check_n_delete_db stomp stomp "Device.STOMP.Connection." + unset UCI_CONFIG_DIR +} + # Create factory reset file db_init() { local reason reason="${1}" + mkdir -p /tmp/obuspa/ # Load configuration config_load $CONFIGURATION config_get SQL_DB_FILE global db_file "/tmp/obuspa/usp.db" @@ -952,7 +1017,12 @@ db_init() # Only sync uci with db in case of non service triggers if [ -f "${DB_DUMP}" ] && [ "${reason}" != "update" ]; then - sync_db_with_uci + sync_uci_with_db + fi + + # remove entries from db if deleted from uci + if [ -f "${DB_DUMP}" ] && [ "${reason}" == "update" ] && [ -f "/tmp/obuspa/obuspa" ]; then + reverse_update_db_with_uci fi # Remove reset file if present @@ -977,6 +1047,8 @@ db_init() update_reset_reason uci_commit ${CONFIGURATION} + cp /etc/config/obuspa /tmp/obuspa/ + [ -f "${DB_DUMP}" ] && rm -f ${DB_DUMP} return 0;