-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Beacon state database model created Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Beacons backend Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Added route to app, fixed return value as tuple Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Small fix in alerts on_next call Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Set any newly saved beacon state to next event for subscription Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Fix typo on subs call and raise HTTP exception when state is not found Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Lint Signed-off-by: Aaron Chong <aaronchongth@gmail.com> * Fix parameter name Signed-off-by: Aaron Chong <aaronchongth@gmail.com> --------- Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
- Loading branch information
1 parent
4634cc5
commit fd92cb3
Showing
8 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/api-server/api_server/models/tortoise_models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/api-server/api_server/models/tortoise_models/beacons.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from tortoise.contrib.pydantic.creator import pydantic_model_creator | ||
from tortoise.fields import BooleanField, CharField | ||
from tortoise.models import Model | ||
|
||
|
||
class BeaconState(Model): | ||
id = CharField(255, pk=True) | ||
online = BooleanField(index=True) | ||
category = CharField(255, null=True, index=True) | ||
activated = BooleanField(index=True) | ||
level = CharField(255, null=True, index=True) | ||
|
||
|
||
BeaconStatePydantic = pydantic_model_creator(BeaconState) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
RmfEvents, | ||
TaskEvents, | ||
alert_events, | ||
beacon_events, | ||
fleet_events, | ||
rmf_events, | ||
task_events, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import List | ||
|
||
from fastapi import HTTPException | ||
from rx import operators as rxops | ||
|
||
from api_server.fast_io import FastIORouter, SubscriptionRequest | ||
from api_server.models import tortoise_models as ttm | ||
from api_server.rmf_io import beacon_events | ||
|
||
router = FastIORouter(tags=["Beacons"]) | ||
|
||
|
||
@router.sub("", response_model=ttm.BeaconStatePydantic) | ||
async def sub_beacons(_req: SubscriptionRequest): | ||
return beacon_events.beacons.pipe(rxops.filter(lambda x: x is not None)) | ||
|
||
|
||
@router.get("", response_model=List[ttm.BeaconStatePydantic]) | ||
async def get_beacons(): | ||
beacons = await ttm.BeaconState.all() | ||
return [await ttm.BeaconStatePydantic.from_tortoise_orm(a) for a in beacons] | ||
|
||
|
||
@router.get("/{beacon_id}", response_model=ttm.BeaconStatePydantic) | ||
async def get_beacon(beacon_id: str): | ||
beacon_state = await ttm.BeaconState.get_or_none(id=beacon_id) | ||
if beacon_state is None: | ||
raise HTTPException(404, f"Beacon with ID {beacon_id} not found") | ||
beacon_state_pydantic = await ttm.BeaconStatePydantic.from_tortoise_orm( | ||
beacon_state | ||
) | ||
return beacon_state_pydantic | ||
|
||
|
||
@router.post("", status_code=201, response_model=ttm.BeaconStatePydantic) | ||
async def save_beacon_state( | ||
beacon_id: str, online: bool, category: str, activated: bool, level: str | ||
): | ||
beacon_state, _ = await ttm.BeaconState.update_or_create( | ||
{ | ||
"online": online, | ||
"category": category, | ||
"activated": activated, | ||
"level": level, | ||
}, | ||
id=beacon_id, | ||
) | ||
if beacon_state is None: | ||
raise HTTPException(404, f"Could not save beacon state with ID {beacon_id}") | ||
beacon_state_pydantic = await ttm.BeaconStatePydantic.from_tortoise_orm( | ||
beacon_state | ||
) | ||
beacon_events.beacons.on_next(beacon_state_pydantic) | ||
return beacon_state_pydantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters