package vnsim.applications.emissions; import java.nio.ByteBuffer; import vnsim.applications.adaptiveTL.WirelessTrafficLight; import vnsim.map.object.Globals; public class EmissionsTrafficLight extends WirelessTrafficLight{ /** * */ private static final long serialVersionUID = 469949308023842125L; public static final int STANDARD_MESSAGE_PERIOD = 500; // milis public EmissionsTrafficLight() { super(); System.out.println("EmissionsTrafficLigth created"); } public EmissionsTrafficLight(WirelessTrafficLight wtl) { super(); /* copy segments */ segments = wtl.segments; /* copy cycle length */ cycleLength = wtl.cycleLength; /* copy colors */ currentColor = wtl.currentColor; //System.out.println("WirelessTrafficLight changed"); } @Override public void init() { super.init(); initialized = true; int standardFPSPeriod = (int)((double)(STANDARD_MESSAGE_PERIOD * Globals.engine.fps) / 1000); messagePeriods.put(new Integer(Globals.PROT_TL_FEEDBACK), new Integer(standardFPSPeriod)); } @Override public byte[] prepareMessage(int messageType) { byte[] bytesToSend = null; //System.out.println("[TL] Broadcast message"); /* broadcasts a message to all the approaching cars */ if (messageType == Globals.PROT_TL_FEEDBACK){ ByteBuffer bb = ByteBuffer.allocate(Globals.TL_FEEDBACK_SIZE); bb.put(Globals.PROT_TL_FEEDBACK); /*//put the index of this intersection bb.putInt(Globals.map.allIntersections.indexOf(this)); bb.putShort((short)cycleLength); int thisSec = (int) (Globals.engine.crtTime / (Globals.SECOND)); int aux = thisSec - (lastCycleStartTime / Globals.SECOND); bb.put((byte)segments.size()); for (int k = 0; k < this.segments.size(); k++) { Road rx = Globals.map.roads.get(segments.get(k).roadIndex); Point px = rx.points.get(segments.get(k).pointIndex); bb.putShort((short)segments.get(k).roadIndex); bb.putShort((short)segments.get(k).pointIndex); if (segments.get(k).direction == true) bb.put((byte)1); else bb.put((byte)0); TrafficLightInfo info = segments.get(k).getLightInfo(); int color = info.getColor(aux); bb.put((byte)color); int remaining = -1; if (color == Globals.GREEN){ remaining = info.greenEnd - aux; } if (color == Globals.RED){ if (aux < info.greenStart) remaining = info.greenStart - aux; else{ remaining = info.greenStart + (cycleLength - aux); // remaining = - remaining; } } bb.putShort((short)remaining); bb.putShort((short)(cycleLength - (info.greenEnd - info.greenStart))); for (int i = 1; i < segments.get(k).endOfQueue.length;i++){ IntersectionCarRecord icr = segments.get(k).endOfQueue[i]; if (icr.getCar() == null){ bb.putDouble(0); }else{ Road r = Globals.map.roads.get(icr.getCar().getRoadIdx()); Point p = r.points.get(icr.getCar().getPointIdx()); double distance = p.getDistance() - px.getDistance(); if (distance < 0) distance = - distance; bb.putDouble(distance); } } }*/ bytesToSend = bb.array(); } return bytesToSend; } }