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