Skip to content

Commit

Permalink
Merge pull request #27 from tesla-local-control/multi-cars
Browse files Browse the repository at this point in the history
Prep for multi-cars, libproduct, liblog and more
  • Loading branch information
iainbullock authored Jul 9, 2024
2 parents 2c810a7 + cb04199 commit 226ea33
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 27 deletions.
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ FROM alpine:3.20.0

# install dependencies
RUN apk add --no-cache \
openssl \
bluez \
mosquitto-clients
bluez \
bluez-depricated \
mosquitto-clients \
openssl

# Create various working directories
RUN mkdir /data

# Copy project files into required locations
COPY app /app
COPY app liblog.sh libproduct.sh /app/

# Copy binaries from build stage
COPY --from=build /app/bin/tesla-control /usr/bin/

CMD [ "/app/run.sh" ]
46 changes: 23 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ volumes:
tesla_ble_mqtt:
external: true

services:
services:
tesla_ble_mqtt:
container_name: tesla_ble_mqtt
image: "iainbullock/tesla_ble_mqtt:latest"

environment:
- TZ='Europe/London'
- TESLA_VIN1=00000000000000000
- TESLA_VIN2=00000000000000000
- TESLA_VIN3=00000000000000000
- BLE_MAC=00:00:00:00:00:00
- MQTT_IP=127.0.0.1
- MQTT_PORT=1883
- MQTT_USER=
- MQTT_PWD=
- SEND_CMD_RETRY_DELAY=5

image: "tesla-local-control/tesla_ble_mqtt:latest"


# Configuration Settings have been moved to the "env" file
env_file:
- env

network_mode: host

# TODO: Needed to open the Bluetooth dongle to send commands to the car
privileged: true
# TODO: Needed to open the Bluetooth device
tty: true

# TODO : Why?
stdin_open: true
tty: true

entrypoint: "/app/run.sh"

# TODO : still valid?
working_dir: /share/tesla_ble_mqtt

volumes:
- tesla_ble_mqtt:/share/tesla_ble_mqtt
- /run/dbus:/run/dbus
network_mode: host
privileged: true

# TODO: most likely not needed to overwrite the image's default
entrypoint: "/app/run.sh"

restart: no
#restart: unless-stopped
#restart: unless-stopped
50 changes: 50 additions & 0 deletions env
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Optional for car presence detection; If multiple cars, separate with , or | or white space
#
BLE_MAC_LIST=

# Default 5 (seconds)
#
BLE_CMD_RETRY_DELAY=

### Default false
#
DEBUG=

# Hostname or IP address
#
MQTT_SERVER=

# Service port # or name
#
MQTT_PORT=1883

# If no username provided, anonymous mode.
#
MQTT_USERNAME=

# If you have special characters, wrap with ' at both ends; escape ' if needed
#
MQTT_PASSWORD=

# Default 120 (seconds)
#
PRESENCE_DETECTION_LOOP_DELAY=

# Default 240 (seconds)
#
PRESENCE_DETECTION_TTL=

# Mandatory; if multiple VINs separate with , or | or white space
#
VIN_LIST=

# Your timezone
# Ref: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#
TZ='Europe/London'


#
# WARNING; If you run Home Assistant, keep this true unless you know what you're doing
#
ENABLE_HA_FEATURES=true
27 changes: 27 additions & 0 deletions liblog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# shellcheck shell=dash
#
# functions for colored output
#
if [ "$COLOR" = "false" ]; then
NOCOLOR='\033[0m'
GREEN=$NOCOLOR
CYAN=$NOCOLOR
YELLOW=$NOCOLOR
MAGENTA=$NOCOLOR
RED=$NOCOLOR
else
NOCOLOR='\033[0m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;32m'
MAGENTA='\033[0;35m'
RED='\033[0;31m'
fi

# shellcheck disable=SC1089
function log_debug { [ "$DEBUG" = "true" ] && echo -e "${NOCOLOR}$1" || true; }
function log_info { echo -e "${GREEN}$1${NOCOLOR}"; }
function log_notice { echo -e "${CYAN}$1${NOCOLOR}"; }
function log_warning { echo -e "${YELLOW}$1${NOCOLOR}"; }
function log_error { echo -e "${MAGENTA}$1${NOCOLOR}"; }
function log_fatal { echo -e "${RED}$1${NOCOLOR}"; }
64 changes: 64 additions & 0 deletions libproduct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function validateEnvVars() {
exitOnError=0

BLE_MAC_PATTERN='^([0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5})(\|[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5})*$'
VIN_PATTERN='^([A-HJ-NPR-Z0-9]{17})(\|[A-HJ-NPR-Z0-9]{17})*$'
INT0PLUS_PATTERN='^[0-9]+$'
INT1PLUS_PATTERN='^[1-9][0-9]*$'
# Hostname & IPv4 Address
MQTT_SERVER_PATTERN='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b|\b([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$'

if ! echo $VIN_LIST | grep -Eq "$VIN_PATTERN"; then
log_fatal "Fatal; VIN_LIST:$VIN_LIST is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $BLE_MAC_LIST | grep -Eq "$BLE_MAC_PATTERN"; then
log_fatal "Fatal; BLE_MAC_LIST:$BLE_MAC_LIST is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $PRESENCE_DETECTION_LOOP_DELAY | grep -Eq "$INT1PLUS_PATTERN"; then
log_fatal "Fatal; PRESENCE_DETECTION_LOOP_DELAY:$PRESENCE_DETECTION_LOOP_DELAY is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $BLE_CMD_RETRY_DELAY | grep -Eq "$INT1PLUS_PATTERN"; then
log_fatal "Fatal; BLE_CMD_RETRY_DELAY:$BLE_CMD_RETRY_DELAY is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $PRESENCE_DETECTION_TTL | grep -Eq "$INT0PLUS_PATTERN"; then
log_fatal "Fatal; PRESENCE_DETECTION_TTL:$PRESENCE_DETECTION_TTL is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $MQTT_SERVER | grep -Eq "$MQTT_SERVER_PATTERN"; then
log_fatal "Fatal; MQTT_SERVER:$MQTT_SERVER is not compliant, please check this setting"
exitOnError=1
fi

if ! echo $MQTT_PORT | grep -Eq "$INT1PLUS_PATTERN"; then
log_fatal "Fatal; MQTT_PORT:$MQTT_PORT is not compliant, please check this setting"
exitOnError=1
fi

if [ "$DEBUG" != 'true' ] && [ "$DEBUG" != 'false' ]; then
log_fatal "Fatal; DEBUG:$DEBUG is not compliant, please check this setting"
exitOnError=1
fi

[ $exitOnError -ne 0 ] \
&& touch /data/.exitOnError \
&& exit 99

}

function initProduct() {
export COLOR=${COLOR:=true}

# Source Logging Library
. /app/liblog.sh

validateEnvVars
}

0 comments on commit 226ea33

Please sign in to comment.