org.hwo.pulscounter/src/org/hwo/pulscounter/ExportSetting.java

202 lines
6.0 KiB
Java

package org.hwo.pulscounter;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import org.hwo.ObjectTable;
import org.hwo.ObjectTable.ObjectSet;
import org.hwo.configuration.ConfigurableAttribute;
import org.hwo.configuration.ConfigurableObject;
import org.hwo.csv.CSV;
import org.hwo.csv.CSVRecord;
import org.hwo.interactiveobjects.InteractiveObject;
import org.hwo.interactiveobjects.ObjectEditorUI;
import org.hwo.models.TableMapper.TableColumn;
import org.hwo.pulscounter.ui.ExportSettingsEditorDialog;
import static org.hwo.logging.Logging.*;
import static org.hwo.logging.LogLevel.*;
@ObjectEditorUI(editor=ExportSettingsEditorDialog.class)
@ConfigurableObject
public class ExportSetting {
@ConfigurableAttribute
private String name;
@ConfigurableAttribute
private TriggerType triggerType;
@ConfigurableAttribute
private Integer triggerSource;
@ConfigurableAttribute
private String fileName;
@ConfigurableAttribute
private String path;
@ConfigurableAttribute
private Boolean extended;
@ConfigurableAttribute
private Boolean autostart;
@ConfigurableAttribute
private Boolean recordDelta;
private SimpleDateFormat dateFormat;
public ExportSetting() {
this.dateFormat = new SimpleDateFormat();
this.name = "unbenannt";
this.path = ".";
this.fileName = "synololog.csv";
this.triggerType = triggerType.ALL;
this.triggerSource = 0;
this.extended = false;
this.autostart = false;
this.recordDelta = false;
}
@TableColumn(label="Benennung",firstColumn=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@TableColumn(label="Auslöser")
public TriggerType getTriggerType() {
return triggerType;
}
public void setTriggerType(TriggerType triggerType) {
this.triggerType = triggerType;
}
@TableColumn(label="Dateiname")
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@TableColumn(label="Verzeichnis")
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Integer getTriggerSource() {
return triggerSource;
}
public void setTriggerSource(Integer triggerSource) {
this.triggerSource = triggerSource;
}
public Boolean getExtended() {
return extended;
}
public void setExtended(Boolean extended) {
this.extended = extended;
}
@TableColumn(label="Autostart",after="Auslöser")
public Boolean getAutostart() {
return autostart;
}
public void setAutostart(Boolean autostart) {
this.autostart = autostart;
}
public Boolean getRecordDelta() {
return recordDelta;
}
public void setRecordDelta(Boolean recordDelta) {
this.recordDelta = recordDelta;
}
private String calculateFileName(String filename, ObjectSet row, int deviceSerial){
Date d = new Date(((long)row.getInteger(0))*1000);
Calendar c = Calendar.getInstance();
c.setTime(d);
filename = filename
.replaceAll("\\%S", String.format("%d", deviceSerial))
.replaceAll("\\%Y", String.format("%04d", new Integer(c.get(Calendar.YEAR))))
.replaceAll("\\%M", String.format("%02d", new Integer(c.get(Calendar.MONTH)+1)))
.replaceAll("\\%D", String.format("%02d", new Integer(c.get(Calendar.DAY_OF_MONTH))))
.replaceAll("\\%h", String.format("%02d", new Integer(c.get(Calendar.HOUR))))
.replaceAll("\\%m", String.format("%02d", new Integer(c.get(Calendar.MINUTE))))
.replaceAll("\\%s", String.format("%02d", new Integer(c.get(Calendar.SECOND))));
// log(DEBUG, "exportFileName for Snapshot %s from [%s: %04d-%02d-%02d %02d:%02d:%02d] is %s", ss, ss.getDeviceSerial(), c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND),filename);
return filename;
}
public void export(){
for (int deviceSerial: PulsCounterApplication.getApplication().getDatabase().getKnownDevices()){
ObjectTable ot = PulsCounterApplication.getApplication().getDatabase().getSnapshotsAsTable(deviceSerial);
if (recordDelta){
PulsCounterApplication.getApplication().getDatabase().calulateTableDeltas(ot);
}
Hashtable<String, CSV> hash = new Hashtable<String, CSV>();
int[] simpleSelection = new int[]{
1,
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
};
for (ObjectSet row: ot.getRows()){
String fn = calculateFileName(fileName, row, deviceSerial);
if ((triggerType==TriggerType.ALL)||(triggerType.getValue().equals(row.getInteger(2)))){
if (triggerSource.equals(-1) || triggerSource.equals( row.getInteger(3) )){
if (!hash.containsKey(fn)){
hash.put(fn, new CSV());
if (extended){
hash.get(fn).getRecords().add(new CSVRecord(new Object[]{
"Zeitstempel", "Datum/Zeit", "Trigger", "Quelle",
"CH0","CH1","CH2","CH3","CH4","CH5","CH6","CH7",
"CH8","CH9","CH10","CH11","CH12","CH13","CH14","CH15",
"CH16","CH17","CH18","CH19","CH20","CH21","CH22","CH23",
"CH24","CH25","CH26","CH27","CH28","CH29","CH30","CH31",
"AN0","AN1","AN2","AN3","AN4","AN5","AN6","AN7"}));
} else {
hash.get(fn).getRecords().add(new CSVRecord(new Object[]{
"Datum/Zeit",
"CH0","CH1","CH2","CH3","CH4","CH5","CH6","CH7",
"CH8","CH9","CH10","CH11","CH12","CH13","CH14","CH15",
"CH16","CH17","CH18","CH19","CH20","CH21","CH22","CH23",
"CH24","CH25","CH26","CH27","CH28","CH29","CH30","CH31",
"AN0","AN1","AN2","AN3","AN4","AN5","AN6","AN7"}));
}
}
hash.get(fn).getRecords().add(new CSVRecord( extended ? row.getValues() : row.selectColums(simpleSelection)));
}
}
}
for (String fn: hash.keySet()){
hash.get(fn).saveToFile(new File(path,fn));
}
}
}
}