package br.usp.nds.agualastro.servico; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Properties; import javax.mail.Flags; import javax.mail.Flags.Flag; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; import javax.mail.search.FlagTerm; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.xml.bind.DatatypeConverter; import br.usp.nds.agualastro.modelo.Atualizador; import br.usp.nds.agualastro.modelo.Leitura; import br.usp.nds.agualastro.persistencia.Aquisicao; import br.usp.nds.agualastro.persistencia.LeituraSensor; import br.usp.nds.agualastro.persistencia.PersistenciaFacade; import br.usp.nds.agualastro.persistencia.Sensor; @Path("leituras") @Produces("text/xml") public class ServicoLeituras { public static void main(String[] args) { float t = 0.0f; System.out.println(t == 0 ? "1" : "0"); } @GET @Path("atualizar") public String atualizar() throws Exception { new Atualizador().atualizar(); return "OK"; } @POST @Path("registrar") public void registrar(@FormParam("data") String data, @FormParam("hora") String hora, @FormParam("latitude") float latitude, @FormParam("longitude") float longitude, @FormParam("temperatura") int temperatura, @FormParam("pressaoAtmosferica") int pressaoAtmosferica, @FormParam("condutividade") int condutividade, @FormParam("oxigenioDissolvido") int oxigenioDissolvido, @FormParam("turbidez") int turbidez, @FormParam("ph") int ph) throws Exception { new PersistenciaFacade().registrar(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(data + " " + hora), latitude, longitude, new int[] {temperatura, pressaoAtmosferica, condutividade, oxigenioDissolvido, turbidez, ph}, null); } @Path("porInstante") @GET public Retorno listarLeiturasPorAquisicao(@QueryParam("instante") String instante) throws Exception { return new Retorno(new PersistenciaFacade().listarLeiturasPorAquisicao(new SimpleDateFormat("yyyyMMddHHmmss").parse(instante))); } @Path("excel.csv") @GET @Produces("text/csv") public String gerarExcel(@QueryParam("inicio") String inicio, @QueryParam("fim") String fim, @QueryParam("navio") String navio) throws Exception { Date dInicio = null; if (inicio != null && !inicio.isEmpty()) { Calendar cInicio = Calendar.getInstance(); cInicio.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(inicio)); cInicio.set(Calendar.HOUR, 0); cInicio.set(Calendar.MINUTE, 0); cInicio.set(Calendar.SECOND, 0); cInicio.set(Calendar.MILLISECOND, 0); dInicio = cInicio.getTime(); } Date dFim = null; if (fim != null && !fim.isEmpty()) { Calendar cFim = Calendar.getInstance(); cFim.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(fim)); cFim.set(Calendar.HOUR, 23); cFim.set(Calendar.MINUTE, 59); cFim.set(Calendar.SECOND, 59); cFim.set(Calendar.MILLISECOND, 999); dFim = cFim.getTime(); } List ls = new PersistenciaFacade().listarLeituras(dInicio, dFim, navio); List leituras = new LinkedList(); for (LeituraSensor l : ls) { leituras.add(new Leitura().carregar(l)); } List sensores = new PersistenciaFacade().listarSensoresPorNavio(navio); Collections.sort(sensores, new Comparator() { public int compare(Sensor o1, Sensor o2) { return new Integer(o1.getCodigo()).compareTo(o2.getCodigo()); } }); StringBuilder sb = new StringBuilder(); sb.append("instante,latitude,longitude"); for (Sensor sensor : sensores) { sb.append(",\""); sb.append(sensor.getNome()); sb.append("\""); } sb.append("\n"); for (int i = 0; i + sensores.size() < leituras.size(); i += sensores.size()) { sb.append(leituras.get(i).getInstanteFormatado2()); sb.append(","); sb.append(leituras.get(i).getLatitude()); sb.append(","); sb.append(leituras.get(i).getLongitude()); for (int j = 0; j < sensores.size(); j++) { sb.append(","); sb.append(leituras.get(i + j).getValor()); } sb.append("\n"); } return sb.toString(); } @Path("leituras") @POST public Retorno listarLeituras(@FormParam("inicio") String inicio, @FormParam("fim") String fim, @FormParam("sensor") int sensor, @FormParam("navio") String navio, @FormParam("esconder") String esconder) throws Exception { Date dInicio = null; if (inicio != null && !inicio.isEmpty()) { Calendar cInicio = Calendar.getInstance(); cInicio.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(inicio)); cInicio.set(Calendar.HOUR, 0); cInicio.set(Calendar.MINUTE, 0); cInicio.set(Calendar.SECOND, 0); cInicio.set(Calendar.MILLISECOND, 0); dInicio = cInicio.getTime(); } Date dFim = null; if (fim != null && !fim.isEmpty()) { Calendar cFim = Calendar.getInstance(); cFim.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(fim)); cFim.set(Calendar.HOUR, 23); cFim.set(Calendar.MINUTE, 59); cFim.set(Calendar.SECOND, 59); cFim.set(Calendar.MILLISECOND, 999); dFim = cFim.getTime(); } List ls = new PersistenciaFacade().listarLeituras(sensor, dInicio, dFim, navio); List leituras = new LinkedList(); for (LeituraSensor l : ls) { if ("0".equals(esconder) || (!"0.0".equals(l.getAquisicao().getLatitude() + "") && !"0.0".equals(l.getAquisicao().getLongitude() + ""))) { leituras.add(new Leitura().carregar(l)); } } return new Retorno(leituras); } @Path("aquisicoes") @POST public Retorno listarAquisicoes(@FormParam("inicio") String inicio, @FormParam("fim") String fim, @FormParam("navio") String navio, @FormParam("esconder") String esconder) throws Exception { Date dInicio = null; if (inicio != null && !inicio.isEmpty()) { Calendar cInicio = Calendar.getInstance(); cInicio.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(inicio)); cInicio.set(Calendar.HOUR, 0); cInicio.set(Calendar.MINUTE, 0); cInicio.set(Calendar.SECOND, 0); cInicio.set(Calendar.MILLISECOND, 0); dInicio = cInicio.getTime(); } Date dFim = null; if (fim != null && !fim.isEmpty()) { Calendar cFim = Calendar.getInstance(); cFim.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(fim)); cFim.set(Calendar.HOUR, 23); cFim.set(Calendar.MINUTE, 59); cFim.set(Calendar.SECOND, 59); cFim.set(Calendar.MILLISECOND, 999); dFim = cFim.getTime(); } List as = new PersistenciaFacade().listarAquisicoes(navio, dInicio, dFim); ListIterator iterator = as.listIterator(); while (iterator.hasNext()) { Aquisicao a = iterator.next(); if ("1".equals(esconder) && a.getLatitude() == 0 && a.getLongitude() == 0) { iterator.remove(); } } return new Retorno(as); } @Path("sensores") @GET public Retorno listarSensores() { return new Retorno(new PersistenciaFacade().listarSensores()); } @Path("sensoresPorNavio") @GET public Retorno listarSensoresPorNavio(@QueryParam("navio") String emailNavio) { return new Retorno(new PersistenciaFacade().listarSensoresPorNavio(emailNavio)); } @Path("navios") @GET public Retorno listarNavios() { return new Retorno(new PersistenciaFacade().listarNavios()); } @Path("debug") @GET @Produces("text/html") public String debug() throws Exception { Properties properties = System.getProperties(); properties.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(properties, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", "gnmdmonitoring@gmail.com", "5@*B6$G&L6ye7IOh"); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); StringBuilder sb = new StringBuilder(); for (Message message : folder.search(new FlagTerm(new Flags(Flag.SEEN), false))) { if (message.getFrom() != null && message.getFrom().length > 0 && message.getFrom()[0].toString() != null && message.getFrom()[0].toString().endsWith("orbcomm.net")) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); message.writeTo(baos); BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); boolean processar = false; String codigo = ""; for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (processar) { codigo += line; } if (line.isEmpty()) { processar = true; } } reader.close(); byte[] bs = DatatypeConverter.parseBase64Binary(codigo); for (int i = 0; i < bs.length - 29; i += 30) { sb.append(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(message.getSentDate())); sb.append("\t"); sb.append(new BigInteger((bs[i + 0] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 1] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 2] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 3] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 4] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 5] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 6] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 7] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 8] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 9] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 10] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 11] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 12] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 13] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 14] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 15] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 16] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 17] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 18] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 19] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 20] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 21] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 22] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 23] + 128) + "").toString(16)); sb.append(" "); sb.append(new BigInteger((bs[i + 24] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 25] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 26] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 27] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 28] + 128) + "").toString(16)); sb.append(new BigInteger((bs[i + 29] + 128) + "").toString(16)); sb.append("\n"); } } } return "
" + sb.toString() + "
"; } }