1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00

drop any acquired locos on Q(uit), reduce heartbeat to 2s

also some indentation fixes
note that 2s heartbeat works best with next version of ED.
This commit is contained in:
mstevetodd 2020-08-13 16:38:22 -04:00
parent 5256c1d021
commit 68255dcadd
2 changed files with 25 additions and 16 deletions

View File

@ -48,7 +48,7 @@
#include "DIAG.h" #include "DIAG.h"
#define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco<MAX_MY_LOCO;loco++) \ #define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco<MAX_MY_LOCO;loco++) \
if (myLocos[loco].throttle==THROTTLECHAR && (CAB<0 || myLocos[loco].cab==CAB)) if ((myLocos[loco].throttle==THROTTLECHAR || '*'==THROTTLECHAR) && (CAB<0 || myLocos[loco].cab==CAB))
WiThrottle * WiThrottle::firstThrottle=NULL; WiThrottle * WiThrottle::firstThrottle=NULL;
bool WiThrottle::annotateLeftRight=false; bool WiThrottle::annotateLeftRight=false;
@ -59,7 +59,7 @@ WiThrottle* WiThrottle::getThrottle( int wifiClient) {
return new WiThrottle( wifiClient); return new WiThrottle( wifiClient);
} }
// One instance of WiTHrottle per connected client, so we know what the locos are // One instance of WiThrottle per connected client, so we know what the locos are
WiThrottle::WiThrottle( int wificlientid) { WiThrottle::WiThrottle( int wificlientid) {
DIAG(F("\nCreating new WiThrottle for client %d\n"),wificlientid); DIAG(F("\nCreating new WiThrottle for client %d\n"),wificlientid);
@ -164,7 +164,15 @@ void WiThrottle::parse(Print & stream, byte * cmdx) {
} }
break; break;
case 'Q': // case 'Q': //
DIAG(F("WiThrottle Quit\n")); LOOPLOCOS('*', -1) { //stop and drop all locos still assigned to this WiThrottle
if (myLocos[loco].throttle!='\0') {
DCC::setThrottle(myLocos[loco].cab,0,1);
DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab);
myLocos[loco].throttle='\0';
}
}
DIAG(F("WiThrottle(%d) Quit\n"), clientid);
delete this; delete this;
break; break;
} }
@ -228,10 +236,10 @@ void WiThrottle::multithrottle(Print & stream, byte * cmd){
} }
} }
break; break;
case '-': // remove loco case '-': // stop and remove loco(s)
LOOPLOCOS(throttleChar, locoid) { LOOPLOCOS(throttleChar, locoid) {
myLocos[loco].throttle='\0'; myLocos[loco].throttle='\0';
DCC::setThrottle(myLocos[loco].cab,0,1); DCC::setThrottle(myLocos[loco].cab,0, DCC::getThrottleDirection(myLocos[loco].cab));
DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab); StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab);
} }
@ -313,9 +321,10 @@ void WiThrottle::loop() {
void WiThrottle::checkHeartbeat() { void WiThrottle::checkHeartbeat() {
// if 2 heartbeats missed... STOP and forget all locos for this client // if 2 heartbeats missed... STOP and forget all locos for this client
if(heartBeatEnable && (millis()-heartBeat > HEARTBEAT_TIMEOUT*2000)) { if(heartBeatEnable && (millis()-heartBeat > HEARTBEAT_TIMEOUT*2000)) {
DIAG(F("WiThrottle hearbeat missed client=%d"),clientid); DIAG(F("WiThrottle(%d) hearbeat missed, dropping connection"),clientid);
for (int loco=0;loco<MAX_MY_LOCO;loco++) { LOOPLOCOS('*', -1) { //stop and drop all locos still assigned to this WiThrottle
if (myLocos[loco].throttle!='\0') { if (myLocos[loco].throttle!='\0') {
DIAG(F(" dropping cab %c"),clientid, myLocos[loco].cab);
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); //eStop DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab)); //eStop
DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address DCC::forgetLoco(myLocos[loco].cab); //unregister this loco address
} }

View File

@ -36,7 +36,7 @@ class WiThrottle {
~WiThrottle(); ~WiThrottle();
static const int MAX_MY_LOCO=10; //maximum number of locos assigned to a single client static const int MAX_MY_LOCO=10; //maximum number of locos assigned to a single client
static const int HEARTBEAT_TIMEOUT=3;// heartbeat at 3secs to provide messaging transport static const int HEARTBEAT_TIMEOUT=2;// heartbeat at 2secs to provide messaging transport
static WiThrottle* firstThrottle; static WiThrottle* firstThrottle;
static int getInt(byte * cmd); static int getInt(byte * cmd);
static int getLocoId(byte * cmd); static int getLocoId(byte * cmd);