--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 10:02:26 800 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 18:26:09 805 @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.ZipEntry; @@ -20,6 +21,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import dk.thoerup.traininfoservice.banedk.TimeoutMap; + public class RequestPlotter extends HttpServlet { private static final long serialVersionUID = 1L; @@ -27,6 +30,8 @@ static final String KML = "application/vnd.google-earth.kml"; static final String KMZ = "application/vnd.google-earth.kmz"; + + Map cache = new TimeoutMap(2*60*1000); class RequestPosition { public String ip; @@ -100,11 +105,32 @@ sb.append( " \n\n" ); - - + sb.append( " \n\n" ); + +/* + String overlay = + " " + + " Absolute Positioning: Top left" + + " Absolute Positioning: Top left" + + " 1" + + " " + + " http://code.google.com/apis/kml/documentation/top_left.jpg" + + " " + + " " + + " " + + " " + + " " + + " " ; + + sb.append(overlay); + */ + + final String STYLE_GREEN = "green"; + final String STYLE_YELLOW = "yellow"; + final String STYLE_RED = "red"; + String oldstyle = null; Date now = new Date(); for(RequestPosition current : list) { @@ -112,21 +138,30 @@ long timediff = now.getTime() - current.time.getTime(); if ( timediff < (3*60*60*1000) ) { - style = "#red"; + style = STYLE_RED; } else if ( timediff < (24*60*60*1000)) { - style = "#yellow"; + style = STYLE_YELLOW; } else { - style = "#green"; + style = STYLE_GREEN; + } + + if ( !style.equals(oldstyle) ) { + if (oldstyle != null) + sb.append( "\n"); + sb.append( "\n"); + sb.append( " " ).append(style).append("\n"); + + oldstyle = style; } sb.append( " \n" ); - sb.append( " " + style + "\n" ); + sb.append( " #" + style + "\n" ); sb.append( " IP=" + current.ip + " Time=" + current.time + "\n" ); sb.append( " " + current.lng + "," + current.lat + ",0\n" ); sb.append( " \n" ); } - - sb.append( "\n" ); + sb.append( "\n" ); + sb.append( "\n" ); sb.append( "\n" ); return sb.toString(); @@ -134,8 +169,19 @@ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + final String KEY = "kmldata"; + + String kmlData = cache.get(KEY); + + if (kmlData == null) { + kmlData = formatXml( getRequestsFromFile() ); + cache.put(KEY, kmlData); + kmlData += ""; + } else { + kmlData += ""; + } - String data = formatXml( getRequestsFromFile() ); + if (req.getParameter("zip") != null) { @@ -143,7 +189,7 @@ ZipOutputStream zip = new ZipOutputStream(baos); zip.putNextEntry( new ZipEntry("trains.kml") ); - zip.write( data.getBytes() ); + zip.write( kmlData.getBytes() ); zip.closeEntry(); zip.close(); @@ -155,7 +201,7 @@ } else { resp.setContentType(KML); - resp.getWriter().print( data ); + resp.getWriter().print( kmlData ); } } }