This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
forked from plarailers/gogatsusai2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from plarailers/feature/ptcs-update-4
PTCS Update 4
- Loading branch information
Showing
15 changed files
with
263 additions
and
206 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,20 +1,33 @@ | ||
import asyncio | ||
|
||
from .train_base import TrainBase | ||
from .wire_pole_client import WirePoleClient | ||
|
||
|
||
class Bridge2: | ||
trains: dict[str, TrainBase] | ||
obstacles: dict[str, WirePoleClient] | ||
|
||
def __init__(self) -> None: | ||
self.trains = {} | ||
self.obstacles = {} | ||
|
||
def add_train(self, train: TrainBase) -> None: | ||
assert train.id not in self.trains | ||
self.trains[train.id] = train | ||
|
||
def add_obstacle(self, obstacle: WirePoleClient) -> None: | ||
assert obstacle.id not in self.obstacles | ||
self.obstacles[obstacle.id] = obstacle | ||
|
||
async def connect_all(self) -> None: | ||
await asyncio.gather(*(train.connect() for train in self.trains.values())) | ||
await asyncio.gather( | ||
*(train.connect() for train in self.trains.values()), | ||
*(obstacle.connect() for obstacle in self.obstacles.values()), | ||
) | ||
|
||
async def disconnect_all(self) -> None: | ||
await asyncio.gather(*(train.disconnect() for train in self.trains.values())) | ||
await asyncio.gather( | ||
*(train.disconnect() for train in self.trains.values()), | ||
*(obstacle.disconnect() for obstacle in self.obstacles.values()), | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,49 @@ | ||
import logging | ||
from typing import Callable | ||
from uuid import UUID | ||
|
||
from bleak import BleakClient | ||
from bleak.backends.characteristic import BleakGATTCharacteristic | ||
|
||
NotifyCollapseCallback = Callable[["WirePoleClient", bool], None] | ||
|
||
|
||
SERVICE_WIRE_POLE_UUID = UUID("62dd9b52-2995-7978-82e2-6abf1ae56555") | ||
CHARACTERISTIC_COLLAPSE_UUID = UUID("79fe0b5c-754c-3fe0-941f-3dc191cf09bf") | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class WirePoleClient: | ||
id: str | ||
_client: BleakClient | ||
|
||
def __init__(self, id: str, address: str) -> None: | ||
self.id = id | ||
self._client = BleakClient(address) | ||
|
||
def __str__(self) -> str: | ||
return f"WirePoleClient({self.id}, {self._client.address})" | ||
|
||
async def connect(self) -> None: | ||
await self._client.connect() | ||
logger.info("%s connected", self) | ||
|
||
async def disconnect(self) -> None: | ||
await self._client.disconnect() | ||
logger.info("%s disconnected", self) | ||
|
||
async def start_notify_collapse(self, callback: NotifyCollapseCallback) -> None: | ||
def wrapped_callback(_characteristic: BleakGATTCharacteristic, data: bytearray): | ||
assert len(data) == 1 | ||
is_collapsed = bool(data[0]) | ||
logger.info("%s notify collapse %s", self, is_collapsed) | ||
callback(self, is_collapsed) | ||
|
||
service = self._client.services.get_service(SERVICE_WIRE_POLE_UUID) | ||
assert service is not None | ||
characteristic = service.get_characteristic(CHARACTERISTIC_COLLAPSE_UUID) | ||
assert characteristic is not None | ||
|
||
await self._client.start_notify(characteristic, wrapped_callback) |
Oops, something went wrong.