Bonjour,
Les données utilisées par le graph (C3.js) sont issues d'un csv.
Je parviens, grâce à joel.drigo à créer mes csv finaux mais sans l'heure, or, je souhaiterai que l'heure où s'est produite une activité apparaisse également dans mes fichiers csv. Comment pourrai je m'y prendre ?
Après import de java.time.LocalTime & java.time.format.DateTimeFormatter, j'ai tenté quelques modifications mais rien n'y a fait.
Je mets un exemple de fichier csv:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 package miseEnFormeDonneesCSV; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; import java.time.LocalDate; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; import com.opencsv.*; public class EntreeTraitement { public static String type; public static int cpt; public static void main(String[] args) throws FileNotFoundException, IOException{ for (int i = 0; i < 7; i++){ if (cpt == 0){type = "eclairages.csv";} if (cpt == 1){type = "volets.csv";} if (cpt == 2){type = "portes.csv";} if (cpt == 3){type = "plan_de_W.csv";} if (cpt == 4){type = "compteurs.csv";} if (cpt == 5){type = "capteurs.csv";} if (cpt == 6){type = "fenetres.csv";} //Lecture du fichier source String chemin = "/home/pi/KNX/logs/csv/" + type; File fichierCSV = new File(chemin); cpt++; // Ecriture via PrintStream dans un fichier String csv = "/home/pi/KNX/logs/data/" + type; File fichierOut = new File(csv); PrintStream out = new PrintStream(new FileOutputStream (fichierOut)); // on commence par lire le csv, on récupère une liste de tableau de String List<String[]> myEntries; try (CSVReader reader = new CSVReader(new FileReader(fichierCSV))) { myEntries = reader.readAll(); Map<LocalDate, Map<String, Integer>> map = myEntries.stream() // on parcourt la liste de String[] par stream //Map<LocalDate, Map<LocalTime, Map<String, Integer>>> map = myEntries.stream() // on parcourt la liste de String[] par stream .skip(1) // on passe la ligne de titres .map(Donnees::new) // on transforme chaque ligne en instance de Donnees .collect(Collectors.groupingBy(Donnees::getDate, Collectors.groupingBy(Donnees::getEquipement))) // on groupe par date, on obtient une Map<LocalDate, Map<String, List<Donnees>>> .entrySet().stream() // on reparcourt les entrées de la map Map<LocalDate, Map<String, List<Donnees>>>, pour la convertir en Map<LocalDate, Map<String, Integer>> .collect(Collectors.toMap(e -> e.getKey(), // on conserve la date comme clef e -> e.getValue().entrySet() // on transforme la Map<String, Donnees> en Map<String, Integer> pour n'avoir que les comptes .stream().collect(Collectors.toMap(ee -> ee.getKey(), // la clef est le nom de l'équipement ee -> ee.getValue().size())))); // la valeur le nombre de Donnees associés (la taille de la liste de Donnees) au nom d'équipement // on produit le résultat out.print("date,"); // on récupère tous les noms d'équipement, triés par ordre alphabétique (on utiliser un tri pour avoir un ordre cohérent entre titres et lignes de données) Set<String> equipements = map.entrySet().stream() .flatMap(e -> e.getValue().keySet().stream()) // on a une Map<Date, Map<String, et on veut un set de String, il faut donc remonter la liste de clef de la seconde map au niveau de la première, ce que flatMap permet .collect(Collectors.toCollection(TreeSet::new)); // le TreeSet pour trier dans l'ordre naturel (il suffit de passer un comparateur pour changer l'ordre) out.println(equipements.stream().collect(Collectors.joining(","))); // la liste des noms d'équipements, séparés par des virgules map.entrySet().stream().sorted(Map.Entry.comparingByKey()) // on tri par date (les clefs) .peek(e -> out.print(e.getKey().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ",")) // la clef c'est la date (on affiche donc la date, avec la virgule qui suit) //.peek(e -> out.print(e.getKey().format(DateTimeFormatter.ofPattern("HH:mm:ss")) + ",")) .forEach(e -> out.println(equipements.stream() // on parcourt les équipements pour afficher les colonnes dans le même ordre que les titres .map(eq -> e.getValue().getOrDefault(eq, 0)) // on récupère le compte correspondant à l'équipement dans la map (ou 0 s'il y en a aucun) .map(String::valueOf) // on a récupérer des Integer, on doit les transformer en String pour pouvoir utiliser Collectors.joining, pour afficher la liste des nombres séparés par des virgules .collect(Collectors.joining(",")))); // on affiche la liste out.close(); } catch (NumberFormatException e){ System.out.println("not a number"); } } } }
Voila si quelqu'un peut m'aider.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 idIndex,date,heure,participant,adresseParticipant,equipement,adresseBus,valeur 1,15-03-2017,09:07:26,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42C4 7,15-03-2017,09:07:47,Indefini,1.1.103,Consommation_Energie_Plaques,4/0/1,0000 16,15-03-2017,09:08:18,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 27,14-03-2017,09:08:51,Indefini,1.1.103,Consommation_Energie_Prises_Cuisines,4/0/2,0001 40,16-03-2017,09:09:22,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42C4 53,15-03-2017,09:10:01,Indefini,1.1.103,Consommation_Puissance_Prises_Cuisines,4/1/2,0000 56,15-03-2017,09:10:13,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 57,15-03-2017,09:10:13,Indefini,1.1.103,Consommation_Energie_Plaques,4/0/1,0000 75,15-03-2017,09:11:11,Indefini,1.1.103,Consommation_Energie_Prises_Cuisines,4/0/2,0001 77,15-03-2017,09:11:18,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42BE 92,15-03-2017,09:12:07,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 102,15-03-2017,09:12:38,Indefini,1.1.103,Consommation_Energie_Plaques,4/0/1,0000 111,15-03-2017,09:13:14,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42C0 113,15-03-2017,09:13:17,Indefini,1.1.103,Consommation_Puissance_Plaques,4/1/1,0000 121,15-03-2017,09:13:37,Indefini,1.1.103,Consommation_Energie_Prises_Cuisines,4/0/2,0001 128,15-03-2017,09:14:01,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 143,15-03-2017,09:14:54,Indefini,1.1.103,Consommation_Puissance_Prises_Cuisines,4/1/2,0000 146,15-03-2017,09:15:04,Indefini,1.1.103,Consommation_Energie_Plaques,4/0/1,0000 148,15-03-2017,09:15:06,Capteur_Zenio_Chambre_SDB,1.1.39,Consommation_Energie_Prises_Cuisines,4/0/2,10 149,15-03-2017,09:15:10,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42BE 166,15-03-2017,09:16:01,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 167,15-03-2017,09:16:03,Indefini,1.1.103,Consommation_Energie_Prises_Cuisines,4/0/2,0001 189,15-03-2017,09:17:06,Indefini,1.1.103,Consommation_Puissance_General,4/1/0,42C0 194,15-03-2017,09:17:29,Indefini,1.1.103,Consommation_Energie_Plaques,4/0/1,0000 204,15-03-2017,09:17:55,Indefini,1.1.103,Consommation_Energie_General,4/0/0,003C 210,15-03-2017,09:18:10,Indefini,1.1.103,Consommation_Puissance_Plaques,4/1/1,0000
Merci d'avance.
Partager