package client;
import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JPanel;
import java.awt.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
/**
*
* @author fojtjan
*/
public class Client_test extends JPanel {
/**
* @param args the command line arguments
*/
// int sample = 0;
int[] sample = new int[1000];
float[] seconds = new float[1000];
short[] channel1 = new short[1000];
short[] channel2 = new short[1000];
float[] channel3 = new float[1000];
float[] channel4 = new float[1000];
byte dataType = 0x02;
int lastSample = 0;
int[] averages = new int[1000];
public Client_test() {
try {
DataInputStream input = null;
DataOutputStream output = null;
Socket client = new Socket("localhost", 1234);
input = new DataInputStream(client.getInputStream());
output = new DataOutputStream(client.getOutputStream());
output.writeByte(dataType);
int counter = 0;
switch (dataType) {
case 0x01://načtení RAW dat
counter = 0;
while (counter < 1000) {
sample[counter] = Integer.reverseBytes(input.readInt());
channel1[counter] = Short.reverseBytes(input.readShort());
channel2[counter] = Short.reverseBytes(input.readShort());
lastSample = counter;
//System.out.println(sample[counter] + " " + channel1[counter] + " " + channel2[counter]);
counter++;
}
//moving average, plovoucí průměr
int avgWin = 20;//800 když chci baseline
int sum = 0;
//1. kanál
for (int i = 0; i < lastSample - avgWin; i++) {//nesmí být delší než j; proto - avgWin
//int average = 0;
for (int j = i; j < i + avgWin; j++) {
sum += channel1[j];
}
float average = sum / avgWin;
averages[i] = Math.round(average);
sum = 0;
//System.out.println("Prumer 1.kanalu: " + averages[i]);
}
//druhý kanál
// int sum = 0;
// int[] averages = new int[1000];
// for(int i = 0; i < lastSample - avgWin; i++)
// {
// //int average = 0;
// for(int j = i; j < i + avgWin; j++)
// {
// sum += channel2[j];
// }
// float average = sum/avgWin;
// averages2[i]= Math.round(average);
// sum = 0;
// System.out.println("Prumer 2.kanalu: " + averages2[i]);
// }
break;
case 0x02://Human data
counter = 0;
while (counter < 1000) {
seconds[counter] = Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
channel3[counter] = Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
channel4[counter] = input.readFloat();//Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
lastSample = counter;
System.out.println(seconds[counter] + " " + channel3[counter] + " " + channel4[counter]);
counter++;
}
break;
}
client.close();
} catch (UnknownHostException ex) {
Logger.getLogger(Client_test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client_test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void paintComponent(Graphics g) {//vykreslení
super.paintComponent(g);
Graphics2D graph = (Graphics2D) g;
Dimension size = getSize();
int w = size.width;
int h = size.height;
switch (dataType) {
case 0x01:
graph.setColor(Color.red);//kanál1
for (int i = 0; i < lastSample; i++) {
int x = sample[i];
int y = ((channel1[i]) + 500) / 4;
int x1 = sample[i + 1];
int y1 = ((channel1[i + 1]) + 500) / 4;
graph.drawLine(x, y, x1, y1);
}
graph.setColor(Color.black); //moving average + baseline(avgWin se zvětší na cca počet vzorků
for (int i = 0; i < lastSample; i++) {
int x = sample[i];
int y = ((averages[i]) + 500) / 4;
int x1 = sample[i + 1];
int y1 = ((averages[i + 1]) + 500) / 4;
graph.drawLine(x, y, x1, y1);
}
graph.setColor(Color.blue);
for (int i = 0; i < lastSample; i++) {//druhý kanál + přidat moving průměr ;-) stejně jako o 3 řádky výš
int x = sample[i];
int y = ((channel2[i]) + 2000) / 4;
int x1 = sample[i + 1];
int y1 = ((channel2[i + 1]) + 2000) / 4;
graph.drawLine(x, y, x1, y1);
}
break;
case 0x02://vykreslení human dat
graph.setColor(Color.red);
for (int i = 0; i < lastSample; i++) {
int x = Math.round(seconds[i] * 500);
int y = (int) (Math.round(channel3[i] * 200.0) + 200);//+ posunutí; * roztažení
int x1 = Math.round(seconds[i + 1] * 500);
int y1 = (int) (Math.round(channel3[i + 1] * 200.0) + 200);
graph.drawLine(i, y, i + 1, y1);
}
graph.setColor(Color.blue);
for (int i = 0; i < lastSample; i++) {
int x = Math.round(seconds[i] * 500);
int y = (int) ((Math.round(channel4[i] * 200.0)) + 500);
int x1 = Math.round(seconds[i + 1] * 500);
int y1 = (int) ((Math.round(channel4[i + 1] * 200)) + 500);
graph.drawLine(i, y, i + 1, y1);
}
break;
}
}
public static void main(String[] args) {
// TODO code application logic here
Client_test client = new Client_test();
JFrame frame = new JFrame("Okno");//definice okna pro vykreslení
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.add(client);
frame.setVisible(true);
}
}