From 67c73836f83fddb2ff3d1c1129b5bfd573819071 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Mon, 18 Apr 2022 01:27:45 +0200 Subject: [PATCH] fixed AUTO->MAN stop mode is working on trains. --- ChangeLog | 6 ++++++ server/interface-z21.cc | 1 - server/locomotive.cc | 21 +++++++++++++++++---- server/main.cc | 11 +++++++++++ server/modelbahn.h | 2 +- server/server.cc | 11 +++++++++++ server/turnout.cc | 31 +++++++++++++++++++++++++++++++ server/turnout.h | 2 ++ 8 files changed, 79 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index da33665..a5d0d0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2022-04-17: +- added "-simulation" parmeter for debugging purpose. + needed to simulate turnout manipulation +- fixed: locomotive AUTO->MAN mode is working if searching for a new way. + + 2022-02-13: - Interfaces are now virtual classes. diff --git a/server/interface-z21.cc b/server/interface-z21.cc index ef3fe96..93b1591 100644 --- a/server/interface-z21.cc +++ b/server/interface-z21.cc @@ -419,7 +419,6 @@ void InterfaceZ21::SetTurnout(Turnout *t, int activate, int outputactive) { debug (DEBUG_INFO | DEBUG_IFACE, "InterfaceZ21 (%s) SetTurnout Addr:%d Acitve:%d Output:%d", name, t->addr, activate, outputactive); - // // if (outputactive == 1 && timer_get(&turnouttimeout) < 100) { diff --git a/server/locomotive.cc b/server/locomotive.cc index 8f76680..c64564b 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -749,7 +749,7 @@ int Locomotives::Loop() { // only in automate do anything alone // if (loco->blockassign[0] == 0) { - debug (0, "%s:%d Locomotive not assigned to any block. Set Mode to Man", __FILE__, __LINE__); + debug (0, "%s:%d Locomotive [%s] not assigned to any block. Set Mode to Man", __FILE__, __LINE__, loco->name); SetModeMan(loco->name); continue; } @@ -768,6 +768,13 @@ int Locomotives::Loop() { } else if (loco->auto_onroute == LOCO_OR_SEARCH) { + // + // check if the loco mode is set to autostop + if (loco->flags & LOCO_F_AUTOSTOP) { + SetModeMan(loco->name); + continue; + } + // // try to find and prepare(lock) a new way. // nothing found check if we can reverse direction, this will be done only @@ -893,14 +900,20 @@ int Locomotives::Loop() { strncpy (loco->auto_wayold, loco->auto_way, WAYDATA_LEN); loco->auto_way[0] = 0; loco->blocknext[0] = 0; - if (strncmp(loco->blockassign, loco->blockdest, REFERENCENAME_LEN) == 0) { - printf ("%s:%d LOCO_OR_ONTHEWAY enter assign == dest stop\n", __FILE__, __LINE__); + + // + // if destination reached or AUTO-MAN mode: stop train. + if ((strncmp(loco->blockassign, loco->blockdest, REFERENCENAME_LEN) == 0) + || (loco->flags & LOCO_F_AUTOSTOP)) { + printf ("%s:%d LOCO_OR_ONTHEWAY enter assign == dest stop train\n", __FILE__, __LINE__); loco->blockdest[0] = 0; loco->auto_onroute = LOCO_OR_ENTERBLOCKSTOP; SetSpeed(loco->name, reverse * loco->vslow); } + + // + // if not try to find next block. else { - // try to find new way printf ("%s:%d LOCO_OR_ONTHEWAY try to find new way\n", __FILE__, __LINE__); if (loco->blockdest[0] == 0) { if ((loco->flags & LOCO_F_RANDOM) && ((loco->flags & LOCO_F_CARGO) || !(server->blocks.GetFlags(block) & BLOCK_F_STATION))) diff --git a/server/main.cc b/server/main.cc index 38b2d7f..37d6911 100644 --- a/server/main.cc +++ b/server/main.cc @@ -18,8 +18,11 @@ int running = 1; Server *server = NULL; Network *network = NULL; +int simulation = 0; int main (int argc, char **argv) { + int i; + // // setup signals // @@ -47,6 +50,14 @@ int main (int argc, char **argv) { debug (0, "* *"); debug (0, "***************************************************"); debug (0, ""); + for (i = 1; i < argc; i++) { + if (strcmp (argv[i], "-simulation") == 0) { + simulation = 1; + debug (0, "WARINING SIMULATION MODE ACTIVE. ALL COMMANDS WILL BE SEND"); + debug (0, "TO THE INTERFACES. TURNOUT AND SENSORDATA WILL BE FORCED."); + sleep (5); + } + } ////////////////////////////////////////////////////////////////////// // diff --git a/server/modelbahn.h b/server/modelbahn.h index 67f4473..6a94013 100644 --- a/server/modelbahn.h +++ b/server/modelbahn.h @@ -27,7 +27,7 @@ extern int running; extern Server *server; extern Network *network; - +extern int simulation; // // to measure the time in ms (used for debugging) diff --git a/server/server.cc b/server/server.cc index 59fcb50..435ca36 100644 --- a/server/server.cc +++ b/server/server.cc @@ -74,6 +74,17 @@ int Server::TurnoutSet(string name, int active) { } } + if (simulation) { + int flags; + + debug (0, "%s:%d WARNING SIMULATION MODE", __FILE__, __LINE__); + flags = turnouts.GetFlags(turnouts.GetIdx(name)); + if (flags & TURNOUT_F_TURNOUT) flags &= ~TURNOUT_F_TURNOUT; + else flags |= TURNOUT_F_TURNOUT; + turnouts.SetFlags(turnouts.GetIdx(name), flags); + turnouts.Set(name, active); + } + if (locked == 0) return turnouts.Set(name, active); else { debug (0, "%s:%d SetJSONTurnout Element %s is locked by %s", __FILE__, __LINE__, name.c_str(), lockedby.c_str()); diff --git a/server/turnout.cc b/server/turnout.cc index d29b26b..f7007d2 100644 --- a/server/turnout.cc +++ b/server/turnout.cc @@ -337,3 +337,34 @@ int Turnouts::GetFlags(int idx) { }; +void Turnouts::SetFlags(int idx, int flags) { + JSONParse jp; + + Lock(); + if (idx <= max && idx >= 0) { + turnouts[idx].flags = flags; + jp.Clear(); + jp.AddObject("turnout", _GetJSON(idx)); + if (network) network->ChangeListPushToAll(jp.ToString()); + } + UnLock(); +}; + + +int Turnouts::GetIdx(string name) { + int i; + int res = -1; + + // + Lock(); + for (i = 0; i < max; i++) if (turnouts[i].name[0] != 0) { + if (name.compare(turnouts[i].name) == 0) { + res = i; + break; + } + } + UnLock(); + + return res; +} + diff --git a/server/turnout.h b/server/turnout.h index c4f2a04..50f191d 100644 --- a/server/turnout.h +++ b/server/turnout.h @@ -55,7 +55,9 @@ class Turnouts { Turnout GetTurnoutFromJSON(JSONParse *j); string GetName(int idx); + int GetIdx(string name); int GetFlags(int idx); + void SetFlags(int idx, int flags); void SetFromBus(string name, int addr, int active); };