package source;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Radoslaf
 */
public class GpsStreamReader extends Thread {

    private DataHandler handler = null;
    private Navigator navi = null;

    // Konstruktor pro MIDlet Navigator
    public GpsStreamReader(String name, DataHandler handler, Navigator navi) {
        super(name);
        this.handler = handler;
        this.navi = navi;
    }
    // Promenne tridy
    private byte[] buf = new byte[256];
    public int len = 0;
    private String cache = null;
    private InputStream is = null;

    /* ---------------------------------------------
     *            Čtení dat z GPS
     * ------------------------------------------*/
    public void run() {
        this.dataRead();
    }

    private void dataRead() {
        this.is = handler.getIs();
        while (navi.b.getIsConnect() == true) {
            //  Sekvence ctení
            try {
                len = this.is.read(buf, 0, 256);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (len != -1) {
                cache = new String(buf, 0, len);
                handler.setCache(cache);
            }
            // Cekání na další data
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            cache = null;
        }
    }
}
