Changeset - d7a4927bc9f9
[Not reviewed]
default
0 1 0
Kemp - 6 years ago 2018-04-09 12:51:56

Renamed private member variables and methods in NeoDevice to use a single underscore and rearranged so public methods appear first. No functionality changes.
1 file changed with 73 insertions and 73 deletions:
0 comments (0 inline, 0 general)
neovi/neodevice.py
Show inline comments
...
 
@@ -88,15 +88,15 @@ class NeoDevice:
 
        messages via the :py:meth:`.NeoDevice.get_messages` method.
 
    """
 
    def __init__(self, device, auto_open=False, launch_msg_queue_thread=True):
 
        self.device = device
 
        self.handle = None
 
        self.tx_id = 1
 
        self.subscriptions = []
 
        self.subscriptions_all = []
 
        self.msg_queue_thread = None
 
        self.pump_messages = False
 
        self.subscriptions_lock = threading.Lock()
 
        self.subscriptions_all_lock = threading.Lock()
 
        self._device = device
 
        self._handle = None
 
        self._tx_id = 1
 
        self._subscriptions = []
 
        self._subscriptions_all = []
 
        self._msg_queue_thread = None
 
        self._pump_messages = False
 
        self._subscriptions_lock = threading.Lock()
 
        self._subscriptions_all_lock = threading.Lock()
 
        
 
        if auto_open:
 
            self.open(launch_msg_queue_thread)
...
 
@@ -114,27 +114,18 @@ class NeoDevice:
 
        
 
        :raises OpenFailedError: If the device cannot be opened.
 
        """
 
        self.handle = neovi.OpenNeoDevice(self.device)
 
        if self.handle is None:
 
        self._handle = neovi.OpenNeoDevice(self._device)
 
        if self._handle is None:
 
            raise OpenFailedError
 
        if launch_msg_queue_thread:
 
            self._start_msg_queue_thread()
 
    
 
    def _start_msg_queue_thread(self):
 
        self.pump_messages = True
 
        self.msg_queue_thread = threading.Thread(target=self._msg_queue_thread)
 
        self.msg_queue_thread.start()
 
        
 
    def _msg_queue_thread(self):
 
        while self.pump_messages:
 
            self._process_msg_queue()
 
    
 
    def get_type(self):
 
        """
 
        :return: The type of device represented. See NEODEVICE_* in
 
            :py:mod:`~neovi.neovi`.
 
        """
 
        return self.device.DeviceType
 
        return self._device.DeviceType
 
    
 
    def get_settings(self):
 
        """
...
 
@@ -165,7 +156,7 @@ class NeoDevice:
 
            in the ICS API documentation
 
            for possible codes (constants not yet defined within pyneovi).
 
        """
 
        return neovi.TxMessages(self.handle, msg, network_id)
 
        return neovi.TxMessages(self._handle, msg, network_id)
 
    
 
    def tx_message(self, network_id, dest, msg_type, payload):
 
        """
...
 
@@ -196,9 +187,9 @@ class NeoDevice:
 
            msg.Data[i + 2] = payload[i]
 
        msg.StatusBitField = 0
 
        msg.StatusBitField2 = 0
 
        msg.DescriptionID = self.tx_id
 
        self.tx_id += 1
 
        return self.tx_raw_message(msg, network_id), self.tx_id - 1
 
        msg.DescriptionID = self._tx_id
 
        self._tx_id += 1
 
        return self.tx_raw_message(msg, network_id), self._tx_id - 1
 
        
 
    def subscribe_to(self, callback, network=None, address=None, msg_type=None, additional_bytes=None, auto_remove=False, user_data=None):
 
        """
...
 
@@ -230,9 +221,9 @@ class NeoDevice:
 
            pyneovi - the value is passed to the callback function along with
 
            the received message.
 
        """
 
        self.subscriptions_lock.acquire()
 
        self.subscriptions.append((callback, network, address, msg_type, [] if additional_bytes is None else additional_bytes, auto_remove, user_data))
 
        self.subscriptions_lock.release()
 
        self._subscriptions_lock.acquire()
 
        self._subscriptions.append((callback, network, address, msg_type, [] if additional_bytes is None else additional_bytes, auto_remove, user_data))
 
        self._subscriptions_lock.release()
 
    
 
    def subscribe_to_all(self, callback):
 
        """
...
 
@@ -252,30 +243,9 @@ class NeoDevice:
 
            in the ICS API documentation
 
            for a complete error list).
 
        """
 
        self.subscriptions_all_lock.acquire()
 
        self.subscriptions_all.append(callback)
 
        self.subscriptions_all_lock.release()
 
    
 
    def _process_msg_queue(self):
 
        result, msgs, errors = self.get_messages()
 
        
 
        self.subscriptions_all_lock.acquire()
 
        for callback in self.subscriptions_all:
 
            callback(result, msgs, errors)
 
        self.subscriptions_all_lock.release()
 
        
 
        self.subscriptions_lock.acquire()
 
        for msg in msgs:
 
            for i in range(len(self.subscriptions) - 1, -1, -1):
 
                callback, network, address, msg_type, additional_bytes, auto_remove, user_data = self.subscriptions[i]
 
                if ((network is None or network == msg.NetworkID) and
 
                        (address is None or address == msg.ArbIDOrHeader) and
 
                        (msg_type is None or msg_type == msg.Data[1]) and
 
                        (additional_bytes is None or structures.array_equal(additional_bytes, msg.Data[2:2 + len(additional_bytes)]))):
 
                    callback(msg, user_data)
 
                    if auto_remove:
 
                        del self.subscriptions[i]
 
        self.subscriptions_lock.release()
 
        self._subscriptions_all_lock.acquire()
 
        self._subscriptions_all.append(callback)
 
        self._subscriptions_all_lock.release()
 
    
 
    def get_messages(self):
 
        """
...
 
@@ -293,7 +263,7 @@ class NeoDevice:
 
            in the ICS API documentation
 
            for a complete error list).
 
        """
 
        return neovi.GetMessages(self.handle)
 
        return neovi.GetMessages(self._handle)
 
    
 
    def setup_networks(self, network_settings):
 
        # We don't know how to handle anything other than the Fire right now.
...
 
@@ -320,31 +290,61 @@ class NeoDevice:
 
        self.set_settings(settings)
 
    
 
    def get_firmware_version(self):
 
        result, info = neovi.GetHWFirmwareInfo(self.handle)
 
        result, info = neovi.GetHWFirmwareInfo(self._handle)
 
        return info if result else None
 

 
    def get_dll_firmware_version(self):
 
        result, info = neovi.GetDLLFirmwareInfo(self.handle)
 
        result, info = neovi.GetDLLFirmwareInfo(self._handle)
 
        return info if result else None
 

 
    def force_firmware_update(self):
 
        neovi.ForceFirmwareUpdate(self.handle)
 
    
 
    def __del__(self):
 
        self.close()
 
    
 
        neovi.ForceFirmwareUpdate(self._handle)
 

 
    def close(self):
 
        """
 
        Close the device and shutdown the message thread (if there is one).
 
        """
 
        self.pump_messages = False
 
        if self.msg_queue_thread is not None:
 
            self.msg_queue_thread.join()
 
        
 
        if self.handle is not None:
 
            neovi.ClosePort(self.handle)
 
            neovi.FreeObject(self.handle)
 
            self.handle = None
 
        self._pump_messages = False
 
        if self._msg_queue_thread is not None:
 
            self._msg_queue_thread.join()
 

 
        if self._handle is not None:
 
            neovi.ClosePort(self._handle)
 
            neovi.FreeObject(self._handle)
 
            self._handle = None
 

 
    def _process_msg_queue(self):
 
        result, msgs, errors = self.get_messages()
 

 
        self._subscriptions_all_lock.acquire()
 
        for callback in self._subscriptions_all:
 
            callback(result, msgs, errors)
 
        self._subscriptions_all_lock.release()
 

 
        self._subscriptions_lock.acquire()
 
        for msg in msgs:
 
            for i in range(len(self._subscriptions) - 1, -1, -1):
 
                callback, network, address, msg_type, additional_bytes, auto_remove, user_data = self._subscriptions[i]
 
                if ((network is None or network == msg.NetworkID) and
 
                        (address is None or address == msg.ArbIDOrHeader) and
 
                        (msg_type is None or msg_type == msg.Data[1]) and
 
                        (additional_bytes is None or structures.array_equal(additional_bytes, msg.Data[2:2 + len(additional_bytes)]))):
 
                    callback(msg, user_data)
 
                    if auto_remove:
 
                        del self._subscriptions[i]
 
        self._subscriptions_lock.release()
 

 
    def _start_msg_queue_thread(self):
 
        self._pump_messages = True
 
        self._msg_queue_thread = threading.Thread(target=self._msg_queue_thread_func)
 
        self._msg_queue_thread.start()
 

 
    def _msg_queue_thread_func(self):
 
        while self._pump_messages:
 
            self._process_msg_queue()
 
    
 
    def __del__(self):
 
        self.close()
 
            
 

 
class NeoFire(NeoDevice):
...
 
@@ -356,10 +356,10 @@ class NeoFire(NeoDevice):
 
        NeoDevice.__init__(self, device, auto_open, launch_msg_queue_thread)
 
       
 
    def get_settings(self):
 
        return neovi.GetFireSettings(self.handle)
 
        return neovi.GetFireSettings(self._handle)
 
    
 
    def set_settings(self, settings, save_to_eeprom=False):
 
        return neovi.SetFireSettings(self.handle, settings, save_to_eeprom)
 
        return neovi.SetFireSettings(self._handle, settings, save_to_eeprom)
 

 

 
class NeoRed(NeoFire):
...
 
@@ -379,10 +379,10 @@ class NeoVCAN3(NeoDevice):
 
        NeoDevice.__init__(self, device, auto_open, launch_msg_queue_thread)
 
       
 
    def get_settings(self):
 
        return neovi.GetVCAN3Settings(self.handle)
 
        return neovi.GetVCAN3Settings(self._handle)
 
    
 
    def set_settings(self, settings, save_to_eeprom=False):
 
        return neovi.SetVCAN3Settings(self.handle, settings, save_to_eeprom)
 
        return neovi.SetVCAN3Settings(self._handle, settings, save_to_eeprom)
 
        
 
        
 
class NeoBlue(NeoDevice):
...
 
@@ -396,7 +396,7 @@ class NeoBlue(NeoDevice):
 
    def set_settings(self, settings, save_to_eeprom=False):
 
        if sizeof(settings) != 1024:
 
            raise InvalidConfigurationError
 
        return neovi.SendConfiguration(self.handle, settings)
 
        return neovi.SendConfiguration(self._handle, settings)
 

 

 
class NeoDWVCAN(NeoBlue):
0 comments (0 inline, 0 general)