From 4fc9f7e3fee92309dbbed3cf2cbff8ce2cd00287 Mon Sep 17 00:00:00 2001 From: Adam Munich Date: Wed, 3 Apr 2019 15:48:45 -0700 Subject: [PATCH] Update discovery.py fixd --- Firmware/fibre/python/fibre/discovery.py | 71 ++++++++++++------------ 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/Firmware/fibre/python/fibre/discovery.py b/Firmware/fibre/python/fibre/discovery.py index 224d0dc6..3489ab3a 100644 --- a/Firmware/fibre/python/fibre/discovery.py +++ b/Firmware/fibre/python/fibre/discovery.py @@ -71,49 +71,50 @@ def find_all(path, serial_number, temp_dir = tempfile.gettempdir() cache_miss = True - json_crc16 = 0 # Fetching the json crc to check cache try: json_crc16 = channel.remote_endpoint_operation(0, struct.pack("> 8) & 0xff)) + + cache_path = temp_dir + '/fibre_schema_cache_' + str(json_crc16) + + try: + json_cache = open(cache_path, 'r+') + logger.debug("Found cache file with crc") + cache_miss = False + + except: + logger.debug("Cache miss, there is no cache file") + cache_miss = True + + # Download the JSON data + if(cache_miss == True): + try: + logger.debug("Getting json schema from USB... this is slow...") + json_bytes = channel.remote_endpoint_read_buffer(0) + json_cache = open(cache_path, 'w+') + json_cache.write(json_bytes.decode("ascii")) + logger.debug("saved json_bytes to file") + + except (TimeoutError, ChannelBrokenException): + logger.debug("no response - probably incompatible") + return + else: + try: + json_bytes = json_cache.read().encode("ascii") + logger.debug("loaded json_bytes from " + cache_path) + + except (TimeoutError, ChannelBrokenException): + logger.debug("could not read cache file") + return + except Exception as error: logger.debug("Error fetching JSON CRC, falling back to downloading full JSON") - #logger.debug(traceback.format_exc()) - - cache_path = temp_dir + '/fibre_schema_cache_' + str(json_crc16) - - try: - json_cache = open(cache_path, 'r+') - logger.debug("Found cache file with crc") - cache_miss = False - except: - logger.debug("Cache miss, there is no cache file") - cache_miss = True - - if (cache_miss): - # Download the JSON data - try: - logger.debug("Getting json schema from USB... this is slow...") - json_bytes = channel.remote_endpoint_read_buffer(0) - json_cache = open(cache_path, 'w+') - json_cache.write(json_bytes.decode("ascii")) - logger.debug("saved json_bytes to file") - - except (TimeoutError, ChannelBrokenException): - logger.debug("no response - probably incompatible") - return - else: - try: - json_bytes = json_cache.read().encode("ascii") - logger.debug("loaded json_bytes from " + cache_path) - - except (TimeoutError, ChannelBrokenException): - logger.debug("could not read cache file") - return - - json_crc16 = fibre.protocol.calc_crc16(fibre.protocol.PROTOCOL_VERSION, json_bytes) + logger.debug("Getting json schema from USB... this is slow...") + json_bytes = channel.remote_endpoint_read_buffer(0) + json_crc16 = fibre.protocol.calc_crc16(fibre.protocol.PROTOCOL_VERSION, json_bytes) channel._interface_definition_crc = json_crc16