/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.geotools.Accelerometer;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 *
 * @author BURKAY
 */
public class StreamFromFile extends Thread {

    public StreamFromFile(){
        
    }

    private void sendData(String data) throws Exception{

        DatagramSocket socket = new DatagramSocket();

        byte[] buf = new byte[256];
        buf = data.getBytes();
        InetAddress addr = InetAddress.getByName(ipAddress);
        DatagramPacket packet = new DatagramPacket(buf, buf.length, addr, portNumber);

        socket.send(packet);
        AccelerometerInterface.getjTextPane2().setText(
                line+"\n"+AccelerometerInterface.getjTextPane2().getText());
    }

    private void init(){
        ipAddress = AccelerometerInterface.getjTextField1().getText();
        portNumber = Integer.parseInt(AccelerometerInterface.getjTextField2().getText());
        fileName = AccelerometerInterface.getjTextField3().getText();

        try{
            in = new BufferedReader(new FileReader(fileName));
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private void readLine(){
        try {
            if ((line = in.readLine()) == null) {
                in.close();
                in = new BufferedReader(new FileReader(fileName));
                line = in.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        AccelerometerInterface.getjTextPane1().setText(
                line+"\n"+AccelerometerInterface.getjTextPane1().getText());
    }

    private void prepareStreamData(){
        String xval = line.substring(2, 6);
        String yval = line.substring(9, 13);
        String zval = line.substring(16, 20);
        Integer x = Integer.parseInt(xval, 16);
        Integer y = Integer.parseInt(yval, 16);
        Integer z = Integer.parseInt(zval, 16);
        if(x>32767) x = x-65536;
        if(y>32767) y = y-65536;
        if(z>32767) z = z-65536;
        x = (x+16)/256;
        y = (y+16)/256;
        z = (z+16)/256;
        Double zd = z*1.0;
        
        double xang = java.lang.Math.atan(x/zd);
        double yang = java.lang.Math.atan(y/zd);

        if(xang==Double.NaN) xang=0;
        if(yang==Double.NaN) yang=0;

        xang = (xang*180)/java.lang.Math.PI;
        yang = (yang*180)/java.lang.Math.PI;

        line = decForm.format(xang)+" | "+decForm.format(yang);
    }
    
    @Override
    public void run(){
        init();
        while(AccelerometerInterface.stream){
            try{
                readLine();
                prepareStreamData();
                sendData(line);
                sleep(980);
            } catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public static String ipAddress="192.168.254.255";
    public static int portNumber=5001;
    public static String fileName;
    public static String line;
    protected BufferedReader in = null;

    java.text.DecimalFormat decForm = new java.text.DecimalFormat("###.##");
}
