Alejandro Acuña
2024-07-30 65a64a81d30f00f1fffd5da6866850e1308e1135
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package art.servers.etdserver.protocols.diamond.files;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public final class DIAMOND_Subdirectory
{
 
    private static final int END[] = {3};
    private static final int NACK[] = {21};
    private static final int ACK[] = {6};
    private static final int CANCEL[] = {3};
    private DecimalFormat formatter = new DecimalFormat("00");
 
    public static final int ESTADO_NORMAL = 0;
    public static final int ESTADO_PENDIENTE_DESCARGA = 1;
    public static final int ESTADO_DESCARGANDO = 2;
    public static final int ESTADO_DESCARGA_CORRECTO = 3;
    public static final int ESTADO_DESCARGA_INCORRECTO = 4;
    public static final int ESTADO_PENDIENTE_BORRAR = 5;
 
 
    public String site;
    public int state = ESTADO_NORMAL;
    public int number;
    public int length;
    public int lanes;
    public int storemode;
    public boolean retrieved;
    public int startmonth;
    public int startday;
    public int startyear;
    public int starthour;
    public int startminute;
    public int stopmonth;
    public int stopday;
    public int stopyear;
    public int stophour;
    public int stopminute;
    public Calendar calendarStart;
    public Calendar calendarStop;
    public String timestampStart;
    public String timestampStop;
 
    // Datos de descarga
 
 
    public long descarga_timestampinicio = 0;
    public int descarga_totalbytes = 0;
    public int descarga_totalpaquetescorrectos = 0;
    public int descarga_totalpaqueteserroneos = 0;
 
 
    // Datos privados
 
    private boolean cancel = false;
 
 
 
 
 
 
     public boolean iguales (DIAMOND_Subdirectory subdirectorio)
     {
         try
         {
             if (subdirectorio.site.equalsIgnoreCase(site) == false) return false;
             if (subdirectorio.number != number) return false;
             if (subdirectorio.length != length) return false;
             if (subdirectorio.startyear != startyear) return false;
             if (subdirectorio.startday != startday) return false;
             if (subdirectorio.starthour != starthour) return false;
             if (subdirectorio.startminute != startminute) return false;
             if (subdirectorio.stopyear != stopyear) return false;
             if (subdirectorio.stopday != stopday) return false;
             if (subdirectorio.stophour != stophour) return false;
             if (subdirectorio.stopminute != stopminute) return false;
             return true;
         }
         catch (Exception e)
         {
         }
 
         return false;
     }
 
 
 
 
    public void cancel () throws Exception
    {
        this.cancel = true;
    }
 
 
 
 
 
//     public DIAMOND_File retrieveFileData (Serial serial) throws Exception
//     {
//         DIAMOND_File file = new DIAMOND_File(receiveFileData(serial));
//         return file;
//     }
//
//
//
//
//
//    
//    public int[] receiveFileData (Serial serial) throws Exception
//    {
//
//       try
//       {
//           descarga_timestampinicio = System.currentTimeMillis();
//           descarga_totalbytes = 0;
//           descarga_totalpaquetescorrectos = 0;
//           descarga_totalpaqueteserroneos = 0;
//           cancel = false;
//
//           estado = ESTADO_DESCARGANDO;
//           if (listener != null) listener.nuevoEstado(numero, ESTADO_DESCARGANDO);
//
//           try { serial.read("\n", 100); } catch (Exception e) {};
//           String comando = "R" + formatter.format(numero) + "\r";
//           String resultado =  DIAMOND.IntToString(serial.read(comando, 50));
//           if (resultado.indexOf(comando) < 0)
//           {
//              throw new Exception("Incorrect answer");
//           }
//
//           int longitud = Integer.parseInt(resultado.substring(comando.length()+1, comando.length()+7), 16);
//
//           // Recibir datos del fichero XModem
//
//           int[] fichero = new int[longitud];
//
//           int[] datos =  serial.read(NACK, 50);
//
//           do
//           {
//               if (datos.length == 132)
//               {
//                   System.arraycopy(datos, 3, fichero, descarga_totalbytes, Math.min(datos.length - 4, fichero.length - descarga_totalbytes));
//                   descarga_totalbytes = descarga_totalbytes + Math.min(datos.length - 4, fichero.length - descarga_totalbytes);
//                   descarga_totalpaquetescorrectos = descarga_totalpaquetescorrectos + 1;
//                   if (descarga_totalbytes < longitud) datos = serial.read(ACK, 50);
//                   if (listener != null) listener.leidos(numero,descarga_totalbytes, descarga_totalpaquetescorrectos);
//               }
//               else
//               {
//                   datos = serial.read(NACK, 50);
//                   descarga_totalpaqueteserroneos = descarga_totalpaqueteserroneos + 1;
//                   if (listener != null) listener.errores(numero,descarga_totalpaqueteserroneos);
//               }
//               
//               if (cancel == true)
//               {
//                   serial.send(CANCEL);
//                   descarga_totalbytes = 0;
//                   descarga_totalpaquetescorrectos = 0;
//                   descarga_totalpaqueteserroneos = 0;
//                   estado = ESTADO_DESCARGA_INCORRECTO;
//                   if (listener != null) listener.nuevoEstado(numero,ESTADO_DESCARGA_INCORRECTO);
//                   if (listener != null) listener.finalizado(numero);
//                   return new int[0];
//               }
//           }
//           while (descarga_totalbytes < longitud);
//
//           serial.send(END);
//           descarga_totalbytes = 0;
//           descarga_totalpaquetescorrectos = 0;
//           descarga_totalpaqueteserroneos = 0;
//           estado = ESTADO_DESCARGA_CORRECTO;
//           if (listener != null) listener.nuevoEstado(numero,ESTADO_DESCARGA_CORRECTO);
//           if (listener != null) listener.finalizado(numero);
//           return fichero;
//
//       }
//       catch (Exception e)
//       {
//           serial.send(END);
//           descarga_totalbytes = 0;
//           descarga_totalpaquetescorrectos = 0;
//           descarga_totalpaqueteserroneos = 0;
//           estado = ESTADO_DESCARGA_INCORRECTO;
//           if (listener != null) listener.nuevoEstado(numero, ESTADO_DESCARGA_INCORRECTO);
//           if (listener != null) listener.finalizado(numero);
//           throw e;
//       }
//
//
//    }
 
 
    
 
    // Opciones de descarga
 
    private SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    private DecimalFormat formato2 = new DecimalFormat("###,###,###");
 
 
 
 
    public String getLongitud()
    {
        return formato2.format(length);
    }
 
 
    public String getTiempoTotalDescarga()
    {
        return formato2.format((System.currentTimeMillis() - descarga_timestampinicio)/1000);
    }
 
 
    public String getRendimientoDescarga()
    {
        long segundos = (System.currentTimeMillis() - descarga_timestampinicio)/1000;
        if (segundos == 0) return "0";
        return formato2.format(descarga_totalbytes / segundos);
    }
 
 
 
    public String getProgreso()
    {
        return formato2.format(descarga_totalbytes) + " de " + formato2.format(length);
    }
 
 
 
 
}