-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataReaderServer.cpp
106 lines (106 loc) · 3.95 KB
/
DataReaderServer.cpp
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
#include "DataReaderServer.h"
#include <map>
#include <string>
#include <sstream>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <vector>
DataReaderServer::DataReaderServer() {
// map all of the strings of variables to numbers
names[1] = "/instrumentation/airspeed-indicator/indicated-speed-kt";
names[2] = "/instrumentation/altimeter/indicated-altitude-ft";
names[3] = "/instrumentation/altimeter/pressure-alt-ft";
names[4] = "/instrumentation/attitude-indicator/indicated-pitch-deg";
names[5] = "/instrumentation/attitude-indicator/indicated-roll-deg";
names[6] = "/instrumentation/attitude-indicator/internal-pitch-deg";
names[7] = "/instrumentation/attitude-indicator/internal-roll-deg";
names[8] = "/instrumentation/encoder/indicated-altitude-ft";
names[9] = "/instrumentation/encoder/pressure-alt-ft";
names[10] = "/instrumentation/gps/indicated-altitude-ft";
names[11] = "/instrumentation/gps/indicated-ground-speed-kt";
names[12] = "/instrumentation/gps/indicated-vertical-speed";
names[13] = "/instrumentation/heading-indicator/indicated-heading-deg";
names[14] = "/instrumentation/magnetic-compass/indicated-heading-deg";
names[15] = "/instrumentation/slip-skid-ball/indicated-slip-skid";
names[16] = "/instrumentation/turn-indicator/indicated-turn-rate";
names[17] = "/instrumentation/vertical-speed-indicator/indicated-speed-fpm";
names[18] = "/controls/flight/aileron";
names[19] = "/controls/flight/elevator";
names[20] = "/controls/flight/rudder";
names[21] = "/controls/flight/flaps";
names[22] = "/controls/engines/current-engine/throttle";
names[23] = "/engines/engine/rpm";
// initialize the values map with the string names.
for(int i = 1; i <= 23; i++) {
values[names[i]] = 0;
}
}
// get a value from the map only if the strings is correct
double DataReaderServer::getValue(string& s) {
if(values.find(s) != values.end()) { // make sure the strings is in
return values[s];
} else{
//throw value not in simulator error
return 0;
}
}
// set a new value for a string in the server
void DataReaderServer::setValue(double d, string& s) {
values[s] = d;
}
// get a strings from the number of position in the xml
string DataReaderServer::getStringFromXMlLocation(int i) {
return names[i];
}
// check if a string is in out list of strings/
bool DataReaderServer::isInList(string& s) {
if(values.find(s) == values.end()) {
return false;
} else {
return true;
}
}
// static method that will run in a diffrent thread and update the values in the server reader.
void updateVals(int newsockfd, int timesPerSec, DataReaderServer* reader, symbolTable* table) {
char buffer[1000];
bzero(buffer, 1000);
int n;
while(true) { // keep running while we have a connection.
// read values into the buffer
n = read(newsockfd, buffer, 1000);
if (n <= 0) {
// error reading from socket
break;
}
// create a string stream from the buffer
istringstream strm(buffer);
double temp;
string str;
string str2;
int i;
// get a line from the stream, the delimiter is ,
while(getline(strm, str)) {
istringstream strm2(str);
i = 1;
while (getline(strm2, str, ',')) {
// save out number
temp = strtod(str.c_str(), nullptr);
// get the right strings for the positions
str2 = reader->getStringFromXMlLocation(i);
// update the value.
reader->setValue(temp, str2);
i++;
//bzero(buffer);
}
strm.clear();
}
double t = timesPerSec / 10;
int wait = t*1000;
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
}
close(newsockfd);
}