|
| 1 | +package org.influxdb.example; |
| 2 | + |
| 3 | +import org.influxdb.InfluxDB; |
| 4 | +import org.influxdb.InfluxDBFactory; |
| 5 | +import org.influxdb.dto.QueryResult; |
| 6 | +import org.influxdb.dto.Query; |
| 7 | + |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author StrakarCe |
| 13 | + * @since 07/05/2021 |
| 14 | + * @version 1 |
| 15 | + */ |
| 16 | +public class Android { |
| 17 | + // put the address IP of your database |
| 18 | + String address = "http://192.168.1.75:8000/"; |
| 19 | + String dbName = "myDatabase"; |
| 20 | + String table = "SERIES"; |
| 21 | + QueryResult actual; |
| 22 | + Boolean flag = false; |
| 23 | + InfluxDB con; |
| 24 | + |
| 25 | + public Android() { |
| 26 | + super(); |
| 27 | + } |
| 28 | + public void queryExecute(final Query query) { |
| 29 | + Thread thread = new Thread(new Runnable() { |
| 30 | + |
| 31 | + @Override |
| 32 | + public void run() { |
| 33 | + try { |
| 34 | + //InfluxDB connector = InfluxDBFactory.connect(address); |
| 35 | + // if you want to open every time |
| 36 | + System.out.println("Send the query to the database ..."); |
| 37 | + // FOR A REAL APP CREATE A LOGGER ; |
| 38 | + List<QueryResult> results = new LinkedList<>(); |
| 39 | + actual = con.query(query); |
| 40 | + } catch (Exception e) { |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + flag = true; // For simplicity, I use a simple flag to know when the thread have finished |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + thread.start(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * It's to open the connexion with the database. |
| 52 | + * In my case I decide to open once, do many query and close. |
| 53 | + */ |
| 54 | + public void connexion() { |
| 55 | + con = InfluxDBFactory.connect(address); |
| 56 | + } |
| 57 | + /** |
| 58 | + * It's to close after my list of query. |
| 59 | + */ |
| 60 | + public void close() { |
| 61 | + con.close(); |
| 62 | + } |
| 63 | + /* |
| 64 | + * simple example of how you can create a query |
| 65 | + */ |
| 66 | + private void queryLauncher(final String query) { |
| 67 | + queryExecute(new Query(query, dbName)); |
| 68 | + while (!flag) { // ugly method to wait the thread |
| 69 | + System.out.println("Wait the thread"); |
| 70 | + } |
| 71 | + flag = false; |
| 72 | + } |
| 73 | + public String getEtat() { |
| 74 | + queryLauncher("select last(value) from PTEC"); |
| 75 | + return actual.getResults().get(0).getSeries().get(0).getValues().get(0).get(1).toString(); |
| 76 | + } |
| 77 | + public String getHC() { |
| 78 | + queryLauncher("SELECT last(value) FROM HCHC"); |
| 79 | + return actual.getResults().get(0).getSeries().get(0).getValues().get(0).get(1).toString(); |
| 80 | + } |
| 81 | + // ------------------------- Example when you want to use it ------------ |
| 82 | + /* |
| 83 | + Android test = new Android(); |
| 84 | + refresh.setOnClickListener(new View.OnClickListener() { |
| 85 | + @Override |
| 86 | + public void onClick(View v) { |
| 87 | + test.connexion(); |
| 88 | + etat2.setText(test.getEtat()); |
| 89 | + hc2.setText(test.getHC()); |
| 90 | + hp2.setText(test.getHP()); |
| 91 | + prix2.setText(test.getDepense()); |
| 92 | + percMens2.setText(test.getPercentageMensuel()); |
| 93 | + percTotal2.setText(test.getPercentageTotal()); |
| 94 | + test.close(); |
| 95 | + } |
| 96 | + }); |
| 97 | + */ |
| 98 | +} |
0 commit comments