@@ -34,16 +34,29 @@ replace_value_at_position() {
3434
3535# Function
3636check_presence () {
37- TYPE =" $1 " # BLE MAC, LN
38- MATCH =" $2 "
37+ BLE_LN =" $1 "
38+ BLE_MAC =" $1 "
3939
4040 CURRENT_TIME_EPOCH=$( date +%s)
41+ MATCH=" ($BLE_LN |$BLE_MAC )"
42+
43+ if [ $BLE_MAC == " FF:FF:FF:FF:FF:FF" ]; then
44+ log_debug " check_presence; looking BLE_MAC for BLE_LN:$BLE_LN "
45+ if grepOutput=$( echo " ${BLTCTL_OUT} " | grep $BLE_LN | tail -1) ; then
46+ BLE_MAC=$( echo $grepOutput | grep -Eo $MAC_REGEX )
47+ log_info " check_presence; found BLE MAC addr:$BLE_MAC for BLE_LN:$BLE_LN "
48+ else
49+ log_debug " check_presence; did not find BLE MAC addr for BLE_LN:$BLE_LN "
50+ fi
51+ fi
52+ # return value to calling function
53+ echo $BLE_MAC
4154
4255 if echo " ${BLTCTL_OUT} " | grep -Eq " $MATCH " ; then
43- log_info " VIN $VIN $TYPE $MATCH presence detected"
56+ log_info " vin: $VIN ble_ln: $BLE_LN match: $MATCH presence detected"
4457
4558 if [ $CURRENT_TIME_EPOCH -ge $PRESENCE_EXPIRE_TIME ]; then
46- log_info " VIN $VIN $MATCH TTL expired, set presence ON"
59+ log_info " vin: $VIN ble_ln: $BLE_LN TTL expired, set presence ON"
4760 set +e
4861 # We need a function for mosquitto_pub w/ retry
4962 MQTT_OUT=$( eval $MOSQUITTO_PUB_BASE --nodelay -t " $MQTT_TOPIC " -m ON 2>&1 )
@@ -57,14 +70,14 @@ check_presence() {
5770
5871 # Update presence expire time
5972 EPOCH_EXPIRE_TIME=$(( CURRENT_TIME_EPOCH + PRESENCE_DETECTION_TTL))
60- log_debug " VIN $VIN $MATCH update presence expire time to $EPOCH_EXPIRE_TIME "
73+ log_debug " vin: $VIN ble_ln: $BLE_LN update presence expire time to $EPOCH_EXPIRE_TIME "
6174 PRESENCE_EXPIRE_TIME_LIST=$( replace_value_at_position " $PRESENCE_EXPIRE_TIME_LIST " \
6275 $position $EPOCH_EXPIRE_TIME )
6376 # END if MATCH
6477 else
65- log_debug " VIN $VIN $TYPE $MATCH presence not detected"
78+ log_debug " vin: $VIN ble_ln: $BLE_LN match: $MATCH presence not detected"
6679 if [ $CURRENT_TIME_EPOCH -ge $PRESENCE_EXPIRE_TIME ]; then
67- log_info " VIN $VIN $TYPE $MATCH presence has expired, setting presence OFF"
80+ log_info " vin: $VIN ble_ln: $BLE_LN presence has expired, setting presence OFF"
6881 set +e
6982 MQTT_OUT=$( eval $MOSQUITTO_PUB_BASE --nodelay -t " $MQTT_TOPIC " -m OFF 2>&1 )
7083 EXIT_STATUS=$?
@@ -74,7 +87,7 @@ check_presence() {
7487 return
7588 log_debug " mqtt topic $MQTT_TOPIC succesfully updated to OFF"
7689 else
77- log_info " VIN $VIN $TYPE $MATCH presence not expired"
90+ log_info " vin: $VIN ble_ln: $BLE_LN presence not expired"
7891 fi # END if expired time
7992 fi # END if ! MATCH
8093}
@@ -85,9 +98,26 @@ bluetoothctl_read() {
8598 # Read BLE data from bluetoothctl or an input file
8699 if [ -z $BLECTL_FILE_INPUT ]; then
87100 log_debug " Launching bluetoothctl to check for BLE presence"
88- BLECTL_TIMEOUT=11
89101 set +e
90- BLTCTL_OUT=$( bluetoothctl --timeout $BLECTL_TIMEOUT scan on 2>&1 | grep -v DEL)
102+ BLTCTL_OUT=$( {
103+ if [ $BLTCTL_COMMAND_DEVICES == " true" ]; then
104+ bltctlCommands=" power on,devices,scan on"
105+ else
106+ bltctlCommands=" power on,scan on"
107+ fi
108+ IFS=' ,'
109+ for bltctlCommand in $bltctlCommands ; do
110+ echo " $bltctlCommand "
111+ sleep 0.2
112+ done
113+
114+ # scan for 10 seconds (Tesla adverstisement each ~9s)
115+ sleep 10
116+
117+ echo " scan off"
118+ echo " power off"
119+ echo " exit"
120+ } | bluetoothctl)
91121 set -e
92122 else
93123 # Read from file, great for testing w/ no Bluetooth adapter
@@ -119,6 +149,7 @@ listen_to_ble() {
119149 while : ; do
120150 bluetoothctl_read
121151
152+ BLTCTL_COMMAND_DEVICES=false
122153 for position in $( seq $n_vins ) ; do
123154 set -- $BLE_LN_LIST
124155 BLE_LN=$( eval " echo \$ ${position} " )
@@ -132,8 +163,18 @@ listen_to_ble() {
132163 MQTT_TOPIC=" tesla_ble/$VIN /binary_sensor/presence"
133164
134165 # Check the presence using both MAC Addr and BLE Local Name
135- log_debug " $( echo " $BLTCTL_OUT " | grep -E " ($BLE_MAC |$BLE_LN )" ) "
136- check_presence " BLE MAC & LN" " ($BLE_MAC |$BLE_LN )"
166+ log_debug " BLTCTL_OUT:$( echo " $BLTCTL_OUT " | grep -E " ($BLE_MAC |$BLE_LN )" ) "
167+ macAddr=$( check_presence $BLE_LN $BLE_MAC )
168+
169+ # Add the MAC address to BLE_MAC_LIST if it's not already present at the position
170+ if [ " $macAddr " != " $BLE_MAC " ]; then
171+ eval " BLE_MAC_LIST=\$ (echo \$ BLE_MAC_LIST | awk '{\$ ${position} =\" $macAddr \" ; print}')"
172+ log_debug " listen_to_ble; BLE_MAC_LIST:$BLE_MAC_LIST "
173+ [ ! -f $KEYS_DIR /${VIN} _macaddr ] && echo $macAddr > $KEYS_DIR /${VIN} _macaddr
174+ fi
175+
176+ # If a MAC addr is unknown, request "bltctl devices"
177+ [ $macAddr == " FF:FF:FF:FF:FF:FF" ] && BLTCTL_COMMAND_DEVICES=true
137178
138179 done
139180 sleep $PRESENCE_DETECTION_LOOP_DELAY
0 commit comments