1 | package vnsim.applications.emissions; |
---|
2 | |
---|
3 | import java.nio.ByteBuffer; |
---|
4 | |
---|
5 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
6 | import vnsim.map.object.Globals; |
---|
7 | |
---|
8 | public class EmissionsTrafficLight extends WirelessTrafficLight{ |
---|
9 | |
---|
10 | /** |
---|
11 | * |
---|
12 | */ |
---|
13 | private static final long serialVersionUID = 469949308023842125L; |
---|
14 | public static final int STANDARD_MESSAGE_PERIOD = 500; // milis |
---|
15 | |
---|
16 | public EmissionsTrafficLight() |
---|
17 | { |
---|
18 | super(); |
---|
19 | System.out.println("EmissionsTrafficLigth created"); |
---|
20 | } |
---|
21 | |
---|
22 | public EmissionsTrafficLight(WirelessTrafficLight wtl) |
---|
23 | { |
---|
24 | super(); |
---|
25 | /* copy segments */ |
---|
26 | segments = wtl.segments; |
---|
27 | /* copy cycle length */ |
---|
28 | cycleLength = wtl.cycleLength; |
---|
29 | /* copy colors */ |
---|
30 | currentColor = wtl.currentColor; |
---|
31 | //System.out.println("WirelessTrafficLight changed"); |
---|
32 | } |
---|
33 | |
---|
34 | @Override |
---|
35 | public void init() |
---|
36 | { |
---|
37 | super.init(); |
---|
38 | initialized = true; |
---|
39 | int standardFPSPeriod = (int)((double)(STANDARD_MESSAGE_PERIOD * Globals.engine.fps) / 1000); |
---|
40 | messagePeriods.put(new Integer(Globals.PROT_TL_FEEDBACK), new Integer(standardFPSPeriod)); |
---|
41 | } |
---|
42 | |
---|
43 | @Override |
---|
44 | public byte[] prepareMessage(int messageType) { |
---|
45 | byte[] bytesToSend = null; |
---|
46 | //System.out.println("[TL] Broadcast message"); |
---|
47 | /* broadcasts a message to all the approaching cars */ |
---|
48 | if (messageType == Globals.PROT_TL_FEEDBACK){ |
---|
49 | ByteBuffer bb = ByteBuffer.allocate(Globals.TL_FEEDBACK_SIZE); |
---|
50 | bb.put(Globals.PROT_TL_FEEDBACK); |
---|
51 | |
---|
52 | /*//put the index of this intersection |
---|
53 | bb.putInt(Globals.map.allIntersections.indexOf(this)); |
---|
54 | |
---|
55 | bb.putShort((short)cycleLength); |
---|
56 | int thisSec = (int) (Globals.engine.crtTime / (Globals.SECOND)); |
---|
57 | int aux = thisSec - (lastCycleStartTime / Globals.SECOND); |
---|
58 | bb.put((byte)segments.size()); |
---|
59 | for (int k = 0; k < this.segments.size(); k++) { |
---|
60 | Road rx = Globals.map.roads.get(segments.get(k).roadIndex); |
---|
61 | Point px = rx.points.get(segments.get(k).pointIndex); |
---|
62 | bb.putShort((short)segments.get(k).roadIndex); |
---|
63 | bb.putShort((short)segments.get(k).pointIndex); |
---|
64 | if (segments.get(k).direction == true) |
---|
65 | bb.put((byte)1); |
---|
66 | else |
---|
67 | bb.put((byte)0); |
---|
68 | |
---|
69 | TrafficLightInfo info = segments.get(k).getLightInfo(); |
---|
70 | int color = info.getColor(aux); |
---|
71 | bb.put((byte)color); |
---|
72 | int remaining = -1; |
---|
73 | if (color == Globals.GREEN){ |
---|
74 | remaining = info.greenEnd - aux; |
---|
75 | } |
---|
76 | if (color == Globals.RED){ |
---|
77 | if (aux < info.greenStart) |
---|
78 | remaining = info.greenStart - aux; |
---|
79 | else{ |
---|
80 | remaining = info.greenStart + (cycleLength - aux); |
---|
81 | // remaining = - remaining; |
---|
82 | } |
---|
83 | } |
---|
84 | bb.putShort((short)remaining); |
---|
85 | bb.putShort((short)(cycleLength - (info.greenEnd - info.greenStart))); |
---|
86 | |
---|
87 | for (int i = 1; i < segments.get(k).endOfQueue.length;i++){ |
---|
88 | IntersectionCarRecord icr = segments.get(k).endOfQueue[i]; |
---|
89 | if (icr.getCar() == null){ |
---|
90 | bb.putDouble(0); |
---|
91 | }else{ |
---|
92 | Road r = Globals.map.roads.get(icr.getCar().getRoadIdx()); |
---|
93 | Point p = r.points.get(icr.getCar().getPointIdx()); |
---|
94 | double distance = p.getDistance() - px.getDistance(); |
---|
95 | if (distance < 0) |
---|
96 | distance = - distance; |
---|
97 | bb.putDouble(distance); |
---|
98 | } |
---|
99 | } |
---|
100 | }*/ |
---|
101 | bytesToSend = bb.array(); |
---|
102 | } |
---|
103 | return bytesToSend; |
---|
104 | } |
---|
105 | } |
---|