Skip to content

Add multiple features without breaking existed #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 80 additions & 15 deletions packages/net/hass-tracker/files/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ register_hook() {
exit 1
fi
interface=$1

hostapd_cli -i$interface -a/usr/lib/hass-tracker/push_event.sh &
}

Expand All @@ -21,21 +21,24 @@ post() {
exit 1
fi
payload=$1

config_get hass_host global host
config_get hass_token global token

resp=$(curl "$hass_host/api/services/device_tracker/see" -sfSX POST \
config_get hass_curl_insecure global curl_insecure

[ -n "$hass_curl_insecure" ] && curl_param="-k"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if hass_curl_insecure=0 this would still add the parameter, contrary to expectation


resp=$(curl "$hass_host/api/services/device_tracker/see" $curl_param -sfSX POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $hass_token" \
--data-binary "$payload" 2>&1)

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

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

Expand All @@ -50,23 +53,53 @@ build_payload() {
host=$2
consider_home=$3
source_name=$4

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

get_ip() {
ret=$(get_ip_arp $@)
[ -z "$ret" ] && ret=$(get_ip_dhcp $@)
echo $ret
}

get_ip_arp() {
# get ip for mac
grep "0x2\s\+$1" /proc/net/arp | cut -f 1 -s -d" "
grep "0x2\s\+$1" /proc/net/arp | cut -f 1 -s -d" " | grep -v '^169.254'
}

get_ip_dhcp() {
# get ip from dhcp table
leasefile="$(uci get dhcp.@dnsmasq[0].leasefile)"
grep "$1" "$leasefile" | cut -f 3 -s -d" "
}

get_host_name() {
ret=$(get_host_name_dhcp $@)
[ -z "$ret" ] && ret=$(get_host_name_dns $@)
echo $ret
}

get_host_name_dns() {
# get hostname for mac
nslookup "$(get_ip $1)" | grep -o "name = .*$" | cut -d ' ' -f 3
domain="$(uci get dhcp.@dnsmasq[0].domain)"
nslookup "$(get_ip $1)" | grep -o "name = .*$" | cut -d ' ' -f 3 | sed -e "s/\\.$domain//"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you strip the domain?

}

get_host_name_dhcp() {
# get hostname for mac
leasefile="$(uci get dhcp.@dnsmasq[0].leasefile)"
grep "$1" "$leasefile" | cut -f 4 -s -d" "
}

push_event() {
logger -t $0 -p debug "push_event $@"
if [ "$#" -eq 3 ]; then
if printf "$1" | grep -E '(add|old)'; then
# event pushed from dnsmasq. format: <add|old> <mac> <ip> <hostname>
mac=$2
hostname=$4
msg="DHCP-$1"
elif [ "$#" -eq 3 ]; then
iface=$1
msg=$2
mac=$3
Expand All @@ -85,12 +118,16 @@ push_event() {
err_msg "Illegal number of push_event parameters"
exit 1
fi

config_get hass_timeout_conn global timeout_conn
config_get hass_timeout_disc global timeout_disc
config_get hass_source_name global source_name `uci get system.@system[0].hostname`

case $msg in
config_get hass_whitelist_devices global whitelist

case $msg in
DHCP*)
timeout=$hass_timeout_conn
;;
"AP-STA-CONNECTED")
timeout=$hass_timeout_conn
;;
Expand All @@ -109,19 +146,47 @@ push_event() {
;;
esac

post $(build_payload "$mac" "$(get_host_name $mac)" "$timeout" "$hass_source_name")
[ -z "$hostname" ] && hostname="$(get_host_name $mac)"
if [ -n "$hass_whitelist_devices" ] && ! array_contains "$hostname" $hass_whitelist_devices; then
logger -t $0 -p warning "push_event ignored, $hostname not in whitelist."
elif [ -z "$hostname" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will ignore devices with no hostname?

logger -t $0 -p warning "sync_state ignored, hostname for $mac is empty."
else
post $(build_payload "$mac" "$hostname" "$timeout" "$hass_source_name")
fi
}

array_contains() {
logger -t $0 -p debug "array_contains $@"
for i in `seq 2 $(($#+1))`; do
next=$(eval "echo \$$i")
if [ "${next}" == "${1}" ]; then
# echo "y"
return 0
fi
done
# echo "n"
return 1
}

sync_state() {
logger -t $0 -p debug "sync_state $@"

config_get hass_timeout_conn global timeout_conn
config_get hass_source_name global source_name `uci get system.@system[0].hostname`
config_get hass_whitelist_devices global whitelist

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" "$(get_host_name $mac)" "$hass_timeout_conn" "$hass_source_name") &
hostname="$(get_host_name $mac)"
if [ -n "$hass_whitelist_devices" ] && ! array_contains "$hostname" $hass_whitelist_devices; then
logger -t $0 -p warning "sync_state ignored, $hostname not in whitelist."
elif [ -z "$hostname" ]; then
logger -t $0 -p warning "sync_state ignored, hostname for $mac is empty."
else
post $(build_payload "$mac" "$hostname" "$hass_timeout_conn" "$hass_source_name") &
fi
done
done
}
2 changes: 2 additions & 0 deletions packages/net/hass-tracker/files/hass-tracker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ config hass-tracker 'global'
option token ''
option timeout_conn '24:00'
option timeout_disc '00:03'
# option whitelist "host1 host2"
# option curl_insecure 1