-
Notifications
You must be signed in to change notification settings - Fork 31
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
ttimasdf
wants to merge
3
commits into
mueslo:master
Choose a base branch
from
ttimasdf:modified
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ register_hook() { | |
exit 1 | ||
fi | ||
interface=$1 | ||
|
||
hostapd_cli -i$interface -a/usr/lib/hass-tracker/push_event.sh & | ||
} | ||
|
||
|
@@ -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" | ||
|
||
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" | ||
} | ||
|
||
|
@@ -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//" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
;; | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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