]> git.tdb.fi Git - r2c2.git/commitdiff
Generalize turnouts to magnet accessories in the central station driver
authorMikko Rasa <tdb@tdb.fi>
Sun, 27 Jan 2013 13:21:13 +0000 (15:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 27 Jan 2013 13:21:13 +0000 (15:21 +0200)
source/libr2c2/centralstation.cpp
source/libr2c2/centralstation.h

index 3a9624af1ba17b0049b17a832e317ecb055bb4dc..833560ae727a5839151105b625da6e363a76a662 100644 (file)
@@ -20,7 +20,7 @@ CentralStation::CentralStation(const string &host):
        power(false),
        halted(false),
        locos_synced(false),
-       turnouts_synced(false),
+       accessories_synced(false),
        sensors_synced(false)
 {
        RefPtr<Net::SockAddr> addr = Net::resolve(host+":15471");
@@ -40,7 +40,7 @@ CentralStation::~CentralStation()
        command("release(1, view)", true);
        for(LocoMap::iterator i=locos.begin(); (i!=locos.end() && !(i->first&0x10000)); ++i)
                command(format("release(%d, view, control)", i->first));
-       for(TurnoutMap::iterator i=turnouts.begin(); (i!=turnouts.end() && !(i->first&0x10000)); ++i)
+       for(AccessoryMap::iterator i=accessories.begin(); (i!=accessories.end() && !(i->first&0x10000)); ++i)
                command(format("release(%d, view, control)", i->first));
        while(IO::poll(socket, IO::P_INPUT, 100*Time::msec))
                while(receive()) ;
@@ -161,73 +161,102 @@ void CentralStation::add_turnout(unsigned addr, const TrackType &type)
                        cross = true;
        }
 
-       unsigned symbol = Turnout::LEFT;
+       MagnetAccessory::Symbol symbol = MagnetAccessory::TURNOUT_LEFT;
        if(cross)
-               symbol = Turnout::DOUBLESLIP;
+               symbol = MagnetAccessory::TURNOUT_DOUBLESLIP;
        else if(left && right)
-               symbol = Turnout::THREEWAY;
+               symbol = MagnetAccessory::TURNOUT_THREEWAY;
        else if(left)
-               symbol = (straight ? Turnout::LEFT : Turnout::CURVED_LEFT);
+               symbol = (straight ? MagnetAccessory::TURNOUT_LEFT : MagnetAccessory::TURNOUT_CURVED_LEFT);
        else if(right)
-               symbol = (straight ? Turnout::RIGHT : Turnout::CURVED_RIGHT);
+               symbol = (straight ? MagnetAccessory::TURNOUT_RIGHT : MagnetAccessory::TURNOUT_CURVED_RIGHT);
 
-       unsigned id = map_address(turnouts, turnout_addr, addr);
+       MagnetAccessory &turnout = add_accessory(addr, MagnetAccessory::TURNOUT, symbol);
+       turnout.bits = type.get_state_bits();
+}
+
+void CentralStation::set_turnout(unsigned addr, unsigned state)
+{
+       set_accessory_state(addr, MagnetAccessory::TURNOUT, state);
+}
+
+unsigned CentralStation::get_turnout(unsigned addr) const
+{
+       return get_accessory_state(addr, MagnetAccessory::TURNOUT);
+}
+
+CentralStation::MagnetAccessory &CentralStation::add_accessory(unsigned addr, MagnetAccessory::Type type, MagnetAccessory::Symbol symbol)
+{
+       unsigned id = map_address(accessories, accessory_addr, addr);
        if(!id)
        {
                id = addr|0x10000;
 
-               Turnout &turnout = turnouts[id];
-               turnout.address = addr;
-               turnout.bits = type.get_state_bits();
-               turnout.symbol = symbol;
+               MagnetAccessory &accessory = accessories[id];
+               accessory.address = addr;
+               accessory.type = type;
+               accessory.symbol = symbol;
 
-               turnout_addr[addr] = id;
+               accessory_addr[addr] = id;
 
-               if(turnouts_synced)
+               if(accessories_synced)
                        command("create(11, append)");
+
+               return accessory;
        }
        else
        {
-               Turnout &turnout = turnouts[id];
+               MagnetAccessory &accessory = accessories[id];
                command(format("request(%d, view, control)", id));
-               if(turnout.symbol!=symbol)
+               if(accessory.symbol!=symbol)
                        command(format("set(%d, symbol[%d])", symbol));
+
+               return accessory;
        }
 }
 
-void CentralStation::set_turnout(unsigned addr, unsigned state)
+void CentralStation::set_accessory_state(unsigned addr, MagnetAccessory::Type type, unsigned state)
 {
-       unsigned id = map_address(turnouts, turnout_addr, addr);
+       unsigned id = map_address(accessories, accessory_addr, addr);
        if(id)
        {
-               Turnout &turnout = turnouts[id];
-               unsigned mask = (1<<turnout.bits)-1;
+               MagnetAccessory &accessory = accessories[id];
+               if(accessory.type!=type)
+                       throw logic_error("accessory type conflict");
+
+               unsigned mask = (1<<accessory.bits)-1;
 
-               if(((state^turnout.state)&mask)==0 || !turnout.synced)
+               if(((state^accessory.state)&mask)==0 || !accessory.synced)
                {
-                       turnout.state = state;
-                       signal_turnout.emit(addr, turnout.state);
+                       accessory.state = state;
+                       accessory_state_changed(accessory);
                        return;
                }
 
-               turnout.state = (turnout.state&mask) | (state&~mask);
+               accessory.state = (accessory.state&mask) | (state&~mask);
 
                command(format("set(%d, state[%d])", id, state&mask));
        }
 }
 
-unsigned CentralStation::get_turnout(unsigned addr) const
+unsigned CentralStation::get_accessory_state(unsigned addr, MagnetAccessory::Type type) const
 {
-       unsigned id = map_address(turnouts, turnout_addr, addr);
+       unsigned id = map_address(accessories, accessory_addr, addr);
        if(id)
        {
-               TurnoutMap::const_iterator i = turnouts.find(id);
-               if(i!=turnouts.end())
+               AccessoryMap::const_iterator i = accessories.find(id);
+               if(i!=accessories.end() && i->second.type==type)
                        return i->second.state;
        }
        return 0;
 }
 
+void CentralStation::accessory_state_changed(const MagnetAccessory &accessory) const
+{
+       if(accessory.type==MagnetAccessory::TURNOUT)
+               signal_turnout.emit(accessory.address, accessory.state);
+}
+
 void CentralStation::add_sensor(unsigned addr)
 {
        sensors.insert(SensorMap::value_type(addr, Sensor()));
@@ -319,8 +348,8 @@ void CentralStation::process_reply(const Message &msg)
        {
                for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
                {
-                       if(turnouts.count(i->first))
-                               turnouts[i->first].synced = true;
+                       if(accessories.count(i->first))
+                               accessories[i->first].synced = true;
 
                        process_object(i->first, i->second);
                }
@@ -372,8 +401,8 @@ void CentralStation::process_reply(const Message &msg)
        {
                for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
                {
-                       TurnoutMap::iterator j = turnouts.find(i->first);
-                       if(j==turnouts.end())
+                       AccessoryMap::iterator j = accessories.find(i->first);
+                       if(j==accessories.end())
                        {
                                bool found = false;
                                Message::AttribMap::const_iterator k = i->second.find("addr");
@@ -381,30 +410,30 @@ void CentralStation::process_reply(const Message &msg)
                                {
                                        unsigned addr = lexical_cast<unsigned>(k->second);
 
-                                       j = turnouts.find(addr|0x10000);
-                                       if(j!=turnouts.end())
+                                       j = accessories.find(addr|0x10000);
+                                       if(j!=accessories.end())
                                        {
                                                command(format("request(%d, view, control)", i->first));
                                                command(format("set(%d, symbol[%d])", i->first, j->second.symbol));
                                                command(format("get(%d, state)", i->first));
 
-                                               turnouts.insert(TurnoutMap::value_type(i->first, j->second));
-                                               turnouts.erase(j);
+                                               accessories.insert(AccessoryMap::value_type(i->first, j->second));
+                                               accessories.erase(j);
 
                                                found = true;
                                        }
                                }
 
                                if(!found)
-                                       turnouts.insert(TurnoutMap::value_type(i->first, Turnout()));
+                                       accessories.insert(AccessoryMap::value_type(i->first, MagnetAccessory()));
                        }
 
                        process_object(i->first, i->second);
                }
 
-               turnouts_synced = true;
+               accessories_synced = true;
 
-               for(TurnoutMap::const_iterator i=turnouts.lower_bound(0x10000); i!=turnouts.end(); ++i)
+               for(AccessoryMap::const_iterator i=accessories.lower_bound(0x10000); i!=accessories.end(); ++i)
                        command("create(11, append)");
        }
        else if(msg.header.value=="queryObjects(26)")
@@ -461,8 +490,8 @@ void CentralStation::process_reply(const Message &msg)
                        if(j!=i->second.end())
                        {
                                unsigned id = lexical_cast<unsigned>(j->second);
-                               TurnoutMap::iterator k = turnouts.lower_bound(0x10000);
-                               if(k!=turnouts.end())
+                               AccessoryMap::iterator k = accessories.lower_bound(0x10000);
+                               if(k!=accessories.end())
                                {
                                        command(format("request(%d, view, control)", id));
                                        command(format("set(%d, addr[%d], symbol[%d], name1[\"Switch\"], name2[\"%d\"], name3[\"\"])",
@@ -470,8 +499,8 @@ void CentralStation::process_reply(const Message &msg)
                                        command(format("set(%d, state[%d])", id, k->second.state&((1<<k->second.bits)-1)));
 
                                        k->second.synced = true;
-                                       turnouts.insert(TurnoutMap::value_type(id, k->second));
-                                       turnouts.erase(k);
+                                       accessories.insert(AccessoryMap::value_type(id, k->second));
+                                       accessories.erase(k);
                                }
                        }
                }
@@ -556,29 +585,29 @@ void CentralStation::process_object(unsigned id, const Message::AttribMap &attri
                        if(funcs_changed&(1<<i))
                                signal_loco_function.emit(loco.address, i, loco.funcs&(1<<i));
        }
-       else if(turnouts.count(id))
+       else if(accessories.count(id))
        {
-               Turnout &turnout = turnouts[id];
+               MagnetAccessory &accessory = accessories[id];
                bool state_changed = false;
                for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
                {
                        if(i->first=="addr")
                        {
-                               turnout_addr.erase(turnout.address);
-                               turnout.address = lexical_cast<unsigned>(i->second);
-                               turnout_addr[turnout.address] = id;
+                               accessory_addr.erase(accessory.address);
+                               accessory.address = lexical_cast<unsigned>(i->second);
+                               accessory_addr[accessory.address] = id;
                        }
                        else if(i->first=="state")
                        {
                                unsigned state = lexical_cast<unsigned>(i->second);
-                               unsigned mask = (1<<turnout.bits)-1;
-                               turnout.state = (turnout.state&~mask) | (state&mask);
+                               unsigned mask = (1<<accessory.bits)-1;
+                               accessory.state = (accessory.state&~mask) | (state&mask);
                                state_changed = true;
                        }
                }
 
                if(state_changed)
-                       signal_turnout.emit(turnout.address, turnout.state);
+                       accessory_state_changed(accessory);
        }
        else if(find(s88.begin(), s88.end(), id)!=s88.end())
        {
@@ -754,11 +783,12 @@ CentralStation::Locomotive::Locomotive():
 { }
 
 
-CentralStation::Turnout::Turnout():
+CentralStation::MagnetAccessory::MagnetAccessory():
        address(0),
-       symbol(0),
+       type(TURNOUT),
+       symbol(TURNOUT_LEFT),
        state(0),
-       bits(0),
+       bits(1),
        synced(false)
 { }
 
index 4539e0b10d00f56db768b9c416b5d138e87ab546..ad3d771f312094e3098b9d5aa2979a8b8b1a6498 100644 (file)
@@ -68,25 +68,31 @@ private:
                Locomotive();
        };
 
-       struct Turnout
+       struct MagnetAccessory
        {
+               enum Type
+               {
+                       TURNOUT
+               };
+
                enum Symbol
                {
-                       LEFT = 0,
-                       RIGHT = 1,
-                       THREEWAY = 2,
-                       DOUBLESLIP = 3,
-                       CURVED_LEFT = 21,
-                       CURVED_RIGHT = 22
+                       TURNOUT_LEFT = 0,
+                       TURNOUT_RIGHT = 1,
+                       TURNOUT_THREEWAY = 2,
+                       TURNOUT_DOUBLESLIP = 3,
+                       TURNOUT_CURVED_LEFT = 21,
+                       TURNOUT_CURVED_RIGHT = 22
                };
 
                unsigned address;
-               unsigned symbol;
+               Type type;
+               Symbol symbol;
                unsigned state;
                unsigned bits;
                bool synced;
 
-               Turnout();
+               MagnetAccessory();
        };
 
        struct Sensor
@@ -98,7 +104,7 @@ private:
 
        typedef std::map<unsigned, unsigned> AddressMap;
        typedef std::map<unsigned, Locomotive> LocoMap;
-       typedef std::map<unsigned, Turnout> TurnoutMap;
+       typedef std::map<unsigned, MagnetAccessory> AccessoryMap;
        typedef std::map<unsigned, Sensor> SensorMap;
 
        Msp::Net::StreamSocket socket;
@@ -110,9 +116,9 @@ private:
        LocoMap locos;
        AddressMap loco_addr;
        bool locos_synced;
-       TurnoutMap turnouts;
-       AddressMap turnout_addr;
-       bool turnouts_synced;
+       AccessoryMap accessories;
+       AddressMap accessory_addr;
+       bool accessories_synced;
        SensorMap sensors;
        std::vector<unsigned> s88;
        bool sensors_synced;
@@ -138,6 +144,13 @@ public:
        virtual void set_turnout(unsigned, unsigned);
        virtual unsigned get_turnout(unsigned) const;
 
+private:
+       MagnetAccessory &add_accessory(unsigned, MagnetAccessory::Type, MagnetAccessory::Symbol);
+       void set_accessory_state(unsigned, MagnetAccessory::Type, unsigned);
+       unsigned get_accessory_state(unsigned, MagnetAccessory::Type) const;
+       void accessory_state_changed(const MagnetAccessory &) const;
+
+public:
        virtual void add_sensor(unsigned);
        virtual void set_sensor(unsigned, bool) { }
        virtual bool get_sensor(unsigned) const;