power(false),
halted(false),
locos_synced(false),
- turnouts_synced(false),
+ accessories_synced(false),
sensors_synced(false)
{
RefPtr<Net::SockAddr> addr = Net::resolve(host+":15471");
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()) ;
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()));
{
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);
}
{
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");
{
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)")
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[\"\"])",
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);
}
}
}
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())
{
{ }
-CentralStation::Turnout::Turnout():
+CentralStation::MagnetAccessory::MagnetAccessory():
address(0),
- symbol(0),
+ type(TURNOUT),
+ symbol(TURNOUT_LEFT),
state(0),
- bits(0),
+ bits(1),
synced(false)
{ }