From 1bb15da3463c584791fd56e43f9f3b802a5d0ddf Mon Sep 17 00:00:00 2001 From: steffen Date: Mon, 25 Jan 2021 23:30:15 +0000 Subject: [PATCH] something works.. unlocking of blocks has to be done --- server/block.cc | 44 ++++++++++++++++- server/block.h | 4 ++ server/locomotive.cc | 115 +++++++++++++++++++++++++++++++++++++++---- server/locomotive.h | 5 +- server/main.cc | 1 - server/railway.cc | 20 ++++---- server/sensor.cc | 16 ++++++ server/sensor.h | 1 + server/server.cc | 3 +- 9 files changed, 184 insertions(+), 25 deletions(-) diff --git a/server/block.cc b/server/block.cc index 39f3171..12a5651 100644 --- a/server/block.cc +++ b/server/block.cc @@ -297,5 +297,47 @@ void Blocks::ClearLockedby(string name) { } } UnLock(); -} +}; + + +string Blocks::GetSensorMinus (string blname) { + string res = ""; + int i; + JSONParse jp; + + Lock(); + for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { + res = blocks[i].s_neg_1; + } + UnLock(); + return res; +}; + + +string Blocks::GetSensorCenter (string blname) { + string res = ""; + int i; + JSONParse jp; + + Lock(); + for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { + res = blocks[i].s_center; + } + UnLock(); + return res; +}; + + +string Blocks::GetSensorPlus (string blname) { + string res = ""; + int i; + JSONParse jp; + + Lock(); + for (i = 0; i < max; i++) if (blname.compare(blocks[i].name) == 0) { + res = blocks[i].s_pos_1; + } + UnLock(); + return res; +}; diff --git a/server/block.h b/server/block.h index cb5e7c6..1d23c4f 100644 --- a/server/block.h +++ b/server/block.h @@ -55,6 +55,10 @@ class Blocks { int SetOff(string blname); int IsOff(string blname); + string GetSensorMinus (string blname); + string GetSensorCenter (string blname); + string GetSensorPlus (string blname); + int SetLockedby (string blname, string lockedby, int lock_onoff); string GetLockedby (string blname); void ClearLockedby(string name); diff --git a/server/locomotive.cc b/server/locomotive.cc index e92ec1c..ab49257 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -123,6 +123,7 @@ Locomotive Locomotives::GetLocomotiveFromJSON(JSONParse *j) { l.blockprev[0] = 0; l.auto_way[0] = 0; l.auto_onroute = 0; + l.auto_timenext = {0}; j->GetValue("name", &s); strncpy (l.name, s.c_str(), REFERENCENAME_LEN); @@ -213,6 +214,7 @@ int Locomotives::Delete(string name) { locomotives[i].flags = 0; locomotives[i].auto_way[0] = 0; locomotives[i].auto_onroute = -1; + locomotives[i].auto_timenext = {0}; changed = 1; break; } @@ -290,16 +292,6 @@ int Locomotives::SetDestination (string name, string block, int direction) { if (direction) snprintf (locomotives[i].blockdest, REFERENCENAME_LEN, "-:%s", block.c_str()); else snprintf (locomotives[i].blockdest, REFERENCENAME_LEN, "+:%s", block.c_str()); printf ("%s:%d Locomotive '%s' blockdest:%s\n", __FILE__, __LINE__, locomotives[i].name, locomotives[i].blockdest); - if (server->railways.FindWay(locomotives[i].blockassign, locomotives[i].blockdest, locomotives[i].name, &way)) { - size_t pos; - - if ((pos = way.find(",b:", 1)) != string::npos) { - strncpy (locomotives[i].blocknext, way.substr(pos+3,string::npos).c_str(), REFERENCENAME_LEN); - strncpy (locomotives[i].auto_way, way.c_str(), WAYDATA_LEN); - server->railways.LockWay(way, locomotives[i].name); - } - } - printf ("%s:%d Locomotive '%s' blockdest:%s\n", __FILE__, __LINE__, locomotives[i].name, locomotives[i].blockdest); break; } UnLock(); @@ -379,6 +371,8 @@ int Locomotives::SetMan(string name) { if (name.compare(locomotives[i].name) == 0) { debug (0, "Locomotives::SetMan (Name:%s)\n", name.c_str()); locomotives[i].flags &= ~(LOCO_F_AUTO | LOCO_F_AUTOSTOP | LOCO_F_RANDOM); + locomotives[i].auto_onroute = 0; + locomotives[i].auto_way[0] = 0; break; } UnLock(); @@ -411,6 +405,7 @@ int Locomotives::SetAuto(string name) { if (name.compare(locomotives[i].name) == 0) { debug (0, "Locomotives::SetAuto (Name:%s)\n", name.c_str()); locomotives[i].flags |= LOCO_F_AUTO; + locomotives[i].auto_onroute = 0; break; } UnLock(); @@ -551,6 +546,106 @@ int Locomotives::AutoCheckWaySingleStep(string way, string locname) { +int Locomotives::Loop() { + int lnum; + string way; + Locomotive *loco = NULL; + string block; + string sensor; + + for (lnum = 0; lnum < max; lnum++) if (locomotives[lnum].name[0] != 0) { + loco = &locomotives[lnum]; + if (loco->flags & LOCO_F_AUTO) { + // + // only in automate do anything alone + // + if (loco->blockassign[0] == 0 || loco->blockdest[0] == 0) continue; + if (loco->auto_onroute == LOCO_OR_NOTHING) { + timer_start(&loco->auto_timenext); + loco->auto_onroute = LOCO_OR_SEARCH; + } + if (loco->auto_onroute == LOCO_OR_SEARCH) { + if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 1000) { + printf ("%s:%d Locomotive::Loop '%s' auto_onroute:%d\n", __FILE__, __LINE__, + loco->name, loco->auto_onroute); + if (server->railways.FindWay(loco->blockassign, loco->blockdest, loco->name, &way)) { + size_t pos; + + if ((pos = way.find(",b:", 1)) != string::npos) { + strncpy (loco->blocknext, way.substr(pos+3,string::npos).c_str(), REFERENCENAME_LEN); + strncpy (loco->auto_way, way.c_str(), WAYDATA_LEN); + if (server->railways.LockWay(way, loco->name) == 1) { + loco->auto_onroute = LOCO_OR_PREPARE; + } + else timer_start(&loco->auto_timenext); + } + } + else timer_start(&loco->auto_timenext); + } + } + + else if (loco->auto_onroute == LOCO_OR_PREPARE) { + if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 1000) { + if (AutoCheckWaySingleStep(loco->auto_way, loco->name) == 1) + loco->auto_onroute = LOCO_OR_ONTHEWAY; + else timer_start(&loco->auto_timenext); + } + } + else if (loco->auto_onroute == LOCO_OR_ONTHEWAY) { + if (loco->flags & LOCO_F_CARGO) SetSpeed(loco->name, loco->vmid); + else SetSpeed(loco->name, loco->vfast); + + loco->auto_onroute = LOCO_OR_ONTHEWAYPREPARE; + } + + else if (loco->auto_onroute == LOCO_OR_ONTHEWAYPREPARE) { + // check enter block + block = loco->blocknext+2; + + if (loco->blocknext[0] == '-') + sensor = server->blocks.GetSensorMinus(block); + else + sensor = server->blocks.GetSensorPlus(block); + if (server->sensors.GetActive(sensor) == 1) { + SetSpeed(loco->name, loco->vslow); + loco->auto_onroute = LOCO_OR_ENTERBLOCK; + + if (loco->blockassign != 0) { + server->blocks.SetLockedby((string)(loco->blockassign+2), loco->name, 0); + } + } + } + else if (loco->auto_onroute == LOCO_OR_ENTERBLOCK) { + // check enter block + block = loco->blocknext+2; + + if (loco->blocknext[0] == '-') + sensor = server->blocks.GetSensorPlus(block); + else + sensor = server->blocks.GetSensorMinus(block); + + if (server->sensors.GetActive(sensor) == 1) { + debug (0, "Locomotives::Loop '%s' UnLockWay\n", loco->name); + SetSpeed(loco->name, 0); + loco->auto_onroute = LOCO_OR_STOPWAIT; + server->railways.UnLockWay(loco->auto_way, loco->name); + strncpy (loco->blockassign, loco->blocknext, REFERENCENAME_LEN); + loco->blocknext[0] = 0; + timer_start(&loco->auto_timenext); + } + } + else if (loco->auto_onroute == LOCO_OR_STOPWAIT) { + if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 5000) { + loco->auto_onroute = LOCO_OR_SEARCH; + } + } + } + } + + return 0; +} + + int Locomotives::Test(string loco) { int res = 0; int i; diff --git a/server/locomotive.h b/server/locomotive.h index bcf8e45..575f531 100644 --- a/server/locomotive.h +++ b/server/locomotive.h @@ -33,8 +33,7 @@ enum { LOCO_OR_ONTHEWAYPREPARE, // prepare way as long as on the way LOCO_OR_ENTERBLOCK, // got new block ready? // if NEXT is empty and way not AutoPrepareWay not finished... slow down - LOCO_OR_STOP, // stopping - LOCO_OR_WAIT // wait some time + LOCO_OR_STOPWAIT // stopping }; struct s_LocoAuto { @@ -64,6 +63,7 @@ struct s_Locomotive { char auto_way[WAYDATA_LEN]; // route to way "b:+blockname,t:name:0,t:name:1,b:-blockname" int auto_onroute; // LOCO_OR_.... + struct timeval auto_timenext; // timeval of the next active step } typedef Locomotive; class Locomotives { @@ -102,6 +102,7 @@ class Locomotives { int SetAssign (string name, string block, int direction); int AutoCheckWaySingleStep(string way, string locname); + int Loop(); string GetName(int idx); diff --git a/server/main.cc b/server/main.cc index eb472de..2014261 100644 --- a/server/main.cc +++ b/server/main.cc @@ -125,4 +125,3 @@ int timer_get(struct timeval *tv) { int timevaldiff (struct timeval *tv1, struct timeval *tv2) { return ((tv1->tv_sec - tv2->tv_sec)*1000 + (tv1->tv_usec - tv2->tv_usec)/1000); }; - diff --git a/server/railway.cc b/server/railway.cc index 5fd8716..22c366b 100644 --- a/server/railway.cc +++ b/server/railway.cc @@ -573,7 +573,7 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri // this will be done in respect of lockedby blocks and ways. // while ((fd_list.size() > 0 || fd_pos.enterfrom >= 0) && found == 0) { - DebugPrintFindWay(fd_data); + // DebugPrintFindWay(fd_data); if (fd_pos.enterfrom < 0) { std::list::iterator iter; @@ -581,13 +581,13 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri if (iter != fd_list.end()) { fd_pos = *iter; fd_list.pop_front(); - printf ("get from stack (%d,%d)\n", fd_pos.x, fd_pos.y); + // printf ("get from stack (%d,%d)\n", fd_pos.x, fd_pos.y); } } - printf ("%s:%d fd_start (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_start.x, fd_start.y, fd_start.enterfrom); - printf ("%s:%d fd_end (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_end.x, fd_end.y, fd_end.enterfrom); - printf ("%s:%d fd_pos (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_pos.x, fd_pos.y, fd_pos.enterfrom); + // printf ("%s:%d fd_start (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_start.x, fd_start.y, fd_start.enterfrom); + // printf ("%s:%d fd_end (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_end.x, fd_end.y, fd_end.enterfrom); + // printf ("%s:%d fd_pos (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_pos.x, fd_pos.y, fd_pos.enterfrom); if (fd_pos.enterfrom < 0 && fd_pos.enterfrom > 3) { fd_pos.enterfrom = -1; fd_pos.x = -1; @@ -692,7 +692,7 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri else { // lockedby = server->blocks.GetLockedby(rpos->name); lockedby = server->blocks.GetLockedby(rpos->name); - if (lockedby.length() == 0) { + if (lockedby.length() == 0 || lockedby.compare(lockedfor) == 0) { if (fd_pos.enterfrom == 0 || fd_pos.enterfrom == 3) fd_pos.way += (string)",b:+:" + (string)rpos->name; else fd_pos.way += (string)",b:-:" + (string)rpos->name; @@ -718,7 +718,7 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri fd_pos.way = ""; } } - printf ("%s:%d fd_pos (%d,%d) enterfrom:%d\n\n", __FILE__, __LINE__, fd_pos.x, fd_pos.y, fd_pos.enterfrom); + //printf ("%s:%d fd_pos (%d,%d) enterfrom:%d\n\n", __FILE__, __LINE__, fd_pos.x, fd_pos.y, fd_pos.enterfrom); } if (found == 0) *next = ""; @@ -759,7 +759,7 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) { int finished = 0; JSONParse jp; - debug (0, "* LockWay Way:'%s' for '%s'", way.c_str(), lockedby.c_str()); + debug (0, "* LockWay Way:'%s' for '%s' lockonoff:%d", way.c_str(), lockedby.c_str(), lockonoff); end.x = start.x = -1; end.y = start.y = -1; @@ -807,8 +807,8 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) { return 0; } - printf ("%s:%d LockWay Way:'%s' start [%d,%d -> %d] to [%d,%d -> %d]\n", __FILE__, __LINE__, way.c_str(), - start.x, start.y, start.enterfrom, end.x, end.y, end.enterfrom); + printf ("%s:%d LockWay Way:'%s' start [%d,%d -> %d] to [%d,%d -> %d] LockenOnOff:%d\n", __FILE__, __LINE__, way.c_str(), + start.x, start.y, start.enterfrom, end.x, end.y, end.enterfrom, lockonoff); // // lock way depending on Way diff --git a/server/sensor.cc b/server/sensor.cc index 7774ffa..b773da0 100644 --- a/server/sensor.cc +++ b/server/sensor.cc @@ -17,6 +17,22 @@ Sensors::~Sensors() { }; +int Sensors::GetActive(string name) { + int i; + int res = 0; + + Lock(); + for (i = 0; i < max; i++) if (sensors[i].name[0] != 0) { + if (name.compare(sensors[i].name) == 0) { + if (sensors[i].flags & SENSOR_F_ACTIVE) res = 1; + break; + } + } + + UnLock(); + + return res; +}; int Sensors::Lock() { diff --git a/server/sensor.h b/server/sensor.h index 8054378..c0136ea 100644 --- a/server/sensor.h +++ b/server/sensor.h @@ -36,6 +36,7 @@ class Sensors { int Change(Sensor *se); int Get(int idx, Sensor *dest); + int GetActive(string name); int Get(string name, Sensor *dest); int Delete(string name); diff --git a/server/server.cc b/server/server.cc index 46f623d..7993a78 100644 --- a/server/server.cc +++ b/server/server.cc @@ -84,7 +84,7 @@ void Server::ThreadProcess() { if (mode == SMODE_STARTUP) { mode = SMODE_MANUAL; printf ("%s:%d ************************************************************ fix me\n", __FILE__, __LINE__); - // SetModeReset(); // currently there is not much to do. + SetModeReset(); // currently there is not much to do. } // @@ -95,6 +95,7 @@ void Server::ThreadProcess() { // // mode manual else if (mode == SMODE_MANUAL) { + server->locomotives.Loop(); } //