def close_connection(self): '''Close the connection.''' verbose('closing connection.. ', newline=False) self.ser.close() success = not self.ser.isOpen() verbose('ok' if success else 'error') return success
def open_connection(self): '''Open the connection to the device.''' verbose('opening \'%s\'.. ' % self.ser.port, newline=False) already_open = self.ser.isOpen() if not already_open: self.ser.open() success = self.ser.isOpen() verbose('ok' if success else 'error') return success
def check_device_status(self): '''Check if the device responds, and also save the size of the tracklogs which's inside the respond.''' verbose('checking device status.. ', newline=False) buf = self._communicate(CHECK_STATUS) success = True if OK in buf else False if success: verbose('ok') self.logsize = convert_logsize_value(buf[-4:]) else: fprint('error: is the device turned on?') return success
def download_data(self): '''Download all tracklogs from the device.''' self.check_if_device_is_empty() verbose('switching device to download mode.. ', newline=False) chunks = int(math.ceil(self.logsize / 128.0)) size_of_chunks = chunks * 128 if OK in self._communicate(INIT_DOWNLOAD_MAINNAV_MG_950D) or \ OK in self._communicate(INIT_DOWNLOAD_QSTART_BT_Q2000): verbose('ok') buf = '' # download first chunk: chunk = 1 buf += self._communicate(DOWNLOAD_CHUNK_FIRST, bytes=132)[3:-1] while chunk < chunks: # download remaining chunks chunk += 1 buf += self._communicate(DOWNLOAD_CHUNK_NEXT, bytes=132)[3:-1] fprint('\rdownloading: %s%%' % int((len(buf) / float(size_of_chunks)) * 100), newline=False) fprint('') # only unnecessary data left, aborting: self._communicate(ABORT_TRANSMISSION, answer=False) verbose('switching device back to standard mode.. ', newline=False) self._communicate(INIT_STANDARD, answer=False) verbose('ok') return buf else: fprint('error, unknown device')
def run(): '''Main function.''' helper.check_requirements() args = _parse_args() helper.verbose_ = args.verbose con = communication.Connection(args.device) if con.open_connection() and con.check_device_status(): if args.memory: con.close_connection() used = (con.logsize - 8192) / 2080000.0 * 100 print 'memory usage: %.1f%%' % used points_remaining = (2080000 - (con.logsize - 8192)) / 16 time_remaining = points_remaining / 3600.0 print '%s points or %.1f hours of tracking remaining' % (points_remaining, time_remaining) elif args.download: raw_data = con.download_data() con.close_connection() if args.raw: _write(((raw_data, 'trackdata.bin'),), args.target_dir) else: tracks = parser.parse(raw_data, con.logsize, args.utc_offset) i = 1 gpx_structures = [] for track in tracks: verbose('creating gpx structure for track #%s.. ' % i, newline=False) gpx_structure = gpx.create_gpx_structure(track) date = track[0]['time'].strftime('%y-%m-%d_%H:%M') gpx_structures.append((gpx_structure, 'track_%s.gpx' % date)) verbose('ok') i += 1 _write(gpx_structures, args.target_dir) elif args.purge: con.purge_log_on_device() con.close_connection() else: con.close_connection() print('nothing to do..')
def parse(data, logsize, utc_offset): '''Main parse function to extract the needed information out of the binary data and return the extracted tracks as a list. @param data: Binary data to parse. @param logsize: The size of the tracklogs, as reported by the device.''' verbose('parsing binary data.. ', newline=False) end_offsets_raw = _parse_tracklog_ends_offsets(data) if end_offsets_raw: end_offsets = _interprete_end_offsets_raw(end_offsets_raw) tracks_raw = _parse_tracklogs(data, end_offsets, logsize) tracks = _interprete_tracks_raw(tracks_raw, utc_offset) verbose('ok') return tracks else: verbose('ok') return None
def verbose(self, message): helper.verbose('[{0}] {1}'.format(Client.__name__, message))
die(e) folder = datetime.datetime.now().strftime('%y-%m-%d_%H:%M') try: os.chdir(folder) except OSError: try: os.mkdir(folder) os.chdir(folder) except OSError, e: die('error while creating folder: \'%s\'' % folder) i = 1 for data in datas: filename = data[1] fullpath = '%s/%s' % (os.getcwd(), filename) if data[1] == 'trackdata.bin': verbose('writing binary data to: \'%s\'.. ' % fullpath, newline=False) else: verbose('writing track #%s to: \'%s\'.. ' % (i, fullpath), newline=False) i += 1 try: while os.path.isfile(filename): filename = '%s_another.%s' % (filename[:-4], filename[-3:]) fd = open(filename, 'w') fd.write(data[0]) except OSError, e: die(e) finally: fd.close() verbose('ok') if __name__ == '__main__':
def __verbose(self, message): helper.verbose('[{0}] {1}'.format(Client.__name__, message))