Skip to content

Commit

Permalink
Merge pull request wh00hw#13 from apatrushev/feature-rfc2217
Browse files Browse the repository at this point in the history
Support connections over plain tcp
  • Loading branch information
wh00hw authored Aug 29, 2023
2 parents 3ea3607 + dd15f55 commit c288073
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/pyflipper/lib/serial_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import os
import socket
import serial
import re
from websocket import create_connection
Expand Down Expand Up @@ -49,4 +51,31 @@ def write(self, msg):
self.ws.send_binary(msg)

def ctrl_c(self):
self.ws.send_binary(b'\x03')
self.ws.send_binary(b'\x03')

class TcpSerial:
def __init__(self, addr) -> None:
host, _, port = addr.partition(':')
port = int(port)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((host, port))
self._read_response()

@error_handler
def send(self, payload: str) -> str:
self._socket.sendall(f"{payload}\r".encode())
return self._read_response()

def write(self, msg):
self._socket.sendall(msg)

def ctrl_c(self):
self._socket.sendall(b'\x03')

def _read_response(self):
buff = ''
while True:
buff += self._socket.recv(1024).decode()
if '>:' in buff:
break
return '\n'.join(buff.splitlines()[1:-2])
11 changes: 7 additions & 4 deletions src/pyflipper/pyflipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .lib.onewire import Onewire
from .lib.ps import Ps
from .lib.rfid import RFID
from .lib.serial_wrapper import LocalSerial, WSSerial
from .lib.serial_wrapper import LocalSerial, WSSerial, TcpSerial
from .lib.storage import Storage
from .lib.subghz import Subghz
from .lib.vibro import Vibro
Expand All @@ -27,11 +27,14 @@
class PyFlipper:

def __init__(self, **kwargs) -> None:
assert bool(kwargs.get('com')) ^ bool(kwargs.get('ws')), "COM or Websocket required"
assert sum(bool(kwargs.get(k)) for k in ('com', 'ws', 'tcp')) == 1, \
'Only one of com, ws, tcp should be specified'
if kwargs.get('com'):
self._serial_wrapper = LocalSerial(com=kwargs['com'])
self._serial_wrapper = LocalSerial(com=kwargs['com'])
elif kwargs.get('ws'):
self._serial_wrapper = WSSerial(ws=kwargs['ws'])
else:
self._serial_wrapper = WSSerial(ws=kwargs['ws'])
self._serial_wrapper = TcpSerial(addr=kwargs['tcp'])
self.vibro = Vibro(serial_wrapper=self._serial_wrapper)
self.date = Date(serial_wrapper=self._serial_wrapper)
self.device_info = DeviceInfo(serial_wrapper=self._serial_wrapper)
Expand Down

0 comments on commit c288073

Please sign in to comment.