Skip to content
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
12 changes: 9 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
CORE_V4_IP=XXX.XXX.XXX.XXX
# ssh tunneling
CLARK_IP=XXX.XXX.XXX

# espresence connected to broker
MQTT_BROKER=localhost
MQTT_PORT=1883
MQTT_TOPIC=espresense/#
ESPRESENSE_BASE_URL=http://10.251.212.248
ESPRESENSE_BASE_URL=http://192.168.69.188
ESPRESENSE_TIMEOUT_SECONDS=5.0
ONLINE_TIMEOUT_SECONDS=20
RETENTION_DAYS=90

# database stuff
POSTGRES_DB=bletrack
POSTGRES_USER=bletrack
POSTGRES_PASSWORD=bletrack_dev_password
DATABASE_URL=postgresql://bletrack:bletrack_dev_password@localhost:5433/bletrack
DATABASE_URL=postgresql://bletrack:bletrack_dev_password@timescaledb:5432/bletrack
# When running the API in Docker Compose, it will automatically override to:
# DATABASE_URL=postgresql://bletrack:bletrack_dev_password@timescaledb:5432/bletrack
10 changes: 9 additions & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ ENV PYTHONUNBUFFERED=1

WORKDIR /app

# for reading configs + curls to the esp32
RUN apt-get update && apt-get install -y \
jq \
curl \
openssh-client \
&& rm -rf /var/lib/apt/lists/*


COPY api/requirements.txt /app/api/requirements.txt
RUN pip install --no-cache-dir -r /app/api/requirements.txt

COPY . /app

EXPOSE 5055

CMD ["uvicorn", "api.server:app", "--host", "0.0.0.0", "--port", "5055"]
ENTRYPOINT ["sh", "/app/api/tun.sh"]
13 changes: 4 additions & 9 deletions api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from pydantic import BaseModel, Field
from dotenv import load_dotenv

from modules.db import (
check_db_health,
Expand All @@ -26,18 +25,14 @@
from modules.espresense_wrapper import ESPresenseWrapper, ESPresenseWrapperError
from modules.mqtt_ingester import MQTTIngester


load_dotenv()


BASE_DIR = Path(__file__).resolve().parent.parent
INDEX_HTML_PATH = BASE_DIR / "static" / "index.html"


ESPRESENSE_BASE_URL = os.getenv("ESPRESENSE_BASE_URL", "http://10.251.212.248")
ESPRESENSE_TIMEOUT_SECONDS = float(os.getenv("ESPRESENSE_TIMEOUT_SECONDS", "5.0"))
RETENTION_DAYS = int(os.getenv("RETENTION_DAYS", "90"))
ONLINE_TIMEOUT_SECONDS = int(os.getenv("ONLINE_TIMEOUT_SECONDS", "60"))
ESPRESENSE_BASE_URL = os.getenv("ESPRESENSE_BASE_URL")
ESPRESENSE_TIMEOUT_SECONDS = float(os.getenv("ESPRESENSE_TIMEOUT_SECONDS"))
RETENTION_DAYS = int(os.getenv("RETENTION_DAYS"))
ONLINE_TIMEOUT_SECONDS = int(os.getenv("ONLINE_TIMEOUT_SECONDS"))


ingester = MQTTIngester()
Expand Down
37 changes: 37 additions & 0 deletions api/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from fastapi import FastAPI, HTTPException
import subprocess
import os
app = FastAPI()

@app.get("/")
async def execute_curl():
"""
Executes the predefined curl command and returns the output.
"""
try:
# Run the curl command
result = subprocess.run(
["curl", "-X", "GET", f"{os.getenv('ESP_IP')}/json/devices/"],
capture_output=True, # Captures stdout and stderr
text=True, # Returns strings instead of bytes
check=True # Raises CalledProcessError if the exit code is non-zero
)

# Return the output of the curl command
return {
"status": "success",
"output": result.stdout.strip()
}

except subprocess.CalledProcessError as e:
# Handles errors if the curl command itself fails (e.g., bad URL, connection refused)
raise HTTPException(
status_code=500,
detail={"error": "Curl command failed", "stderr": e.stderr.strip()}
)
except Exception as e:
# Handles any other unexpected Python errors
raise HTTPException(
status_code=500,
detail={"error": "An unexpected error occurred", "message": str(e)}
)
51 changes: 18 additions & 33 deletions api/tun.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
#!/bin/bash
#!/bin/sh

CORE_V4_IP=$(cat /app/config/config.json | jq -r ".CORE_V4_IP")
set -x

DOCKER_CONTAINER_KNOWN_HOSTS=/app/ssh/known_hosts
SSH_KEY=/app/ssh_key
SSH_KNOWN_HOSTS=/app/known_hosts

DOCKER_CONTAINER_SSH_KEYS=/app/ssh/id_rsa
CLARK_PORT=5055
BLE_PORT=5055

CORE_V4_PORT=10000

LEDSIGN_PORT=10000
if [ -z ${CLARK_IP}]; then
echo "Clark IP not found. Check .env and if you set it."
exit 1
fi

CORE_V4_HOST=sce@${CORE_V4_IP}
CLARK_HOST=sce@${CLARK_IP}

open_ssh_tunnel () {
echo "running command"
echo "ssh \
-o UserKnownHostsFile=${DOCKER_CONTAINER_KNOWN_HOSTS} \
-o StrictHostKeyChecking=no \
-i ${DOCKER_CONTAINER_SSH_KEYS} \
-f -g -N -R 0.0.0.0:${CORE_V4_PORT}:localhost:${LEDSIGN_PORT} ${CORE_V4_HOST}"
ssh \
-o UserKnownHostsFile=${DOCKER_CONTAINER_KNOWN_HOSTS} \
-o UserKnownHostsFile=${SSH_KNOWN_HOSTS} \
-o StrictHostKeyChecking=no \
-i ${DOCKER_CONTAINER_SSH_KEYS} \
-f -g -N -R 0.0.0.0:${CORE_V4_PORT}:localhost:${LEDSIGN_PORT} ${CORE_V4_HOST}
-i ${SSH_KEY} \
-f -g -N -R 0.0.0.0:${CLARK_PORT}:localhost:${BLE_PORT} ${CLARK_HOST}
}

ls /app
ls /app/ssh

chmod 600 ${DOCKER_CONTAINER_SSH_KEYS}
chmod 600 ${SSH_KEY}

open_ssh_tunnel

# if no value is sent along with the invocation of the script,
# run the server. otherwise just open the ssh tunnel. i.e.
#
# to open the tunnel and start the server:
# $ ./tun.sh
#
# to only open the tunnel:
# $ ./tun.sh tunnel-only
if [ -z "$1" ]
then
python /app/api/server.py
fi
# this is for testing the tunnel, uncomment to use the test_server
# exec uvicorn api.test_server:app --host 0.0.0.0 --port 5055
python3 /app/api/server.py $@
3 changes: 0 additions & 3 deletions config/config.example.json

This file was deleted.

31 changes: 8 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ services:
container_name: bletrack-api
restart: always
working_dir: /app
command: uvicorn api.server:app --host 0.0.0.0 --port 5055
env_file:
- .env
environment:
MQTT_BROKER: mosquitto
MQTT_PORT: 1883
DATABASE_URL: postgresql://${POSTGRES_USER:-bletrack}:${POSTGRES_PASSWORD:-bletrack_dev_password}@timescaledb:5432/${POSTGRES_DB:-bletrack}
- PYTHONPATH=/app
volumes:
- ./:/app
- ~/.ssh/id_ed25519-tunnel.pub:/app/ssh_key.pub
- ~/.ssh/id_ed25519-tunnel:/app/ssh_key
- ~/.ssh/known_hosts:/app/known_hosts
- ./api:/app/api
- ./modules:/app/modules
ports:
- "5055:5055"
depends_on:
Expand All @@ -30,24 +31,6 @@ services:
start_period: 30s
networks:
- ble_net

tunnel:
build:
context: ./tunnel
dockerfile: Dockerfile
container_name: bletrack-tunnel
restart: always
environment:
CORE_V4_IP: ${CORE_V4_IP}
volumes:
- ~/.ssh/id_rsa:/app/ssh/id_rsa:ro
- ~/.ssh/known_hosts:/app/ssh/known_hosts:ro
depends_on:
api:
condition: service_healthy
networks:
- ble_net

mosquitto:
image: eclipse-mosquitto:latest
container_name: mosquitto
Expand All @@ -68,6 +51,8 @@ services:
restart: always
ports:
- "5433:5432"
env_file:
- .env
environment:
POSTGRES_DB: ${POSTGRES_DB:-bletrack}
POSTGRES_USER: ${POSTGRES_USER:-bletrack}
Expand Down
23 changes: 0 additions & 23 deletions frontend/.gitignore

This file was deleted.

20 changes: 0 additions & 20 deletions frontend/Dockerfile

This file was deleted.

70 changes: 0 additions & 70 deletions frontend/README.md

This file was deleted.

21 changes: 0 additions & 21 deletions frontend/nginx/nginx.conf

This file was deleted.

Loading