-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedSession.cpp
More file actions
301 lines (263 loc) · 6.08 KB
/
edSession.cpp
File metadata and controls
301 lines (263 loc) · 6.08 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
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
#include "edSession.h"
#include "nodeConfigurator.h"
edSession::edSession(log4cpp::Category *logger,int sessionuid)
{
//ctor
this->sessionuid = sessionuid;
loco = -1;
sessionid = 0;
for (int i=0;i<FN_SIZE;i++){
fns[i]=FN_STATE::OFF;
fnstype[i]=FN_TYPE::SWITCH;
}
fnstype[2] = FN_TYPE::MOMENTARY;// #horn is momentary
speed = 0;
direction = 0;
ad_type = 'S';
sessionType = 'T';
this->logger = logger;
orphan = false;
session_set = false;
session_name = "";
//set inital time
struct timespec spec;
clock_gettime(CLOCK_REALTIME,&spec);
setCbusTime(spec);
setEDTime(spec);
}
edSession::~edSession()
{
//dtor
}
bool edSession::isSessionSet(){
return session_set;
}
void edSession::configChanged(){
getMomentaryFNs();
getMomentaryFNs(this->getLoco());
}
//generic configuration
void edSession::getMomentaryFNs(){
string val;
val = config->getMomentaryFn();
setMomentaryFNs(val);
}
//specific for each loco
void edSession::getMomentaryFNs(int loco){
stringstream ss;
string s;
ss << "R";
ss << loco;
s = ss.str();
logger->debug("[edSession] Get fn config for loco %s", s.c_str());
if (!config->existConfigEntry(s)){
logger->debug("[edSession] No momentary FNs found");
return;
}
logger->debug("[edSession] Fn config exists");
string val = config->getPairValue(s);
logger->debug("[edSession] Fn config is %s", val.c_str());
setMomentaryFNs(val);
}
void edSession::setMomentaryFNs(string val){
int i;
logger->debug("[edSession] FNs %s", val.c_str());
if (val.size() > 0){
vector <string> vals;
vals = split(val,',',vals);
if (!vals.empty()){
logger->debug("[edSession] Reset the Fns to toggle");
for (i=0;i<FN_SIZE;i++){
fnstype[i]=FN_TYPE::SWITCH;
}
for (auto s:vals){
logger->debug("[edSession] Set Fn %s to momentary", s.c_str());
i = atoi(s.c_str());
if (i < FN_SIZE){
setFnType(i, FN_TYPE::MOMENTARY);
}
}
}
}
else logger->debug("[edSession] No momentary FNs found");
}
string edSession::getMomentary(){
int i;
stringstream ss;
string s;
for (i=0;i<FN_SIZE;i++){
if (fnstype[i] == FN_TYPE::MOMENTARY){
ss << i;
ss << ",";
}
}
s = ss.str();
if (s.size() > 0){
s = s.substr(0, s.size() - 1);
}
return s;
}
vector<string> & edSession::split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s+' ');
string item;
while(getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
bool edSession::isOrphan(){
return orphan;
}
void edSession::setOrphan(bool orphan){
this->orphan = orphan;
}
void edSession::setNodeConfigurator(nodeConfigurator *config){
this->config = config;
}
void edSession::setLoco(int loco){
this->loco = loco;
}
int edSession::getLoco(){
return loco;
}
void edSession::setClientId(int client_id){
this->client_id = client_id;
}
int edSession::getClientId(){
return client_id;
}
void edSession::setClientIP(int client_ip){
this->client_ip = client_ip;
}
int edSession::getClientIP(){
return client_ip;
}
int edSession::getSessionUid(){
return sessionuid;
}
void edSession::setSpeed(byte val){
this->speed = val;
}
byte edSession::getSpeed(){
return speed;
}
void edSession::setSession(byte session){
this->sessionid = session;
session_set = true;
}
byte edSession::getSession(){
return sessionid;
}
void edSession::setDirection(byte direction){
this->direction = direction;
}
byte edSession::getDirection(){
return direction;
}
void edSession::setAddressType(byte val){
this->ad_type = val;
}
byte edSession::getAddressType(){
return ad_type;
}
void edSession::setCbusTime(struct timespec val){
this->cbus_time = val;
}
struct timespec edSession::getCbusTime(){
return cbus_time;
}
void edSession::setEDTime(struct timespec val){
this->ed_time = val;
}
struct timespec edSession::getEDTime(){
return ed_time;
}
void edSession::setEdName(string edname){
this->session_name = edname;
}
string edSession::getEdName(){
return session_name;
}
void edSession::setEdHW(string hw){
this->hname = hw;
}
string edSession::getEdHW(){
return hname;
}
void edSession::setLocoName(string loconame){
this->loconame = loconame;
}
string edSession::getLocoName(){
return loconame;
}
void edSession::setSessionType(char stype){
sessionType = stype;
}
char edSession::getSessionType(){
return sessionType;
}
void edSession::setFnType(int fn ,FN_TYPE state){
fnstype[fn] = state;
}
FN_TYPE edSession::getFnType(int fn){
return fnstype[fn];
}
void edSession::setFnState(int fn ,FN_STATE state){
fns[fn] = state;
}
FN_STATE edSession::getFnState(int fn){
return fns[fn];
}
byte edSession::setBit(byte val, int pos){
if (pos > 8) return val;
return val | (1<<pos);
}
byte edSession::clearBit(byte val, int pos){
if (pos > 8) return val;
return val & ~(1<<pos);
}
byte edSession::getDccByte(int fn){
/*
# create the byte for the DCC
#1 is F0(FL) to F4
#2 is F5 to F8
#3 is F9 to F12
#4 is F13 to F20
#5 is F21 to F28
# see http://www.nmra.org/sites/default/files/s-9.2.1_2012_07.pdf
*/
byte fnbyte = 0x00;
int i = 0;
int j = 0;
if ((fn >=0) & (fn <= 4)){
i=0;j=4;
}
if ((5 <= fn) & (fn <= 8)){
i=5;j=8;
}
if ((9 <= fn) & (fn <= 12 )){
i=9;j=12;
}
if ((13 <= fn) & (fn <= 20 )){
i=13;j=20;
}
if ((21 <= fn) & (fn <= 28)){
i=21;j=28;
}
if ((i == 0) & (j == 0)) return -1;
int k = 0;
for (int f = i;f <= j; f++){
if (fns[f]== 1){
if (f == 0){// #special case: light
fnbyte = setBit(fnbyte, 4);
}
else{
fnbyte = setBit(fnbyte, k);
}
}
if (f != 0) k++;
}
return fnbyte;
}