From 3259bd65bd548041cb3b6ef60c5de0bf734360fb Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Sun, 7 Jul 2024 22:51:26 -0400 Subject: [PATCH 1/5] Adding liblogging.sh --- liblogging.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 liblogging.sh diff --git a/liblogging.sh b/liblogging.sh new file mode 100644 index 0000000..19e07e1 --- /dev/null +++ b/liblogging.sh @@ -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}"; } From 126e80f8ce685825c140cbe614c6acc09123c0e1 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Sun, 7 Jul 2024 22:51:38 -0400 Subject: [PATCH 2/5] Adding libproduct.sh --- libproduct.sh | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 libproduct.sh diff --git a/libproduct.sh b/libproduct.sh new file mode 100644 index 0000000..da64fbf --- /dev/null +++ b/libproduct.sh @@ -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/liblogging.sh + + validateEnvVars +} From 21eb4d675076f71c44f71b76dbe76da0c1236188 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Sun, 7 Jul 2024 22:51:58 -0400 Subject: [PATCH 3/5] Move docker env vars to env file --- docker-compose.yml | 46 +++++++++++++++++++++--------------------- env | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 env diff --git a/docker-compose.yml b/docker-compose.yml index 70668dd..8fc500d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/env b/env new file mode 100644 index 0000000..fa91298 --- /dev/null +++ b/env @@ -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 From b21c500d04638cb8bbc1d6fff1fee94aeb5e1784 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Sun, 7 Jul 2024 23:05:43 -0400 Subject: [PATCH 4/5] Dockerfile cmd, bluez-depricated & libraries --- Dockerfile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index afe5876..51150c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 liblogging.sh libproduct.sh /app/ # Copy binaries from build stage COPY --from=build /app/bin/tesla-control /usr/bin/ + +CMD [ "/app/run.sh" ] From 1f48b0277d7959143790ba08d7037f40b5a0dc2e Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Sun, 7 Jul 2024 23:06:14 -0400 Subject: [PATCH 5/5] rename liblogging to liblog --- Dockerfile | 2 +- liblogging.sh => liblog.sh | 0 libproduct.sh | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename liblogging.sh => liblog.sh (100%) diff --git a/Dockerfile b/Dockerfile index 51150c5..df9d061 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,7 @@ RUN apk add --no-cache \ RUN mkdir /data # Copy project files into required locations -COPY app liblogging.sh libproduct.sh /app/ +COPY app liblog.sh libproduct.sh /app/ # Copy binaries from build stage COPY --from=build /app/bin/tesla-control /usr/bin/ diff --git a/liblogging.sh b/liblog.sh similarity index 100% rename from liblogging.sh rename to liblog.sh diff --git a/libproduct.sh b/libproduct.sh index da64fbf..30f7f74 100644 --- a/libproduct.sh +++ b/libproduct.sh @@ -58,7 +58,7 @@ function initProduct() { export COLOR=${COLOR:=true} # Source Logging Library - . /app/liblogging.sh + . /app/liblog.sh validateEnvVars }