Renaming TcpTransport to SocketTransport

This commit is contained in:
László Károlyi 2018-04-23 12:59:22 +02:00
parent eb192f5ef8
commit 0d2c8e229e
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE
3 changed files with 19 additions and 19 deletions

View file

@ -20,7 +20,7 @@ from ecrterm.packets.bmp import BCD
from ecrterm.transmission._transmission import Transmission
from ecrterm.transmission.signals import ACK, DLE, ETX, NAK, STX, TRANSMIT_OK
from ecrterm.transmission.transport_serial import SerialTransport
from ecrterm.transmission.transport_tcp import TcpTransport
from ecrterm.transmission.transport_socket import SocketTransport
from ecrterm.utils import is_stringlike
@ -132,12 +132,6 @@ class ECR(object):
_state_registered = None
_state_connected = None
def __get_last(self):
if self.transmitter is not None:
return self.transmitter.last
# !: Last is a short access for transmitter.last if possible.
last = property(__get_last)
def __init__(self, device='/dev/ttyUSB0', password='123456'):
"""
Initializes an ECR object and connects to the serial device
@ -147,13 +141,13 @@ class ECR(object):
You can access the Protocol Handler on low level as
`transmission`.
Pass `tcp://` prefixed IP address and port for TCP/IP transport:
`tcp://192.168.1.163:20007`
Pass `socket://` prefixed IP address and port for TCP/IP
transport: `socket://192.168.1.163:20007`
"""
if device.startswith('/'):
self.transport = SerialTransport(device)
elif device.startswith('tcp://'):
self.transport = TcpTransport(uri=device)
elif device.startswith('socket://'):
self.transport = SocketTransport(uri=device)
# self.transport.slog = ecr_log
self.daylog = []
self.daylog_template = ''
@ -170,6 +164,12 @@ class ECR(object):
else:
raise Exception('ECR could not connect.')
def __get_last(self):
if self.transmitter is not None:
return self.transmitter.last
# !: Last is a short access for transmitter.last if possible.
last = property(__get_last)
def register(self, config_byte):
"""
registers this ECR at the PT, locking menus
@ -301,10 +301,10 @@ class ECR(object):
- restarts pt: @see self.restart()
"""
self.transport.reset()
if not self.transport.is_tcp:
if self.transport.insert_delays:
sleep(1)
ret = self.restart()
if not self.transport.is_tcp:
if self.transport.insert_delays:
sleep(1)
return ret
@ -361,7 +361,7 @@ class ECR(object):
use `last` property to access last packet transmitted.
"""
if not self.transport.is_tcp:
if self.transport.insert_delays:
# we actually make a small sleep, allowing better flow.
sleep(0.2)
transmission = self.transmitter.transmit(packet)
@ -380,7 +380,7 @@ class ECR(object):
status = self.status()
while status:
print(TERMINAL_STATUS_CODES.get(status, 'Unknown Status'))
if not self.transport.is_tcp:
if self.transport.insert_delays:
sleep(2)
status = self.status()

View file

@ -104,7 +104,7 @@ class SerialMessage(object):
class SerialTransport(Transport):
SerialCls = serial.Serial
slog = noop
is_tcp = False
insert_delays = True
def __init__(self, device):
self.device = device

View file

@ -3,15 +3,15 @@ from socket import create_connection
from ecrterm.common import Transport
class TcpTransport(Transport):
class SocketTransport(Transport):
"""Transport for TCP/IP."""
is_tcp = True
insert_delays = False
def __init__(self, uri: str):
"""Setup the IP and Port."""
prefix, ip, port = uri.split(':')
self.port = int(port)
self.ip = ip[2:]
self.ip = ip[2:] # Trim '//' from the beginning
def connect(self, timeout: int=30):
"""Connect to the TCP socket."""