mqtt method

void mqtt(
  1. Merchant merchant,
  2. Payment payment,
  3. Urls urls
)

Implementation

void mqtt(Merchant merchant, Payment payment, Urls urls) async {
  this.context = context;
  final client = MqttServerClient(urls.mqttBaseUrl, '');
  client.keepAlivePeriod = 10;
  client.logging(on: true);
  client.onDisconnected = _onDisconnected;
  client.onSubscribed = _onSubscribed;
  final connMess = MqttConnectMessage()
      .withClientIdentifier(
          new DateTime.now().millisecondsSinceEpoch.toString())
      .withWillQos(MqttQos.atLeastOnce);
  client.connectionMessage = connMess;
  try {
    await client.connect();
  } on Exception catch (e) {
    print('MQTT::client exception - $e');
    client.disconnect();
    throw e;
  }

  /// Check we are connected
  if (client.connectionStatus!.state == MqttConnectionState.connected) {
  } else {
    print(
        'MQTT::ERROR Mosquitto client connection failed - disconnecting, state is ${client.connectionStatus!.state}');
    //throw error failed to connect to mqtt
    client.disconnect();
    throw 'Could not connect to mqtt client';
  }

  client.published!.listen((MqttPublishMessage message) {
    print("MQTT: MESSAGE RECEIVED");
    Map<String, dynamic> mqttPayload = json.decode(
        MqttPublishPayload.bytesToStringAsString(message.payload.message));
    client.disconnect();
    if (mqttPayload['responseCode'] == "00" ||
        mqttPayload['responseCode'] == "0") {
      transactionSuccessfullCallback(mqttPayload);
    } else {
      transactionFailureCallback(mqttPayload);
    }
  });
  var transactionTopic = "merchant_portal/" +
      merchant.merchantId +
      "/" +
      payment.transactionReference;
  client.subscribe(transactionTopic, MqttQos.atMostOnce);
}