Skip to content

Commit

Permalink
Added purge pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
afiler committed Jun 2, 2013
1 parent 03eda45 commit f5e37d7
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 68 deletions.
Binary file modified com/busdrone/BusReport.class
Binary file not shown.
49 changes: 36 additions & 13 deletions com/busdrone/BusReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BusReport {
double lat;
double lon;
double heading;
boolean inService; // XXX
boolean inService = true; // XXX
long timestamp = java.lang.Long.MIN_VALUE;
long age = java.lang.Long.MIN_VALUE;

Expand All @@ -50,25 +50,48 @@ public String toEventJson() {
return toEvent().toJson();
}

public String syncAndDump(HashMap<String,Event> eventStore) {
public boolean isDeletable() {
//return Math.abs(lat) > 89 || Math.abs(lon) > 89 || !inService || age >= 1000*60*10;

boolean retval = Math.abs(lat) > 89 || Math.abs(lon) > 179 || !inService || age >= 1000*60*10;

/*if (retval) {
System.out.println("["+uid+"] "+
" Math.abs(lat) > 89:"+(Math.abs(lat) > 89)+
" Math.abs(lon) > 179:"+(Math.abs(lon) > 179) +
" !inService:"+!inService+
" age >= 1000*60*10:"+(age >= 1000*60*10));
}*/

return retval;
}

public String syncAndDump(HashMap<String,BusReport> reportStore) {
cleanup();

String key = "com.busdrone.reports/"+uid;

Event event = (Event)eventStore.get(key);
BusReport oldBus = (BusReport)reportStore.get(key);

if (event == null) {
eventStore.put(key, this.toEvent());
return this.toEventJson();
if (isDeletable()) {
if (oldBus != null) {
Event event = new Event("remove_vehicle");
event.uid = uid;
reportStore.remove(key);
String json = event.toJson();
System.out.println(json);
return json;
} else {
return null;
}
}

BusReport oldBus = event.vehicle;

if (!oldBus.equals(this)) {
event.vehicle = this;

if (oldBus == null || !oldBus.equals(this)) {
reportStore.put(key, this);
return this.toEventJson();
} else {
return null;
}

return null;
}

@Override public boolean equals(Object aThat) {
Expand Down
Binary file modified com/busdrone/BusReportServer.class
Binary file not shown.
25 changes: 3 additions & 22 deletions com/busdrone/BusReportServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.busdrone.NextBusFetcher;
import com.busdrone.BusViewFetcher;

public class BusReportServer extends WebSocketServer {
public HashMap<String,Event> eventStore = new HashMap<String,Event>();
public HashMap<String,BusReport> reportStore = new HashMap<String,BusReport>();

public static void main( String[] args ) throws InterruptedException , IOException {
WebSocketImpl.DEBUG = false;
Expand Down Expand Up @@ -71,27 +67,12 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {

Event event = new Event("init");

for (Map.Entry<String, Event> entry : eventStore.entrySet()) {
event.addVehicle(entry.getValue().vehicle);
for (Map.Entry<String, BusReport> entry : reportStore.entrySet()) {
event.addVehicle(entry.getValue());
}

String json = event.toJson();
System.out.println(json);
conn.send(json);

/* StringBuilder builder = new StringBuilder();
builder.append("[");
for (String key : db.hkeys("buses")) {
if (builder.length() > 1) builder.append(",");
builder.append(db.get(key));
}
builder.append("]");
synchronized (conn) {
conn.send(builder.toString());
conn.send(db.get("com.busdrone.reports.nextbus"));
conn.send(db.get("com.busdrone.reports.wsferry"));
conn.send(db.get("com.busdrone.reports.onebusaway"));
*/
}

@Override
Expand Down
Binary file modified com/busdrone/Fetcher.class
Binary file not shown.
4 changes: 2 additions & 2 deletions com/busdrone/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class Fetcher extends Thread {
public void run() {
while (true) {
try {
this.runOnce(server.eventStore);
this.runOnce(server.reportStore);
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand All @@ -23,5 +23,5 @@ public void run() {
}
}

public abstract void runOnce(HashMap<String,Event> eventStore) throws Exception;
public abstract void runOnce(HashMap<String,BusReport> reportStore) throws Exception;
}
Binary file modified com/busdrone/NextBusFetcher.class
Binary file not shown.
13 changes: 5 additions & 8 deletions com/busdrone/NextBusFetcher.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.busdrone;

import java.util.ArrayList;
import java.util.HashMap;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;

import redis.clients.jedis.Jedis;

import com.google.gson.Gson;

import com.busdrone.Fetcher;

public class NextBusFetcher extends Fetcher {
public static String operator = "metro.kingcounty.gov";
public static String dataProvider = "nextbus.com";
public static String dataProvider = "com.nextbus";
public static String vehicleType = "streetcar";
public static String endpointUrl = "https://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=seattle-sc&r=southlakeunion&t=0";
public static String typeId = "slu";
Expand All @@ -29,7 +26,7 @@ public NextBusFetcher(BusReportServer s) {
}

@Override
public void runOnce(HashMap<String,Event> eventStore) throws Exception {
public void runOnce(HashMap<String,BusReport> reportStore) throws Exception {
//ArrayList<BusReport> reports = new ArrayList<BusReport>();

Builder parser = new Builder();
Expand Down Expand Up @@ -61,8 +58,8 @@ public void runOnce(HashMap<String,Event> eventStore) throws Exception {
report.lat = Double.parseDouble(vehicle.getAttribute("lat").getValue());
report.lon = Double.parseDouble(vehicle.getAttribute("lon").getValue());
report.heading = Double.parseDouble(vehicle.getAttribute("heading").getValue());
report.age = Long.parseLong(vehicle.getAttribute("secsSinceReport").getValue());
report.timestamp = reportTimestamp - (report.age * 1000);
report.age = Long.parseLong(vehicle.getAttribute("secsSinceReport").getValue()) * 1000;
report.timestamp = reportTimestamp - (report.age);
if (report.coach.equals("1")) report.color = "#b2df0000"; //"rgba(223,0,0,0.7)";
else if (report.coach.equals("2")) report.color = "#b2df7f00"; //"rgba(223,127,0,0.7)";
else if (report.coach.equals("3")) report.color = "#b2df007f"; //"rgba(127,0,223,0.7)";
Expand All @@ -75,7 +72,7 @@ public void runOnce(HashMap<String,Event> eventStore) throws Exception {
//reports.add(report.cleanup());
//busReports.put(report.vehicleId, report);
//server.sendToAll(report.toEventJson());
server.sendToAll(report.syncAndDump(eventStore));
server.sendToAll(report.syncAndDump(reportStore));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Binary file modified com/busdrone/OBAFetcher.class
Binary file not shown.
4 changes: 2 additions & 2 deletions com/busdrone/OBAFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public OBAFetcher(BusReportServer s) {
}

@Override
public void runOnce(HashMap<String,Event> eventStore) throws Exception {
public void runOnce(HashMap<String,BusReport> reportStore) throws Exception {
int updated=0;
ArrayList<BusReport> reports = new ArrayList<BusReport>();
Builder parser = new Builder();
Expand Down Expand Up @@ -103,7 +103,7 @@ public void runOnce(HashMap<String,Event> eventStore) throws Exception {
//}

//busReports.put(report.vehicleId, report);
server.sendToAll(report.syncAndDump(eventStore));
server.sendToAll(report.syncAndDump(reportStore));

} catch (Exception e) {
//System.out.println(vehicleStatuses.get(i));
Expand Down
Binary file added com/busdrone/Reaper.class
Binary file not shown.
22 changes: 22 additions & 0 deletions com/busdrone/Reaper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.busdrone;

import java.util.HashMap;
import java.util.Map;

public class Reaper extends Fetcher {

public Reaper(BusReportServer s) {
super();
server = s;
sleepSecs = 60; // XXX XXXXXXXX
}

@Override
public void runOnce(HashMap<String, BusReport> reportStore) throws Exception {
for (Map.Entry<String, BusReport> entry : reportStore.entrySet()) {
BusReport report = entry.getValue();
if (report.isDeletable()) server.sendToAll(report.syncAndDump(reportStore));
}
}

}
Binary file modified com/busdrone/WSFerryFetcher.class
Binary file not shown.
35 changes: 17 additions & 18 deletions com/busdrone/WSFerryFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class WSFerryFetcher extends Fetcher {
public static String vehicleType = "ferry";
public static String color = "#b2017359"; //"rgba(1,115,89,0.7)";

public static SimpleDateFormat wsfDatetimeFormat = new SimpleDateFormat("M/d H:m");
public static SimpleDateFormat wsfDatetimeFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
public static SimpleDateFormat vesselDatetimeFormat = new SimpleDateFormat("M/d H:m");

Gson gson = new Gson();

Expand All @@ -34,15 +35,16 @@ public WSFerryFetcher(BusReportServer s) {
}

@Override
public void runOnce(HashMap<String,Event> eventStore) throws Exception {
public void runOnce(HashMap<String,BusReport> reportStore) throws Exception {
URLConnection connection = new URL(endpointUrl).openConnection();
ArrayList<BusReport> reports = new ArrayList<BusReport>();
JsonParser parser = new JsonParser();

for (JsonElement element :
parser.parse(new InputStreamReader(connection.getInputStream()))
.getAsJsonObject().getAsJsonArray("vessellist"))
{
JsonObject jsonResponse = parser.parse(new InputStreamReader(connection.getInputStream()))
.getAsJsonObject();

long mainTimestamp = wsfDatetimeFormat.parse(jsonResponse.get("timestamp").getAsString()).getTime();

for (JsonElement element : jsonResponse.getAsJsonArray("vessellist")) {
if (!element.isJsonObject()) continue;

try {
Expand All @@ -65,27 +67,24 @@ public void runOnce(HashMap<String,Event> eventStore) throws Exception {
report.lon = o.get("lon").getAsDouble();
report.speedMph = o.get("speed").getAsInt();
report.heading = o.get("head").getAsInt();
report.timestamp = parseDatetime(o.get("datetime").getAsString());
report.timestamp = parseVesselDatetime(o.get("datetime").getAsString());
report.age = mainTimestamp - report.timestamp;

//reports.add(report.cleanup());
//server.sendToAll(report.toEventJson());
server.sendToAll(report.syncAndDump(reportStore));

server.sendToAll(report.syncAndDump(eventStore));
} catch (Exception e) {

} catch (Exception e) {}
}

}

//String json = gson.toJson(reports.toArray());
//server.sendToAll(json);
//db.set("com.busdrone.reports.wsferry", json);

}

@SuppressWarnings("deprecation")
public long parseDatetime(String s) {
public long parseVesselDatetime(String s) {
Date now = new Date();
try {
Date d = wsfDatetimeFormat.parse(s);
Date d = vesselDatetimeFormat.parse(s);
int year = now.getYear();
if (d.getMonth() == 12 && d.getDay() == 31 && now.getMonth() == 1)
year--;
Expand Down
3 changes: 0 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@

function onWsMessage(evt) {
var data = JSON.parse(evt.data);
console.log(data.type);

if (data.type == 'update_vehicle') {
updateBus(data.vehicle);
Expand All @@ -241,8 +240,6 @@
}

function updateBus(bus) {
//console.log(bus);

if (bus.lat > 89 || bus.lat < -89 || !bus.route || bus.route == 'null' ||
getFreshness(bus) == 'dead' ) {
deleteBus(bus);
Expand Down

0 comments on commit f5e37d7

Please sign in to comment.