|
21 | 21 | import logging |
22 | 22 | import socket |
23 | 23 | from threading import Thread |
24 | | -from typing import Any, AnyStr, Callable, Dict, List, Optional |
| 24 | +from typing import Any, AnyStr, Callable, Dict, List, Optional, Union |
25 | 25 |
|
26 | 26 | from .animation_info import AnimationInfo |
27 | 27 | from .animation_to_run_params import AnimationToRunParams |
| 28 | +from .client_params import ClientParams |
28 | 29 | from .command import Command |
29 | 30 | from .current_strip_color import CurrentStripColor |
30 | 31 | from .end_animation import EndAnimation |
31 | 32 | from .global_vars import * |
32 | 33 | from .json_decoder import ALSJsonDecoder |
| 34 | +from .json_encoder import ALSJsonEncoder |
33 | 35 | from .message import Message |
34 | 36 | from .running_animation_params import RunningAnimationParams |
35 | 37 | from .section import Section |
36 | 38 | from .strip_info import StripInfo |
37 | 39 |
|
38 | 40 |
|
39 | | -class AnimationSender(object): |
| 41 | +class AnimationSender: |
40 | 42 | """Handles communications with the server""" |
41 | 43 |
|
42 | 44 | def __init__(self, ip_address: str, port_num: int): |
@@ -65,6 +67,7 @@ def __init__(self, ip_address: str, port_num: int): |
65 | 67 | self.on_new_section_callback: Optional[Callable[['Section'], Any]] = None |
66 | 68 | self.on_new_strip_info_callback: Optional[Callable[['StripInfo'], Any]] = None |
67 | 69 |
|
| 70 | + self._encoder: 'ALSJsonEncoder' = ALSJsonEncoder() |
68 | 71 | self._recv_thread: Optional['Thread'] = None |
69 | 72 | self._partial_data: bytes = b'' |
70 | 73 |
|
@@ -120,9 +123,16 @@ def end(self) -> 'AnimationSender': |
120 | 123 |
|
121 | 124 | return self |
122 | 125 |
|
123 | | - def send_data(self, animation_json: AnyStr) -> 'AnimationSender': |
124 | | - """Send a new animation to the server""" |
125 | | - json_bytes = bytearray(animation_json + ";;;", 'utf-8') |
| 126 | + def send(self, data: Union['AnimationToRunParams', 'ClientParams', |
| 127 | + 'Command', 'EndAnimation', 'Section']) -> 'AnimationSender': |
| 128 | + """Send data to the server""" |
| 129 | + self.send_json(self._encoder.encode(data)) |
| 130 | + |
| 131 | + return self |
| 132 | + |
| 133 | + def send_json(self, json: AnyStr) -> 'AnimationSender': |
| 134 | + """Send encoded JSON to the server""" |
| 135 | + json_bytes = bytearray(json + ";;;", 'utf-8') |
126 | 136 | self.connection.sendall(json_bytes) |
127 | 137 |
|
128 | 138 | return self |
|
0 commit comments