X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fdummy.cpp;h=90638f63bd8c438b2cfa845a7c0d20173992df41;hb=9f0d428e974defa64cdf8e7b7072967327250958;hp=9ccb1bcceb94f6fe9630b8ad04e328a5feffcec8;hpb=73e4a36bbf828e07b83a779b696875e1f80045cc;p=r2c2.git diff --git a/source/libr2c2/dummy.cpp b/source/libr2c2/dummy.cpp index 9ccb1bc..90638f6 100644 --- a/source/libr2c2/dummy.cpp +++ b/source/libr2c2/dummy.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include "dummy.h" @@ -7,22 +9,12 @@ using namespace Msp; namespace R2C2 { -Dummy::Dummy(const string ¶ms): - power(true) -{ - vector opts = split(params, ':'); - for(vector::const_iterator i=opts.begin(); i!=opts.end(); ++i) - { - string::size_type equals = i->find('='); - if(equals!=string::npos) - { - string name = i->substr(0, equals); - string value = i->substr(equals+1); - if(name=="turnout_delay") - turnout_delay = lexical_cast(value)*Time::msec; - } - } -} +Dummy::Dummy(const Options &opts): + power(true), + halted(false), + turnout_delay(opts.get("turnout_delay", 0.0f)*Time::sec), + turnout_fail_rate(opts.get("turnout_fail_rate", 0.0f)*RAND_MAX) +{ } void Dummy::set_power(bool p) { @@ -30,6 +22,18 @@ void Dummy::set_power(bool p) signal_power.emit(power); } +void Dummy::halt(bool h) +{ + halted = h; + if(halted) + { + for(map::iterator i=locos.begin(); i!=locos.end(); ++i) + if(i->second.speed) + set_loco_speed(i->first, 0); + } + signal_halt.emit(halted); +} + const char *Dummy::enumerate_protocols(unsigned i) const { if(i==0) @@ -62,7 +66,8 @@ void Dummy::set_turnout(unsigned addr, unsigned state) turnout.timeout = Time::now()+turnout_delay; else { - turnout.state = state; + if(!turnout_fail_rate || rand()>=turnout_fail_rate) + turnout.state = state; signal_turnout.emit(addr, state); } } @@ -77,6 +82,9 @@ unsigned Dummy::get_turnout(unsigned addr) const void Dummy::set_loco_speed(unsigned addr, unsigned speed) { + if(speed && halted) + return; + LocoState &loco = locos[addr]; loco.speed = speed; signal_loco_speed.emit(addr, speed, loco.reverse); @@ -111,6 +119,11 @@ bool Dummy::get_sensor(unsigned addr) const return false; } +float Dummy::get_telemetry_value(const string &name) const +{ + throw key_error(name); +} + void Dummy::tick() { Time::TimeStamp t = Time::now(); @@ -118,6 +131,11 @@ void Dummy::tick() { if(i->second.timeout && t>=i->second.timeout) { + if(turnout_fail_rate && rand()first); + i->second.pending = i->second.state; + } i->second.state = i->second.pending; i->second.timeout = Time::TimeStamp(); signal_turnout.emit(i->first, i->second.state);