| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
package tempcontroller.base; |
| 11 |
|
|
| 12 |
|
import java.io.BufferedInputStream; |
| 13 |
|
import java.io.BufferedOutputStream; |
| 14 |
|
import java.io.File; |
| 15 |
|
import java.io.FileInputStream; |
| 16 |
|
import java.io.FileOutputStream; |
| 17 |
|
import java.io.IOException; |
| 18 |
|
import java.io.ObjectInputStream; |
| 19 |
|
import java.io.ObjectOutputStream; |
| 20 |
|
import java.io.Serializable; |
| 21 |
|
import java.text.DecimalFormat; |
| 22 |
|
import java.util.regex.Matcher; |
| 23 |
|
import java.util.regex.Pattern; |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
1 |
public class Utilities { |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public static final long SEGUNDO = 1000; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
public static final long MINUTO = SEGUNDO * 60; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
public static final long HORA = MINUTO * 60; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private static Utilities singleton; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
public void gravaSerializable(final File f, final Serializable obj) |
| 57 |
|
throws IOException { |
| 58 |
1 |
ObjectOutputStream oos = null; |
| 59 |
|
try { |
| 60 |
1 |
oos = new ObjectOutputStream( |
| 61 |
|
new BufferedOutputStream(new FileOutputStream(f))); |
| 62 |
1 |
oos.writeObject(obj); |
| 63 |
|
} finally { |
| 64 |
1 |
if (oos != null) { |
| 65 |
1 |
oos.close(); |
| 66 |
|
} |
| 67 |
|
} |
| 68 |
1 |
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
public Serializable leSerializable(final File f) |
| 79 |
|
throws IOException, ClassNotFoundException { |
| 80 |
1 |
ObjectInputStream ois = null; |
| 81 |
1 |
Serializable toReturn = null; |
| 82 |
|
try { |
| 83 |
1 |
ois = new ObjectInputStream( |
| 84 |
|
new BufferedInputStream(new FileInputStream(f))); |
| 85 |
1 |
toReturn = (Serializable) ois.readObject(); |
| 86 |
1 |
ois.close(); |
| 87 |
|
} finally { |
| 88 |
1 |
if (ois != null) { |
| 89 |
1 |
ois.close(); |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
1 |
return toReturn; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
public static Utilities getUtilities() { |
| 102 |
6 |
synchronized (Utilities.class) { |
| 103 |
6 |
if (singleton == null) { |
| 104 |
1 |
singleton = new Utilities(); |
| 105 |
|
} |
| 106 |
6 |
} |
| 107 |
|
|
| 108 |
6 |
return singleton; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
1 |
private static Pattern patternTemperatura = |
| 112 |
|
Pattern.compile("(-?\\d*\\.?,?\\d*)?( +)?([CKF])?"); |
| 113 |
1 |
private static DecimalFormat decimalFormat = |
| 114 |
|
new DecimalFormat("###,###.#"); |
| 115 |
|
public static final int KELVIN_DELTA = 273; |
| 116 |
|
public static final int KELVIN_LIMIAR = 200; |
| 117 |
|
|
| 118 |
|
public double parseTemperatura(String s) { |
| 119 |
5 |
double toReturn = Double.NaN; |
| 120 |
|
|
| 121 |
|
try { |
| 122 |
5 |
Matcher m = patternTemperatura.matcher(s); |
| 123 |
|
|
| 124 |
5 |
if(m.matches()) { |
| 125 |
5 |
String parteDecimal = m.group(1); |
| 126 |
|
|
| 127 |
5 |
toReturn = decimalFormat.parse(s).doubleValue(); |
| 128 |
5 |
String indicadorEscala = m.group(3); |
| 129 |
|
|
| 130 |
5 |
if("C".equals(indicadorEscala)) { |
| 131 |
3 |
toReturn = toReturn + KELVIN_DELTA; |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
5 |
} catch(Exception e) {} |
| 135 |
|
|
| 136 |
5 |
return toReturn; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
public String formatTemperatura(final double temperatura) { |
| 140 |
4 |
if(Double.isNaN(temperatura)) { |
| 141 |
0 |
return "Ambiente"; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
4 |
StringBuilder builder = new StringBuilder(); |
| 145 |
4 |
double auxTemperatura = temperatura; |
| 146 |
4 |
String indicadorEscala = " K"; |
| 147 |
|
|
| 148 |
4 |
if(temperatura > KELVIN_LIMIAR) { |
| 149 |
1 |
auxTemperatura = temperatura - KELVIN_DELTA; |
| 150 |
1 |
indicadorEscala = " C"; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
4 |
builder.append(decimalFormat.format(auxTemperatura)); |
| 154 |
4 |
builder.append(indicadorEscala); |
| 155 |
|
|
| 156 |
4 |
return builder.toString(); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
1 |
private static Pattern patternTempo = |
| 160 |
|
Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)"); |
| 161 |
|
|
| 162 |
|
public long parseTempo(String s) { |
| 163 |
4 |
long toReturn = 0L; |
| 164 |
|
|
| 165 |
|
try { |
| 166 |
4 |
Matcher m = patternTempo.matcher(s); |
| 167 |
|
|
| 168 |
4 |
if(m.matches()) { |
| 169 |
4 |
String horasStr = m.group(1); |
| 170 |
4 |
String minutosStr = m.group(2); |
| 171 |
4 |
String segundosStr = m.group(3); |
| 172 |
4 |
int horas = Integer.parseInt(horasStr); |
| 173 |
4 |
int minutos = Integer.parseInt(minutosStr); |
| 174 |
4 |
int segundos = Integer.parseInt(segundosStr); |
| 175 |
|
|
| 176 |
4 |
toReturn = SEGUNDO * segundos + MINUTO * minutos + HORA * horas; |
| 177 |
|
} |
| 178 |
4 |
} catch(Exception e) {} |
| 179 |
|
|
| 180 |
4 |
return toReturn; |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
public String formatTempo(final long tempo) { |
| 184 |
5 |
if(tempo <= SEGUNDO) { |
| 185 |
1 |
return "00:00:00"; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
4 |
long tempoAux = tempo; |
| 189 |
4 |
long horas = tempoAux / HORA; |
| 190 |
4 |
tempoAux = tempoAux % HORA; |
| 191 |
4 |
long minutos = tempoAux / MINUTO; |
| 192 |
4 |
tempoAux = tempoAux % MINUTO; |
| 193 |
4 |
long segundos = tempoAux / SEGUNDO; |
| 194 |
|
|
| 195 |
4 |
StringBuilder builder = new StringBuilder(); |
| 196 |
|
|
| 197 |
4 |
if(horas >= 10) { |
| 198 |
0 |
builder.append(horas); |
| 199 |
|
} else { |
| 200 |
4 |
builder.append('0'); |
| 201 |
4 |
builder.append(horas); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
4 |
builder.append(':'); |
| 205 |
|
|
| 206 |
4 |
if(minutos >= 10) { |
| 207 |
2 |
builder.append(minutos); |
| 208 |
|
} else { |
| 209 |
2 |
builder.append('0'); |
| 210 |
2 |
builder.append(minutos); |
| 211 |
|
} |
| 212 |
|
|
| 213 |
4 |
builder.append(':'); |
| 214 |
|
|
| 215 |
4 |
if(segundos >= 10) { |
| 216 |
3 |
builder.append(segundos); |
| 217 |
|
} else { |
| 218 |
1 |
builder.append(0); |
| 219 |
1 |
builder.append(segundos); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
4 |
return builder.toString(); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
public double fromCelsiusToKelvin(double celsius) { |
| 226 |
0 |
return celsius + KELVIN_DELTA; |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
public double fromKelvinToCelsius(double kelvin) { |
| 230 |
0 |
return kelvin - KELVIN_DELTA; |
| 231 |
|
} |
| 232 |
|
} |