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.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,SnapShot ss){ Date d = new Date(((long)ss.getTimestamp())*1000); Calendar c = Calendar.getInstance(); c.setTime(d); filename = filename .replaceAll("\\%S", String.format("%d", ss.getDeviceSerial())) .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)))); 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(){ Hashtable hash = new Hashtable(); for (SnapShot ss: PulsCounterApplication.getApplication().getDatabase().loadSnapshots(0)) { String fn = calculateFileName(fileName, ss); if ((triggerType==TriggerType.ALL)||(ss.getTriggerType()==triggerType)){ if (triggerSource.equals(-1) || triggerSource.equals(ss.getSource())){ 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(ss.getCSVRecord(extended)); } } } for (String fn: hash.keySet()){ if (recordDelta){ CSV csv = hash.get(fn); if (!csv.getRecords().isEmpty()){ Iterator iter = csv.getRecords().iterator(); iter.next(); CSVRecord n = iter.next(); Integer[] vals = new Integer[32]; for (int i=0;i<32;i++){ vals[i] = n.getIntegerValue(extended ? i + 4 : i + 1 ); } while (iter.hasNext()){ n = iter.next(); for (int i=0;i<32;i++){ Integer v = n.getIntegerValue(extended ? i + 4 : i + 1 ); n.setValue(extended ? i + 4 : i + 1 , v - vals[i]); vals[i] = v; } } csv.getRecords().remove(1); } } hash.get(fn).saveToFile(new File(path,fn)); } } }