From b370dbf930745e3a37d546e838552183d79d7691 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sun, 4 Feb 2024 16:41:27 +0100 Subject: [PATCH] AutoCheckWaySingleStep: stops preparing way at next block --- ChangeLog | 3 ++ server/locomotive.cc | 67 ++++++++++++++++++++++---------------------- server/locomotive.h | 4 +-- server/network.h | 1 - server/railway.cc | 13 --------- server/server.h | 1 - server/session.cc | 18 ------------ 7 files changed, 38 insertions(+), 69 deletions(-) diff --git a/ChangeLog b/ChangeLog index 93bb3dd..585bc5c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2024-01-04: +- fixed: AutoCheckWaySingleStep: stops preparing way at next block + 2024-01-16: - fixed: not all pthread_mutexes has been initialization. diff --git a/server/locomotive.cc b/server/locomotive.cc index b0c8deb..7f66a1d 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -700,33 +700,51 @@ int Locomotives::SchedulerNextStep(Locomotive *loc) { return 0; } + + // // 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. // 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 // -int Locomotives::AutoCheckWaySingleStep(string way, string locname, int *data) { - int res = 0; +int Locomotives::AutoCheckWaySingleStep(string way, Locomotive *loc, int *data) { size_t pos1; size_t curpos; - size_t blockpos; int cnt; int state; int newdata = 0; string turnout; + string nextblock; + size_t nextblockpos; + int blflags = 0; int x, y; Railway r; 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; do { // // read all ways from the begin with stop at "data" curpos = way.find (",t:", curpos+1); + if (curpos != string::npos && nextblockpos != string::npos && curpos > nextblockpos) break; + newdata++; if (curpos != string::npos) { 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++)) { cnt++; 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) { - 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); return 0; } 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); return 0; } 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] == '1') server->turnouts.Set(way.substr(curpos+3, pos1-curpos-3), 1); (*data) = newdata; @@ -775,15 +793,14 @@ int Locomotives::AutoCheckWaySingleStep(string way, string locname, int *data) { } } 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; } } } while (curpos != string::npos); - if (curpos == string::npos) res = 1; - - return res; + if (curpos == string::npos || (nextblockpos != string::npos && curpos > nextblockpos)) return 1; + return 0; } @@ -883,7 +900,7 @@ int Locomotives::Loco_PrepareWay(Locomotive *loco) { 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 (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) || (server->blocks.GetFlags(block) & BLOCK_F_SPEEDLIMIT)) return 1; else return 2; @@ -1045,7 +1062,7 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) { s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags); 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. // 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) { 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 debug (0, "* Locomotives::Loco_BlockEnterNext finished preparing way for loco: %s", loco->name); @@ -1252,21 +1269,3 @@ int Locomotives::Loop() { 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; -}; - diff --git a/server/locomotive.h b/server/locomotive.h index dd8a597..83f180f 100644 --- a/server/locomotive.h +++ b/server/locomotive.h @@ -84,6 +84,7 @@ class Locomotives { JSONParse _GetJSON(int idx); int SchedulerNextStep(Locomotive *loc); // automode, calculate next step void SendUpdate(Locomotive *loc); + int Loco_SearchAndLock(Locomotive *loco); int Loco_PrepareWay(Locomotive *loco); int Loco_OnRoute(Locomotive *loco); @@ -114,7 +115,7 @@ class Locomotives { int SetAssign (string name, string block, int direction); int GetFlags(string name); - int AutoCheckWaySingleStep(string way, string locname, int *data); + int AutoCheckWaySingleStep(string way, Locomotive *loc, int *data); int Loop(); string GetName(int idx); @@ -122,7 +123,6 @@ class Locomotives { JSONParse GetJSON(string name); void GetJSONAll(JSONParse *json); Locomotive GetLocomotiveFromJSON(JSONParse *j); - int Test(string loco); }; diff --git a/server/network.h b/server/network.h index 8d1f676..f702b84 100644 --- a/server/network.h +++ b/server/network.h @@ -66,7 +66,6 @@ private: void SetJSONLocoDest(JSONParse *jp); void SetJSONLocoAssign(JSONParse *jp); void SetJSONLocoReset(JSONParse *jp); - void SetJSONLocoTest(JSONParse *jp); void SetJSONLocoMan(JSONParse *jp); void SetJSONLocoAutoMan(JSONParse *jp); void SetJSONLocoAuto(JSONParse *jp); diff --git a/server/railway.cc b/server/railway.cc index 7d28ec0..0ab783a 100644 --- a/server/railway.cc +++ b/server/railway.cc @@ -10,19 +10,6 @@ Railways::Railways() { railways = NULL; pthread_mutex_init(&mtx, NULL); 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); - } }; diff --git a/server/server.h b/server/server.h index d4e2a9b..7dc938e 100644 --- a/server/server.h +++ b/server/server.h @@ -169,7 +169,6 @@ public: int LocomotiveSetAutoMan(string name) { return locomotives.SetModeAutoMan(name); }; int LocomotiveSetAutoRand(string name) { return locomotives.SetModeAutoRand(name); }; int LocomotiveSetAutoShed(string name) { return locomotives.SetModeAutoShed(name); }; - int LocomotiveTest(string name) { return locomotives.Test(name); }; ///////////////////////////////////////// // Sensor diff --git a/server/session.cc b/server/session.cc index c752a34..09598e1 100644 --- a/server/session.cc +++ b/server/session.cc @@ -154,9 +154,6 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) { else if (command.compare("locomotivereset") == 0) { SetJSONLocoReset(jin); } - else if (command.compare("locomotivetest") == 0) { - SetJSONLocoTest(jin); - } // // 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) { string loco; JSONParse jout;