content: import 'package:logging/logging.dart';
  import 'package:mqtt/src/mqtt_client_connector.dart';
  import 'package:mqtt/src/provider/mqtt_auth_provider.dart';
  import 'package:mqtt_client/mqtt_client.dart';
  
  final _log = Logger('MqttClientTMSOauthConnector');
  
  class MqttClientTMSOauthConnector implements MqttClientConnector {
    MqttClientTMSOauthConnector({required MqttAuthProvider mqttAuthProvider}) : _mqttAuthProvider = mqttAuthProvider;
  
    final MqttAuthProvider _mqttAuthProvider;
  
    @override
    Future<bool> connect(MqttClient client, String company, String train) async {
      _log.info('Connecting to TMS mqtt using oauth token');
  
      final sferaAuthToken = await _mqttAuthProvider.tmsToken(company: company, train: train, role: 'active');
      _log.info('Received TMS sfera token=${sferaAuthToken?.substring(0, 20)}');
  
      if (sferaAuthToken != null) {
        try {
          final mqttClientConnectionStatus = await client.connect('JWT', 'OPENID~AzureAD_IMTS~~$sferaAuthToken');
          _log.info('mqttClientConnectionStatus=$mqttClientConnectionStatus');
  
          if (mqttClientConnectionStatus?.state == MqttConnectionState.connected) {
            _log.info('Successfully connected to MQTT broker');
            return true;
          }
        } catch (e) {
          _log.severe('Exception during connect', e);
        }
      }
  
      _log.warning('Failed to connect to MQTT broker');
      return false;
    }
  }
  