1
0
mirror of https://github.com/undera/pylgbst.git synced 2020-11-18 19:37:26 -08:00

Add lock for device detects

This commit is contained in:
Andrey Pokhilko 2020-08-12 09:34:33 +03:00
parent c73311528d
commit 35e3868a64

View File

@ -137,7 +137,7 @@ class Hub(object):
if dev_type in PERIPHERAL_TYPES: if dev_type in PERIPHERAL_TYPES:
self.peripherals[port] = PERIPHERAL_TYPES[dev_type](self, port) self.peripherals[port] = PERIPHERAL_TYPES[dev_type](self, port)
else: else:
log.warning("Have not dedicated class for peripheral type 0x%x on port 0x%x", dev_type, port) log.warning("Have not dedicated class for peripheral type %x on port %x", dev_type, port)
self.peripherals[port] = Peripheral(self, port) self.peripherals[port] = Peripheral(self, port)
log.info("Attached peripheral: %s", self.peripherals[msg.port]) log.info("Attached peripheral: %s", self.peripherals[msg.port])
@ -199,6 +199,7 @@ class MoveHub(Hub):
# noinspection PyTypeChecker # noinspection PyTypeChecker
def __init__(self, connection=None): def __init__(self, connection=None):
self._comm_lock = threading.RLock()
if connection is None: if connection is None:
connection = get_connection_auto(hub_name=self.DEFAULT_NAME) connection = get_connection_auto(hub_name=self.DEFAULT_NAME)
@ -252,32 +253,34 @@ class MoveHub(Hub):
# noinspection PyTypeChecker # noinspection PyTypeChecker
def _handle_device_change(self, msg): def _handle_device_change(self, msg):
super(MoveHub, self)._handle_device_change(msg) with self._comm_lock:
if isinstance(msg, MsgHubAttachedIO) and msg.event != MsgHubAttachedIO.EVENT_DETACHED: super(MoveHub, self)._handle_device_change(msg)
port = msg.port if isinstance(msg, MsgHubAttachedIO) and msg.event != MsgHubAttachedIO.EVENT_DETACHED:
if port == self.PORT_A: port = msg.port
self.motor_A = self.peripherals[port] if port == self.PORT_A:
elif port == self.PORT_B: self.motor_A = self.peripherals[port]
self.motor_B = self.peripherals[port] elif port == self.PORT_B:
elif port == self.PORT_AB: self.motor_B = self.peripherals[port]
self.motor_AB = self.peripherals[port] elif port == self.PORT_AB:
elif port == self.PORT_C: self.motor_AB = self.peripherals[port]
self.port_C = self.peripherals[port] elif port == self.PORT_C:
elif port == self.PORT_D: self.port_C = self.peripherals[port]
self.port_D = self.peripherals[port] elif port == self.PORT_D:
elif port == self.PORT_LED: self.port_D = self.peripherals[port]
self.led = self.peripherals[port] elif port == self.PORT_LED:
elif port == self.PORT_TILT_SENSOR: self.led = self.peripherals[port]
self.tilt_sensor = self.peripherals[port] elif port == self.PORT_TILT_SENSOR:
elif port == self.PORT_CURRENT: self.tilt_sensor = self.peripherals[port]
self.current = self.peripherals[port] elif port == self.PORT_CURRENT:
elif port == self.PORT_VOLTAGE: self.current = self.peripherals[port]
self.voltage = self.peripherals[port] elif port == self.PORT_VOLTAGE:
self.voltage = self.peripherals[port]
if type(self.peripherals[port]) == VisionSensor: if type(self.peripherals[port]) == VisionSensor:
self.vision_sensor = self.peripherals[port] self.vision_sensor = self.peripherals[port]
elif type(self.peripherals[port]) == EncodedMotor and port not in (self.PORT_A, self.PORT_B, self.PORT_AB): elif type(self.peripherals[port]) == EncodedMotor \
self.motor_external = self.peripherals[port] and port not in (self.PORT_A, self.PORT_B, self.PORT_AB):
self.motor_external = self.peripherals[port]
class TrainHub(Hub): class TrainHub(Hub):