Skip to content

Commit 9378b40

Browse files
committed
Create send method on AnimationSender
1 parent 6e5da62 commit 9378b40

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ branches:
1313

1414
stages:
1515
- name: test
16-
- name: deploy python
16+
- name: deploy
1717
if: branch =~ ^v.*$
1818

1919
jobs:
@@ -23,7 +23,7 @@ jobs:
2323
script: tox
2424
after_success: bash <(curl -s https://codecov.io/bash)
2525

26-
- stage: deploy python
26+
- stage: deploy
2727
install: skip
2828
script: skip
2929
deploy:

animatedledstrip/animation_sender.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@
2121
import logging
2222
import socket
2323
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
2525

2626
from .animation_info import AnimationInfo
2727
from .animation_to_run_params import AnimationToRunParams
28+
from .client_params import ClientParams
2829
from .command import Command
2930
from .current_strip_color import CurrentStripColor
3031
from .end_animation import EndAnimation
3132
from .global_vars import *
3233
from .json_decoder import ALSJsonDecoder
34+
from .json_encoder import ALSJsonEncoder
3335
from .message import Message
3436
from .running_animation_params import RunningAnimationParams
3537
from .section import Section
3638
from .strip_info import StripInfo
3739

3840

39-
class AnimationSender(object):
41+
class AnimationSender:
4042
"""Handles communications with the server"""
4143

4244
def __init__(self, ip_address: str, port_num: int):
@@ -65,6 +67,7 @@ def __init__(self, ip_address: str, port_num: int):
6567
self.on_new_section_callback: Optional[Callable[['Section'], Any]] = None
6668
self.on_new_strip_info_callback: Optional[Callable[['StripInfo'], Any]] = None
6769

70+
self._encoder: 'ALSJsonEncoder' = ALSJsonEncoder()
6871
self._recv_thread: Optional['Thread'] = None
6972
self._partial_data: bytes = b''
7073

@@ -120,9 +123,16 @@ def end(self) -> 'AnimationSender':
120123

121124
return self
122125

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')
126136
self.connection.sendall(json_bytes)
127137

128138
return self

get_raw.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from animatedledstrip import AnimationSender, Command
22
from animatedledstrip.json_encoder import ALSJsonEncoder
33

4-
sender = AnimationSender("10.0.0.91", 6)
4+
ip = input('Enter IP address: ')
5+
port = int(input('Enter port: '))
6+
7+
sender = AnimationSender(ip, port)
58
encoder = ALSJsonEncoder()
69

710

@@ -14,4 +17,6 @@ def receive(data: bytes):
1417
sender.start()
1518

1619
while True:
17-
sender.send_data(encoder.encode(Command(input())))
20+
cmd = input('Enter command: ')
21+
if cmd != '':
22+
sender.send(Command(cmd))

0 commit comments

Comments
 (0)