From f0bf67f740ac62b383a10ac0f9def35c734643aa Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sat, 23 Dec 2023 18:20:27 +0100 Subject: [PATCH] sensors are working again, split blocks should finally work --- server/block.cc | 113 ++++++++++++++++++++++++++++++++++--------- server/block.h | 8 +-- server/locomotive.cc | 33 +++++-------- 3 files changed, 106 insertions(+), 48 deletions(-) diff --git a/server/block.cc b/server/block.cc index 8d6bbf0..2d56cf9 100644 --- a/server/block.cc +++ b/server/block.cc @@ -380,59 +380,126 @@ int Blocks::GetFlags (string blname) { }; -string Blocks::GetSensorEnter (int direction, string blname) { - string res = ""; + +/* + * return a pointer to the blockdata in case of split blocks also return the second block + */ +int Blocks::GetBlocksPtr (string blname, Block **b1, Block **b2) { int i; - JSONParse jp; - Lock(); + (*b1) = NULL; + (*b2) = NULL; + for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { - res = blocks[i].s_enter[direction]; + (*b1) = &blocks[i]; } - UnLock(); - return res; -}; + if ((*b1) == NULL) { + debug (0, "Blocks::%s Could not find block: %s", __FUNCTION__, blname.c_str()); + return 0; + } + + if ((*b1)->flags & BLOCK_F_SPLIT) { + for (i = 0; i < max; i++) if (strcmp ((*b1)->secondblock, blocks[i].name) == 0) { + (*b2) = &blocks[i]; + } + if ((*b2) == NULL) { + debug (0, "Blocks::%s Could not find second block: %s", __FUNCTION__, (*b1)->secondblock); + return 0; + } + } + return 1; +} -string Blocks::GetSensorSlow (int direction, string blname) { - std::string res = ""; - int i; + +string Blocks::GetSensorEnter (int direction, string blname, int locoflags) { + string res = ""; JSONParse jp; + Block *b1 = NULL; + Block *b2 = NULL; Lock(); - for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { - res = blocks[i].s_slow[direction]; + if (!GetBlocksPtr(blname, &b1, &b2)) return res; + + if (!(locoflags & LOCO_F_SHORTTRAIN) && (b1->flags & BLOCK_F_SPLIT)) { + if (direction == 0) { + if (b1->flags & BLOCK_F_SPLITPOS) res = b1->s_enter[direction]; + else res = b2->s_enter[direction]; + } + else { + if (b1->flags & BLOCK_F_SPLITPOS) res = b2->s_enter[direction]; + else res = b1->s_enter[direction]; + } + } + + if (res.length() == 0) { + res = b1->s_enter[direction]; } + UnLock(); return res; }; -string Blocks::GetSensorStop (int direction, string blname) { +string Blocks::GetSensorSlow (int direction, string blname, int locoflags) { string res = ""; - int i; JSONParse jp; + Block *b1 = NULL; + Block *b2 = NULL; Lock(); - for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { - res = blocks[i].s_stop[direction]; + if (!GetBlocksPtr(blname, &b1, &b2)) return res; + + if (!(locoflags & LOCO_F_SHORTTRAIN) && (b1->flags & BLOCK_F_SPLIT)) { + if (direction == 0) { + if (b1->flags & BLOCK_F_SPLITPOS) res = b2->s_slow[direction]; + else res = b1->s_slow[direction]; + } + else { + if (b1->flags & BLOCK_F_SPLITPOS) res = b1->s_slow[direction]; + else res = b2->s_slow[direction]; + } + } + if (res.length() == 0) { + res = b1->s_slow[direction]; } + + if (res.length() == 0) { + res = b1->s_enter[direction]; + } + UnLock(); return res; }; -string Blocks::GetSensorShortStop (int direction, string blname) { +string Blocks::GetSensorStop (int direction, string blname, int locoflags) { string res = ""; - int i; JSONParse jp; - + Block *b1 = NULL; + Block *b2 = NULL; + Lock(); - for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { - res = blocks[i].s_shortstop[direction]; + if (!GetBlocksPtr(blname, &b1, &b2)) return res; + + if ((locoflags & LOCO_F_SHORTTRAIN) && b1->s_shortstop[direction][0] != 0) + res = b1->s_shortstop[direction]; + + if (!(locoflags & LOCO_F_SHORTTRAIN) && (b1->flags & BLOCK_F_SPLIT)) { + if (direction == 0) { + if (b1->flags & BLOCK_F_SPLITPOS) res = b2->s_stop[direction]; + else res = b1->s_stop[direction]; + } + else { + if (b1->flags & BLOCK_F_SPLITPOS) res = b1->s_stop[direction]; + else res = b2->s_stop[direction]; + } + } + if (res.length() == 0) { + res = b1->s_stop[direction]; } + UnLock(); return res; }; - diff --git a/server/block.h b/server/block.h index d0ed9fd..afe384d 100644 --- a/server/block.h +++ b/server/block.h @@ -72,6 +72,7 @@ class Blocks { int last_blockidx; // to speed up things Block* FindBlock(string name); + int GetBlocksPtr(string blname, Block **b1, Block **b2); public: Blocks(); ~Blocks(); @@ -89,10 +90,9 @@ class Blocks { int SetOff(string blname); int IsOff(string blname); - string GetSensorEnter (int direction, string blname); - string GetSensorSlow (int direction, string blname); - string GetSensorStop (int direction, string blname); - string GetSensorShortStop (int direction, string blname); + string GetSensorEnter (int direction, string blname, int locoflags); + string GetSensorSlow (int direction, string blname, int locoflags); + string GetSensorStop (int direction, string blname, int locoflags); int GetFlags (string blname); string GetSecondBlock(string block); diff --git a/server/locomotive.cc b/server/locomotive.cc index 1bb47a7..176392a 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -859,7 +859,6 @@ int Locomotives::Loco_OnRoute(Locomotive *loco) { string s_enter; string s_slow; string s_stop; - string s_shortstop; int dir_reverse = 0; char *tmpc = NULL; string way; @@ -874,18 +873,16 @@ int Locomotives::Loco_OnRoute(Locomotive *loco) { if (loco->blocknext[0] == '-') dir_reverse = 1; else dir_reverse = 0; - s_shortstop = server->blocks.GetSensorShortStop(dir_reverse, block); - s_stop = server->blocks.GetSensorStop(dir_reverse, block); - s_slow = server->blocks.GetSensorSlow(dir_reverse, block); - s_enter = server->blocks.GetSensorEnter(dir_reverse, block); + s_stop = server->blocks.GetSensorStop(dir_reverse, block, loco->flags); + s_slow = server->blocks.GetSensorSlow(dir_reverse, block, loco->flags); + s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags); if ( server->sensors.GetActive(s_enter) == 1 || server->sensors.GetActive(s_stop) == 1 || - server->sensors.GetActive(s_slow) == 1 || server->sensors.GetActive(s_shortstop) == 1) { // entering block? + server->sensors.GetActive(s_slow) == 1) { // entering block? debug (0, "* Locomotive '%s' EnterBlock '%s'", loco->name, loco->blocknext); debug (0, "* %s,%d Sensor Enter '%s' = %d", __FILE__, __LINE__, s_enter.c_str(), server->sensors.GetActive(s_enter)); debug (0, "* %s,%d Sensor Slow '%s' = %d", __FILE__, __LINE__, s_slow.c_str(), server->sensors.GetActive(s_slow)); - debug (0, "* %s,%d Sensor Sh.Stop '%s' = %d", __FILE__, __LINE__, s_shortstop.c_str(), server->sensors.GetActive(s_shortstop)); debug (0, "* %s,%d Sensor Stop '%s' = %d", __FILE__, __LINE__, s_stop.c_str(), server->sensors.GetActive(s_stop)); // assignment <-- next, the assignment has to be checked @@ -943,7 +940,6 @@ int Locomotives::Loco_BlockEnterStop(Locomotive *loco) { string s_enter; string s_slow; string s_stop; - string s_shortstop; int dir_reverse = 0; string way; @@ -955,14 +951,11 @@ int Locomotives::Loco_BlockEnterStop(Locomotive *loco) { if (loco->blockassign[0] == '-') dir_reverse = 1; else dir_reverse = 0; - s_shortstop = server->blocks.GetSensorShortStop(dir_reverse, block); - s_stop = server->blocks.GetSensorStop(dir_reverse, block); - s_slow = server->blocks.GetSensorSlow(dir_reverse, block); - s_enter = server->blocks.GetSensorEnter(dir_reverse, block); - - if ( server->sensors.GetActive(s_stop) == 1 || - ((loco->flags & LOCO_F_SHORTTRAIN) && server->sensors.GetActive(s_shortstop) == 1)) { + s_stop = server->blocks.GetSensorStop(dir_reverse, block, loco->flags); + s_slow = server->blocks.GetSensorSlow(dir_reverse, block, loco->flags); + s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags); + if (server->sensors.GetActive(s_stop) == 1) { debug (0, "Locomotives::Loop EnterBlockStop '%s' UnLockWay\n", loco->name); SetSpeed(loco->name, 0); loco->auto_onroute = LOCO_OR_STOPWAIT; @@ -996,13 +989,11 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) { if (loco->blockassign[0] == '-') dir_reverse = 1; else dir_reverse = 0; - s_shortstop = server->blocks.GetSensorShortStop(dir_reverse, block); - s_stop = server->blocks.GetSensorStop(dir_reverse, block); - s_slow = server->blocks.GetSensorSlow(dir_reverse, block); - s_enter = server->blocks.GetSensorEnter(dir_reverse, block); + s_stop = server->blocks.GetSensorStop(dir_reverse, block, loco->flags); + s_slow = server->blocks.GetSensorSlow(dir_reverse, block, loco->flags); + s_enter = server->blocks.GetSensorEnter(dir_reverse, block, loco->flags); - if ( server->sensors.GetActive(s_stop) == 1 || - ((loco->flags & LOCO_F_SHORTTRAIN) && server->sensors.GetActive(s_shortstop) == 1)) { + if (server->sensors.GetActive(s_stop) == 1) { if (loco->auto_timenext.tv_sec > 0) { //