|
| 1 | +# Copyright 2026 A Team |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +import launch |
| 22 | +from launch.some_substitutions_type import SomeSubstitutionsType |
| 23 | +from launch import LaunchContext, Substitution |
| 24 | +from launch.actions import OpaqueCoroutine |
| 25 | +from launch.utilities import normalize_to_list_of_substitutions |
| 26 | +from typing import Mapping, Union, Any |
| 27 | + |
| 28 | +import asyncio |
| 29 | +import json |
| 30 | +import websockets |
| 31 | + |
| 32 | + |
| 33 | +class ChangeGameControllerTeamName(OpaqueCoroutine): |
| 34 | + """Action that sends config delta commands to the GC API""" |
| 35 | + |
| 36 | + def __init__(self, color: Union[Substitution, str], name: Union[Substitution, str], gc_address: SomeSubstitutionsType) -> None: |
| 37 | + """Initialize the action.""" |
| 38 | + super().__init__(coroutine=self.my_coroutine) |
| 39 | + self.__color = color |
| 40 | + self.__team_name = name |
| 41 | + self.__gc_address = normalize_to_list_of_substitutions(gc_address) |
| 42 | + |
| 43 | + async def my_coroutine(self, context: LaunchContext, *args, **kwargs): |
| 44 | + gc_address = context.perform_substitution(self.__gc_address[0]) |
| 45 | + color = '' |
| 46 | + if isinstance(self.__color, Substitution): |
| 47 | + color = context.perform_substitution(self.__color) |
| 48 | + else: |
| 49 | + color = self.__color |
| 50 | + color = color.upper() |
| 51 | + team_name = '' |
| 52 | + if isinstance(self.__team_name, Substitution): |
| 53 | + team_name = context.perform_substitution(self.__team_name) |
| 54 | + else: |
| 55 | + team_name = self.__team_name |
| 56 | + |
| 57 | + launch.logging.get_logger().info(f'Color: {color} Name: {team_name}') |
| 58 | + |
| 59 | + await self.send_json_payload(gc_address, color, team_name) |
| 60 | + |
| 61 | + async def send_json_payload(self, address: str, color: str, name: str): |
| 62 | + payload = { |
| 63 | + 'change': { |
| 64 | + 'origin': 'UI', |
| 65 | + 'revertible': True, |
| 66 | + 'update_team_state_change': { |
| 67 | + 'for_team': color, |
| 68 | + 'team_name': name |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + server_url = f'ws://{address}:8081/api/control' |
| 73 | + launch.logging.get_logger().info(f'Sending: {json.dumps(payload)}') |
| 74 | + while True: |
| 75 | + try: |
| 76 | + async with websockets.connect(server_url) as ws: |
| 77 | + await ws.send(json.dumps(payload)) |
| 78 | + # await ws.recv() |
| 79 | + break |
| 80 | + except ConnectionRefusedError: |
| 81 | + continue |
0 commit comments