Skip to content

Commit

Permalink
Merge pull request #39 from flosoft/multiple-cars
Browse files Browse the repository at this point in the history
Basic support for multiple cars.
  • Loading branch information
flosoft authored Aug 9, 2024
2 parents f1a893c + 790fe3c commit 822370d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env_sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ MAPBOX_TOKEN=pk.BLA
BACKEND_PROVIDER=teslalogger
BACKEND_PROVIDER_BASE_URL=http://insert-base-api-here:withport/
BACKEND_PROVIDER_CAR_ID=1
BACKEND_PROVIDER_MULTICAR=False

TZ=Europe/Berlin
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ Copy the `.env_sample` to `.env` and configure the variables. This file will nee

| Variable | Example | Required |
|---------------------------|------------------------------------------------------------------------------------------|----------|
| PORT | `5051` | N |
| DATA_DIR | `/data/` | N |
| PORT | `5051` | Y |
| DATA_DIR | `/data/` | Y |
| SECRET_KEY | `RANDOMLY_GENERATED_HERE` | Y |
| ADMIN_PASSWORD | `PASSWORD_FOR_ADMIN_PAGE` | Y |
| BASE_URL | `/map` | Y |
| MAPBOX_TOKEN | `pk.BLA` | Y |
| BACKEND_PROVIDER | `teslalogger` OR `teslamate` | N |
| BACKEND_PROVIDER | `teslalogger` OR `teslamate` | Y |
| BACKEND_PROVIDER_BASE_URL | `http://insert-base-api-here:withport/`<br>TeslaLogger Example: `http://raspberry:5010/` | Y |
| BACKEND_PROVIDER_CAR_ID | `1` | N |
| BACKEND_PROVIDER_CAR_ID | `1` | Y |
| BACKEND_PROVIDER_MULTICAR | `False` OR `True` | N |
| TZ | `Europe/Berlin` | Y |

- `ADMIN_PASSWORD` variable is the password in plaintext for the user "admin"
Expand Down
23 changes: 21 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
BACKEND_PROVIDER = os.getenv('BACKEND_PROVIDER', 'teslalogger')
BACKEND_PROVIDER_BASE_URL = os.getenv('BACKEND_PROVIDER_BASE_URL')
BACKEND_PROVIDER_CAR_ID = os.getenv('BACKEND_PROVIDER_CAR_ID', 1)
BACKEND_PROVIDER_MULTICAR = os.getenv('BACKEND_PROVIDER_MULTICAR', False)

# Backend provider instanciation
BackendProviderFactory(BACKEND_PROVIDER, BACKEND_PROVIDER_BASE_URL, BACKEND_PROVIDER_CAR_ID)
Expand Down Expand Up @@ -156,6 +157,14 @@ def carstate(shortuuid):

if result:
if result.expiry > time.time():
# Update Attribute for car_id if in DB result else use ENV variable - only if MultiCar is enabled
if BACKEND_PROVIDER_MULTICAR == 'True':
if result.carid:
new_car_id = result.carid
else:
new_car_id = BACKEND_PROVIDER_CAR_ID
BackendProviderFactory.provider.car_id = new_car_id

provider = BackendProviderFactory.get_instance()
provider.refresh_data()

Expand Down Expand Up @@ -207,14 +216,24 @@ def map_admin():
db.session.commit()

result = db.session.query(Share).where(Share.expiry > time.time()).all()

# Update Attribute for car_id if in DB result else use ENV variable
if BACKEND_PROVIDER_MULTICAR == 'True':
if request.args.get('carid'):
new_car_id = request.args.get('carid')
else:
new_car_id = BACKEND_PROVIDER_CAR_ID
BackendProviderFactory.provider.car_id = new_car_id
else:
new_car_id = BACKEND_PROVIDER_CAR_ID

provider = BackendProviderFactory.get_instance()
provider.refresh_data()

if 'uuid' in locals():
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, uuid=uuid, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude])
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, uuid=uuid, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude], multicar=BACKEND_PROVIDER_MULTICAR, carid=new_car_id)
else:
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude])
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude], multicar=BACKEND_PROVIDER_MULTICAR, carid=new_car_id)

@app.template_filter('fromtimestamp')
def _jinja2_filter_datetime(date, fmt=None):
Expand Down
32 changes: 32 additions & 0 deletions migrations/versions/aae5eeb8d2bc_multi_car_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Multi Car Support
Revision ID: aae5eeb8d2bc
Revises:
Create Date: 2024-08-05 22:51:19.163085
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'aae5eeb8d2bc'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('share', schema=None) as batch_op:
batch_op.add_column(sa.Column('carid', sa.Integer(), nullable=True))

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('share', schema=None) as batch_op:
batch_op.drop_column('carid')

# ### end Alembic commands ###
1 change: 1 addition & 0 deletions models/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ class Share(db.Model):
lat = db.Column(db.Float, nullable=True)
lng = db.Column(db.Float, nullable=True)
expiry = db.Column(db.Integer, nullable=False)
carid = db.Column(db.Integer, nullable=True)
43 changes: 43 additions & 0 deletions templates/map_admin.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,49 @@
</div>
</div>
</div>

{% if multicar == 'True' %}
<div class="hidden sm:block" aria-hidden="true">
<div class="py-5">
<div class="border-t border-gray-200"></div>
</div>
</div>

<div class="container mx-auto">
<div class="mt-10 sm:mt-0">
<div class="md:grid md:grid-cols-3 md:gap-6">
<div class="md:col-span-1">
<div class="px-4 sm:px-0">
<h3 class="text-lg font-medium leading-6 text-gray-900">Change Car</h3>
<p class="mt-1 text-sm text-gray-600">Use another Car from your backend</p>
</div>
</div>
<div class="mt-5 md:col-span-2 md:mt-0">
<form action="#" method="GET">
<div class="overflow-hidden shadow sm:rounded-md">
<div class="bg-white px-4 py-5 sm:p-6">

<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="lat" class="block text-sm font-medium text-gray-700">Car ID</label>
<input type="text" name="carid" id="carid" value="{{ carid }}"
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-gray-500 focus:ring-gray-500 sm:text-sm">
</div>


</div>
</div>
<div class="bg-gray-50 px-4 py-3 text-right sm:px-6">
<button type="submit"
class="inline-flex justify-center rounded-md border border-transparent bg-gray-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2">Change Car</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endif %}
</body>

</html>

0 comments on commit 822370d

Please sign in to comment.