Skip to content

Commit

Permalink
several fixups (#250)
Browse files Browse the repository at this point in the history
* when wifi is disconnected, stop emitting gps positions
 * fix up the requirements.txt to pull in my doctored version of the obd librarys
 * stop deleting OBD keys under any circumstances
  • Loading branch information
sprintf authored Jul 5, 2022
1 parent 6b7371e commit 6d8fce3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
20 changes: 18 additions & 2 deletions lemon_pi/car/meringue_comms_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

import grpc

from lemon_pi.car.event_defs import WifiConnectedEvent, WifiDisconnectedEvent
from lemon_pi.car.radio_interface import RadioInterface
from lemon_pi.shared.data_provider_interface import GpsProvider
from lemon_pi.shared.events import EventHandler
from lemon_pi.shared.meringue_comms import MeringueComms, build_auth_header
from lemon_pi_pb2 import CarNumber, ToCarMessage, ToPitMessage
from lemon_pi_pb2_grpc import CommsServiceStub

logger = logging.getLogger(__name__)


class MeringueCommsCar(Thread, MeringueComms):
class MeringueCommsCar(Thread, MeringueComms, EventHandler):

def __init__(self, car_number: str, key: str, ping_frequency: int):
Thread.__init__(self)
Expand All @@ -24,6 +26,9 @@ def __init__(self, car_number: str, key: str, ping_frequency: int):
self.radio_interface = None
self.stopped: bool = False
self.gps_provider = None
self.wifi_working = False
WifiConnectedEvent.register_handler(self)
WifiDisconnectedEvent.register_handler(self)

def set_radio_interface(self, radio_interface: RadioInterface):
self.radio_interface = radio_interface
Expand All @@ -38,7 +43,9 @@ def pinger(self):
position = self.gps_provider.get_gps_position()
if position:
msg.ping.gps.CopyFrom(position)
self.send_message_from_car(msg)
# do not emit these if wifi is not connected
if self.wifi_working:
self.send_message_from_car(msg)
except grpc.RpcError:
pass
sleep(self.ping_frequency)
Expand Down Expand Up @@ -69,3 +76,12 @@ def run(self):
else:
logger.exception("unknown error")
sleep(10)

def handle_event(self, event, **kwargs):
if event == WifiConnectedEvent:
self.wifi_working = True
return

if event == WifiDisconnectedEvent:
self.wifi_working = False
return
10 changes: 7 additions & 3 deletions lemon_pi/car/obd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ def run(self) -> None:
keys_to_delete.append(cmd)
time.sleep(10)
time.sleep(0.5)
for dead_key in keys_to_delete:
del ObdReader.refresh_rate[dead_key]
logger.info(f"removed {dead_key}")
# leaving this functionality out for now, seems fragile and harmful
# for dead_key in keys_to_delete:
# del ObdReader.refresh_rate[dead_key]
# logger.info(f"removed {dead_key}")

except Exception as e:
logger.exception("bad stuff in OBD land %s", e)
Expand All @@ -90,6 +91,9 @@ def run(self) -> None:
time.sleep(10)

def connect(self, old_connection):
if not UsbDetector.detected(UsbDevice.OBD):
return None

port = UsbDetector.get(UsbDevice.OBD)
if not port:
return None
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ grpcio-tools==1.40.0
guizero>=1.3.0
haversine>=2.3.0
numpy>=1.21
obd==0.7.1
git+https://github.com/sprintf/python-OBD@master
Pillow>=8.1.1
Pint==0.7.2
protobuf==3.16.0
Expand Down

0 comments on commit 6d8fce3

Please sign in to comment.