-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroad.cpp
More file actions
176 lines (149 loc) · 5.26 KB
/
road.cpp
File metadata and controls
176 lines (149 loc) · 5.26 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "road.h"
#include <mqtt/async_client.h>
#include <iostream>
#include <sstream>
using namespace std;
// const char *usernames[10] = { "dEbAFpM2yBy9dIadlole", "pWQz4F2GdINRECQQOhh1", "BPkATfb0SZMiFPcV7W52", "Q00AndXXEuB2j8KLRZVb", "AEk60S1AO9mQa5VxfUk0",
// "hDJGprUOHITMbX3axxvt", "owrq16RdiUjlVPpVmrCU", "8Fhe5DMXkDPZc0oJ1ae8", "dnOnfnaylBP2Gw77k59g", "CLBwIHWZrMajQA2mUBrr"};
Road::Road(int numCars, float endPosition, WorldClock* clock, mqtt::async_client* client, RoadIdentifier id, int rd_num, Cross* cross) {
this->numCars = numCars;
this->endPosition = endPosition;
this->clock = clock;
this->client = client;
this->id = id;
this->cross = cross;
this->rd_num = rd_num;
lastEntered = NULL;
time = -1;
entranceOpen = true;
intersectionOpen = true;
pthread_mutex_init(&entranceMutex, NULL);
pthread_mutex_init(&turnMutex, NULL);
}
void Road::start(Road* rd, mqtt::async_client* carClient) {
if (rd_num == 1) {
for(int i=0; i<numCars; i++) {
cars.push_back(new Car(i, this, carClient));
}
}
if (rd_num == 2) {
for(int i=0; i<numCars; i++) {
cars.push_back(new Car(i+5, this, carClient));
}
}
for(int i=0; i<numCars; i++) {
auto msg = mqtt::make_message("v1/gateway/connect","{\"device\":\"Group#8Car"+to_string(cars[i]->id)+"\",\"type\":\"ProjectGroup#8\"}",1,false);
client->publish(msg)->wait();
}
// Zero out location
stringstream initialLocation;
initialLocation << "{";
for(int i=0; i<numCars; i++) {
initialLocation << "\"Group#8Car" << cars[i]->id << "\": {";
initialLocation << "\"latitude\": 0.0,";
initialLocation << "\"longitude\":0.0,";
initialLocation << "\"speed\":0.0}";
if(i<numCars-1) {
initialLocation << ",";
}
}
initialLocation << "}";
for(int i=0; i<numCars; i++) {
cars[i]->start();
}
}
bool Road::tryEnterRoad(int time) {
bool success = false;
pthread_mutex_lock(&entranceMutex);
if(time > this->time) {
entranceOpen = true;
for(int i=0; i<numCars; i++) {
if(cars[i]->active == 1) {
//printf("ROAD CHECKING POSITION OF CAR %d at time %d\n", cars[i]->id, time);
float position = cars[i]->getPosition(time);
if(position < BUFFER_SPACING) {
entranceOpen = false;
break;
}
}
}
this->time = time;
success = entranceOpen;
entranceOpen = false;
} else {
success = false;
}
pthread_mutex_unlock(&entranceMutex);
return success;
}
int Road::getCarNum() {
return this->numCars;
}
int Road::getRdID() {
return this->id;
}
void Road::setCarNum(int n) {
this->numCars = n;
}
mqtt::async_client* Road::getClient() {
return this->client;
}
bool Road::print(int time) {
int notFinished = numCars;
printf("\n\n==============================================\n");
printf("ROAD STATUS FOR TIME %d\n", time);
for(int i=0; i<cars.size(); i++) {
if(cars[i]->active == 0) {
notFinished -= 1;
} else {
float position = cars[i]->getPosition(time);
float speed = cars[i]->getSpeed(time);
printf("CAR %d IS AT POS %f ON ROAD %d GOING %fm/s\n", cars[i]->id, position, cars[i]->start_road->id, speed);
}
}
return notFinished == 0;
}
void Road::visualize(int time) {
stringstream location;
location << "{";
bool started = false;
for(int i=0; i < cars.size(); i++) {
location << "\"Group#8Car" << cars[i]->id << "\": {";
if(cars[i]->getPosition(time) <= 100) {
float position = cars[i]->getPosition(time);
auto coord = GetLatLon(position, cars[i]->start_road->id);
if(position >= 30.0 && position < 56.0){
// if(!started){
// printf("Running Cross Cycle");
// cross->Start();
// started = true;
// }
//cr = true;
location << "\"inCross\": " << true << ",";
} else {
location << "\"inCross\": " << false << ",";
}
location << "\"position\": " << cars[i]->getPosition(time) << ",";
location << "\"speed\" : " << cars[i]->getSpeed(time) << ",";
location << "\"latitude\": "<< coord.lat << ",";
location << "\"longitude\": " << coord.lon << "}";
} else {
location << "\"inCross\": " << false << ",";
location << "\"position\": " << 0.0 << ",";
location << "\"speed\" : " << 0.0<< ",";
location << "\"latitude\": 0.0,";
location << "\"longitude\":0.0}";
}
if(i<numCars-1) {
location << ",";
}
}
location << "}";
auto msg = mqtt::make_message("v1/gateway/attributes", location.str(), 1, false);
client->publish(msg);
//cout << location.str() << endl;
// if (cr) {
// auto msg2 = mqtt::make_message("v1/gateway/cross", cross.str(), 1, false);
// client->publish(msg2);
// }
}