Prevent ECU polling from hanging on stalled sockets
This commit is contained in:
+35
-22
@@ -4,6 +4,7 @@
|
||||
import socket
|
||||
import binascii
|
||||
import logging
|
||||
import time
|
||||
|
||||
class APSystemsInvalidData(Exception):
|
||||
pass
|
||||
@@ -58,13 +59,40 @@ class APSystemsECUR:
|
||||
self.reader = None
|
||||
self.writer = None
|
||||
|
||||
def query_ecu(self):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((self.ipaddr,self.port))
|
||||
def _query(self, command, description):
|
||||
deadline = time.monotonic() + self.timeout
|
||||
with socket.create_connection(
|
||||
(self.ipaddr, self.port), timeout=self.timeout) as sock:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise socket.timeout(f'{description} timed out connecting to ECU')
|
||||
sock.settimeout(remaining)
|
||||
sock.sendall(command.encode('utf-8'))
|
||||
|
||||
response = bytearray()
|
||||
while True:
|
||||
terminator = response.find(self.recv_suffix)
|
||||
if terminator >= 0:
|
||||
end = terminator + len(self.recv_suffix)
|
||||
return bytes(response[:end])
|
||||
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise socket.timeout(f'{description} timed out waiting for ECU response')
|
||||
sock.settimeout(remaining)
|
||||
chunk = sock.recv(self.recv_size)
|
||||
if not chunk:
|
||||
raise APSystemsInvalidData(
|
||||
f'{description} connection closed before response terminator')
|
||||
|
||||
response.extend(chunk)
|
||||
if len(response) > self.recv_size:
|
||||
raise APSystemsInvalidData(
|
||||
f'{description} response exceeded {self.recv_size} bytes')
|
||||
|
||||
def query_ecu(self):
|
||||
logging.debug('ecu query: %s', self.ecu_query)
|
||||
sock.sendall(self.ecu_query.encode('utf-8'))
|
||||
self.ecu_raw_data = sock.recv(self.recv_size)
|
||||
self.ecu_raw_data = self._query(self.ecu_query, 'ECU query')
|
||||
|
||||
logging.debug('raw ecu data: %s', self.ecu_raw_data)
|
||||
logging.debug('ecu id: %s', self.ecu_raw_data[13:25])
|
||||
@@ -73,33 +101,18 @@ class APSystemsECUR:
|
||||
|
||||
self.process_ecu_data()
|
||||
|
||||
sock.shutdown(socket.SHUT_RDWR)
|
||||
sock.close()
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((self.ipaddr,self.port))
|
||||
|
||||
cmd = self.inverter_query_prefix + self.ecu_id + self.inverter_query_suffix
|
||||
logging.debug('inverter data cmd: %s', cmd)
|
||||
sock.sendall(cmd.encode('utf-8'))
|
||||
self.inverter_raw_data = sock.recv(self.recv_size)
|
||||
self.inverter_raw_data = self._query(cmd, 'Inverter query')
|
||||
|
||||
logging.debug('raw inverter data: %s', self.inverter_raw_data)
|
||||
|
||||
sock.shutdown(socket.SHUT_RDWR)
|
||||
sock.close()
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((self.ipaddr,self.port))
|
||||
|
||||
cmd = self.inverter_signal_prefix + self.ecu_id + self.inverter_signal_suffix
|
||||
logging.debug('inverter signal cmd: %s', cmd)
|
||||
sock.sendall(cmd.encode('utf-8'))
|
||||
self.inverter_raw_signal = sock.recv(self.recv_size)
|
||||
self.inverter_raw_signal = self._query(cmd, 'Inverter signal query')
|
||||
|
||||
logging.debug('raw signal data: %s', self.inverter_raw_signal)
|
||||
|
||||
sock.shutdown(socket.SHUT_RDWR)
|
||||
sock.close()
|
||||
|
||||
data = self.process_inverter_data()
|
||||
|
||||
data['ecu_id'] = self.ecu_id
|
||||
|
||||
+4
-1
@@ -152,7 +152,10 @@ async def get_data():
|
||||
client.write_points(points)
|
||||
|
||||
logging.info('Wrote %s points to InfluxDB', len(points))
|
||||
|
||||
else:
|
||||
logging.info(
|
||||
'ECU poll completed; no new sample (ECU time: %s)',
|
||||
data['timestamp'])
|
||||
|
||||
prev_ecu_timestamp = data['timestamp']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user