wadda / gps3

Python 2.7 - 3.5 interface to gpsd
MIT License
76 stars 32 forks source link

JSON Leftovers #1

Closed wadda closed 8 years ago

wadda commented 8 years ago

The socket read is run through the JSON

def refresh(self, gpsd_data_package):
    """Sets new socket data as Fix attributes
    Arguments:
        self:
        gpsd_data_package (json object):
    Provides:
    self attribute dictionaries, e.g., self.TPV['lat'], self.SKY['gdop']
    Raises:
    AttributeError: 'str' object has no attribute 'keys' when the device falls out of the system
    ValueError, KeyError: most likely extra, or mangled JSON data, should not happen, but that
    applies to a lot of things.
    """
    try:
        fresh_data = json.loads(gpsd_data_package)  # The reserved word 'class' is popped from JSON object class
        package_name = fresh_data.pop('class', 'ERROR')  # gpsd data package errors are also 'ERROR'.
        package = getattr(self, package_name, package_name)  # packages are named for JSON object class
        for key in package.keys():  # TODO: Rollover and retry.  It fails here when device disappears
            package[key] = fresh_data.get(key, 'n/a')  # Updates and restores 'n/a' if key is absent in the socket
            # response, present --> 'key: 'n/a'' instead.'
    except AttributeError:  # 'str' object has no attribute 'keys'
        print('No Data')
        return
    except (ValueError, KeyError) as error:
        sys.stderr.write(str(error))  # Look for extra data in stream
        return

As you see this last ValueError, KeyError throw a error with extraneous data that is in the JSON object shipped from GPSD

[io@io gps3] :( python3 human.py -host 192.168.0.4 Keyboard interrupt received Terminated by user Good Bye. . Extra data: line 1 column 2 - line 2 column 1 (char 1 - 15)Extra data: line 1 column 5 - line 2 column 1 (char 4 - 512)Extra data: line 1 column 2 - line 2 column 1 (char 1 - 97)Extra data: line 1 column 2 - line 2 column 1 (char 1 - 113)Expecting value: line 1 column 1 (char 0)Expecting value: line 1 column 1 (char 0)Extra data: line 1 column 2 - line 2 column 1 (char 1 - 159)Expecting value: line 1 column 1 (char 0)[io@io gps3] :(

I'm looking for a way to join this data with the data that is already in the buffer. I'm also open to ideas.

wadda commented 8 years ago

I'm currently testing adding

while '\n' not in self.response:
    fragment = self.streamSock.makefile()
    self.response += fragment.readline()

` at line 128 in gps3.py to see if it resolves the problem without causing any more.

wadda commented 8 years ago

Partial success, after a 24 hour run the errors are few, and all at the beginning, so it seems.

netty@netty:~/Downloads$ python3 human.py Keyboard interrupt received Terminated by user Good Bye.

Expecting value: line 1 column 1 (char 0)Expecting value: line 1 column 1 (char 0)Expecting value: line 1 column 1 (char 0)Expecting value: line 1 column 1 (char 0)netty@netty:~/Downloads$