Search in sources :

Example 6 with EcuDataItem

use of com.fr3ts0n.ecu.EcuDataItem in project AndrOBD by fr3ts0n.

the class Kw1281Prot method savePreset.

/**
 * save current preset data
 */
protected void savePreset() {
    Thread saveThd = new Thread() {

        @Override
        public void run() {
            EcuDataPv vid = (EcuDataPv) VidPvs.get(0);
            String fileName = vid.get(EcuDataPv.FID_VALUE).toString().trim() + ".prs";
            // notify about property change
            firePropertyChange(new PropertyChangeEvent(this, "preset", lastPresetName, fileName));
            // remember preset
            lastPresetName = fileName;
            // 1st VID is used as preset filename
            File pstFile = new File(fileName);
            // if file aleady exists, we don't save it
            if (!pstFile.exists()) {
                log.info("Save Preset: " + fileName);
                FileWriter wtr = null;
                try {
                    wtr = new FileWriter(pstFile);
                    // Loop through all known data grous
                    Iterator<Entry<Integer, Vector<EcuDataItem>>> it = knownGrpItems.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Integer, Vector<EcuDataItem>> itmSet = it.next();
                        // write group number
                        wtr.write(String.format("%02X\t", itmSet.getKey()));
                        // loop through all items within data group
                        Vector<EcuDataItem> itms = itmSet.getValue();
                        Iterator<EcuDataItem> itItm = itms.iterator();
                        while (itItm.hasNext()) {
                            EcuDataItem currItm = itItm.next();
                            // write PID of item
                            wtr.write(String.format("%02X\t", currItm.pid));
                        }
                        // finish data group entry
                        wtr.write("\n");
                    }
                    log.info("Preset saved: " + fileName);
                } catch (IOException ex) {
                    log.severe("SavePreset: " + ex.toString());
                } finally {
                    try {
                        wtr.close();
                    } catch (IOException ex) {
                        log.severe("SavePreset: " + ex.toString());
                    }
                }
            } else {
                log.info("Preset saving skipped: " + fileName);
            }
        }
    };
    // MIN priority to NOT disturb communication
    saveThd.setPriority(Thread.MIN_PRIORITY);
    saveThd.start();
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) FileWriter(java.io.FileWriter) IOException(java.io.IOException) EcuDataPv(com.fr3ts0n.ecu.EcuDataPv) Entry(java.util.Map.Entry) File(java.io.File) Vector(java.util.Vector) EcuDataItem(com.fr3ts0n.ecu.EcuDataItem)

Aggregations

EcuDataItem (com.fr3ts0n.ecu.EcuDataItem)6 EcuDataPv (com.fr3ts0n.ecu.EcuDataPv)4 PropertyChangeEvent (java.beans.PropertyChangeEvent)3 IOException (java.io.IOException)2 Vector (java.util.Vector)2 Conversion (com.fr3ts0n.ecu.Conversion)1 EcuCodeItem (com.fr3ts0n.ecu.EcuCodeItem)1 VagConversion (com.fr3ts0n.ecu.VagConversion)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1