AutoCheckWaySingleStep: stops preparing way at next block

master
Steffen Pohle 2 years ago
parent 63e9778929
commit b370dbf930

@ -1,3 +1,6 @@
2024-01-04:
- fixed: AutoCheckWaySingleStep: stops preparing way at next block
2024-01-16: 2024-01-16:
- fixed: not all pthread_mutexes has been initialization. - fixed: not all pthread_mutexes has been initialization.

@ -700,33 +700,51 @@ int Locomotives::SchedulerNextStep(Locomotive *loc) {
return 0; return 0;
} }
// //
// with each call only check one single step of the way, if we have to // with each call only check one single step of the way, if we have to
// turn a turnout try to do it and return 0. // turn a turnout try to do it and return 0.
// if everything is set up till the destination (next) block return 1 // if everything is set up till the destination (next) block return 1
// to speed things up: we only prepare ways which we have locked already // to speed things up: we only prepare ways which we have locked already
// //
int Locomotives::AutoCheckWaySingleStep(string way, string locname, int *data) { int Locomotives::AutoCheckWaySingleStep(string way, Locomotive *loc, int *data) {
int res = 0;
size_t pos1; size_t pos1;
size_t curpos; size_t curpos;
size_t blockpos;
int cnt; int cnt;
int state; int state;
int newdata = 0; int newdata = 0;
string turnout; string turnout;
string nextblock;
size_t nextblockpos;
int blflags = 0;
int x, y; int x, y;
Railway r; Railway r;
if (*data < 0) *data = 0; if (*data < 0) *data = 0;
debug (0, "Locomotives::AutoCheckWaySingleStep Prepare for Loco: %s Way:%s data:%d blocknext:%s", loc->name, way.c_str(), *data, loc->blocknext);
debug (0, "Locomotives::AutoCheckWaySingleStep Prepare for Loco: %s Way:%s data:%d", locname.c_str(), way.c_str(), *data); //
// find final next block
// check if it is a split block and a long train
blflags = server->blocks.GetFlags(loc->blocknext+2);
nextblock = loc->blocknext+2;
if (!(loc->flags & LOCO_F_SHORTTRAIN) && (blflags & BLOCK_F_SPLIT)) {
if ((loc->blocknext[0] == '+' && (blflags & BLOCK_F_SPLITPOS)) ||
(loc->blocknext[0] == '-' && !(blflags & BLOCK_F_SPLITPOS)) ) {
nextblock = server->blocks.GetSecondBlock(loc->blocknext+2);
}
}
nextblockpos = way.find(nextblock);
debug (0, "Locomotives::AutoCheckWaySingleStep nextblock:%s", nextblock.c_str());
curpos = 0; curpos = 0;
do { do {
// //
// read all ways from the begin with stop at "data" // read all ways from the begin with stop at "data"
curpos = way.find (",t:", curpos+1); curpos = way.find (",t:", curpos+1);
if (curpos != string::npos && nextblockpos != string::npos && curpos > nextblockpos) break;
newdata++; newdata++;
if (curpos != string::npos) { if (curpos != string::npos) {
pos1 = way.find(":", curpos+3); pos1 = way.find(":", curpos+3);
@ -750,23 +768,23 @@ int Locomotives::AutoCheckWaySingleStep(string way, string locname, int *data) {
while (server->railways.FindReference(&x, &y, turnout, cnt++)) { while (server->railways.FindReference(&x, &y, turnout, cnt++)) {
cnt++; cnt++;
r = server->railways.RailwayGet(x, y); r = server->railways.RailwayGet(x, y);
debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s LockedBy:%s", locname.c_str(), turnout.c_str(), r.lockedby); debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s LockedBy:%s", loc->name, turnout.c_str(), r.lockedby);
// //
// //
if (locname.compare(r.lockedby) == 0 || r.lockedby[0] == 0) { if (strncmp(loc->name, r.lockedby, REFERENCENAME_LEN) == 0 || r.lockedby[0] == 0) {
if (way[pos1+1] == '0' && state != 0) { if (way[pos1+1] == '0' && state != 0) {
debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Not Equal -> Set:0", locname.c_str(), turnout.c_str()); debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Not Equal -> Set:0", loc->name, turnout.c_str());
server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 0); server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 0);
return 0; return 0;
} }
else if (way[pos1+1] == '1' && state != 1) { else if (way[pos1+1] == '1' && state != 1) {
debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Not Equal -> Set:1", locname.c_str(), turnout.c_str()); debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Not Equal -> Set:1", loc->name, turnout.c_str());
server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 1); server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 1);
return 0; return 0;
} }
else if (newdata > *data) { else if (newdata > *data) {
debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Equal Reset:%c", locname.c_str(), turnout.c_str(), way[pos1+1]); debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Equal Reset:%c", loc->name, turnout.c_str(), way[pos1+1]);
if (way[pos1+1] == '0') server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 0); if (way[pos1+1] == '0') server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 0);
if (way[pos1+1] == '1') server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 1); if (way[pos1+1] == '1') server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 1);
(*data) = newdata; (*data) = newdata;
@ -775,15 +793,14 @@ int Locomotives::AutoCheckWaySingleStep(string way, string locname, int *data) {
} }
} }
if (cnt == 0) { if (cnt == 0) {
debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Reference not found", locname.c_str(), turnout.c_str()); debug (0, "Locomotives::AutoCheckWaySingleStep Loco:%s Turnout:%s Reference not found", loc->name, turnout.c_str());
return 0; return 0;
} }
} }
} while (curpos != string::npos); } while (curpos != string::npos);
if (curpos == string::npos) res = 1; if (curpos == string::npos || (nextblockpos != string::npos && curpos > nextblockpos)) return 1;
return 0;
return res;
} }
@ -883,7 +900,7 @@ int Locomotives::Loco_PrepareWay(Locomotive *loco) {
block = ((string)(loco->blocknext+2)).substr(0, ((string)(loco->blocknext+2)).find(",")); block = ((string)(loco->blocknext+2)).substr(0, ((string)(loco->blocknext+2)).find(","));
if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > LOCO_TO_TURNOUT) { if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > LOCO_TO_TURNOUT) {
if (AutoCheckWaySingleStep(loco->auto_way, loco->name, &loco->auto_data) == 1) { if (AutoCheckWaySingleStep(loco->auto_way, loco, &loco->auto_data) == 1) {
if ((loco->flags & LOCO_F_CARGO) || if ((loco->flags & LOCO_F_CARGO) ||
(server->blocks.GetFlags(block) & BLOCK_F_SPEEDLIMIT)) return 1; (server->blocks.GetFlags(block) & BLOCK_F_SPEEDLIMIT)) return 1;
else return 2; else return 2;
@ -1045,7 +1062,7 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) {
s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags); s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags);
if (server->sensors.GetActive(s_stop) == 1) { if (server->sensors.GetActive(s_stop) == 1) {
if (!AutoCheckWaySingleStep(loco->auto_way, loco->name, &loco->auto_data)) { if (!AutoCheckWaySingleStep(loco->auto_way, loco, &loco->auto_data)) {
// //
// end of block and not finished preparing the way. // end of block and not finished preparing the way.
// stop everything // stop everything
@ -1074,7 +1091,7 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) {
if (loco->auto_timenext.tv_sec > 0 && timer_get(&loco->auto_timenext) > LOCO_TO_TURNOUT) { if (loco->auto_timenext.tv_sec > 0 && timer_get(&loco->auto_timenext) > LOCO_TO_TURNOUT) {
debug (0, "* Locomotives::Loco_BlockEnterNext prepare way for loco: %s", loco->name); debug (0, "* Locomotives::Loco_BlockEnterNext prepare way for loco: %s", loco->name);
if (AutoCheckWaySingleStep(loco->auto_way, loco->name, &loco->auto_data) == 1) { if (AutoCheckWaySingleStep(loco->auto_way, loco, &loco->auto_data) == 1) {
// //
// finished preparing new way, need to stay in ENTERBLOCKNEXT to unlock old way // finished preparing new way, need to stay in ENTERBLOCKNEXT to unlock old way
debug (0, "* Locomotives::Loco_BlockEnterNext finished preparing way for loco: %s", loco->name); debug (0, "* Locomotives::Loco_BlockEnterNext finished preparing way for loco: %s", loco->name);
@ -1252,21 +1269,3 @@ int Locomotives::Loop() {
return 0; return 0;
} }
int Locomotives::Test(string loco) {
int res = 0;
int i;
int j = 0;
printf ("%s:%d LocoTest.\n", __FILE__, __LINE__);
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0)
if (loco.compare(locomotives[i].name) == 0) {
res = AutoCheckWaySingleStep(locomotives[i].auto_way, loco, &j);
}
printf ("%s:%d LocoTest ---> Return %d\n", __FILE__, __LINE__, res);
return 0;
};

@ -84,6 +84,7 @@ class Locomotives {
JSONParse _GetJSON(int idx); JSONParse _GetJSON(int idx);
int SchedulerNextStep(Locomotive *loc); // automode, calculate next step int SchedulerNextStep(Locomotive *loc); // automode, calculate next step
void SendUpdate(Locomotive *loc); void SendUpdate(Locomotive *loc);
int Loco_SearchAndLock(Locomotive *loco); int Loco_SearchAndLock(Locomotive *loco);
int Loco_PrepareWay(Locomotive *loco); int Loco_PrepareWay(Locomotive *loco);
int Loco_OnRoute(Locomotive *loco); int Loco_OnRoute(Locomotive *loco);
@ -114,7 +115,7 @@ class Locomotives {
int SetAssign (string name, string block, int direction); int SetAssign (string name, string block, int direction);
int GetFlags(string name); int GetFlags(string name);
int AutoCheckWaySingleStep(string way, string locname, int *data); int AutoCheckWaySingleStep(string way, Locomotive *loc, int *data);
int Loop(); int Loop();
string GetName(int idx); string GetName(int idx);
@ -122,7 +123,6 @@ class Locomotives {
JSONParse GetJSON(string name); JSONParse GetJSON(string name);
void GetJSONAll(JSONParse *json); void GetJSONAll(JSONParse *json);
Locomotive GetLocomotiveFromJSON(JSONParse *j); Locomotive GetLocomotiveFromJSON(JSONParse *j);
int Test(string loco);
}; };

@ -66,7 +66,6 @@ private:
void SetJSONLocoDest(JSONParse *jp); void SetJSONLocoDest(JSONParse *jp);
void SetJSONLocoAssign(JSONParse *jp); void SetJSONLocoAssign(JSONParse *jp);
void SetJSONLocoReset(JSONParse *jp); void SetJSONLocoReset(JSONParse *jp);
void SetJSONLocoTest(JSONParse *jp);
void SetJSONLocoMan(JSONParse *jp); void SetJSONLocoMan(JSONParse *jp);
void SetJSONLocoAutoMan(JSONParse *jp); void SetJSONLocoAutoMan(JSONParse *jp);
void SetJSONLocoAuto(JSONParse *jp); void SetJSONLocoAuto(JSONParse *jp);

@ -10,19 +10,6 @@ Railways::Railways() {
railways = NULL; railways = NULL;
pthread_mutex_init(&mtx, NULL); pthread_mutex_init(&mtx, NULL);
changed = 0; changed = 0;
//
// do some tests on start
string way = "b:-:B43,t:w90:0,b:-:B1,t:w28:1,t:w30:1,t:w31:0,t:w32:0,t:w33:0,t:w33:0,t:w34:1,t:w35:0,t:w36:1,t:w31:1,t:w30:1,t:w28:1,b:+:B1,t:w90:1,b:+:B42a,t:w89:0,b:+:B42b,t:w91:1,b:+:BTest,t:w95:1,b:-:B31";
if (!FindWayCheckIfNoDoubleTurnouts(&way) == 0) {
printf ("%s:%d FindWayCheckIfNoDoubleTurnouts not ok\n", __FILE__, __LINE__);
raise (SIGINT);
}
way = "b:-:B43,t:w90:0,b:-:B1,t:w28:1,t:w32:0,t:w33:0,t:w33:0,t:w34:1,t:w35:0,t:w36:1,t:w31:1,t:w30:1,t:w28:1,b:+:B1,t:w90:1,b:+:B42a,t:w89:0,b:+:B42b,t:w91:1,b:+:BTest,t:w95:1,b:-:B31";
if (FindWayCheckIfNoDoubleTurnouts(&way) == 0) {
printf ("%s:%d FindWayCheckIfNoDoubleTurnouts not ok\n", __FILE__, __LINE__);
raise (SIGINT);
}
}; };

@ -169,7 +169,6 @@ public:
int LocomotiveSetAutoMan(string name) { return locomotives.SetModeAutoMan(name); }; int LocomotiveSetAutoMan(string name) { return locomotives.SetModeAutoMan(name); };
int LocomotiveSetAutoRand(string name) { return locomotives.SetModeAutoRand(name); }; int LocomotiveSetAutoRand(string name) { return locomotives.SetModeAutoRand(name); };
int LocomotiveSetAutoShed(string name) { return locomotives.SetModeAutoShed(name); }; int LocomotiveSetAutoShed(string name) { return locomotives.SetModeAutoShed(name); };
int LocomotiveTest(string name) { return locomotives.Test(name); };
///////////////////////////////////////// /////////////////////////////////////////
// Sensor // Sensor

@ -154,9 +154,6 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) {
else if (command.compare("locomotivereset") == 0) { else if (command.compare("locomotivereset") == 0) {
SetJSONLocoReset(jin); SetJSONLocoReset(jin);
} }
else if (command.compare("locomotivetest") == 0) {
SetJSONLocoTest(jin);
}
// //
// poweron / poweroff / save and resetdata // poweron / poweroff / save and resetdata
// //
@ -535,21 +532,6 @@ void Session::SetJSONLocoAssign(JSONParse *jp) {
}; };
void Session::SetJSONLocoTest(JSONParse *jp) {
string loco;
JSONParse jout;
jp->GetValue("locomotive", &loco);
server->LockThread();
server->LocomotiveTest(loco);
jout.Clear();
jout.AddObject("locomotive", server->LocomotiveGetJSON(loco));
if (network) network->ChangeListPushToAll(jout.ToString());
server->UnLockThread();
};
void Session::SetJSONLocoReset(JSONParse *jp) { void Session::SetJSONLocoReset(JSONParse *jp) {
string loco; string loco;
JSONParse jout; JSONParse jout;

Loading…
Cancel
Save