def __init__(self, adapter, port): """ :param adapter: The adapter that will serve as an interface for interacting with the broker """ self._port = port # A list of items that we will need to discover for. # The base protocol will use this dictionary to feed items to # the UI self.items = [] self._error_codes = [] # The list of connected item IDs found in the initial sweep in # connectionMade() self._item_ids = [] BaseProtocol.__init__(self) # Set the LineReceiver to line mode. This causes lineReceived to be called # when data is sent to the serial port. We will get a line whenever the END_BYTE # appears in the buffer self.setLineMode() self.delimiter = END_BYTE_STR # The buffer that we will be storing the data that arrives via the serial connection self._binary_buffer = bytearray() self.adapter = adapter # Event IDs are 16-bit (2 byte) numbers so we need a radix # of 65535 or 0xFFFF in hex # NOTE: The number of bits in an event ID is subject to change, # the constant NUM_EVENT_ID_BITS can easily be changed to accommodate this. self._event_id_generator = message_id_generator((2**self.NUM_EVENT_ID_BITS)) # From parlay.utils, calls _message_queue_handler() whenever # a new message is added to the MessageQueue object self._message_queue = MessageQueue(self._message_queue_handler) self._attached_item_d = None # Dictionary that maps ID # to Deferred object self._discovery_msg_ids = {} # Sequence number is a nibble as of now, so the domain should be # 0 <= seq number <= 15 # which means the radix will be 16, but to be safe I'll do # 2^SEQ_BITS where SEQ_BITS is a member constant that can easily be changed self._seq_num = message_id_generator((2**self.SEQ_BITS)) # ACKs should be deferred objects because you want to catch them on the way # back via asynchronous communication. self._ack_deferred = defer.Deferred() # Store discovered item IDs so that we do not push duplicates to the # item requesting the discovery self._in_progress = False self._discovery_deferred = defer.Deferred() self._ack_table = {seq_num : defer.Deferred() for seq_num in range(2**self.SEQ_BITS)} self._ack_window = SlidingACKWindow(self.WINDOW_SIZE, self.NUM_RETRIES)
def get_open_params_defaults(cls): """ Returns a list of parameters defaults. These will be displayed in the UI. :return: default args: the default arguments provided to the user in the UI """ default_args = BaseProtocol.get_open_params_defaults() potential_serials = [port_list[0] for port_list in list_ports.comports()] default_args['port'] = potential_serials return default_args
def get_open_params_defaults(cls): """Override base class function to show dropdowns for defaults""" from serial.tools import list_ports defaults = BaseProtocol.get_open_params_defaults() potential_serials = [x[0] for x in list_ports.comports()] defaults['port'] = potential_serials defaults['baudrate'] = [300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400] defaults['delimiter'] = "\n" return defaults
def get_open_params_defaults(cls): """Override base class function to show dropdowns for defaults""" from serial.tools import list_ports defaults = BaseProtocol.get_open_params_defaults() potential_serials = [x[0] for x in list_ports.comports()] defaults['port'] = potential_serials defaults['baudrate'] = [ 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400 ] defaults['delimiter'] = "\n" return defaults
def get_discovery(self): """ Hitting the "discovery" button on the UI triggers this generator. Run a discovery for everything connected to this protocol and return a list of of all connected: items, messages, and endpoint types """ print "----------------------------" print "Discovery function started!" print "----------------------------" t1 = time.time() # If there is a deferred item, yield that first if self._attached_item_d is not None: yield self._attached_item_d # If we were already in the process of a discovery we should # return a deferred object. if self._in_progress: defer.returnValue(self._discovery_deferred) self._in_progress = True self.items = [] for subsystem_id in self._subsystem_ids: try: yield self._get_item_discovery_info(subsystem_id) except Exception as e: print("Exception while discovering! Skipping subsystem : " + str(subsystem_id) + "\n " + str(e)) self._in_progress = False t2 = time.time() print "Discovery took", (t2 - t1), "seconds" # At this point self.items should be populated with # the ParlayStandardItem objects that represent the items we discovered. # By calling BaseProtocol's get_discovery() function we can get that information # to the adapter and furthermore to the broker. defer.returnValue(BaseProtocol.get_discovery(self))
def __init__(self, port): self._parlay_name = port if not hasattr(self, "items"): self.items = [LineItem(self._parlay_name, self._parlay_name, self)] BaseProtocol.__init__(self)