Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 24, 2025

Refactor emulator implementations to eliminate code duplication by extracting common logic into abstract base classes.

Changes

Created MdnsEmulator base class

  • TCP server lifecycle (bind, listen, close)
  • mDNS service registration/unregistration
  • Network interface discovery
  • Socket write operations and hex/byte conversion utilities

Created BluetoothEmulator base class

  • BLE peripheral manager lifecycle
  • Connection state management and event subscriptions
  • Android permission handling
  • Service/characteristic management and advertisement

Refactored 5 emulator classes

  • FtmsMdnsEmulator → extends MdnsEmulator
  • OpenBikeControlMdnsEmulator → extends MdnsEmulator
  • ZwiftEmulator → extends BluetoothEmulator
  • OpenBikeControlBluetoothEmulator → extends BluetoothEmulator
  • RemotePairing → extends BluetoothEmulator

Example

Before:

class FtmsMdnsEmulator extends TrainerConnection {
  ServerSocket? _tcpServer;
  Registration? _mdnsRegistration;
  
  Future<InternetAddress?> _findLocalIP() async {
    final interfaces = await NetworkInterface.list();
    // ... 15 lines of interface discovery
  }
  
  Future<void> _createTcpServer() async {
    _tcpServer = await ServerSocket.bind(/* ... */);
    // ... 10 lines of server setup
  }
}

After:

class FtmsMdnsEmulator extends MdnsEmulator {
  @override
  Future<void> startServer() async {
    final localIP = await findLocalIP();  // Inherited
    await createTcpServer(36867);         // Inherited
    await registerMdnsService(/* ... */); // Inherited
  }
}

Impact

  • ~293 lines of duplicate code eliminated
  • Fixed missing await keywords in async notification calls
  • Consistent patterns across all emulator types
Original prompt

use a base class "MdnsEmulator" for FtmsMdnsEmulator and OpenBikeControlMdnsEmulator, and put the shared logic to that base class

do the same with ZwiftEmulator and OpenBikeControlBluetoothEmulator and RemotePairing in a base class "BluetoothEmulator"


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: jonasbark <1151304+jonasbark@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor to introduce base classes for MDNS and Bluetooth emulators Extract base classes MdnsEmulator and BluetoothEmulator Dec 24, 2025
Copilot AI requested a review from jonasbark December 24, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants