Skip to content

Commit

Permalink
Merge pull request #13 from jfjlaros/feature/0005-is-socket-connection
Browse files Browse the repository at this point in the history
Feature/0005 is socket connection
  • Loading branch information
jfjlaros committed Dec 23, 2020
2 parents bc0445d + 068aa57 commit cd14123
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions simple_rpc/simple_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from serial import serial_for_url
from serial.serialutil import SerialException
from serial.urlhandler.protocol_socket import Serial as socket_serial

from .extras import make_function
from .io import read, read_byte_string, until, write
Expand All @@ -24,17 +25,16 @@ def __init__(self, device, baudrate=9600, wait=1, autoconnect=True):
:arg int wait: Time in seconds before communication starts.
:arg bool autoconnect: Automatically connect.
"""
self._device = device
self._wait = wait

self._connection = serial_for_url(device)
self._connection.baudrate = baudrate
self._connection = serial_for_url(
device, do_not_open=True, baudrate=baudrate)
self._is_socket = isinstance(self._connection, socket_serial)
self._version = (0, 0, 0)
self._endianness = b'<'
self._size_t = b'H'
self.methods = {}

self.close()
if autoconnect:
self.open()

Expand All @@ -44,6 +44,33 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

def _open(self):
if not self._connection.isOpen():
try:
self._connection.open()
except SerialException as error:
raise IOError(error.strerror.split(':')[0])

def _close(self):
if self._connection.isOpen():
self._connection.close()

def _auto_open(f):
"""Decorator for automatic opening and closing of ethernet sockets."""
def _auto_open_wrapper(self, *args, **kwargs):
if self._is_socket:
self._open()

result = f(self, *args, **kwargs)

if self._is_socket:
self._close()

return result

return _auto_open_wrapper


def _select(self, index):
"""Initiate a remote procedure call, select the method.
Expand Down Expand Up @@ -73,6 +100,7 @@ def _read(self, obj_type):
return read(
self._connection, self._endianness, self._size_t, obj_type)

@_auto_open
def _get_methods(self):
"""Get remote procedure call methods.
Expand Down Expand Up @@ -103,14 +131,7 @@ def _get_methods(self):

def open(self):
"""Connect to device."""
if self.is_open():
return

self._connection.port = self._device
try:
self._connection.open()
except SerialException as error:
raise IOError(error.strerror.split(':')[0])
self._open()
sleep(self._wait)

self.methods = self._get_methods()
Expand All @@ -120,22 +141,19 @@ def open(self):

def close(self):
"""Disconnect from device."""
if not self.is_open():
return

for method in self.methods:
delattr(self, method)
self.methods.clear()

self.methods = {}

if (self._connection):
self._connection.close()
self._close()

def is_open(self):
"""Query device state."""
"""Query interface state."""
if self._is_socket:
return bool(self.methods)
return self._connection.isOpen()


@_auto_open
def call_method(self, name, *args):
"""Execute a method.
Expand Down

0 comments on commit cd14123

Please sign in to comment.