iopsys-feed/usp-js/files/etc/init.d/uspjs
Erik Karlsson 176bbf7566 usp-js: fix publishing of USP agent endpoint ID
Also remove triggering on "network" and "mosquitto" configuration
change as it is not necessary re-publish on "network" change and
re-publishing on "mosquitto" change does not work anyway as it is not
possible to guarantee that "uspjs" is reloaded after "mosquitto" has
already been reloaded and is up and running.
2023-04-11 09:47:36 +00:00

68 lines
1.6 KiB
Bash
Executable file

#!/bin/sh /etc/rc.common
START=99
STOP=01
USE_PROCD=1
log()
{
echo "$*"|logger -t usp-js -p debug
}
get_oui_from_db()
{
db -q get device.deviceinfo.ManufacturerOUI
}
get_serial_from_db()
{
db -q get device.deviceinfo.SerialNumber
}
publish_endpoint()
{
local AgentEndpointID serial oui user pass
if ! uci -q get obuspa.testmqtt; then
return 0;
fi
# return if mosquitto_pub is not present
if [ ! "$(command -v mosquitto_pub)" ]; then
log "mosquitto_pub not present can't publish EndpointID"
return 0;
fi
# Get endpoint id from obuspa config first
config_load obuspa
config_get AgentEndpointID localagent EndpointID ""
if [ -z "${AgentEndpointID}" ]; then
serial=$(get_serial_from_db)
oui=$(get_oui_from_db)
AgentEndpointID="os::${oui}-${serial//+/%2B}"
fi
config_get user testmqtt Username ""
config_get pass testmqtt Password ""
# publish Agent's EndpointID in mosquito broker for discovery by usp-js
# This is a work around till obuspa adds supports for mDNS discovery
if [ -n "${user}" ] && [ -n "${pass}" ]; then
log "Publishing EndpointID ${AgentEndpointID} to local mqtt broker with username, password"
mosquitto_pub -r -t "obuspa/EndpointID" -m "${AgentEndpointID}" -u "${user}" -P "${pass}"
elif [ -n "${user}" ]; then
log "Publishing EndpointID ${AgentEndpointID} to local mqtt broker with username only"
mosquitto_pub -r -t "obuspa/EndpointID" -m "${AgentEndpointID}" -u "${user}"
else
log "Publishing EndpointID ${AgentEndpointID} to local mqtt broker"
mosquitto_pub -r -t "obuspa/EndpointID" -m "${AgentEndpointID}"
fi
}
start_service() {
publish_endpoint
}
service_triggers() {
procd_add_reload_trigger "obuspa"
}