influxdata / influxdb-client-dart

InfluxDB (v2+) Client Library for Dart and Flutter
MIT License
25 stars 9 forks source link

Connect to two diferrent IPs #131

Closed AFazakas-UAH closed 2 months ago

AFazakas-UAH commented 1 year ago

Hi. I a developing an APP with flutter, in this case I want to check if the connection to the first IP has been made correctly, make the query correctly; otherwise try to make the connection to the second IP. Next I leave you the code that I have implemented and it works correctly with the first IP, however when I select the second IP it does not give any error, but the query is not done, because it returns the empty variables.


import '../Cuidador/screen_Paciente.dart';

class InfluxDBService {
  late InfluxDBClient client;

  // var url = 'http://192.168.72.11:8086'; // PC POM
  var url = 'http://212.256.20.330:8086'; // PC POM

  var token =
      'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  var org = 'XXXXXXXXXXXXXXXXXX';
  var bucket = 'XXXXXXXXXXXXXXXX';
  InfluxDBService() {
    //PC POM
    client = InfluxDBClient(
      url: url, // PC POM
      token: token,
      org: org,
      bucket: bucket,
      followRedirects: true,
      maxRedirects: 5,
      debug: true,
    );
  }

  Future GetSteps(COD_PACIENTE_WEARABLE, startDateString, endDateString) async {
    var startDate = DateTime.parse(startDateString);
    var endDate = DateTime.parse(endDateString);
    var queryService = client.getQueryService();
    var StepsQuery = '''
      from(bucket: "${bucket}")
        |> range(start: ${startDate.toIso8601String()}, stop: ${endDate.toIso8601String()})
        |> filter(fn: (r) => r["_measurement"] == "${COD_PACIENTE_WEARABLE}")
        |> filter(fn: (r) => r["Data_type"] == "MISC")
        |> filter(fn: (r) => r["Sensor_type"] == "Movement")
        |> filter(fn: (r) => r["_field"] == "Steps")     
    ''';
    var StepsResponse = await queryService.query(StepsQuery);
    // Convertir el Stream en una lista
    var StepsRecords = await StepsResponse.toList();
    client.close();
    // Procesa los resultados y devuelve el nivel de la batería
    // Procesar los resultados
    var StepList = <StepData>[];
    for (var record in StepsRecords) {
      var Step = record["_value"];
      var timestampString = record["_time"];
      var timestamp = DateTime.parse(timestampString).toLocal();

      StepList.add(StepData(Step, timestamp));
    }
    StepList.sort((a, b) => b.timestamp.compareTo(a.timestamp));

    return StepList;
  }
}
powersj commented 1 year ago

Hi @AFazakas-UAH,

Not sure I am following your question.

Are you asking to write code that will fallback to the second URL in the event there is an error? Or attempt to write to both URLs?

In the query example, there is code that demonstrates how to capture an error: https://github.com/influxdata/influxdb-client-dart/blob/main/example/query_example.dart#L46-L47 You could use that error if it is not empty/null to then switch between URLs.

Let us know, thanks!

AFazakas-UAH commented 1 year ago

Sorry it took me so long to respond. Yes, I am trying to fallback to second URl in case that the query is empty, but I have a problem, the timeout of the query It is too long, and I can't modify It

srebhan commented 2 months ago

@AFazakas-UAH I think you need to implement the fallback yourself, as @powersj said. You can find an example under the link he posted.