mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 21:27:49 +02:00
Refactor communication to daemon
This commit is contained in:
@@ -4,41 +4,45 @@ from driver.models import DriverConfiguration
|
||||
|
||||
|
||||
class Connector:
|
||||
|
||||
def __init__(self):
|
||||
self.config = DriverConfiguration.get_solo()
|
||||
|
||||
def __send_data(self, message):
|
||||
# to be encoded
|
||||
resp = b''
|
||||
# convert to binary if str is received
|
||||
if isinstance(message, str):
|
||||
message = message.encode()
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.connect((self.config.remote_host, self.config.remote_port))
|
||||
sock.settimeout(self.config.timeout / 1000) # milliseconds
|
||||
sock.sendall(message)
|
||||
resp = sock.recv(1024)
|
||||
|
||||
print(resp)
|
||||
return True
|
||||
while True:
|
||||
try:
|
||||
resp += sock.recv(1024)
|
||||
except socket.timeout:
|
||||
break
|
||||
return resp
|
||||
|
||||
def passthrough(self, data):
|
||||
self.__send_data(data)
|
||||
return self.__send_data(data)
|
||||
|
||||
def ops(self, address, data, function=False):
|
||||
if function:
|
||||
message = "<F {0} {1} {2}>".format(address, data['function'],
|
||||
data['state'])
|
||||
message = "<F {0} {1} {2}>".format(
|
||||
address, data['function'], data['state'])
|
||||
else:
|
||||
message = "<t 1 {0} {1} {2}>".format(address, data['speed'],
|
||||
data['direction'])
|
||||
message = "<t 1 {0} {1} {2}>".format(
|
||||
address, data['speed'], data['direction'])
|
||||
self.__send_data(message)
|
||||
return True
|
||||
|
||||
def infra(self, data):
|
||||
power = data['power']
|
||||
if "track" in data:
|
||||
track = " {}".forma(data['track'].upper())
|
||||
track = " {}".format(data["track"].upper())
|
||||
else:
|
||||
track = ""
|
||||
|
||||
if power:
|
||||
if data["power"]:
|
||||
self.__send_data('<1{}>'.format(track))
|
||||
else:
|
||||
self.__send_data('<0{}>'.format(track))
|
||||
|
Reference in New Issue
Block a user