示例#1
0
    def __init__(self, appname):

        self.appname = appname
        self.static_set = False

        #
        # Structures for Data Capture
        #
        self.info = info  # sim_info
        self.data = dict()

        #
        # GameController Fu
        #
        self.game_controller = GameController()
        self.game_controller.setInitialStatus()
        if self.game_controller.device is None:
            self.data['game_controller'] = 'No Game Controller found'
        else:
            self.data['game_controller'] = self.game_controller.device.get_name()

        #
        # Read the config
        #
        config_dict, self.valid_config = self.read_and_validate_config()
        self.button_forward = config_dict[BUTTON_FORWARD]
        self.button_backward = config_dict[BUTTON_BACK]
        self.raspberry_ip = config_dict[RASPBERRY_IP]
        self.raspberry_udp_port = config_dict[RASPBERRY_UDP_PORT]
        self.hz = config_dict[HZ]
        self.show_debug_window = config_dict[SHOW_DEBUG_WINDOW]

        self.data['raspberry_ip'] = self.raspberry_ip
        self.data['raspberry_udp_port'] = self.raspberry_udp_port

        ac.log("{} config: {}".format(self.appname, pformat(config_dict, indent=0, width=10000)))
        #
        # Set the Interval for Updates
        #
        self.interval = 1.0 / self.hz
        self.data['interval'] = self.interval
        self.data['hz'] = self.hz
        self.last_update_tick = 0
        self.frames_skipped = 0

        #
        # The Debugwindow as Application
        #
        fields_from_static = ('max_rpm', 'car_model', 'player_nick', 'max_fuel', 'num_cars')
        fields_from_physics = ('abs', 'drs', 'tc', 'kers_charge', 'kers_input', 'rpms', 'fuel', 'gear', 'speed_kmh', 'pit_limiter_on')
        fields_from_graphics = ('position', 'number_of_laps', 'completed_laps', 'i_current_time', 'i_last_time', 'i_best_time')

        self.field_shown_in_debug_window = (['static.{0}'.format(s) for s in fields_from_static] +
                                            ['physics.{0}'.format(s) for s in fields_from_physics] +
                                            ['graphics.{0}'.format(s) for s in fields_from_graphics] +
                                            ['frames_skipped', 'interval', 'raspberry_ip', 'raspberry_udp_port'])
        if self.show_debug_window:
            self.debug_window = DataProviderIngameDebugWindow(self.appname, 300, 500)
            for name in self.field_shown_in_debug_window:
                self.debug_window.add_label(Label(self.debug_window.app_id, name, '{}: N/A'.format(name)))
            self.debug_window.pack()

        #
        # create the UDP Socket
        #
        self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
示例#2
0
class PDU1800DataProvider:
    def __init__(self, appname):

        self.appname = appname
        self.static_set = False

        #
        # Structures for Data Capture
        #
        self.info = info  # sim_info
        self.data = dict()

        #
        # GameController Fu
        #
        self.game_controller = GameController()
        self.game_controller.setInitialStatus()
        if self.game_controller.device is None:
            self.data['game_controller'] = 'No Game Controller found'
        else:
            self.data['game_controller'] = self.game_controller.device.get_name()

        #
        # Read the config
        #
        config_dict, self.valid_config = self.read_and_validate_config()
        self.button_forward = config_dict[BUTTON_FORWARD]
        self.button_backward = config_dict[BUTTON_BACK]
        self.raspberry_ip = config_dict[RASPBERRY_IP]
        self.raspberry_udp_port = config_dict[RASPBERRY_UDP_PORT]
        self.hz = config_dict[HZ]
        self.show_debug_window = config_dict[SHOW_DEBUG_WINDOW]

        self.data['raspberry_ip'] = self.raspberry_ip
        self.data['raspberry_udp_port'] = self.raspberry_udp_port

        ac.log("{} config: {}".format(self.appname, pformat(config_dict, indent=0, width=10000)))
        #
        # Set the Interval for Updates
        #
        self.interval = 1.0 / self.hz
        self.data['interval'] = self.interval
        self.data['hz'] = self.hz
        self.last_update_tick = 0
        self.frames_skipped = 0

        #
        # The Debugwindow as Application
        #
        fields_from_static = ('max_rpm', 'car_model', 'player_nick', 'max_fuel', 'num_cars')
        fields_from_physics = ('abs', 'drs', 'tc', 'kers_charge', 'kers_input', 'rpms', 'fuel', 'gear', 'speed_kmh', 'pit_limiter_on')
        fields_from_graphics = ('position', 'number_of_laps', 'completed_laps', 'i_current_time', 'i_last_time', 'i_best_time')

        self.field_shown_in_debug_window = (['static.{0}'.format(s) for s in fields_from_static] +
                                            ['physics.{0}'.format(s) for s in fields_from_physics] +
                                            ['graphics.{0}'.format(s) for s in fields_from_graphics] +
                                            ['frames_skipped', 'interval', 'raspberry_ip', 'raspberry_udp_port'])
        if self.show_debug_window:
            self.debug_window = DataProviderIngameDebugWindow(self.appname, 300, 500)
            for name in self.field_shown_in_debug_window:
                self.debug_window.add_label(Label(self.debug_window.app_id, name, '{}: N/A'.format(name)))
            self.debug_window.pack()

        #
        # create the UDP Socket
        #
        self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    @staticmethod
    def read_and_validate_config():
        config = ConfigParser(defaults=DEFAULT_CONFIG_DICT, default_section=INI_SECTION)
        if not os.path.isfile(INI_FILE):
            with open(INI_FILE, 'w') as f:
                config.write(f)
        with open(INI_FILE) as f:
            config.read_file(f)

        bt_forward = config.get(INI_SECTION, BUTTON_FORWARD, fallback=DEFAULT_CONFIG_DICT[BUTTON_FORWARD])
        bt_back = config.get(INI_SECTION, BUTTON_BACK, fallback=DEFAULT_CONFIG_DICT[BUTTON_BACK])
        raspberry_ip = config.get(INI_SECTION, RASPBERRY_IP, fallback=DEFAULT_CONFIG_DICT[RASPBERRY_IP])
        raspberry_udp_port = config.get(INI_SECTION, RASPBERRY_UDP_PORT, fallback=DEFAULT_CONFIG_DICT[RASPBERRY_UDP_PORT])
        hz = config.get(INI_SECTION, HZ, fallback=DEFAULT_CONFIG_DICT[HZ])
        show_debug_window = config.getboolean(INI_SECTION, SHOW_DEBUG_WINDOW, fallback=False)

        try:
            bt_forward = int(bt_forward)
            bt_back = int(bt_back)
            raspberry_udp_port = int(raspberry_udp_port)
            hz = int(hz)
            if not validate_ip(raspberry_ip):
                raise Exception('IP for Raspberry PI is not valid')
            if not 1024 < raspberry_udp_port <= 65535:
                raise Exception('UDP Port for Raspberry PI is not valid')
        except Exception as e:
            ac.log(str(e))
            ac.console(str(e))
            return DEFAULT_CONFIG_DICT, False
        return dict(button_forward=bt_forward, button_back=bt_back, raspberry_ip=raspberry_ip,
                    raspberry_udp_port=raspberry_udp_port, hz=hz, show_debug_window=show_debug_window), True

    def update_labels_in_debug_window(self):
        for label_name in self.field_shown_in_debug_window:
            self.debug_window.labels[label_name].setText("{}: {}".format(label_name, getit(label_name, self.data)))

    def emit(self):
        self.udp_sock.sendto(pickle.dumps(self.data, protocol=2), (self.raspberry_ip, self.raspberry_udp_port))

    def update(self, deltaT):
        #
        # Reading the static section within the shared memory needs to be done within the acUpdate(), but the
        # first frame is all we need.
        #


        if not self.static_set:
            self.data['static'] = struct_to_hash(self.info.static)
            self.static_set = True

        #
        # Check if we should update
        #
        current_tick = perf_counter()
        if current_tick - self.last_update_tick < self.interval:
            self.frames_skipped += 1
            return
        self.last_update_tick = current_tick

        #
        # Dynamic updates
        #
        self.data['delta'] = ac.getCarState(0, acsys.CS.PerformanceMeter)   # Delta gibts aus der Python API

        self.data['graphics'] = struct_to_hash(self.info.graphics)
        self.data['physics'] = struct_to_hash(self.info.physics)
        self.data['frames_skipped'] = self.frames_skipped
        self.frames_skipped = 0
        #
        # Handle events
        #
        for event in pygame.event.get():
            if event.type == pygame.JOYBUTTONDOWN:
                self.data['button_pressed'] = event.dict['button']

        # if self.valid_config and self.i % 10 == 0:
        if self.valid_config:
            self.emit()

        # Only update the debugwindow if we haven't
        if self.show_debug_window:
            self.update_labels_in_debug_window()