|
| 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