Esempio n. 1
0
def main(game=Games.DIRT_RALLY,
         udp_host=UDP_IP,
         udp_port=UDP_PORT,
         callback=None):
    Debug.set_log_level(LogLevel(2))
    Debug.toggle(True)
    try:
        scan = Scan(game, udp_host, udp_port, callback)
        scan.start()
        return scan
    except Exception as e:
        Debug.err(e)
Esempio n. 2
0
    def run(self):
        try:
            if self.game['db_file'] is None:
                return
            app_root = os.path.dirname(os.path.realpath(__file__))
            data_path = '/../data/'
            if getattr(sys, 'frozen', False):
                data_path = '/data/'
            conn = sqlite3.connect(app_root + data_path + self.game['db_file'])
            db = conn.cursor()

            index = 1
            value = None
            last_update = datetime.now()
            found = False
            Debug.head("Track Scanner")
            Debug.log('ID | Name | Track Length | Z POS | Laps')
            if self.callback is not None:
                self.callback("Track Scanner")
                self.callback('ID | Name | Track Length | Z POS | Laps')

            while not self.finished:
                if last_update is not None:
                    delta = datetime.now() - last_update
                    if delta.seconds >= 10:
                        found = False
                last_update = datetime.now()
                try:
                    data, address = self.sock.recvfrom(512)
                except socket.timeout:
                    continue
                except socket.error:
                    break
                if not data:
                    print('no data')
                    continue
                if found:
                    print('found')
                    continue
                stats = struct.unpack('66f', data[0:264])
                new_value = "%.14f;%d;%d" % \
                            (
                                stats[Telemetry.TRACK_LENGTH],
                                int(stats[Telemetry.Z_POSITION]),
                                int(stats[Telemetry.TOTAL_LAPS])
                            )
                if new_value != value:
                    value = new_value
                    index += 1
                    db.execute(
                        'SELECT id,name FROM Tracks WHERE length = ? AND (start_z = "" OR round(start_z) = ?)',
                        (stats[Telemetry.TRACK_LENGTH],
                         stats[Telemetry.Z_POSITION]))
                    res = db.fetchall()
                    track_name = 'unknown'
                    track_index = -1
                    if len(res) >= 1:
                        for (index, name) in res:
                            track_index = index
                            track_name = name
                            break
                    Debug.log('%d;%s;%s' % (track_index, track_name, value))
                    if self.callback is not None:
                        self.callback('%d;%s;%s' %
                                      (track_index, track_name, value))
                    found = True
            Debug.notice('Scan loop ended')
            if db is not None:
                db.close()
            if conn is not None:
                conn.close()
        except sqlite3.Error as e:
            Debug.err("Database connection error")
            Debug.err(e)
        except Exception as e:
            Debug.warn(e)