-
Notifications
You must be signed in to change notification settings - Fork 53
/
TrafficLightApp.java
114 lines (93 loc) · 3.89 KB
/
TrafficLightApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contact: mosaic@fokus.fraunhofer.de
*/
package org.eclipse.mosaic.app.tutorial;
import org.eclipse.mosaic.app.tutorial.message.GreenWaveMsg;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.CamBuilder;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedAcknowledgement;
import org.eclipse.mosaic.fed.application.ambassador.simulation.communication.ReceivedV2xMessage;
import org.eclipse.mosaic.fed.application.app.AbstractApplication;
import org.eclipse.mosaic.fed.application.app.api.CommunicationApplication;
import org.eclipse.mosaic.fed.application.app.api.os.TrafficLightOperatingSystem;
import org.eclipse.mosaic.interactions.communication.V2xMessageTransmission;
import org.eclipse.mosaic.lib.util.scheduling.Event;
import org.eclipse.mosaic.rti.TIME;
import org.apache.commons.lang3.Validate;
public final class TrafficLightApp extends AbstractApplication<TrafficLightOperatingSystem> implements CommunicationApplication {
public final static String SECRET = "open sesame!";
private final static short GREEN_DURATION = 10;
private static final String DEFAULT_PROGRAM = "1";
private static final String GREEN_PROGRAM = "0";
private static final Integer MIN_DISTANCE = 15;
@Override
public void onStartup() {
getLog().infoSimTime(this, "Initialize application");
getOs().getAdHocModule().enable();
getLog().infoSimTime(this, "Activated Wifi Module");
setRed();
}
@Override
public void onShutdown() {
getLog().infoSimTime(this, "Shutdown application");
}
private void setGreen() {
getOs().switchToProgram(GREEN_PROGRAM);
getLog().infoSimTime(this, "Setting traffic lights to GREEN");
getOs().getEventManager().addEvent(
getOs().getSimulationTime() + GREEN_DURATION * TIME.SECOND,
(e) -> setRed()
);
}
private void setRed() {
getOs().switchToProgram(DEFAULT_PROGRAM);
getLog().infoSimTime(this, "Setting traffic lights to RED");
}
@Override
public void onMessageReceived(ReceivedV2xMessage receivedV2xMessage) {
if (!(receivedV2xMessage.getMessage() instanceof GreenWaveMsg)) {
return;
}
getLog().infoSimTime(this, "Received GreenWaveMsg");
if (!((GreenWaveMsg) receivedV2xMessage.getMessage()).getMessage().equals(SECRET)) {
return;
}
getLog().infoSimTime(this, "Received correct passphrase: {}", SECRET);
Validate.notNull(receivedV2xMessage.getMessage().getRouting().getSource().getSourcePosition(),
"The source position of the sender cannot be null");
if (!(receivedV2xMessage.getMessage().getRouting().getSource().getSourcePosition()
.distanceTo(getOs().getPosition()) <= MIN_DISTANCE)) {
getLog().infoSimTime(this, "Vehicle that sent message is too far away.");
return;
}
if (DEFAULT_PROGRAM.equals(getOs().getCurrentProgram().getProgramId())) {
setGreen();
}
}
@Override
public void onAcknowledgementReceived(ReceivedAcknowledgement acknowledgedMessage) {
// nop
}
@Override
public void onCamBuilding(CamBuilder camBuilder) {
// nop
}
@Override
public void onMessageTransmitted(V2xMessageTransmission v2xMessageTransmission) {
// nop
}
@Override
public void processEvent(Event event) throws Exception {
// nop
}
}