-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainV4.c
477 lines (421 loc) · 12.5 KB
/
mainV4.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*SMART PARKING SYSTEM MACHINE INDEPENDANT SOFTWARE
HANYUAN YE
CHRIS YU
YI FAN YU
ECE 150 1A FALL 2017
*/
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
/* THINGS LEFT TO DO:
-oled reset needs to pathFinding entrance 1 on, pathFinding entrance 2 on;
-timer to activate standalone statistics collection is not implemented
-think of better statistics to satisfy our customer needs
-Statistics:
-Most used parking spots
-Least used parking spots
-Most/Least used entrances
-Average idle time
-LOGLEVEL is still not implemented hardish
*/
const int LOGLEVEL = 2; //
// 3 means log the action of every single function (debugging)
// 2 means log the action of functions that call other functions and their exits (RECOMMENDED)
// 1 means log only the functions that call other functions and not their exits (RECOMMENDED)
// 0 means no logging (dangerous)
const int TOTAL_SLOTS = 11; //total slot allowed
const int OCCUPIED = 1; //occupied is 1
const int UNOCCUPIED = 0; //unocuped is 0
int usage = 0; // logs the usage of our system;
int rejected = 0; // logs how many cars get bounced because of full ParkLot
int shutdown = 0;
const int MAX_TIME = 10; // will alert the operator if anyone stays more than MAX_TIME;
//keeps track of the time the program boots up;
time_t current_time;
struct tm* current_time_format;
//current_time_format = localtime(¤t_time);
typedef struct
{
int occupy;
int timeStamp; // when the person arrives
int reserved;
float timeTotalSpent;
// when the car shows up at the entrance, the system reserves
}ParkingSpace;
/////////////////////////////////////////////////////////////////////////////////////////
//
//FUNCTIONS SIGNATURE
ParkingSpace* BootUp(ParkingSpace* map,char fileName[],int reboot)
// maybe allocate memory to all the parking spots and reset them to zero or
// read from a file that contains the information in case the program is restarting;
// even logs to the right files since we do not want to have a big fat log file for weeks
{
FILE* fnBootup;
if (!reboot){
// if reboot is false;
for (int i = 0; i < TOTAL_SLOTS; i++)
{
map[i].occupy = UNOCCUPIED;
map[i].timeStamp = UNOCCUPIED;
map[i].timeTotalSpent = 0;
}
} else{ // reboot is true
fnBootup = fopen("lastInfo.txt", "r");
int location;
time_t timeRecorded;
while (fscanf(fnBootup, "%d, %d", location, timeRecorded) == 2)
{
map[location].occupy = OCCUPIED;
map[location].reserved = OCCUPIED;
map[location].timeStamp = timeRecorded;
}
fclose(fnBootup);
}
return map;
}
void shutDown (char fileName[], ParkingSpace* map)
// cleans up the mess
// stores current parking lot information in a csv file ready to be read again by BootUp;
{
FILE* fnShutdown;
fnShutdown = fopen("lastInfo.txt", "a");
for (int i = 0; i < TOTAL_SLOTS; i++)
{
if (map[i].occupy == OCCUPIED)
{
fprintf(fnShutdown,"%d, %d,\n", i, map[i].timeStamp);
}
}
fclose(fnShutdown);
}
void eventLog(const char name[], const int loggingRequirement)
//prints out to the eventLogs.txt
//logs function calls
{
time_t timeSec;
struct tm* timeFormated;
timeSec = time(NULL);
timeFormated = localtime(&timeSec);
printf("it is now %d , %d \n", timeFormated -> tm_hour, timeFormated -> tm_min);
//unfinished time function logger;
FILE* fnLogs;
fnLogs = fopen("eventLogs.txt", "a");
fprintf (fnLogs, "%d-%02d-%02d %02d:%02d:%02d ",(timeFormated -> tm_year)+1900, timeFormated -> tm_mon,
timeFormated -> tm_mday,timeFormated -> tm_hour,timeFormated -> tm_min,
timeFormated -> tm_sec);
fprintf (fnLogs, "entering %s\n", name);
fclose(fnLogs);
}
void errorLog(const int lineNumber, const char errorMessage[], const int importance)
// prints to the eventLogs.txt Warnings and ERRORs that occured
// importance 0: ERROR, importance 1: WARNING
// ERROR will likely cause program crash so flush that
// WARNING will detect problems of the system such as no more parking spot;
{
FILE *error = fopen("eventLog.txt", "a");
char warningTitle[8] = "WARNING";
char errorTitle[6] = "ERROR";
//char initialMessage[8] = (importance == 1) ? "WARNING" : "ERROR"; // if importance is 1 print warning;
if (importance){
fprintf(error, "%s : %s",warningTitle, errorMessage);
}
else{
fprintf(error, "%s : %s",errorTitle, errorMessage);
}
fprintf(error, " recorded on line: %d\n", lineNumber);
fclose(error);
}
//COULD be implemented with OLED, could also be displayed on the oled at all time
float checkCapacity(ParkingSpace* map, int virtCapac)
//checks the capacity of the parkingSpace as of rightNow
//returns the decimal value such as 0.50
// if virtual > 0; will instead look at reservations.
{
eventLog("checkCapacity",LOGLEVEL); // logs
int occupiedCount = 0;
int reservedCount = 0;
float capacity;
for (int i = 0; i < 12; i ++)
{
if (map[i].occupy == OCCUPIED)
occupiedCount++;
if (map[i].reserved == OCCUPIED)
reservedCount++;
}
if (virtCapac){
capacity = reservedCount / TOTAL_SLOTS;
}
else{
capacity = occupiedCount / TOTAL_SLOTS;
}
return capacity;
}
void enterF(ParkingSpace *map, int location)
//triggers everytime a new car has arrived at his defined parkingLot
//put occupy as occupied when everything works out
{
//logs when a car has entered a parking spot
eventLog("enterF",LOGLEVEL); // logs
map[location].occupy = OCCUPIED;
map[location].timeStamp = current_time;
}
int exitF(ParkingSpace *map, int location)
//triggers everytime a car leaves his parking lot
//
{
eventLog("exitF",LOGLEVEL); // logs
// logs an exit
// return the duration in second of the stay;
float duration = current_time - map[location].timeStamp;
map[location].
map[location].occupy = UNOCCUPIED;
map[location].timeStamp = UNOCCUPIED;
return duration;
}
/////////////////////////////////////////////////////////////////////////
// STATISTICS CALCULATION-
// once every hour would be good
// checkCapacity would be part of statistics
int maxTimeStay(ParkingSpace *map)
// return the maximum stay time for the time of the check
{
eventLog("maxTimeOfStay",LOGLEVEL); // logs
int max = map[0].timeStamp;
int unoccupiedCount = 0;
for (int i = 0; i < TOTAL_SLOTS; i ++)
{
if (map[i].occupy == UNOCCUPIED)
{
unoccupiedCount ++;
continue;
}
if (map[i].timeStamp > max)
{
max = map[i].timeStamp;
}
}
if (unoccupiedCount == TOTAL_SLOTS)
return 0;
// just to be safe
return max;
}
int minTimeStay(ParkingSpace *map)
{
eventLog("minTimeStay",LOGLEVEL); // logs
int min = 0;
int unoccupiedCount = 0;
for (int i = 0; i < TOTAL_SLOTS; i++)
{
if (map[i].occupy == UNOCCUPIED)
{
unoccupiedCount ++;
continue;
}
if (map[i].timeStamp > min)
{
min = map[i].timeStamp;
}
}
if (unoccupiedCount == TOTAL_SLOTS)
return 0;
return min;
}
float average(ParkingSpace *map, int curr)
{
eventLog("average",LOGLEVEL); // logs
float sum = 0;
int unoccupiedCount = 0;
for (int i = 0; i < TOTAL_SLOTS; i ++)
{
if (map[i].occupy == UNOCCUPIED)
{
unoccupiedCount ++;
continue;
}
sum += difftime(curr, map[i].timeStamp);
}
if (sum == 0)
return 0;
float temp = TOTAL_SLOTS - unoccupiedCount;
float average = sum / temp;
return average;
}
int* mostPopularParkingSpace(float useByParkingLot[])
// 0 is the first 10 is the last
// checks the parking spot with a car in it the longest amount of time
// holly shit what happens when there are 2 with exact pupolar parking space
// maybe return a struct that tells us the amount of popular parkingSpaces
{
eventLog("mostPopularParkingSpace",LOGLEVEL);
int repetitionCounter = 1; //gets incremented everytime currentMaxValue = temp
int repetitionArray[11];
int currentMax = 0; // current popular parking spot;
time_t currentMaxValue = map[currentMax].timeTotalSpent + (current_time - map[currentMax].timeStamp);
time_t temp; // temp to take the next input data
for (int i = 1; i < TOTAL_SLOTS; i++){
if (map[i].occupy = OCCUPIED){
temp = map[i].timeTotalSpent + (current_time - map[currentMax].timeStamp);
// need to add the extra time spent there if car is still parked
}else{ // parking slot is not being used;
temp = map[i].timeTotalSpent;
}
//processing the data
if (currentMaxValue < temp){
currentMax = i;
currentMaxValue = temp;
repetitionCounter = 1;
// we have a new max Value; // reset repetition
}
else if (currentMaxValue == temp){
repetitionArray[repetitionCounter-1] = i;
repetitionCounter++;
}
}
// declare a pointer to the popular spots;
int* popularArray;
if (repetitionCounter != 1){ // this means we have not 1 max;
popularArray = malloc(repetitionCounter * sizeof(*popularArray) );
// create an array with size equal to how many repetitionCounter we have
for (int j = 0; j < repetitionCounter ; j++ ){
popularArray[j] = repetitionArray[j];
}
} else {
popularArray = malloc(sizeof (*popularArray));
popularArray[0] = currentMax;
}
return popularArray;
}
void statistics(ParkingSpace *map)
{
eventLog("statistics",LOGLEVEL); // logs
int* popular;
int* notVeryPopular; // POPULAR / not very popular parking Spots
int numPopular;
int numNotVeryPopular;
float totalTimeOccupied[11];
for (int i; i < 11; i++){ // give totalTImeOccupied the amount of time
if (map[i].occupy == 1){
totalTimeOccupied = map[i].timeTotalSpent + (current_time - map[i].timeStamp);
} else{
totalTimeOccupied = map[i].timeTotalSpent;
}
}
float capacity = checkCapacity(map);
int maxDuration = maxTimeStay(map);
if (maxDuration != 0)
// maxDuration = difftime(curr, maxDuration);
// int minDuration = minTimeStay(map);
// if (minDuration != 0)
// minDuration = difftime(curr, minDuration);
// float avgDuration = average(map, curr);
FILE *stats = fopen("stats.txt", "a");
if (stats == NULL)
{
// eventLog("opening failed",LOGLEVEL); // logs
// logError(__LINE__);
}
// else
// {
// fprintf(stats, "Currrent Time: %ld, Capacity: %f, Maximum Duration: %d, Minimum Duration: %d, Average Duration: %f\n"
// , curr, capacity, maxDuration, minDuration, avgDuration);
// fclose(stats);
// }
}
int input(ParkingSpace *map, int location)
{
//Something was inputted
eventLog("input",LOGLEVEL); // logs
FILE *record = fopen("record.txt", "a");
if (record == NULL)
{
errorLog(__LINE__);
//perror("not able to open record.txt");
return -5;
}
statistics(map);
if ( ( checkCapacity (map) ) >= 1.00)
{ // if full
logError(__LINE__);
// logs this attempt to join
//display something on the oled
return -1;
}
if (map[location].occupy == OCCUPIED) // the car exits the parking lot;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//potential bug if the shortest path gives a wrong parkingSpace
{
int duration = exitF(map, location);
if (duration > MAX_TIME) // if more than necessary time;
fprintf(record, "Exit recorded at location: %d, at time %ld, with duration %d seconds. Duration over maximum alloted time by %d seconds.\n"
, location, current_time, duration, duration - MAX_TIME);
else // if normal exit
fprintf(record, "Exit recorded at location: %d, at time %ld, with duration %d seconds.\n", location, current_time, duration);
}
else // if normal entry;
{ // parkingLot number
enterF(map, location);
usage ++;
fprintf(record, "Entry recorded at location: %d, at time %ld. Car number %d.\n", location, current_time, usage);
}
fclose(record);
return 0;
}
int testmain()
{
ParkingSpace map[TOTAL_SLOTS];
for (int i = 0; i < TOTAL_SLOTS; i ++)
{
map[i].occupy = UNOCCUPIED;
map[i].timeStamp = UNOCCUPIED;
}
logError(__LINE__);
time_t start, end;
time(&start);
input(map, 0);
sleep(2);
time(&start);
input(map, 4);
sleep(5);
time(&start);
input(map, 3);
sleep(1);
time(&start);
input(map, 11);
sleep(5);
input(map, 1);
sleep(9);
input(map,1);
sleep(2);
input(map,11);
exit(0);
return 0;
}
int test2main()
{
int startTime;
int currTime;
//Boot up
while (shutdown = false)
{
//read time
// compare
// if on for certain amount of time, call reset oled function
// ask if there is an input from the entrances
// call shortest path function - > which calls input - > which calls everything else
// if exit, call chris function - > which calls exit
// we can implement time interval statistics using currtime - start time % something
// if currtime - starttime is big enough, call shutdown, set shutdown to true
}
return 0;
}
int main{
return 0;
}
/* take in filename
change last digit of filename to +1 of whatever
return filename
craete file with that filename
*/