Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
log debug where appropriate
remove assumption that openwrt is running dhcp
catch curl errors
various minor and style fixes
  • Loading branch information
mueslo committed Mar 5, 2018
1 parent 7b810c1 commit 9171356
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
44 changes: 24 additions & 20 deletions packages/net/hass/files/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function err_msg {
}

function register_hook {
logger -t $0 -p info "register_hook $@"
logger -t $0 -p debug "register_hook $@"
if [ "$#" -ne 1 ]; then
err_msg "register_hook missing interface"
exit 1
Expand All @@ -15,7 +15,7 @@ function register_hook {
}

function post {
logger -t $0 -p info "post $@"
logger -t $0 -p debug "post $@"
if [ "$#" -ne 1 ]; then
err_msg "POST missing payload"
exit 1
Expand All @@ -25,14 +25,22 @@ function post {
config_get hass_host global host
config_get hass_pw global pw

curl "$hass_host/api/services/device_tracker/see" -X POST \
resp=$(curl "$hass_host/api/services/device_tracker/see" -sfSX POST \
-H 'Content-Type: application/json' \
-H "X-HA-Access: $hass_pw" \
--data-binary "$payload"
--data-binary "$payload" 2>&1)

if [ $? -eq 0 ]; then
level=debug
else
level=error
fi

logger -t $0 -p $level "post response $resp"
}

function build_payload {
logger -t $0 -p info "build_payload $@"
logger -t $0 -p debug "build_payload $@"
if [ "$#" -ne 3 ]; then
err_msg "Invalid payload parameters"
logger -t $0 -p warning "push_event not handled"
Expand All @@ -42,25 +50,21 @@ function build_payload {
host=$2
consider_home=$3

echo "{\"mac\":\"$mac\",\"host_name\":\"$host\",\"consider_home\":$consider_home,\"source_type\":\"router\"}"
}

function get_info {
cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $1
echo "{\"mac\":\"$mac\",\"host_name\":\"$host\",\"consider_home\":\"$consider_home\",\"source_type\":\"router\"}"
}

function host_name {
# todo: if openwrt is not dhcp issuer, get hostname from local reverse dns
get_info $1 | cut -f 3 -s -d" "
function get_ip {
# get ip for mac
grep "0x2\s\+$1" /proc/net/arp | cut -f 1 -s -d" "
}

function ip {
# todo: if openwrt is not dhcp issuer, get ip from arp table
get_info $1 | cut -f 2 -s -d" "
function get_host_name {
# get hostname for mac
nslookup "$(get_ip $1)" | grep -oP "(?<=name = ).*$"
}

function push_event {
logger -t $0 -p info "push_event $@"
logger -t $0 -p debug "push_event $@"
if [ "$#" -ne 3 ]; then
err_msg "Illegal number of push_event parameters"
exit 1
Expand Down Expand Up @@ -88,18 +92,18 @@ function push_event {
;;
esac

post $(build_payload "$mac" "$(host_name $mac)" "$timeout")
post $(build_payload "$mac" "$(get_host_name $mac)" "$timeout")
}

function sync_state {
logger -t $0 -p info "sync_state $@"
logger -t $0 -p debug "sync_state $@"

config_get hass_timeout_conn global timeout_conn

for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`; do
maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
for mac in $maclist; do
post $(build_payload "$mac" "$(host_name $mac)" "$hass_timeout_conn")
post $(build_payload "$mac" "$(get_host_name $mac)" "$hass_timeout_conn") &
done
done
}
6 changes: 3 additions & 3 deletions packages/net/hass/files/hass.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
config hass 'global'
option host '1.2.3.4:8123'
option host 'ip.or.name.example:8123'
option pw ''
option timeout_conn '86400'
option timeout_disc '180'
option timeout_conn '24:00'
option timeout_disc '00:03'
10 changes: 8 additions & 2 deletions packages/net/hass/files/hass.init
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ PROG=/usr/bin/hassd.sh
PIDFILE=/var/run/hass.pid

start_service() {
procd_open_instance "hass"
procd_set_param command "$PROG"
procd_open_instance hass
procd_set_param command $PROG
procd_set_param pidfile $PIDFILE
procd_close_instance
}
Expand All @@ -26,3 +26,9 @@ stop_service() {
kill $pid
done
}

reload_service()
{
stop
start
}
4 changes: 2 additions & 2 deletions packages/net/hass/files/hassd.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# todo: hostapd white (black) list so we don't have to listen to all APs

source "/lib/functions.sh"
source /lib/functions.sh
config_load hass

logger -t $0 -p info "Starting up"
Expand All @@ -22,7 +22,7 @@ while read line ; do
interface=$(echo "$line" | grep -oP '"path":"hostapd\.\K[^"]*(?="\} \}$)')
if [ $? = 0 ]
then
logger -t $0 "$interface is up, setting up hook"
logger -t $0 -p info "$interface is up, setting up hook"
register_hook $interface
fi

Expand Down
2 changes: 1 addition & 1 deletion packages/net/hass/files/push_event.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

source "/lib/functions.sh"
source /lib/functions.sh
config_load hass

source /usr/lib/hass/functions.sh
Expand Down

0 comments on commit 9171356

Please sign in to comment.