Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11550a4

Browse files
committed
Cliente en Java compila y se ejecuta, aún no ha sido probado en comunicación
1 parent 1d9570b commit 11550a4

8 files changed

+210
-320
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#*.jar
88
*.war
99
*.ear
10-
10+
*.graphml
1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*

ControlThread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public ControlThread(Window _gui, Socket _socket, double _setpoint, int _threadI
2121
this.setpoint = _setpoint;
2222
this.gui = _gui;
2323
this.threadId = _threadId;
24-
try
24+
try
2525
{
2626
this.out = new DataOutputStream(this.socket.getOutputStream());
2727
this.in = new DataInputStream(this.socket.getInputStream());
2828
out.flush();
29-
}
29+
}
3030
catch (IOException e)
3131
{
3232
System.out.println("Error obteniendo los streams de un cliente controlador");

Main.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33

44
public class Main implements ActionListener
55
{
6-
private static ThreadServer myThread;
6+
private static ThreadClient myThread;
7+
private static boolean init_client;
78
public static void main(String []args)
89
{
910
Window root = new Window();
11+
init_client = true;
1012
root.btn_server_start.addActionListener(new Main());
1113
root.btn_server_stop.addActionListener(new Main());
12-
myThread = new ThreadServer(root);
14+
myThread = new ThreadClient(root);
1315
}
1416

1517
public void actionPerformed(ActionEvent e)
1618
{
1719
if(e.getActionCommand().equals("start"))
1820
{
19-
System.out.println("Iniciando el Cliente");
20-
myThread.start();
21+
if(init_client)
22+
{
23+
System.out.println("Iniciando el Cliente");
24+
myThread.start();
25+
init_client = false;
26+
}
27+
else
28+
{
29+
System.out.println("Enviando nuevos valores");
30+
myThread.dataFlag = 1;
31+
}
2132
}
2233
else if(e.getActionCommand().equals("stop"))
2334
{
2435
System.out.println("Finalizando el Cliente");
2536
myThread.terminate();
26-
2737
}
28-
29-
}
38+
}
3039
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# export CLASSPATH=$CLASSPATH:./lib/jfreechart-1.0.19.jar:./lib/jcommon-1.0.23.jar
44

55

6-
SRC = Main.java Window.java ThreadServer.java FileManager.java ControlThread.java
6+
SRC = Main.java Window.java ThreadClient.java FileManager.java ControlThread.java
77

88
all: ${SRC}
99
javac $^

ThreadClient.java

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import java.io.DataInputStream;
2+
import java.io.DataOutputStream;
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
import javax.swing.JFileChooser;
8+
9+
10+
public class ThreadClient extends Thread
11+
{
12+
private final int SERVER_PORT = 9090;
13+
public int dataFlag;
14+
public int finishFlag;
15+
private Window gui;
16+
private String SERVER_IP;
17+
private Socket socket;
18+
private DataOutputStream out;
19+
private DataInputStream in;
20+
private volatile boolean alive;
21+
22+
23+
// Parámetros de control
24+
private double setpoint_temp;
25+
private double setpoint_flow;
26+
private double u_temp;
27+
private double u_flow;
28+
private double out_temp;
29+
private double out_flow;
30+
private double time_value;
31+
32+
33+
public ThreadClient(Window _gui)
34+
{
35+
this.gui = _gui;
36+
}//ThreadServer
37+
38+
public void terminate()
39+
{
40+
this.finishFlag = 1;
41+
System.out.format("La cantidad de elementos es: %d\n", this.gui.output_I.getItemCount());
42+
43+
// option to save the captured data
44+
double[][] logged_signal_I = this.gui.output_I.toArray();
45+
double[][] control_signal_I = this.gui.input_I.toArray();
46+
double[][] setpoint_signal_I = this.gui.setpoint_I.toArray();
47+
double[][] logged_signal_II = this.gui.output_II.toArray();
48+
double[][] control_signal_II = this.gui.input_II.toArray();
49+
double[][] setpoint_signal_II = this.gui.setpoint_II.toArray();
50+
51+
// Dialogo para seleccionar donde almacenar el archivo
52+
JFileChooser fileChooser = new JFileChooser();
53+
fileChooser.setDialogTitle("Indicar el archivo a guardar");
54+
int userSelection = fileChooser.showSaveDialog(this.gui);
55+
56+
if (userSelection == JFileChooser.APPROVE_OPTION)
57+
{
58+
File fileToSave = fileChooser.getSelectedFile();
59+
String filePath = fileToSave.getAbsolutePath();
60+
FileManager fileSaving = new FileManager(filePath.concat("_PID_I"));
61+
fileSaving.save_pid_signals(setpoint_signal_I, logged_signal_I, control_signal_I);
62+
filePath = fileToSave.getAbsolutePath();
63+
fileSaving = new FileManager(filePath.concat("_PID_II"));
64+
fileSaving.save_pid_signals(setpoint_signal_II, logged_signal_II, control_signal_II);
65+
System.out.println("Archivo guardado");
66+
}
67+
else
68+
{
69+
System.out.println("Datos descartados");
70+
}
71+
72+
}//terminate
73+
74+
public void run()
75+
{
76+
this.alive = true;
77+
this.dataFlag = 1;
78+
this.finishFlag = 0;
79+
80+
// Crear cliente y establecer conexión
81+
try
82+
{
83+
this.SERVER_IP = this.gui.txt_server_ip.getText();
84+
socket = new Socket(SERVER_IP, SERVER_PORT);
85+
out = new DataOutputStream(socket.getOutputStream());
86+
in = new DataInputStream(socket.getInputStream());
87+
}
88+
catch(IOException e)
89+
{
90+
System.out.println("No se pudo establecer la conexión con el servidor");
91+
e.printStackTrace();
92+
this.alive = false;
93+
}
94+
95+
96+
while(this.alive)
97+
{
98+
// Enviar Finish y Data Flags
99+
try
100+
{
101+
this.out.writeInt(finishFlag);
102+
this.out.writeInt(dataFlag);
103+
}catch(IOException e)
104+
{
105+
System.out.println("Error enviando la señalización al servidor");
106+
e.printStackTrace();
107+
this.alive = false;
108+
}
109+
110+
111+
if(finishFlag == 1)
112+
{
113+
// Terminar todo el hilo adecuadamente (cerrar la conexión)
114+
System.out.println("Cerrando la conexión");
115+
this.alive = false;
116+
}
117+
else if(dataFlag == 1)
118+
{
119+
// Capturar parámetros de usuario
120+
setpoint_temp = Double.parseDouble(this.gui.txt_setpoint_temp.getText());
121+
setpoint_flow = Double.parseDouble(this.gui.txt_setpoint_flow.getText());
122+
123+
// Enviar valores de los nuevos setpoints para los controladores
124+
try
125+
{
126+
this.out.writeDouble(setpoint_temp);
127+
this.out.writeDouble(setpoint_flow);
128+
}
129+
catch(IOException e)
130+
{
131+
System.out.println("Error enviando valores de setpoint");
132+
e.printStackTrace();
133+
this.alive = false;
134+
}
135+
dataFlag = 0;
136+
}
137+
138+
// Lectura de valores provenientes de la planta
139+
if(finishFlag == 0)
140+
{
141+
try
142+
{
143+
this.u_temp = this.in.readDouble();
144+
this.u_flow = this.in.readDouble();
145+
this.out_temp = this.in.readDouble();
146+
this.out_flow = this.in.readDouble();
147+
this.time_value = this.in.readDouble();
148+
} catch (IOException e)
149+
{
150+
System.out.println("No se pudo hacer la lectura de la señal o la transferencia del finish");
151+
e.printStackTrace();
152+
this.alive = false;
153+
}
154+
}
155+
// Actualizar gráficos con los nuevos valores
156+
this.UpdateChart();
157+
}
158+
try
159+
{
160+
System.out.println("Esperando para cerrar los sockets");
161+
Thread.sleep(4000);
162+
} catch (InterruptedException e1)
163+
{
164+
System.out.println("Error esperando para cerrar los sockets");
165+
e1.printStackTrace();
166+
}
167+
try
168+
{
169+
this.out.close();
170+
this.in.close();
171+
this.socket.close();
172+
} catch (IOException e)
173+
{
174+
System.out.println("No se pudo cerrar el socket al dejar el hilo");
175+
e.printStackTrace();
176+
}
177+
System.out.println("Socket principal cerrado exitosamente & hilo terminado");
178+
}// void run
179+
180+
public void UpdateChart()
181+
{
182+
this.gui.output_I.add(time_value, this.out_temp);
183+
this.gui.input_I.add(time_value, this.u_temp);
184+
this.gui.setpoint_I.add(time_value, this.setpoint_temp);
185+
this.gui.output_I.add(time_value, this.out_flow);
186+
this.gui.input_I.add(time_value, this.u_flow);
187+
this.gui.setpoint_I.add(time_value, this.setpoint_flow);
188+
}//UpdateChart
189+
}

0 commit comments

Comments
 (0)